From 5f4611d65e40eae3ca6191a15f68d69ea5a1c4cb Mon Sep 17 00:00:00 2001 From: Kirill Rogovoy Date: Tue, 20 Jul 2021 21:24:52 +0300 Subject: WIP --- app/models/character-store/character-store.test.ts | 7 --- app/models/character-store/character-store.ts | 38 --------------- app/models/character/character.test.ts | 10 ---- app/models/character/character.ts | 18 ------- app/models/environment.ts | 40 --------------- app/models/extensions/with-environment.ts | 17 ------- app/models/extensions/with-root-store.ts | 17 ------- app/models/index.ts | 5 -- app/models/root-store/root-store-context.ts | 22 --------- app/models/root-store/root-store.ts | 20 -------- app/models/root-store/setup-root-store.ts | 57 ---------------------- 11 files changed, 251 deletions(-) delete mode 100644 app/models/character-store/character-store.test.ts delete mode 100644 app/models/character-store/character-store.ts delete mode 100644 app/models/character/character.test.ts delete mode 100644 app/models/character/character.ts delete mode 100644 app/models/environment.ts delete mode 100644 app/models/extensions/with-environment.ts delete mode 100644 app/models/extensions/with-root-store.ts delete mode 100644 app/models/index.ts delete mode 100644 app/models/root-store/root-store-context.ts delete mode 100644 app/models/root-store/root-store.ts delete mode 100644 app/models/root-store/setup-root-store.ts (limited to 'app/models') diff --git a/app/models/character-store/character-store.test.ts b/app/models/character-store/character-store.test.ts deleted file mode 100644 index fc17694..0000000 --- a/app/models/character-store/character-store.test.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { CharacterStoreModel } from './character-store' - -test('can be created', () => { - const instance = CharacterStoreModel.create({}) - - expect(instance).toBeTruthy() -}) diff --git a/app/models/character-store/character-store.ts b/app/models/character-store/character-store.ts deleted file mode 100644 index 2496fcd..0000000 --- a/app/models/character-store/character-store.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { Instance, SnapshotOut, types } from 'mobx-state-tree' -import { CharacterModel, CharacterSnapshot } from '../character/character' -import { CharacterApi } from '../../services/api/character-api' -import { withEnvironment } from '../extensions/with-environment' - -/** - * Example store containing Rick and Morty characters - */ -export const CharacterStoreModel = types - .model('CharacterStore') - .props({ - characters: types.optional(types.array(CharacterModel), []), - }) - .extend(withEnvironment) - .actions((self) => ({ - saveCharacters: (characterSnapshots: CharacterSnapshot[]) => { - self.characters.replace(characterSnapshots) - }, - })) - .actions((self) => ({ - getCharacters: async () => { - const characterApi = new CharacterApi(self.environment.api) - const result = await characterApi.getCharacters() - - if (result.kind === 'ok') { - self.saveCharacters(result.characters) - } else { - __DEV__ && console.tron.log(result.kind) - } - }, - })) - -type CharacterStoreType = Instance -export interface CharacterStore extends CharacterStoreType {} -type CharacterStoreSnapshotType = SnapshotOut -export interface CharacterStoreSnapshot extends CharacterStoreSnapshotType {} -export const createCharacterStoreDefaultModel = () => - types.optional(CharacterStoreModel, {}) diff --git a/app/models/character/character.test.ts b/app/models/character/character.test.ts deleted file mode 100644 index d7bfac7..0000000 --- a/app/models/character/character.test.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { CharacterModel } from './character' - -test('can be created', () => { - const instance = CharacterModel.create({ - id: 1, - name: 'Rick Sanchez', - }) - - expect(instance).toBeTruthy() -}) diff --git a/app/models/character/character.ts b/app/models/character/character.ts deleted file mode 100644 index a9a2e46..0000000 --- a/app/models/character/character.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Instance, SnapshotOut, types } from 'mobx-state-tree' - -/** - * Rick and Morty character model. - */ -export const CharacterModel = types.model('Character').props({ - id: types.identifierNumber, - name: types.maybe(types.string), - status: types.maybe(types.string), - image: types.maybe(types.string), -}) - -type CharacterType = Instance -export interface Character extends CharacterType {} -type CharacterSnapshotType = SnapshotOut -export interface CharacterSnapshot extends CharacterSnapshotType {} -export const createCharacterDefaultModel = () => - types.optional(CharacterModel, {}) diff --git a/app/models/environment.ts b/app/models/environment.ts deleted file mode 100644 index a29586b..0000000 --- a/app/models/environment.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { Api } from '../services/api' - -let ReactotronDev -if (__DEV__) { - const { Reactotron } = require('../services/reactotron') - ReactotronDev = Reactotron -} - -/** - * The environment is a place where services and shared dependencies between - * models live. They are made available to every model via dependency injection. - */ -export class Environment { - constructor() { - // create each service - if (__DEV__) { - // dev-only services - this.reactotron = new ReactotronDev() - } - this.api = new Api() - } - - async setup() { - // allow each service to setup - if (__DEV__) { - await this.reactotron.setup() - } - await this.api.setup() - } - - /** - * Reactotron is only available in dev. - */ - reactotron: typeof ReactotronDev - - /** - * Our api. - */ - api: Api -} diff --git a/app/models/extensions/with-environment.ts b/app/models/extensions/with-environment.ts deleted file mode 100644 index 1fc190e..0000000 --- a/app/models/extensions/with-environment.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { getEnv, IStateTreeNode } from 'mobx-state-tree' -import { Environment } from '../environment' - -/** - * Adds a environment property to the node for accessing our - * Environment in strongly typed. - */ -export const withEnvironment = (self: IStateTreeNode) => ({ - views: { - /** - * The environment. - */ - get environment() { - return getEnv(self) - }, - }, -}) diff --git a/app/models/extensions/with-root-store.ts b/app/models/extensions/with-root-store.ts deleted file mode 100644 index 72ab478..0000000 --- a/app/models/extensions/with-root-store.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { getRoot, IStateTreeNode } from 'mobx-state-tree' -import { RootStoreModel } from '../root-store/root-store' - -/** - * Adds a rootStore property to the node for a convenient - * and strongly typed way for stores to access other stores. - */ -export const withRootStore = (self: IStateTreeNode) => ({ - views: { - /** - * The root store. - */ - get rootStore() { - return getRoot(self) - }, - }, -}) diff --git a/app/models/index.ts b/app/models/index.ts deleted file mode 100644 index 74909fc..0000000 --- a/app/models/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -export * from './extensions/with-environment' -export * from './extensions/with-root-store' -export * from './root-store/root-store' -export * from './root-store/root-store-context' -export * from './root-store/setup-root-store' diff --git a/app/models/root-store/root-store-context.ts b/app/models/root-store/root-store-context.ts deleted file mode 100644 index f01a35c..0000000 --- a/app/models/root-store/root-store-context.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { createContext, useContext } from 'react' -import { RootStore } from './root-store' - -/** - * Create a context we can use to - * - Provide access to our stores from our root component - * - Consume stores in our screens (or other components, though it's - * preferable to just connect screens) - */ -const RootStoreContext = createContext({} as RootStore) - -/** - * The provider our root component will use to expose the root store - */ -export const RootStoreProvider = RootStoreContext.Provider - -/** - * A hook that screens can use to gain access to our stores, with - * `const { someStore, someOtherStore } = useStores()`, - * or less likely: `const rootStore = useStores()` - */ -export const useStores = () => useContext(RootStoreContext) diff --git a/app/models/root-store/root-store.ts b/app/models/root-store/root-store.ts deleted file mode 100644 index 56425bc..0000000 --- a/app/models/root-store/root-store.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Instance, SnapshotOut, types } from 'mobx-state-tree' -import { CharacterStoreModel } from '../character-store/character-store' - -/** - * A RootStore model. - */ -// prettier-ignore -export const RootStoreModel = types.model("RootStore").props({ - characterStore: types.optional(CharacterStoreModel, {} as any), -}) - -/** - * The RootStore instance. - */ -export interface RootStore extends Instance {} - -/** - * The data of a RootStore. - */ -export interface RootStoreSnapshot extends SnapshotOut {} diff --git a/app/models/root-store/setup-root-store.ts b/app/models/root-store/setup-root-store.ts deleted file mode 100644 index b1a741b..0000000 --- a/app/models/root-store/setup-root-store.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { onSnapshot } from 'mobx-state-tree' -import { RootStoreModel, RootStore } from './root-store' -import { Environment } from '../environment' -import * as storage from '../../utils/storage' - -/** - * The key we'll be saving our state as within async storage. - */ -const ROOT_STATE_STORAGE_KEY = 'root' - -/** - * Setup the environment that all the models will be sharing. - * - * The environment includes other functions that will be picked from some - * of the models that get created later. This is how we loosly couple things - * like events between models. - */ -export async function createEnvironment() { - const env = new Environment() - await env.setup() - return env -} - -/** - * Setup the root state. - */ -export async function setupRootStore() { - let rootStore: RootStore - let data: any - - // prepare the environment that will be associated with the RootStore. - const env = await createEnvironment() - try { - // load data from storage - data = (await storage.load(ROOT_STATE_STORAGE_KEY)) || {} - rootStore = RootStoreModel.create(data, env) - } catch (e) { - // if there's any problems loading, then let's at least fallback to an empty state - // instead of crashing. - rootStore = RootStoreModel.create({}, env) - - // but please inform us what happened - __DEV__ && console.tron.error(e.message, null) - } - - // reactotron logging - if (__DEV__) { - env.reactotron.setRootStore(rootStore, data) - } - - // track changes & save to storage - onSnapshot(rootStore, (snapshot) => - storage.save(ROOT_STATE_STORAGE_KEY, snapshot), - ) - - return rootStore -} -- cgit v1.2.3