Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-1073

Named "AppTypes" generics on the App constructor

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None

      Goals

      In it's current form the App constructor takes generic type arguments enabling developers to "lock down" the type of the "functions factory", user custom data and profile data. As this pattern grows however, it's annoying for developers to have to provide these as positional type arguments to the App class. Instead we should support "named" generic type aka. a single AppTypes generic type which enumerates the individual types used throughout the app.

      Below is a proff of concept:

      Unable to find source-code formatter for language: typescript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      // The following are types that are internal to the SDK
      
      type BaseFunctionsFactory = {
          callFunction(name: string, args: unknown[]): Promise<unknown>;
      };
      
      type DefaultFunctionsFactory = BaseFunctionsFactory & {
          [name: string]: (...args: unknown[]) => Promise<unknown>;
      };
      
      type BaseAppTypes = {
          FunctionsFactory: unknown,
          CustomUserDataType: unknown,
          ProfileDataType: unknown
      };
      
      type AppTypes<T extends Partial<BaseAppTypes>> = T & BaseAppTypes;
      
      declare class User<T extends BaseAppTypes> {
          get functions(): T["FunctionsFactory"];
          get data(): T["CustomUserDataType"];
      }
      
      class App<T extends BaseAppTypes> {
          currentUser: User<T> | null = null;
      }
      
      // Below are the types as used from an app
      
      // The user can declare their own app's types, wrapping them in AppTypes to allow defaults to be applied for (sub)types that is not declared.
      type MyAppTypes = AppTypes<{
          FunctionsFactory: {
              echo: () => Promise<void>
          },
      }>;
      
      // Using the types
      const app = new App<MyAppTypes>();
      if (app.currentUser) {
          app.currentUser.functions.echo();
      }
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            kraen.hansen@mongodb.com Kræn Hansen
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: