2.7.3 • Published 4 years ago
@vzh/mobx-utils v2.7.3
Example
RootStore
import { action } from 'mobx';
import { BaseRootStore, NotificationsStore, WorkerStore } from '@vzh/mobx-utils/stores';
import { JSONModel, JSONSerializable } from '@vzh/mobx-utils/serialization';
import AuthStore from './AuthStore';
import AppStore from './AppStore';
import SignUpStore from './SignUpStore';
import SignInStore from './SignInStore';
export type RootStoreState = Pick<Partial<RootStore>, 'appStore' | 'authStore'>;
export default class RootStore extends BaseRootStore implements JSONSerializable<RootStoreState> {
readonly _serializable = this;
readonly authStore: AuthStore;
readonly appStore: AppStore;
readonly signInStore: SignInStore;
readonly signUpStore: SignUpStore;
// ...
constructor(initialState: Partial<JSONModel<RootStoreState>> = {}) {
super();
const appNotificationsStore = this.createNotificationsStore();
const appWorkerStore = this.createWorkerStore();
this.authStore = new AuthStore(
this,
appNotificationsStore,
appWorkerStore,
initialState.authStore
);
this.appStore = new AppStore(
this,
appNotificationsStore,
appWorkerStore,
initialState.appStore
);
this.signInStore = new SignInStore(this, appNotificationsStore, appWorkerStore);
this.signUpStore = new SignUpStore(this, appNotificationsStore, appWorkerStore);
}
createNotificationsStore(): NotificationsStore<this, AppNotification> {
return new NotificationsStore<this, AppNotification>(this, 10000);
}
createWorkerStore<TaskKeys extends string = never>(): WorkerStore<this, TaskKeys> {
return new WorkerStore(this);
}
toJSON(): JSONModel<RootStoreState> {
return {
appStore: this.appStore.toJSON(),
authStore: this.authStore.toJSON(),
};
}
}
AppStore
import { RequestableStore, NotificationsStore, WorkerStore } from '@vzh/mobx-utils/stores';
import RootStore from './RootStore';
export default class AppStore
extends RequestableStore<
RootStore,
NotificationsStore<RootStore, AppNotification>,
WorkerStore<RootStore>
>
implements JSONSerializable<AppStore> {
readonly _serializable = this;
// ...
constructor(
rootStore: RootStore,
notifications: NotificationsStore<RootStore, AppNotification>,
worker: WorkerStore<RootStore>,
initialState?: JSONModel<AppStore>
) {
super(rootStore, notifications, worker);
if (initialState) {
// ...
}
}
@withRequest<AppStore>({
// Keep live last result of call of loadData for 10min or while authStore.sessionId is changed.
memo: { lifetime: 60 * 10, inputs: (self) => [self.rootStore.authStore.sessionId] },
})
loadData = flow(function* loadData(this: AppStore) {
const { authStore } = this.rootStore;
if (authStore.isLoggedIn) {
const data = yield api.fetchSomeData();
// ...
} else {
const data = yield api.fetchOtherData();
// ...
}
});
toJSON(): JSONModel<AppStore> {
return {
// ...
};
}
}
AppView
import React, { useEffect } from 'react';
import { observer } from 'mobx-react-lite';
function AppView({ rootStore }: AppViewProps): JSX.Element {
const {
authStore,
appStore,
appStore: { notifications, worker },
} = rootStore;
useEffect(() => {
appStore.loadData();
}, [appStore, authStore.isLoggedIn]);
return (
<RootStoreContext.Provider value={rootStore}>
<AppLoader loading={worker.isPending()} />
<AppNotifications notifications={notifications} position="window-top" />
</RootStoreContext.Provider>
);
}
export default observer(AppView);
2.7.3
4 years ago
2.7.2
5 years ago
2.7.1
5 years ago
2.7.0
5 years ago
2.6.0
5 years ago
2.5.0
5 years ago
2.4.0
5 years ago
2.3.6
5 years ago
2.3.5
5 years ago
2.3.3
5 years ago
2.3.2
5 years ago
2.3.0
5 years ago
2.3.1
5 years ago
2.2.0
5 years ago
2.1.1
5 years ago
2.1.0
5 years ago
2.0.1
5 years ago
2.0.0
5 years ago
1.29.0
5 years ago
1.28.3
5 years ago
1.28.1
5 years ago
1.28.0
5 years ago
1.27.0
6 years ago
1.27.1
6 years ago
1.26.0
6 years ago
1.25.0
6 years ago
1.24.6
6 years ago
1.24.5
6 years ago
1.24.4
6 years ago
1.24.3
6 years ago
1.24.2
6 years ago
1.24.1
6 years ago
1.24.0
6 years ago
1.23.2
6 years ago
1.23.1
6 years ago
1.23.0
6 years ago
1.22.1
6 years ago
1.22.0
6 years ago
1.21.2
6 years ago
1.21.1
6 years ago
1.21.0
6 years ago
1.20.1
6 years ago
1.20.0
6 years ago
1.19.2
6 years ago
1.19.1
6 years ago