diff options
Diffstat (limited to 'src/stores/lib/TypedStore.ts')
-rw-r--r-- | src/stores/lib/TypedStore.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/stores/lib/TypedStore.ts b/src/stores/lib/TypedStore.ts new file mode 100644 index 000000000..5d8bf3bbd --- /dev/null +++ b/src/stores/lib/TypedStore.ts | |||
@@ -0,0 +1,46 @@ | |||
1 | import { computed, IReactionPublic, observable } from 'mobx'; | ||
2 | import { Actions } from 'src/actions/lib/actions'; | ||
3 | import { ApiInterface } from 'src/api'; | ||
4 | import { Stores } from 'src/stores.types'; | ||
5 | import Reaction from './Reaction'; | ||
6 | |||
7 | export default abstract class TypedStore { | ||
8 | _reactions: Reaction[] = []; | ||
9 | |||
10 | @observable _status: any = null; | ||
11 | |||
12 | @computed get actionStatus() { | ||
13 | return this._status || []; | ||
14 | } | ||
15 | |||
16 | set actionStatus(status) { | ||
17 | this._status = status; | ||
18 | } | ||
19 | |||
20 | constructor( | ||
21 | public stores: Stores, | ||
22 | public api: ApiInterface, | ||
23 | public actions: Actions, | ||
24 | ) {} | ||
25 | |||
26 | registerReactions(reactions: { (r: IReactionPublic): void }[]): void { | ||
27 | for (const reaction of reactions) { | ||
28 | this._reactions.push(new Reaction(reaction)); | ||
29 | } | ||
30 | } | ||
31 | |||
32 | public abstract setup(): void; | ||
33 | |||
34 | initialize(): void { | ||
35 | this.setup(); | ||
36 | for (const reaction of this._reactions) reaction.start(); | ||
37 | } | ||
38 | |||
39 | teardown(): void { | ||
40 | for (const reaction of this._reactions) reaction.stop(); | ||
41 | } | ||
42 | |||
43 | resetStatus(): void { | ||
44 | this._status = null; | ||
45 | } | ||
46 | } | ||