diff options
author | Ricardo Cino <ricardo@cino.io> | 2022-06-22 16:21:23 +0200 |
---|---|---|
committer | Vijay Aravamudhan <vraravam@users.noreply.github.com> | 2022-06-23 17:50:07 +0530 |
commit | caf43fc8d64c27a1aad4a5d6507313c513c1a447 (patch) | |
tree | c0a7bcb950cdf555b3e8a33a57e2241e7051861d /src/stores/FeaturesStore.ts | |
parent | Workaroud for in-app Password Recovery (#342) (diff) | |
download | ferdium-app-caf43fc8d64c27a1aad4a5d6507313c513c1a447.tar.gz ferdium-app-caf43fc8d64c27a1aad4a5d6507313c513c1a447.tar.zst ferdium-app-caf43fc8d64c27a1aad4a5d6507313c513c1a447.zip |
chore: featureStore and GlobalErrorStore JS => TS
Diffstat (limited to 'src/stores/FeaturesStore.ts')
-rw-r--r-- | src/stores/FeaturesStore.ts | 76 |
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 @@ | |||
1 | import { computed, observable, runInAction } from 'mobx'; | ||
2 | |||
3 | import CachedRequest from './lib/CachedRequest'; | ||
4 | import serviceProxy from '../features/serviceProxy'; | ||
5 | import basicAuth from '../features/basicAuth'; | ||
6 | import workspaces from '../features/workspaces'; | ||
7 | import quickSwitch from '../features/quickSwitch'; | ||
8 | import publishDebugInfo from '../features/publishDebugInfo'; | ||
9 | import communityRecipes from '../features/communityRecipes'; | ||
10 | import todos from '../features/todos'; | ||
11 | import appearance from '../features/appearance'; | ||
12 | import TypedStore from './lib/TypedStore'; | ||
13 | |||
14 | export 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 | } | ||