diff options
author | Paul Frazee <pfrazee@gmail.com> | 2022-07-19 15:37:24 -0500 |
---|---|---|
committer | Paul Frazee <pfrazee@gmail.com> | 2022-07-19 15:37:24 -0500 |
commit | dc55f580049d284c6e01271e3885c4fa23a8f458 (patch) | |
tree | 64112d4525410ef3ca8553901af81f5df1e216da /src/state/index.ts | |
parent | 6b32698b3e020e5910c92b72a1677e7cd56287d6 (diff) | |
download | voidsky-dc55f580049d284c6e01271e3885c4fa23a8f458.tar.zst |
Replace mobx-state-tree with mobx and get a basic home feed rendering
Diffstat (limited to 'src/state/index.ts')
-rw-r--r-- | src/state/index.ts | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/src/state/index.ts b/src/state/index.ts index 24c3b9430..91726dc6e 100644 --- a/src/state/index.ts +++ b/src/state/index.ts @@ -1,33 +1,35 @@ -import {onSnapshot} from 'mobx-state-tree' -import { - RootStoreModel, - RootStore, - createDefaultRootStore, -} from './models/root-store' -import {Environment} from './env' +import {autorun} from 'mobx' +import {AdxClient, blueskywebSchemas} from '@adxp/mock-api' +import {RootStoreModel} from './models/root-store' +import * as libapi from './lib/api' import * as storage from './lib/storage' // import * as auth from './auth' TODO const ROOT_STATE_STORAGE_KEY = 'root' export async function setupState() { - let rootStore: RootStore + let rootStore: RootStoreModel let data: any - const env = new Environment() - await env.setup() + const api = new AdxClient({ + pds: 'http://localhost', + schemas: blueskywebSchemas, + }) + await libapi.setup(api) + rootStore = new RootStoreModel(api) try { data = (await storage.load(ROOT_STATE_STORAGE_KEY)) || {} - rootStore = RootStoreModel.create(data, env) + rootStore.hydrate(data) } catch (e) { console.error('Failed to load state from storage', e) - rootStore = RootStoreModel.create(createDefaultRootStore(), env) } // track changes & save to storage - onSnapshot(rootStore, snapshot => - storage.save(ROOT_STATE_STORAGE_KEY, snapshot), - ) + autorun(() => { + const snapshot = rootStore.serialize() + console.log('saving', snapshot) + storage.save(ROOT_STATE_STORAGE_KEY, snapshot) + }) // TODO rootStore.session.setAuthed(true) @@ -47,4 +49,3 @@ export async function setupState() { } export {useStores, RootStoreModel, RootStoreProvider} from './models/root-store' -export type {RootStore} from './models/root-store' |