diff options
author | 2022-06-22 22:10:39 +0200 | |
---|---|---|
committer | 2022-06-23 06:55:42 +0530 | |
commit | 5dcc1aff28290fe3b7e2ed5cbe99d3034c0b6f34 (patch) | |
tree | 81f643c38eecdfa49476e0b9dac3aed9107ecf99 /src/stores/RecipesStore.js | |
parent | Upgrade 'electron' to '19.0.6' (diff) | |
download | ferdium-app-5dcc1aff28290fe3b7e2ed5cbe99d3034c0b6f34.tar.gz ferdium-app-5dcc1aff28290fe3b7e2ed5cbe99d3034c0b6f34.tar.zst ferdium-app-5dcc1aff28290fe3b7e2ed5cbe99d3034c0b6f34.zip |
chore: recipes/request stores js => ts
Diffstat (limited to 'src/stores/RecipesStore.js')
-rw-r--r-- | src/stores/RecipesStore.js | 162 |
1 files changed, 0 insertions, 162 deletions
diff --git a/src/stores/RecipesStore.js b/src/stores/RecipesStore.js deleted file mode 100644 index 3d3a506cc..000000000 --- a/src/stores/RecipesStore.js +++ /dev/null | |||
@@ -1,162 +0,0 @@ | |||
1 | import { action, computed, observable } from 'mobx'; | ||
2 | import { readJSONSync } from 'fs-extra'; | ||
3 | import semver from 'semver'; | ||
4 | |||
5 | import Store from './lib/Store'; | ||
6 | import CachedRequest from './lib/CachedRequest'; | ||
7 | import Request from './lib/Request'; | ||
8 | import { matchRoute } from '../helpers/routing-helpers'; | ||
9 | import { asarRecipesPath } from '../helpers/asar-helpers'; | ||
10 | |||
11 | const debug = require('../preload-safe-debug')('Ferdium:RecipeStore'); | ||
12 | |||
13 | export default class RecipesStore extends Store { | ||
14 | @observable allRecipesRequest = new CachedRequest(this.api.recipes, 'all'); | ||
15 | |||
16 | @observable installRecipeRequest = new Request(this.api.recipes, 'install'); | ||
17 | |||
18 | @observable getRecipeUpdatesRequest = new Request(this.api.recipes, 'update'); | ||
19 | |||
20 | constructor(...args) { | ||
21 | super(...args); | ||
22 | |||
23 | // Register action handlers | ||
24 | this.actions.recipe.install.listen(this._install.bind(this)); | ||
25 | this.actions.recipe.update.listen(this._update.bind(this)); | ||
26 | |||
27 | // Reactions | ||
28 | this.registerReactions([this._checkIfRecipeIsInstalled.bind(this)]); | ||
29 | } | ||
30 | |||
31 | setup() { | ||
32 | return this.all; | ||
33 | } | ||
34 | |||
35 | @computed get all() { | ||
36 | return this.allRecipesRequest.execute().result || []; | ||
37 | } | ||
38 | |||
39 | @computed get active() { | ||
40 | const match = matchRoute( | ||
41 | '/settings/services/add/:id', | ||
42 | this.stores.router.location.pathname, | ||
43 | ); | ||
44 | if (match) { | ||
45 | const activeRecipe = this.one(match.id); | ||
46 | if (activeRecipe) { | ||
47 | return activeRecipe; | ||
48 | } | ||
49 | |||
50 | debug(`Recipe ${match.id} not installed`); | ||
51 | } | ||
52 | |||
53 | return null; | ||
54 | } | ||
55 | |||
56 | @computed get recipeIdForServices() { | ||
57 | return this.stores.services.all.map(s => s.recipe.id); | ||
58 | } | ||
59 | |||
60 | one(id) { | ||
61 | return this.all.find(recipe => recipe.id === id); | ||
62 | } | ||
63 | |||
64 | isInstalled(id) { | ||
65 | return !!this.one(id); | ||
66 | } | ||
67 | |||
68 | // Actions | ||
69 | async _install({ recipeId }) { | ||
70 | const recipe = await this.installRecipeRequest.execute(recipeId)._promise; | ||
71 | await this.allRecipesRequest.invalidate({ immediately: true })._promise; | ||
72 | |||
73 | return recipe; | ||
74 | } | ||
75 | |||
76 | @action async _update() { | ||
77 | const recipeIds = this.recipeIdForServices; | ||
78 | const recipes = {}; | ||
79 | |||
80 | // Hackfix, reference this.all to fetch services | ||
81 | debug(`Check Recipe updates for ${this.all.map(recipe => recipe.id)}`); | ||
82 | |||
83 | for (const r of recipeIds) { | ||
84 | const recipe = this.one(r); | ||
85 | recipes[r] = recipe.version; | ||
86 | } | ||
87 | |||
88 | if (Object.keys(recipes).length === 0) return; | ||
89 | |||
90 | const remoteUpdates = await this.getRecipeUpdatesRequest.execute(recipes) | ||
91 | ._promise; | ||
92 | |||
93 | // Check for local updates | ||
94 | const allJsonFile = asarRecipesPath('all.json'); | ||
95 | const allJson = readJSONSync(allJsonFile); | ||
96 | const localUpdates = []; | ||
97 | |||
98 | for (const recipe of Object.keys(recipes)) { | ||
99 | const version = recipes[recipe]; | ||
100 | |||
101 | // Find recipe in local recipe repository | ||
102 | const localRecipe = allJson.find(r => r.id === recipe); | ||
103 | |||
104 | if (localRecipe && semver.lt(version, localRecipe.version)) { | ||
105 | localUpdates.push(recipe); | ||
106 | } | ||
107 | } | ||
108 | |||
109 | const updates = [...remoteUpdates, ...localUpdates]; | ||
110 | debug( | ||
111 | 'Got update information (local, remote):', | ||
112 | localUpdates, | ||
113 | remoteUpdates, | ||
114 | ); | ||
115 | |||
116 | const length = updates.length - 1; | ||
117 | const syncUpdate = async i => { | ||
118 | const update = updates[i]; | ||
119 | |||
120 | this.actions.recipe.install({ recipeId: update }); | ||
121 | await this.installRecipeRequest._promise; | ||
122 | |||
123 | this.installRecipeRequest.reset(); | ||
124 | |||
125 | if (i === length) { | ||
126 | this.stores.ui.showServicesUpdatedInfoBar = true; | ||
127 | } else if (i < length) { | ||
128 | syncUpdate(i + 1); | ||
129 | } | ||
130 | }; | ||
131 | |||
132 | if (length >= 0) { | ||
133 | syncUpdate(0); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | async _checkIfRecipeIsInstalled() { | ||
138 | const { router } = this.stores; | ||
139 | |||
140 | const match = | ||
141 | router.location && | ||
142 | matchRoute('/settings/services/add/:id', router.location.pathname); | ||
143 | if (match) { | ||
144 | const recipeId = match.id; | ||
145 | |||
146 | if (!this.stores.recipes.isInstalled(recipeId)) { | ||
147 | router.push('/settings/recipes'); | ||
148 | debug(`Recipe ${recipeId} is not installed, trying to install it`); | ||
149 | |||
150 | const recipe = await this.installRecipeRequest.execute(recipeId) | ||
151 | ._promise; | ||
152 | if (recipe) { | ||
153 | await this.allRecipesRequest.invalidate({ immediately: true }) | ||
154 | ._promise; | ||
155 | router.push(`/settings/services/add/${recipeId}`); | ||
156 | } else { | ||
157 | router.push('/settings/recipes'); | ||
158 | } | ||
159 | } | ||
160 | } | ||
161 | } | ||
162 | } | ||