diff options
author | Ricardo Cino <ricardo@cino.io> | 2022-06-23 18:10:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-23 16:10:39 +0000 |
commit | 6b2c2b8dfb86245a1747bf7977159f5129461863 (patch) | |
tree | 28944f62a962d8a658262ea902f8554d4419fa9e /src/stores | |
parent | chore: featureStore and GlobalErrorStore JS => TS (diff) | |
download | ferdium-app-6b2c2b8dfb86245a1747bf7977159f5129461863.tar.gz ferdium-app-6b2c2b8dfb86245a1747bf7977159f5129461863.tar.zst ferdium-app-6b2c2b8dfb86245a1747bf7977159f5129461863.zip |
chore: servicesStore + models into typescript (#344)
Diffstat (limited to 'src/stores')
-rw-r--r-- | src/stores/ServicesStore.ts (renamed from src/stores/ServicesStore.js) | 101 |
1 files changed, 69 insertions, 32 deletions
diff --git a/src/stores/ServicesStore.js b/src/stores/ServicesStore.ts index 999b48d92..caa44146f 100644 --- a/src/stores/ServicesStore.js +++ b/src/stores/ServicesStore.ts | |||
@@ -5,7 +5,9 @@ import ms from 'ms'; | |||
5 | import { ensureFileSync, pathExistsSync, writeFileSync } from 'fs-extra'; | 5 | import { ensureFileSync, pathExistsSync, writeFileSync } from 'fs-extra'; |
6 | import { join } from 'path'; | 6 | import { join } from 'path'; |
7 | 7 | ||
8 | import Store from './lib/Store'; | 8 | import { Stores } from 'src/stores.types'; |
9 | import { ApiInterface } from 'src/api'; | ||
10 | import { Actions } from 'src/actions/lib/actions'; | ||
9 | import Request from './lib/Request'; | 11 | import Request from './lib/Request'; |
10 | import CachedRequest from './lib/CachedRequest'; | 12 | import CachedRequest from './lib/CachedRequest'; |
11 | import { matchRoute } from '../helpers/routing-helpers'; | 13 | import { matchRoute } from '../helpers/routing-helpers'; |
@@ -14,39 +16,56 @@ import { | |||
14 | getRecipeDirectory, | 16 | getRecipeDirectory, |
15 | getDevRecipeDirectory, | 17 | getDevRecipeDirectory, |
16 | } from '../helpers/recipe-helpers'; | 18 | } from '../helpers/recipe-helpers'; |
19 | import Service from '../models/Service'; | ||
17 | import { workspaceStore } from '../features/workspaces'; | 20 | import { workspaceStore } from '../features/workspaces'; |
18 | import { DEFAULT_SERVICE_SETTINGS, KEEP_WS_LOADED_USID } from '../config'; | 21 | import { DEFAULT_SERVICE_SETTINGS, KEEP_WS_LOADED_USID } from '../config'; |
19 | import { cleanseJSObject } from '../jsUtils'; | 22 | import { cleanseJSObject } from '../jsUtils'; |
20 | import { SPELLCHECKER_LOCALES } from '../i18n/languages'; | 23 | import { SPELLCHECKER_LOCALES } from '../i18n/languages'; |
21 | import { ferdiumVersion } from '../environment-remote'; | 24 | import { ferdiumVersion } from '../environment-remote'; |
25 | import TypedStore from './lib/TypedStore'; | ||
22 | 26 | ||
23 | const debug = require('../preload-safe-debug')('Ferdium:ServiceStore'); | 27 | const debug = require('../preload-safe-debug')('Ferdium:ServiceStore'); |
24 | 28 | ||
25 | export default class ServicesStore extends Store { | 29 | export default class ServicesStore extends TypedStore { |
26 | @observable allServicesRequest = new CachedRequest(this.api.services, 'all'); | 30 | @observable allServicesRequest: CachedRequest = new CachedRequest( |
31 | this.api.services, | ||
32 | 'all', | ||
33 | ); | ||
27 | 34 | ||
28 | @observable createServiceRequest = new Request(this.api.services, 'create'); | 35 | @observable createServiceRequest: Request = new Request( |
36 | this.api.services, | ||
37 | 'create', | ||
38 | ); | ||
29 | 39 | ||
30 | @observable updateServiceRequest = new Request(this.api.services, 'update'); | 40 | @observable updateServiceRequest: Request = new Request( |
41 | this.api.services, | ||
42 | 'update', | ||
43 | ); | ||
31 | 44 | ||
32 | @observable reorderServicesRequest = new Request( | 45 | @observable reorderServicesRequest: Request = new Request( |
33 | this.api.services, | 46 | this.api.services, |
34 | 'reorder', | 47 | 'reorder', |
35 | ); | 48 | ); |
36 | 49 | ||
37 | @observable deleteServiceRequest = new Request(this.api.services, 'delete'); | 50 | @observable deleteServiceRequest: Request = new Request( |
51 | this.api.services, | ||
52 | 'delete', | ||
53 | ); | ||
38 | 54 | ||
39 | @observable clearCacheRequest = new Request(this.api.services, 'clearCache'); | 55 | @observable clearCacheRequest: Request = new Request( |
56 | this.api.services, | ||
57 | 'clearCache', | ||
58 | ); | ||
40 | 59 | ||
41 | @observable filterNeedle = null; | 60 | @observable filterNeedle: string | null = null; |
42 | 61 | ||
43 | // Array of service IDs that have recently been used | 62 | // Array of service IDs that have recently been used |
44 | // [0] => Most recent, [n] => Least recent | 63 | // [0] => Most recent, [n] => Least recent |
45 | // No service ID should be in the list multiple times, not all service IDs have to be in the list | 64 | // No service ID should be in the list multiple times, not all service IDs have to be in the list |
46 | @observable lastUsedServices = []; | 65 | @observable lastUsedServices: string[] = []; |
47 | 66 | ||
48 | constructor(...args) { | 67 | constructor(stores: Stores, api: ApiInterface, actions: Actions) { |
49 | super(...args); | 68 | super(stores, api, actions); |
50 | 69 | ||
51 | // Register action handlers | 70 | // Register action handlers |
52 | this.actions.service.setActive.listen(this._setActive.bind(this)); | 71 | this.actions.service.setActive.listen(this._setActive.bind(this)); |
@@ -207,14 +226,16 @@ export default class ServicesStore extends Store { | |||
207 | this.serviceMaintenanceTick.cancel(); | 226 | this.serviceMaintenanceTick.cancel(); |
208 | } | 227 | } |
209 | 228 | ||
210 | /** | 229 | _serviceMaintenanceTicker() { |
211 | * Сheck for services to become hibernated. | ||
212 | */ | ||
213 | serviceMaintenanceTick = debounce(() => { | ||
214 | this._serviceMaintenance(); | 230 | this._serviceMaintenance(); |
215 | this.serviceMaintenanceTick(); | 231 | this.serviceMaintenanceTick(); |
216 | debug('Service maintenance tick'); | 232 | debug('Service maintenance tick'); |
217 | }, ms('10s')); | 233 | } |
234 | |||
235 | /** | ||
236 | * Сheck for services to become hibernated. | ||
237 | */ | ||
238 | serviceMaintenanceTick = debounce(this._serviceMaintenanceTicker, ms('10s')); | ||
218 | 239 | ||
219 | /** | 240 | /** |
220 | * Run various maintenance tasks on services | 241 | * Run various maintenance tasks on services |
@@ -271,7 +292,7 @@ export default class ServicesStore extends Store { | |||
271 | } | 292 | } |
272 | 293 | ||
273 | // Computed props | 294 | // Computed props |
274 | @computed get all() { | 295 | @computed get all(): Service[] { |
275 | if (this.stores.user.isLoggedIn) { | 296 | if (this.stores.user.isLoggedIn) { |
276 | const services = this.allServicesRequest.execute().result; | 297 | const services = this.allServicesRequest.execute().result; |
277 | if (services) { | 298 | if (services) { |
@@ -289,7 +310,7 @@ export default class ServicesStore extends Store { | |||
289 | return []; | 310 | return []; |
290 | } | 311 | } |
291 | 312 | ||
292 | @computed get enabled() { | 313 | @computed get enabled(): Service[] { |
293 | return this.all.filter(service => service.isEnabled); | 314 | return this.all.filter(service => service.isEnabled); |
294 | } | 315 | } |
295 | 316 | ||
@@ -344,9 +365,14 @@ export default class ServicesStore extends Store { | |||
344 | } | 365 | } |
345 | 366 | ||
346 | @computed get filtered() { | 367 | @computed get filtered() { |
347 | return this.all.filter(service => | 368 | if (this.filterNeedle !== null) { |
348 | service.name.toLowerCase().includes(this.filterNeedle.toLowerCase()), | 369 | return this.all.filter(service => |
349 | ); | 370 | service.name.toLowerCase().includes(this.filterNeedle!.toLowerCase()), |
371 | ); | ||
372 | } | ||
373 | |||
374 | // Return all if there is no filterNeedle present | ||
375 | return this.all; | ||
350 | } | 376 | } |
351 | 377 | ||
352 | @computed get active() { | 378 | @computed get active() { |
@@ -382,8 +408,9 @@ export default class ServicesStore extends Store { | |||
382 | return this.active && this.active.isTodosService; | 408 | return this.active && this.active.isTodosService; |
383 | } | 409 | } |
384 | 410 | ||
385 | one(id) { | 411 | // TODO: This can actually return undefined as well |
386 | return this.all.find(service => service.id === id); | 412 | one(id: string): Service { |
413 | return this.all.find(service => service.id === id) as Service; | ||
387 | } | 414 | } |
388 | 415 | ||
389 | async _showAddServiceInterface({ recipeId }) { | 416 | async _showAddServiceInterface({ recipeId }) { |
@@ -449,7 +476,11 @@ export default class ServicesStore extends Store { | |||
449 | 476 | ||
450 | @action async _createFromLegacyService({ data }) { | 477 | @action async _createFromLegacyService({ data }) { |
451 | const { id } = data.recipe; | 478 | const { id } = data.recipe; |
452 | const serviceData = {}; | 479 | const serviceData: { |
480 | name?: string; | ||
481 | team?: string; | ||
482 | customUrl?: string; | ||
483 | } = {}; | ||
453 | 484 | ||
454 | if (data.name) { | 485 | if (data.name) { |
455 | serviceData.name = data.name; | 486 | serviceData.name = data.name; |
@@ -530,7 +561,7 @@ export default class ServicesStore extends Store { | |||
530 | } | 561 | } |
531 | } | 562 | } |
532 | 563 | ||
533 | @action async _deleteService({ serviceId, redirect }) { | 564 | @action async _deleteService({ serviceId, redirect }): Promise<void> { |
534 | const request = this.deleteServiceRequest.execute(serviceId); | 565 | const request = this.deleteServiceRequest.execute(serviceId); |
535 | 566 | ||
536 | if (redirect) { | 567 | if (redirect) { |
@@ -538,14 +569,14 @@ export default class ServicesStore extends Store { | |||
538 | } | 569 | } |
539 | 570 | ||
540 | this.allServicesRequest.patch(result => { | 571 | this.allServicesRequest.patch(result => { |
541 | remove(result, c => c.id === serviceId); | 572 | remove(result, (c: Service) => c.id === serviceId); |
542 | }); | 573 | }); |
543 | 574 | ||
544 | await request._promise; | 575 | await request._promise; |
545 | this.actionStatus = request.result.status; | 576 | this.actionStatus = request.result.status; |
546 | } | 577 | } |
547 | 578 | ||
548 | @action async _openRecipeFile({ recipe, file }) { | 579 | @action async _openRecipeFile({ recipe, file }): Promise<void> { |
549 | // Get directory for recipe | 580 | // Get directory for recipe |
550 | const normalDirectory = getRecipeDirectory(recipe); | 581 | const normalDirectory = getRecipeDirectory(recipe); |
551 | const devDirectory = getDevRecipeDirectory(recipe); | 582 | const devDirectory = getDevRecipeDirectory(recipe); |
@@ -702,7 +733,7 @@ export default class ServicesStore extends Store { | |||
702 | setTimeout(() => { | 733 | setTimeout(() => { |
703 | document | 734 | document |
704 | .querySelector('.services__webview-wrapper.is-active') | 735 | .querySelector('.services__webview-wrapper.is-active') |
705 | .scrollIntoView({ | 736 | ?.scrollIntoView({ |
706 | behavior: 'smooth', | 737 | behavior: 'smooth', |
707 | block: 'end', | 738 | block: 'end', |
708 | inline: 'nearest', | 739 | inline: 'nearest', |
@@ -1046,7 +1077,13 @@ export default class ServicesStore extends Store { | |||
1046 | service.lastHibernated = Date.now(); | 1077 | service.lastHibernated = Date.now(); |
1047 | } | 1078 | } |
1048 | 1079 | ||
1049 | @action _awake({ serviceId, automatic }) { | 1080 | @action _awake({ |
1081 | serviceId, | ||
1082 | automatic, | ||
1083 | }: { | ||
1084 | serviceId: string; | ||
1085 | automatic?: boolean; | ||
1086 | }) { | ||
1050 | const now = Date.now(); | 1087 | const now = Date.now(); |
1051 | const service = this.one(serviceId); | 1088 | const service = this.one(serviceId); |
1052 | const automaticTag = automatic ? ' automatically ' : ' '; | 1089 | const automaticTag = automatic ? ' automatically ' : ' '; |
@@ -1223,7 +1260,7 @@ export default class ServicesStore extends Store { | |||
1223 | } | 1260 | } |
1224 | } | 1261 | } |
1225 | 1262 | ||
1226 | _shareSettingsWithServiceProcess() { | 1263 | _shareSettingsWithServiceProcess(): void { |
1227 | const settings = { | 1264 | const settings = { |
1228 | ...this.stores.settings.app, | 1265 | ...this.stores.settings.app, |
1229 | isDarkThemeActive: this.stores.ui.isDarkThemeActive, | 1266 | isDarkThemeActive: this.stores.ui.isDarkThemeActive, |
@@ -1234,7 +1271,7 @@ export default class ServicesStore extends Store { | |||
1234 | }); | 1271 | }); |
1235 | } | 1272 | } |
1236 | 1273 | ||
1237 | _cleanUpTeamIdAndCustomUrl(recipeId, data) { | 1274 | _cleanUpTeamIdAndCustomUrl(recipeId, data): any { |
1238 | const serviceData = data; | 1275 | const serviceData = data; |
1239 | const recipe = this.stores.recipes.one(recipeId); | 1276 | const recipe = this.stores.recipes.one(recipeId); |
1240 | 1277 | ||