aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/lib
diff options
context:
space:
mode:
authorLibravatar Ricardo Cino <ricardo@cino.io>2022-06-22 00:32:18 +0200
committerLibravatar GitHub <noreply@github.com>2022-06-21 22:32:18 +0000
commit73ba955e344c8ccedd43235495ef8b72b5a2b6fd (patch)
tree03766ab32fefe7e83026a14393527f1dcbaed849 /src/stores/lib
parentdocs: add cino as a contributor for infra [skip ci] (#330) (diff)
downloadferdium-app-73ba955e344c8ccedd43235495ef8b72b5a2b6fd.tar.gz
ferdium-app-73ba955e344c8ccedd43235495ef8b72b5a2b6fd.tar.zst
ferdium-app-73ba955e344c8ccedd43235495ef8b72b5a2b6fd.zip
chore: Transform AppStore.js into Typescript (#329)
* turn actions into typescript * correct tsconfig * added TypedStore
Diffstat (limited to 'src/stores/lib')
-rw-r--r--src/stores/lib/Reaction.ts14
-rw-r--r--src/stores/lib/Store.js13
-rw-r--r--src/stores/lib/TypedStore.ts46
3 files changed, 62 insertions, 11 deletions
diff --git a/src/stores/lib/Reaction.ts b/src/stores/lib/Reaction.ts
index 0ca24a6fa..3966c8073 100644
--- a/src/stores/lib/Reaction.ts
+++ b/src/stores/lib/Reaction.ts
@@ -1,24 +1,24 @@
1import { autorun } from 'mobx'; 1import { autorun, IReactionDisposer, IReactionPublic } from 'mobx';
2 2
3export default class Reaction { 3export default class Reaction {
4 reaction; 4 public reaction: (r: IReactionPublic) => any;
5 5
6 isRunning = false; 6 private isRunning: boolean = false;
7 7
8 dispose; 8 public dispose?: IReactionDisposer;
9 9
10 constructor(reaction) { 10 constructor(reaction: any) {
11 this.reaction = reaction; 11 this.reaction = reaction;
12 } 12 }
13 13
14 start() { 14 start(): void {
15 if (!this.isRunning) { 15 if (!this.isRunning) {
16 this.dispose = autorun(this.reaction); 16 this.dispose = autorun(this.reaction);
17 this.isRunning = true; 17 this.isRunning = true;
18 } 18 }
19 } 19 }
20 20
21 stop() { 21 stop(): void {
22 if (this.isRunning) { 22 if (this.isRunning) {
23 this.dispose?.(); 23 this.dispose?.();
24 this.isRunning = false; 24 this.isRunning = false;
diff --git a/src/stores/lib/Store.js b/src/stores/lib/Store.js
index a867c3a46..739a47729 100644
--- a/src/stores/lib/Store.js
+++ b/src/stores/lib/Store.js
@@ -2,12 +2,16 @@ import { computed, observable } from 'mobx';
2import Reaction from './Reaction'; 2import Reaction from './Reaction';
3 3
4export default class Store { 4export default class Store {
5 stores = {}; 5 /** @type Stores */
6 stores;
6 7
7 api = {}; 8 /** @type ApiInterface */
9 api;
8 10
9 actions = {}; 11 /** @type Actions */
12 actions;
10 13
14 /** @type Reaction[] */
11 _reactions = []; 15 _reactions = [];
12 16
13 // status implementation 17 // status implementation
@@ -28,8 +32,9 @@ export default class Store {
28 } 32 }
29 33
30 registerReactions(reactions) { 34 registerReactions(reactions) {
31 for (const reaction of reactions) 35 for (const reaction of reactions) {
32 this._reactions.push(new Reaction(reaction)); 36 this._reactions.push(new Reaction(reaction));
37 }
33 } 38 }
34 39
35 setup() {} 40 setup() {}
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 @@
1import { computed, IReactionPublic, observable } from 'mobx';
2import { Actions } from 'src/actions/lib/actions';
3import { ApiInterface } from 'src/api';
4import { Stores } from 'src/stores.types';
5import Reaction from './Reaction';
6
7export 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}