diff options
Diffstat (limited to 'src/stores/FeaturesStore.js')
-rw-r--r-- | src/stores/FeaturesStore.js | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/stores/FeaturesStore.js b/src/stores/FeaturesStore.js new file mode 100644 index 000000000..10c893d3f --- /dev/null +++ b/src/stores/FeaturesStore.js | |||
@@ -0,0 +1,50 @@ | |||
1 | import { computed, observable } from 'mobx'; | ||
2 | |||
3 | import Store from './lib/Store'; | ||
4 | import CachedRequest from './lib/CachedRequest'; | ||
5 | |||
6 | import delayApp from '../features/delayApp'; | ||
7 | import spellchecker from '../features/spellchecker'; | ||
8 | import serviceProxy from '../features/serviceProxy'; | ||
9 | |||
10 | import { DEFAULT_FEATURES_CONFIG } from '../config'; | ||
11 | |||
12 | export default class FeaturesStore extends Store { | ||
13 | @observable defaultFeaturesRequest = new CachedRequest(this.api.features, 'default'); | ||
14 | @observable featuresRequest = new CachedRequest(this.api.features, 'features'); | ||
15 | |||
16 | async setup() { | ||
17 | this.registerReactions([ | ||
18 | this._monitorLoginStatus.bind(this), | ||
19 | ]); | ||
20 | |||
21 | await this.featuresRequest._promise; | ||
22 | setTimeout(this._enableFeatures.bind(this), 1); | ||
23 | } | ||
24 | |||
25 | @computed get anonymousFeatures() { | ||
26 | return this.defaultFeaturesRequest.execute().result || DEFAULT_FEATURES_CONFIG; | ||
27 | } | ||
28 | |||
29 | @computed get features() { | ||
30 | if (this.stores.user.isLoggedIn) { | ||
31 | return this.featuresRequest.execute().result || DEFAULT_FEATURES_CONFIG; | ||
32 | } | ||
33 | |||
34 | return DEFAULT_FEATURES_CONFIG; | ||
35 | } | ||
36 | |||
37 | _monitorLoginStatus() { | ||
38 | if (this.stores.user.isLoggedIn) { | ||
39 | this.featuresRequest.invalidate({ immediately: true }); | ||
40 | } else { | ||
41 | this.defaultFeaturesRequest.invalidate({ immediately: true }); | ||
42 | } | ||
43 | } | ||
44 | |||
45 | _enableFeatures() { | ||
46 | delayApp(this.stores, this.actions); | ||
47 | spellchecker(this.stores, this.actions); | ||
48 | serviceProxy(this.stores, this.actions); | ||
49 | } | ||
50 | } | ||