-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstore.ts
60 lines (53 loc) · 1.93 KB
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { action, Action, createContextStore, thunk, Thunk } from "easy-peasy";
import EventEmiter, { IEventEmiter } from "./event";
export type Messages = { [term: string]: string };
export type LoadMessagesFunction = (lang: string) => Messages;
export type Events = { [value: string]: any };
export type EventsSubscribe = { [value: string]: any };
export type ListenEventFunction = (data: any) => Events;
export interface ContextStoreModel {
sendEvent: (id: string, ...args: Array<any>) => Promise<any>;
navigate: (
commands: string | Array<string>,
options?: { queryParams?: any }
) => any;
basename: string;
setBasename: Action<ContextStoreModel, string | undefined>;
language: string;
setLanguage: Action<ContextStoreModel, string | undefined>;
loadMessages: LoadMessagesFunction;
eventEmiter: IEventEmiter;
messages: Messages;
setEventState: Action<ContextStoreModel, any | undefined>;
setEvent: Thunk<ContextStoreModel, any | undefined, any>;
event: any;
}
const ContextStore = createContextStore<ContextStoreModel>(
(initialData) =>
initialData || {
sendEvent: async () => {},
navigate: () => {},
basename: "/",
setBasename: action<ContextStoreModel>((state, value) => {
state.basename = value || "/";
}),
language: "en",
setLanguage: action<ContextStoreModel>((state, value) => {
state.language = value || "en";
state.messages = state.loadMessages?.(state.language);
}),
loadMessages: () => ({}),
messages: {},
event: {},
eventEmiter: new EventEmiter(),
setEventState: action<ContextStoreModel>((state, value) => {
state.event = value;
}),
setEvent: thunk(async (actions, payload, helpers) => {
const state = await (helpers.getState());
state.eventEmiter.dispatch(payload.type, payload.data);
actions.setEventState(payload);
})
}
);
export default ContextStore;