diff options
Diffstat (limited to 'src/features/utils')
-rw-r--r-- | src/features/utils/ActionBinding.js | 29 | ||||
-rw-r--r-- | src/features/utils/FeatureStore.js | 43 |
2 files changed, 61 insertions, 11 deletions
diff --git a/src/features/utils/ActionBinding.js b/src/features/utils/ActionBinding.js new file mode 100644 index 000000000..497aa071b --- /dev/null +++ b/src/features/utils/ActionBinding.js | |||
@@ -0,0 +1,29 @@ | |||
1 | export default class ActionBinding { | ||
2 | action; | ||
3 | |||
4 | isActive = false; | ||
5 | |||
6 | constructor(action) { | ||
7 | this.action = action; | ||
8 | } | ||
9 | |||
10 | start() { | ||
11 | if (!this.isActive) { | ||
12 | const { action } = this; | ||
13 | action[0].listen(action[1]); | ||
14 | this.isActive = true; | ||
15 | } | ||
16 | } | ||
17 | |||
18 | stop() { | ||
19 | if (this.isActive) { | ||
20 | const { action } = this; | ||
21 | action[0].off(action[1]); | ||
22 | this.isActive = false; | ||
23 | } | ||
24 | } | ||
25 | } | ||
26 | |||
27 | export const createActionBindings = actions => ( | ||
28 | actions.map(a => new ActionBinding(a)) | ||
29 | ); | ||
diff --git a/src/features/utils/FeatureStore.js b/src/features/utils/FeatureStore.js index 66b66a104..b6e0fbce3 100644 --- a/src/features/utils/FeatureStore.js +++ b/src/features/utils/FeatureStore.js | |||
@@ -1,21 +1,42 @@ | |||
1 | import Reaction from '../../stores/lib/Reaction'; | 1 | import { union } from 'lodash'; |
2 | 2 | ||
3 | export class FeatureStore { | 3 | export class FeatureStore { |
4 | _actions = null; | 4 | _actions = null; |
5 | 5 | ||
6 | _reactions = null; | 6 | _reactions = null; |
7 | 7 | ||
8 | _listenToActions(actions) { | 8 | stop() { |
9 | if (this._actions) this._actions.forEach(a => a[0].off(a[1])); | 9 | this._stopActions(); |
10 | this._actions = []; | 10 | this._stopReactions(); |
11 | actions.forEach(a => this._actions.push(a)); | ||
12 | this._actions.forEach(a => a[0].listen(a[1])); | ||
13 | } | 11 | } |
14 | 12 | ||
15 | _startReactions(reactions) { | 13 | // ACTIONS |
16 | if (this._reactions) this._reactions.forEach(r => r.stop()); | 14 | |
17 | this._reactions = []; | 15 | _registerActions(actions) { |
18 | reactions.forEach(r => this._reactions.push(new Reaction(r))); | 16 | this._actions = union(this._actions, actions); |
19 | this._reactions.forEach(r => r.start()); | 17 | this._startActions(); |
18 | } | ||
19 | |||
20 | _startActions(actions = this._actions) { | ||
21 | actions.forEach(a => a.start()); | ||
22 | } | ||
23 | |||
24 | _stopActions(actions = this._actions) { | ||
25 | actions.forEach(a => a.stop()); | ||
26 | } | ||
27 | |||
28 | // REACTIONS | ||
29 | |||
30 | _registerReactions(reactions) { | ||
31 | this._reactions = union(this._reactions, reactions); | ||
32 | this._startReactions(); | ||
33 | } | ||
34 | |||
35 | _startReactions(reactions = this._reactions) { | ||
36 | reactions.forEach(r => r.start()); | ||
37 | } | ||
38 | |||
39 | _stopReactions(reactions = this._reactions) { | ||
40 | reactions.forEach(r => r.stop()); | ||
20 | } | 41 | } |
21 | } | 42 | } |