Skip to content

Instantly share code, notes, and snippets.

@aight8
Last active November 5, 2023 16:57
Show Gist options
  • Select an option

  • Save aight8/21d78a05ff0f648fb93671879b2c83aa to your computer and use it in GitHub Desktop.

Select an option

Save aight8/21d78a05ff0f648fb93671879b2c83aa to your computer and use it in GitHub Desktop.

Revisions

  1. aight8 revised this gist Nov 5, 2023. 1 changed file with 12 additions and 3 deletions.
    15 changes: 12 additions & 3 deletions snippet.ts
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,13 @@
    function createRalmApp() {
    return new Realm.App({
    id: 'protein-xtnum',
    id: 'xxxxxx',
    })
    }

    function createRalm(user: Realm.User | null) {
    const realmConfig = {
    schema: [
    Amino,
    Source,
    // your schemas..
    ],
    deleteRealmIfMigrationNeeded: true,
    } as Realm.Configuration
    @@ -36,6 +35,7 @@ class AppState {
    devDebugMobxEvents()
    }

    // write back the app user the user property to trigger a rerender
    updateUser() {
    this.user = this.app.currentUser
    }
    @@ -46,19 +46,26 @@ export const State = new AppState()
    Navigation
    .events()
    .registerAppLaunchedListener(() => {
    // a function which sets the default options
    defaults()

    // runs initially
    // re-render when the user property changes
    autorun(() => {
    if (!State.user) {
    // calls Navigation.setRoot for the login layout
    Layout.setLoginRoot()
    } else {
    // calls Navigation.setRoot for the user layout
    Layout.setUserRoot()
    }
    }, {
    name: 'Set Navigation Layout on User Change',
    })
    })

    // runs initially
    // re-renders when the app reports a change (especially user login/logout/switch)
    autorun(() => {
    State.app.addListener(() => {
    State.updateUser()
    @@ -67,6 +74,8 @@ autorun(() => {
    name: '[State.app] notification -> update State.user',
    })

    // runs initially
    // re-renders when the user reports a change (something of the user changed so it must be invalidated)
    autorun(() => {
    State.user?.addListener(() => {
    State.updateUser()
  2. aight8 renamed this gist Nov 5, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. aight8 created this gist Nov 5, 2023.
    76 changes: 76 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    function createRalmApp() {
    return new Realm.App({
    id: 'protein-xtnum',
    })
    }

    function createRalm(user: Realm.User | null) {
    const realmConfig = {
    schema: [
    Amino,
    Source,
    ],
    deleteRealmIfMigrationNeeded: true,
    } as Realm.Configuration

    // if (user) {
    // realmConfig.sync = {
    // flexible: true,
    // user: user,
    // }
    // }
    return new Realm(realmConfig)
    }

    class AppState {
    app: Realm.App = createRalmApp()
    user: Realm.User | null = this.app.currentUser
    realm: Realm = createRalm(this.user)

    constructor() {
    makeAutoObservable(this, {
    app: observable.ref,
    user: observable.ref,
    realm: observable.ref,
    })
    devDebugMobxEvents()
    }

    updateUser() {
    this.user = this.app.currentUser
    }
    }

    export const State = new AppState()

    Navigation
    .events()
    .registerAppLaunchedListener(() => {
    defaults()

    autorun(() => {
    if (!State.user) {
    Layout.setLoginRoot()
    } else {
    Layout.setUserRoot()
    }
    }, {
    name: 'Set Navigation Layout on User Change',
    })
    })

    autorun(() => {
    State.app.addListener(() => {
    State.updateUser()
    })
    }, {
    name: '[State.app] notification -> update State.user',
    })

    autorun(() => {
    State.user?.addListener(() => {
    State.updateUser()
    })
    }, {
    name: '[State.user] notification -> update State.user',
    })