aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores
diff options
context:
space:
mode:
authorLibravatar Ricardo Cino <ricardo@cino.io>2022-06-22 22:10:39 +0200
committerLibravatar Vijay Aravamudhan <vraravam@users.noreply.github.com>2022-06-23 06:55:42 +0530
commit5dcc1aff28290fe3b7e2ed5cbe99d3034c0b6f34 (patch)
tree81f643c38eecdfa49476e0b9dac3aed9107ecf99 /src/stores
parentUpgrade 'electron' to '19.0.6' (diff)
downloadferdium-app-5dcc1aff28290fe3b7e2ed5cbe99d3034c0b6f34.tar.gz
ferdium-app-5dcc1aff28290fe3b7e2ed5cbe99d3034c0b6f34.tar.zst
ferdium-app-5dcc1aff28290fe3b7e2ed5cbe99d3034c0b6f34.zip
chore: recipes/request stores js => ts
Diffstat (limited to 'src/stores')
-rw-r--r--src/stores/RecipePreviewsStore.ts (renamed from src/stores/RecipePreviewsStore.js)36
-rw-r--r--src/stores/RecipesStore.ts (renamed from src/stores/RecipesStore.js)35
-rw-r--r--src/stores/RequestStore.ts (renamed from src/stores/RequestStore.js)33
3 files changed, 65 insertions, 39 deletions
diff --git a/src/stores/RecipePreviewsStore.js b/src/stores/RecipePreviewsStore.ts
index ef0bca430..500f69b40 100644
--- a/src/stores/RecipePreviewsStore.js
+++ b/src/stores/RecipePreviewsStore.ts
@@ -1,44 +1,58 @@
1import { action, computed, observable } from 'mobx'; 1import { action, computed, observable } from 'mobx';
2import { Actions } from 'src/actions/lib/actions';
3import { ApiInterface } from 'src/api';
4import Recipe from 'src/models/Recipe';
5import { Stores } from 'src/stores.types';
2 6
3import Store from './lib/Store';
4import CachedRequest from './lib/CachedRequest'; 7import CachedRequest from './lib/CachedRequest';
5import Request from './lib/Request'; 8import Request from './lib/Request';
9import TypedStore from './lib/TypedStore';
6 10
7export default class RecipePreviewsStore extends Store { 11export default class RecipePreviewsStore extends TypedStore {
8 @observable allRecipePreviewsRequest = new CachedRequest( 12 @observable allRecipePreviewsRequest = new CachedRequest(
9 this.api.recipePreviews, 13 this.api.recipePreviews,
10 'all', 14 'all',
11 ); 15 );
12 16
13 @observable featuredRecipePreviewsRequest = new CachedRequest(this.api.recipePreviews, 'featured'); 17 @observable featuredRecipePreviewsRequest = new CachedRequest(
18 this.api.recipePreviews,
19 'featured',
20 );
14 21
15 @observable searchRecipePreviewsRequest = new Request(this.api.recipePreviews, 'search'); 22 @observable searchRecipePreviewsRequest = new Request(
23 this.api.recipePreviews,
24 'search',
25 );
16 26
17 constructor(...args) { 27 constructor(stores: Stores, api: ApiInterface, actions: Actions) {
18 super(...args); 28 super(stores, api, actions);
19 29
20 // Register action handlers 30 // Register action handlers
21 this.actions.recipePreview.search.listen(this._search.bind(this)); 31 this.actions.recipePreview.search.listen(this._search.bind(this));
22 } 32 }
23 33
24 @computed get all() { 34 async setup(): Promise<void> {
35 // Not implemented
36 }
37
38 @computed get all(): Recipe[] {
25 return this.allRecipePreviewsRequest.execute().result || []; 39 return this.allRecipePreviewsRequest.execute().result || [];
26 } 40 }
27 41
28 @computed get featured() { 42 @computed get featured(): Recipe[] {
29 return this.featuredRecipePreviewsRequest.execute().result || []; 43 return this.featuredRecipePreviewsRequest.execute().result || [];
30 } 44 }
31 45
32 @computed get searchResults() { 46 @computed get searchResults(): Recipe[] {
33 return this.searchRecipePreviewsRequest.result || []; 47 return this.searchRecipePreviewsRequest.result || [];
34 } 48 }
35 49
36 @computed get dev() { 50 @computed get dev(): Recipe[] {
37 return this.stores.recipes.all.filter(r => r.local); 51 return this.stores.recipes.all.filter(r => r.local);
38 } 52 }
39 53
40 // Actions 54 // Actions
41 @action _search({ needle }) { 55 @action _search({ needle }): void {
42 if (needle !== '') { 56 if (needle !== '') {
43 this.searchRecipePreviewsRequest.execute(needle); 57 this.searchRecipePreviewsRequest.execute(needle);
44 } 58 }
diff --git a/src/stores/RecipesStore.js b/src/stores/RecipesStore.ts
index 3d3a506cc..af2aa7fb0 100644
--- a/src/stores/RecipesStore.js
+++ b/src/stores/RecipesStore.ts
@@ -2,23 +2,27 @@ import { action, computed, observable } from 'mobx';
2import { readJSONSync } from 'fs-extra'; 2import { readJSONSync } from 'fs-extra';
3import semver from 'semver'; 3import semver from 'semver';
4 4
5import Store from './lib/Store'; 5import { Stores } from 'src/stores.types';
6import { ApiInterface } from 'src/api';
7import { Actions } from 'src/actions/lib/actions';
8import Recipe from 'src/models/Recipe';
6import CachedRequest from './lib/CachedRequest'; 9import CachedRequest from './lib/CachedRequest';
7import Request from './lib/Request'; 10import Request from './lib/Request';
8import { matchRoute } from '../helpers/routing-helpers'; 11import { matchRoute } from '../helpers/routing-helpers';
9import { asarRecipesPath } from '../helpers/asar-helpers'; 12import { asarRecipesPath } from '../helpers/asar-helpers';
13import TypedStore from './lib/TypedStore';
10 14
11const debug = require('../preload-safe-debug')('Ferdium:RecipeStore'); 15const debug = require('../preload-safe-debug')('Ferdium:RecipeStore');
12 16
13export default class RecipesStore extends Store { 17export default class RecipesStore extends TypedStore {
14 @observable allRecipesRequest = new CachedRequest(this.api.recipes, 'all'); 18 @observable allRecipesRequest = new CachedRequest(this.api.recipes, 'all');
15 19
16 @observable installRecipeRequest = new Request(this.api.recipes, 'install'); 20 @observable installRecipeRequest = new Request(this.api.recipes, 'install');
17 21
18 @observable getRecipeUpdatesRequest = new Request(this.api.recipes, 'update'); 22 @observable getRecipeUpdatesRequest = new Request(this.api.recipes, 'update');
19 23
20 constructor(...args) { 24 constructor(stores: Stores, api: ApiInterface, actions: Actions) {
21 super(...args); 25 super(stores, api, actions);
22 26
23 // Register action handlers 27 // Register action handlers
24 this.actions.recipe.install.listen(this._install.bind(this)); 28 this.actions.recipe.install.listen(this._install.bind(this));
@@ -28,11 +32,12 @@ export default class RecipesStore extends Store {
28 this.registerReactions([this._checkIfRecipeIsInstalled.bind(this)]); 32 this.registerReactions([this._checkIfRecipeIsInstalled.bind(this)]);
29 } 33 }
30 34
31 setup() { 35 async setup(): Promise<void> {
32 return this.all; 36 // Initially load all recipes
37 this.all;
33 } 38 }
34 39
35 @computed get all() { 40 @computed get all(): Recipe[] {
36 return this.allRecipesRequest.execute().result || []; 41 return this.allRecipesRequest.execute().result || [];
37 } 42 }
38 43
@@ -53,20 +58,20 @@ export default class RecipesStore extends Store {
53 return null; 58 return null;
54 } 59 }
55 60
56 @computed get recipeIdForServices() { 61 @computed get recipeIdForServices(): string[] {
57 return this.stores.services.all.map(s => s.recipe.id); 62 return this.stores.services.all.map(s => s.recipe.id);
58 } 63 }
59 64
60 one(id) { 65 one(id: string): Recipe | undefined {
61 return this.all.find(recipe => recipe.id === id); 66 return this.all.find(recipe => recipe.id === id);
62 } 67 }
63 68
64 isInstalled(id) { 69 isInstalled(id: string): boolean {
65 return !!this.one(id); 70 return !!this.one(id);
66 } 71 }
67 72
68 // Actions 73 // Actions
69 async _install({ recipeId }) { 74 async _install({ recipeId }): Promise<Recipe> {
70 const recipe = await this.installRecipeRequest.execute(recipeId)._promise; 75 const recipe = await this.installRecipeRequest.execute(recipeId)._promise;
71 await this.allRecipesRequest.invalidate({ immediately: true })._promise; 76 await this.allRecipesRequest.invalidate({ immediately: true })._promise;
72 77
@@ -82,7 +87,9 @@ export default class RecipesStore extends Store {
82 87
83 for (const r of recipeIds) { 88 for (const r of recipeIds) {
84 const recipe = this.one(r); 89 const recipe = this.one(r);
85 recipes[r] = recipe.version; 90 if (recipe) {
91 recipes[r] = recipe.version;
92 }
86 } 93 }
87 94
88 if (Object.keys(recipes).length === 0) return; 95 if (Object.keys(recipes).length === 0) return;
@@ -93,7 +100,7 @@ export default class RecipesStore extends Store {
93 // Check for local updates 100 // Check for local updates
94 const allJsonFile = asarRecipesPath('all.json'); 101 const allJsonFile = asarRecipesPath('all.json');
95 const allJson = readJSONSync(allJsonFile); 102 const allJson = readJSONSync(allJsonFile);
96 const localUpdates = []; 103 const localUpdates: string[] = [];
97 104
98 for (const recipe of Object.keys(recipes)) { 105 for (const recipe of Object.keys(recipes)) {
99 const version = recipes[recipe]; 106 const version = recipes[recipe];
@@ -134,7 +141,7 @@ export default class RecipesStore extends Store {
134 } 141 }
135 } 142 }
136 143
137 async _checkIfRecipeIsInstalled() { 144 async _checkIfRecipeIsInstalled(): Promise<void> {
138 const { router } = this.stores; 145 const { router } = this.stores;
139 146
140 const match = 147 const match =
diff --git a/src/stores/RequestStore.js b/src/stores/RequestStore.ts
index 71a5360ac..03ad2c7db 100644
--- a/src/stores/RequestStore.js
+++ b/src/stores/RequestStore.ts
@@ -1,27 +1,32 @@
1import { ipcRenderer } from 'electron'; 1import { ipcRenderer } from 'electron';
2import { action, computed, observable } from 'mobx'; 2import { action, computed, observable } from 'mobx';
3import ms from 'ms'; 3import ms from 'ms';
4
5import { Actions } from 'src/actions/lib/actions';
6import { ApiInterface } from 'src/api';
7import { Stores } from 'src/stores.types';
8import CachedRequest from './lib/CachedRequest';
4import { LOCAL_PORT } from '../config'; 9import { LOCAL_PORT } from '../config';
5 10
6import Store from './lib/Store'; 11import TypedStore from './lib/TypedStore';
7 12
8const debug = require('../preload-safe-debug')('Ferdium:RequestsStore'); 13const debug = require('../preload-safe-debug')('Ferdium:RequestsStore');
9 14
10export default class RequestStore extends Store { 15export default class RequestStore extends TypedStore {
11 @observable userInfoRequest; 16 @observable userInfoRequest: CachedRequest;
12 17
13 @observable servicesRequest; 18 @observable servicesRequest: CachedRequest;
14 19
15 @observable showRequiredRequestsError = false; 20 @observable showRequiredRequestsError = false;
16 21
17 @observable localServerPort = LOCAL_PORT; 22 @observable localServerPort = LOCAL_PORT;
18 23
19 retries = 0; 24 retries: number = 0;
20 25
21 retryDelay = ms('2s'); 26 retryDelay: number = ms('2s');
22 27
23 constructor(...args) { 28 constructor(stores: Stores, api: ApiInterface, actions: Actions) {
24 super(...args); 29 super(stores, api, actions);
25 30
26 this.actions.requests.retryRequiredRequests.listen( 31 this.actions.requests.retryRequiredRequests.listen(
27 this._retryRequiredRequests.bind(this), 32 this._retryRequiredRequests.bind(this),
@@ -30,32 +35,32 @@ export default class RequestStore extends Store {
30 this.registerReactions([this._autoRetry.bind(this)]); 35 this.registerReactions([this._autoRetry.bind(this)]);
31 } 36 }
32 37
33 setup() { 38 async setup(): Promise<void> {
34 this.userInfoRequest = this.stores.user.getUserInfoRequest; 39 this.userInfoRequest = this.stores.user.getUserInfoRequest;
35 this.servicesRequest = this.stores.services.allServicesRequest; 40 this.servicesRequest = this.stores.services.allServicesRequest;
36 41
37 ipcRenderer.on('localServerPort', (event, data) => { 42 ipcRenderer.on('localServerPort', (_, data) => {
38 if (data.port) { 43 if (data.port) {
39 this.localServerPort = data.port; 44 this.localServerPort = data.port;
40 } 45 }
41 }); 46 });
42 } 47 }
43 48
44 @computed get areRequiredRequestsSuccessful() { 49 @computed get areRequiredRequestsSuccessful(): boolean {
45 return !this.userInfoRequest.isError && !this.servicesRequest.isError; 50 return !this.userInfoRequest.isError && !this.servicesRequest.isError;
46 } 51 }
47 52
48 @computed get areRequiredRequestsLoading() { 53 @computed get areRequiredRequestsLoading(): boolean {
49 return this.userInfoRequest.isExecuting || this.servicesRequest.isExecuting; 54 return this.userInfoRequest.isExecuting || this.servicesRequest.isExecuting;
50 } 55 }
51 56
52 @action _retryRequiredRequests() { 57 @action _retryRequiredRequests(): void {
53 this.userInfoRequest.reload(); 58 this.userInfoRequest.reload();
54 this.servicesRequest.reload(); 59 this.servicesRequest.reload();
55 } 60 }
56 61
57 // Reactions 62 // Reactions
58 _autoRetry() { 63 _autoRetry(): void {
59 const delay = (this.retries <= 10 ? this.retries : 10) * this.retryDelay; 64 const delay = (this.retries <= 10 ? this.retries : 10) * this.retryDelay;
60 if (!this.areRequiredRequestsSuccessful && this.stores.user.isLoggedIn) { 65 if (!this.areRequiredRequestsSuccessful && this.stores.user.isLoggedIn) {
61 setTimeout(() => { 66 setTimeout(() => {