diff options
author | Dominik Guzei <dominik.guzei@gmail.com> | 2019-04-12 12:12:25 +0200 |
---|---|---|
committer | Dominik Guzei <dominik.guzei@gmail.com> | 2019-04-12 12:12:25 +0200 |
commit | 70ed64197835377ef701d2ee80830a50cfec93b4 (patch) | |
tree | 30841b41db1670a625cd744adc8b576fe7bc7875 /src | |
parent | disable autofocus on create workspace input for free users (diff) | |
download | ferdium-app-70ed64197835377ef701d2ee80830a50cfec93b4.tar.gz ferdium-app-70ed64197835377ef701d2ee80830a50cfec93b4.tar.zst ferdium-app-70ed64197835377ef701d2ee80830a50cfec93b4.zip |
improve starting and stopping logic for workspace actions and reactions
Diffstat (limited to 'src')
-rw-r--r-- | src/features/utils/FeatureStore.js | 36 | ||||
-rw-r--r-- | src/features/workspaces/store.js | 67 | ||||
-rw-r--r-- | src/stores/lib/Reaction.js | 14 |
3 files changed, 78 insertions, 39 deletions
diff --git a/src/features/utils/FeatureStore.js b/src/features/utils/FeatureStore.js index 48962561d..d863f7464 100644 --- a/src/features/utils/FeatureStore.js +++ b/src/features/utils/FeatureStore.js | |||
@@ -5,38 +5,40 @@ export class FeatureStore { | |||
5 | 5 | ||
6 | _reactions = null; | 6 | _reactions = null; |
7 | 7 | ||
8 | stop() { | ||
9 | this._stopActions(); | ||
10 | this._stopReactions(); | ||
11 | } | ||
12 | |||
13 | // ACTIONS | ||
14 | |||
8 | _registerActions(actions) { | 15 | _registerActions(actions) { |
9 | this._actions = []; | 16 | this._actions = []; |
10 | actions.forEach(a => this._actions.push(a)); | 17 | actions.forEach(a => this._actions.push(a)); |
11 | this._startListeningToActions(); | 18 | this._startActions(this._actions); |
12 | } | 19 | } |
13 | 20 | ||
14 | _startListeningToActions() { | 21 | _startActions(actions = this._actions) { |
15 | this._stopListeningToActions(); | 22 | actions.forEach(a => a[0].listen(a[1])); |
16 | this._actions.forEach(a => a[0].listen(a[1])); | ||
17 | } | 23 | } |
18 | 24 | ||
19 | _stopListeningToActions() { | 25 | _stopActions(actions = this._actions) { |
20 | this._actions.forEach(a => a[0].off(a[1])); | 26 | actions.forEach(a => a[0].off(a[1])); |
21 | } | 27 | } |
22 | 28 | ||
29 | // REACTIONS | ||
30 | |||
23 | _registerReactions(reactions) { | 31 | _registerReactions(reactions) { |
24 | this._reactions = []; | 32 | this._reactions = []; |
25 | reactions.forEach(r => this._reactions.push(new Reaction(r))); | 33 | reactions.forEach(r => this._reactions.push(new Reaction(r))); |
26 | this._startReactions(); | 34 | this._startReactions(this._reactions); |
27 | } | 35 | } |
28 | 36 | ||
29 | _startReactions() { | 37 | _startReactions(reactions = this._reactions) { |
30 | this._stopReactions(); | 38 | reactions.forEach(r => r.start()); |
31 | this._reactions.forEach(r => r.start()); | ||
32 | } | ||
33 | |||
34 | _stopReactions() { | ||
35 | this._reactions.forEach(r => r.stop()); | ||
36 | } | 39 | } |
37 | 40 | ||
38 | stop() { | 41 | _stopReactions(reactions = this._reactions) { |
39 | this._stopListeningToActions(); | 42 | reactions.forEach(r => r.stop()); |
40 | this._stopReactions(); | ||
41 | } | 43 | } |
42 | } | 44 | } |
diff --git a/src/features/workspaces/store.js b/src/features/workspaces/store.js index 4841a4e08..bb18dc182 100644 --- a/src/features/workspaces/store.js +++ b/src/features/workspaces/store.js | |||
@@ -55,32 +55,65 @@ export default class WorkspacesStore extends FeatureStore { | |||
55 | return !this.isPremiumUpgradeRequired; | 55 | return !this.isPremiumUpgradeRequired; |
56 | } | 56 | } |
57 | 57 | ||
58 | // ========== PRIVATE PROPERTIES ========= // | ||
59 | |||
60 | _wasDrawerOpenBeforeSettingsRoute = null; | ||
61 | |||
62 | _freeUserActions = []; | ||
63 | |||
64 | _premiumUserActions = []; | ||
65 | |||
66 | _allActions = []; | ||
67 | |||
68 | _freeUserReactions = []; | ||
69 | |||
70 | _premiumUserReactions = []; | ||
71 | |||
72 | _allReactions = []; | ||
73 | |||
74 | // ========== PUBLIC API ========= // | ||
75 | |||
58 | start(stores, actions) { | 76 | start(stores, actions) { |
59 | debug('WorkspacesStore::start'); | 77 | debug('WorkspacesStore::start'); |
60 | this.stores = stores; | 78 | this.stores = stores; |
61 | this.actions = actions; | 79 | this.actions = actions; |
62 | 80 | ||
63 | this._registerActions([ | 81 | // ACTIONS |
82 | |||
83 | this._freeUserActions = [ | ||
84 | [workspaceActions.toggleWorkspaceDrawer, this._toggleWorkspaceDrawer], | ||
85 | [workspaceActions.openWorkspaceSettings, this._openWorkspaceSettings], | ||
86 | ]; | ||
87 | this._premiumUserActions = [ | ||
64 | [workspaceActions.edit, this._edit], | 88 | [workspaceActions.edit, this._edit], |
65 | [workspaceActions.create, this._create], | 89 | [workspaceActions.create, this._create], |
66 | [workspaceActions.delete, this._delete], | 90 | [workspaceActions.delete, this._delete], |
67 | [workspaceActions.update, this._update], | 91 | [workspaceActions.update, this._update], |
68 | [workspaceActions.activate, this._setActiveWorkspace], | 92 | [workspaceActions.activate, this._setActiveWorkspace], |
69 | [workspaceActions.deactivate, this._deactivateActiveWorkspace], | 93 | [workspaceActions.deactivate, this._deactivateActiveWorkspace], |
70 | [workspaceActions.toggleWorkspaceDrawer, this._toggleWorkspaceDrawer], | 94 | ]; |
71 | [workspaceActions.openWorkspaceSettings, this._openWorkspaceSettings], | 95 | this._allActions = this._freeUserActions.concat(this._premiumUserActions); |
72 | ]); | 96 | this._registerActions(this._allActions); |
73 | 97 | ||
74 | this._registerReactions([ | 98 | // REACTIONS |
75 | this._setWorkspaceBeingEditedReaction, | 99 | |
76 | this._setActiveServiceOnWorkspaceSwitchReaction, | 100 | this._freeUserReactions = [ |
101 | this._stopPremiumActionsAndReactions, | ||
102 | this._openDrawerWithSettingsReaction, | ||
77 | this._setFeatureEnabledReaction, | 103 | this._setFeatureEnabledReaction, |
78 | this._setIsPremiumFeatureReaction, | 104 | this._setIsPremiumFeatureReaction, |
79 | this._activateLastUsedWorkspaceReaction, | ||
80 | this._openDrawerWithSettingsReaction, | ||
81 | this._cleanupInvalidServiceReferences, | 105 | this._cleanupInvalidServiceReferences, |
82 | this._disableActionsForFreeUser, | 106 | ]; |
83 | ]); | 107 | this._premiumUserReactions = [ |
108 | this._setActiveServiceOnWorkspaceSwitchReaction, | ||
109 | this._activateLastUsedWorkspaceReaction, | ||
110 | this._setWorkspaceBeingEditedReaction, | ||
111 | ]; | ||
112 | this._allReactions = this._freeUserReactions.concat(this._premiumUserReactions); | ||
113 | |||
114 | this._registerReactions(this._allReactions); | ||
115 | |||
116 | console.log(this._reactions); | ||
84 | 117 | ||
85 | getUserWorkspacesRequest.execute(); | 118 | getUserWorkspacesRequest.execute(); |
86 | this.isFeatureActive = true; | 119 | this.isFeatureActive = true; |
@@ -110,9 +143,7 @@ export default class WorkspacesStore extends FeatureStore { | |||
110 | return workspace.services.map(id => services.one(id)).filter(s => !!s); | 143 | return workspace.services.map(id => services.one(id)).filter(s => !!s); |
111 | } | 144 | } |
112 | 145 | ||
113 | // ========== PRIVATE ========= // | 146 | // ========== PRIVATE METHODS ========= // |
114 | |||
115 | _wasDrawerOpenBeforeSettingsRoute = null; | ||
116 | 147 | ||
117 | _getWorkspaceById = id => this.workspaces.find(w => w.id === id); | 148 | _getWorkspaceById = id => this.workspaces.find(w => w.id === id); |
118 | 149 | ||
@@ -280,11 +311,13 @@ export default class WorkspacesStore extends FeatureStore { | |||
280 | } | 311 | } |
281 | }; | 312 | }; |
282 | 313 | ||
283 | _disableActionsForFreeUser = () => { | 314 | _stopPremiumActionsAndReactions = () => { |
284 | if (!this.isUserAllowedToUseFeature) { | 315 | if (!this.isUserAllowedToUseFeature) { |
285 | this._stopListeningToActions(); | 316 | this._stopActions(this._premiumUserActions); |
317 | this._stopReactions(this._premiumUserReactions); | ||
286 | } else { | 318 | } else { |
287 | this._startListeningToActions(); | 319 | this._startActions(this._premiumUserActions); |
320 | this._startReactions(this._premiumUserReactions); | ||
288 | } | 321 | } |
289 | } | 322 | } |
290 | } | 323 | } |
diff --git a/src/stores/lib/Reaction.js b/src/stores/lib/Reaction.js index 46aa4dae6..b123ec01c 100644 --- a/src/stores/lib/Reaction.js +++ b/src/stores/lib/Reaction.js | |||
@@ -4,21 +4,25 @@ import { autorun } from 'mobx'; | |||
4 | export default class Reaction { | 4 | export default class Reaction { |
5 | reaction; | 5 | reaction; |
6 | 6 | ||
7 | hasBeenStarted; | 7 | isRunning = false; |
8 | 8 | ||
9 | dispose; | 9 | dispose; |
10 | 10 | ||
11 | constructor(reaction) { | 11 | constructor(reaction) { |
12 | this.reaction = reaction; | 12 | this.reaction = reaction; |
13 | this.hasBeenStarted = false; | ||
14 | } | 13 | } |
15 | 14 | ||
16 | start() { | 15 | start() { |
17 | this.dispose = autorun(() => this.reaction()); | 16 | if (!this.isRunning) { |
18 | this.hasBeenStarted = true; | 17 | this.dispose = autorun(() => this.reaction()); |
18 | this.isRunning = true; | ||
19 | } | ||
19 | } | 20 | } |
20 | 21 | ||
21 | stop() { | 22 | stop() { |
22 | if (this.hasBeenStarted) this.dispose(); | 23 | if (this.isRunning) { |
24 | this.dispose(); | ||
25 | this.isRunning = true; | ||
26 | } | ||
23 | } | 27 | } |
24 | } | 28 | } |