Last active
November 5, 2023 16:57
-
-
Save aight8/21d78a05ff0f648fb93671879b2c83aa to your computer and use it in GitHub Desktop.
Revisions
-
aight8 revised this gist
Nov 5, 2023 . 1 changed file with 12 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,13 @@ function createRalmApp() { return new Realm.App({ id: 'xxxxxx', }) } function createRalm(user: Realm.User | null) { const realmConfig = { schema: [ // 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() -
aight8 renamed this gist
Nov 5, 2023 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
aight8 created this gist
Nov 5, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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', })