From 70ed64197835377ef701d2ee80830a50cfec93b4 Mon Sep 17 00:00:00 2001 From: Dominik Guzei Date: Fri, 12 Apr 2019 12:12:25 +0200 Subject: improve starting and stopping logic for workspace actions and reactions --- src/features/utils/FeatureStore.js | 36 ++++++++++---------- src/features/workspaces/store.js | 67 ++++++++++++++++++++++++++++---------- 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 { _reactions = null; + stop() { + this._stopActions(); + this._stopReactions(); + } + + // ACTIONS + _registerActions(actions) { this._actions = []; actions.forEach(a => this._actions.push(a)); - this._startListeningToActions(); + this._startActions(this._actions); } - _startListeningToActions() { - this._stopListeningToActions(); - this._actions.forEach(a => a[0].listen(a[1])); + _startActions(actions = this._actions) { + actions.forEach(a => a[0].listen(a[1])); } - _stopListeningToActions() { - this._actions.forEach(a => a[0].off(a[1])); + _stopActions(actions = this._actions) { + actions.forEach(a => a[0].off(a[1])); } + // REACTIONS + _registerReactions(reactions) { this._reactions = []; reactions.forEach(r => this._reactions.push(new Reaction(r))); - this._startReactions(); + this._startReactions(this._reactions); } - _startReactions() { - this._stopReactions(); - this._reactions.forEach(r => r.start()); - } - - _stopReactions() { - this._reactions.forEach(r => r.stop()); + _startReactions(reactions = this._reactions) { + reactions.forEach(r => r.start()); } - stop() { - this._stopListeningToActions(); - this._stopReactions(); + _stopReactions(reactions = this._reactions) { + reactions.forEach(r => r.stop()); } } 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 { return !this.isPremiumUpgradeRequired; } + // ========== PRIVATE PROPERTIES ========= // + + _wasDrawerOpenBeforeSettingsRoute = null; + + _freeUserActions = []; + + _premiumUserActions = []; + + _allActions = []; + + _freeUserReactions = []; + + _premiumUserReactions = []; + + _allReactions = []; + + // ========== PUBLIC API ========= // + start(stores, actions) { debug('WorkspacesStore::start'); this.stores = stores; this.actions = actions; - this._registerActions([ + // ACTIONS + + this._freeUserActions = [ + [workspaceActions.toggleWorkspaceDrawer, this._toggleWorkspaceDrawer], + [workspaceActions.openWorkspaceSettings, this._openWorkspaceSettings], + ]; + this._premiumUserActions = [ [workspaceActions.edit, this._edit], [workspaceActions.create, this._create], [workspaceActions.delete, this._delete], [workspaceActions.update, this._update], [workspaceActions.activate, this._setActiveWorkspace], [workspaceActions.deactivate, this._deactivateActiveWorkspace], - [workspaceActions.toggleWorkspaceDrawer, this._toggleWorkspaceDrawer], - [workspaceActions.openWorkspaceSettings, this._openWorkspaceSettings], - ]); + ]; + this._allActions = this._freeUserActions.concat(this._premiumUserActions); + this._registerActions(this._allActions); - this._registerReactions([ - this._setWorkspaceBeingEditedReaction, - this._setActiveServiceOnWorkspaceSwitchReaction, + // REACTIONS + + this._freeUserReactions = [ + this._stopPremiumActionsAndReactions, + this._openDrawerWithSettingsReaction, this._setFeatureEnabledReaction, this._setIsPremiumFeatureReaction, - this._activateLastUsedWorkspaceReaction, - this._openDrawerWithSettingsReaction, this._cleanupInvalidServiceReferences, - this._disableActionsForFreeUser, - ]); + ]; + this._premiumUserReactions = [ + this._setActiveServiceOnWorkspaceSwitchReaction, + this._activateLastUsedWorkspaceReaction, + this._setWorkspaceBeingEditedReaction, + ]; + this._allReactions = this._freeUserReactions.concat(this._premiumUserReactions); + + this._registerReactions(this._allReactions); + + console.log(this._reactions); getUserWorkspacesRequest.execute(); this.isFeatureActive = true; @@ -110,9 +143,7 @@ export default class WorkspacesStore extends FeatureStore { return workspace.services.map(id => services.one(id)).filter(s => !!s); } - // ========== PRIVATE ========= // - - _wasDrawerOpenBeforeSettingsRoute = null; + // ========== PRIVATE METHODS ========= // _getWorkspaceById = id => this.workspaces.find(w => w.id === id); @@ -280,11 +311,13 @@ export default class WorkspacesStore extends FeatureStore { } }; - _disableActionsForFreeUser = () => { + _stopPremiumActionsAndReactions = () => { if (!this.isUserAllowedToUseFeature) { - this._stopListeningToActions(); + this._stopActions(this._premiumUserActions); + this._stopReactions(this._premiumUserReactions); } else { - this._startListeningToActions(); + this._startActions(this._premiumUserActions); + this._startReactions(this._premiumUserReactions); } } } 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'; export default class Reaction { reaction; - hasBeenStarted; + isRunning = false; dispose; constructor(reaction) { this.reaction = reaction; - this.hasBeenStarted = false; } start() { - this.dispose = autorun(() => this.reaction()); - this.hasBeenStarted = true; + if (!this.isRunning) { + this.dispose = autorun(() => this.reaction()); + this.isRunning = true; + } } stop() { - if (this.hasBeenStarted) this.dispose(); + if (this.isRunning) { + this.dispose(); + this.isRunning = true; + } } } -- cgit v1.2.3-54-g00ecf