From 73ba955e344c8ccedd43235495ef8b72b5a2b6fd Mon Sep 17 00:00:00 2001 From: Ricardo Cino Date: Wed, 22 Jun 2022 00:32:18 +0200 Subject: chore: Transform AppStore.js into Typescript (#329) * turn actions into typescript * correct tsconfig * added TypedStore --- src/stores/lib/Reaction.ts | 14 +++++++------- src/stores/lib/Store.js | 13 +++++++++---- src/stores/lib/TypedStore.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 src/stores/lib/TypedStore.ts (limited to 'src/stores/lib') 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 @@ -import { autorun } from 'mobx'; +import { autorun, IReactionDisposer, IReactionPublic } from 'mobx'; export default class Reaction { - reaction; + public reaction: (r: IReactionPublic) => any; - isRunning = false; + private isRunning: boolean = false; - dispose; + public dispose?: IReactionDisposer; - constructor(reaction) { + constructor(reaction: any) { this.reaction = reaction; } - start() { + start(): void { if (!this.isRunning) { this.dispose = autorun(this.reaction); this.isRunning = true; } } - stop() { + stop(): void { if (this.isRunning) { this.dispose?.(); 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'; import Reaction from './Reaction'; export default class Store { - stores = {}; + /** @type Stores */ + stores; - api = {}; + /** @type ApiInterface */ + api; - actions = {}; + /** @type Actions */ + actions; + /** @type Reaction[] */ _reactions = []; // status implementation @@ -28,8 +32,9 @@ export default class Store { } registerReactions(reactions) { - for (const reaction of reactions) + for (const reaction of reactions) { this._reactions.push(new Reaction(reaction)); + } } 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 @@ +import { computed, IReactionPublic, observable } from 'mobx'; +import { Actions } from 'src/actions/lib/actions'; +import { ApiInterface } from 'src/api'; +import { Stores } from 'src/stores.types'; +import Reaction from './Reaction'; + +export default abstract class TypedStore { + _reactions: Reaction[] = []; + + @observable _status: any = null; + + @computed get actionStatus() { + return this._status || []; + } + + set actionStatus(status) { + this._status = status; + } + + constructor( + public stores: Stores, + public api: ApiInterface, + public actions: Actions, + ) {} + + registerReactions(reactions: { (r: IReactionPublic): void }[]): void { + for (const reaction of reactions) { + this._reactions.push(new Reaction(reaction)); + } + } + + public abstract setup(): void; + + initialize(): void { + this.setup(); + for (const reaction of this._reactions) reaction.start(); + } + + teardown(): void { + for (const reaction of this._reactions) reaction.stop(); + } + + resetStatus(): void { + this._status = null; + } +} -- cgit v1.2.3-54-g00ecf