aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/FeaturesStore.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/stores/FeaturesStore.ts')
-rw-r--r--src/stores/FeaturesStore.ts76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/stores/FeaturesStore.ts b/src/stores/FeaturesStore.ts
new file mode 100644
index 000000000..b63c252df
--- /dev/null
+++ b/src/stores/FeaturesStore.ts
@@ -0,0 +1,76 @@
1import { computed, observable, runInAction } from 'mobx';
2
3import CachedRequest from './lib/CachedRequest';
4import serviceProxy from '../features/serviceProxy';
5import basicAuth from '../features/basicAuth';
6import workspaces from '../features/workspaces';
7import quickSwitch from '../features/quickSwitch';
8import publishDebugInfo from '../features/publishDebugInfo';
9import communityRecipes from '../features/communityRecipes';
10import todos from '../features/todos';
11import appearance from '../features/appearance';
12import TypedStore from './lib/TypedStore';
13
14export default class FeaturesStore extends TypedStore {
15 @observable defaultFeaturesRequest = new CachedRequest(
16 this.api.features,
17 'default',
18 );
19
20 @observable featuresRequest = new CachedRequest(
21 this.api.features,
22 'features',
23 );
24
25 @observable features = {};
26
27 async setup(): Promise<void> {
28 this.registerReactions([
29 this._updateFeatures,
30 this._monitorLoginStatus.bind(this),
31 ]);
32
33 await this.featuresRequest._promise;
34 setTimeout(this._setupFeatures.bind(this), 1);
35 }
36
37 @computed get anonymousFeatures(): any {
38 return this.defaultFeaturesRequest.execute().result || {};
39 }
40
41 _updateFeatures = (): void => {
42 const features = {};
43 if (this.stores.user.isLoggedIn) {
44 let requestResult = {};
45 try {
46 requestResult = this.featuresRequest.execute().result;
47 } catch (error) {
48 console.error(error);
49 }
50 Object.assign(features, requestResult);
51 }
52 runInAction('FeaturesStore::_updateFeatures', () => {
53 this.features = features;
54 });
55 };
56
57 _monitorLoginStatus(): void {
58 if (this.stores.user.isLoggedIn) {
59 this.featuresRequest.invalidate({ immediately: true });
60 } else {
61 this.defaultFeaturesRequest.execute();
62 this.defaultFeaturesRequest.invalidate({ immediately: true });
63 }
64 }
65
66 _setupFeatures(): void {
67 serviceProxy(this.stores);
68 basicAuth();
69 workspaces(this.stores, this.actions);
70 quickSwitch();
71 publishDebugInfo();
72 communityRecipes(this.stores, this.actions);
73 todos(this.stores, this.actions);
74 appearance(this.stores);
75 }
76}