From c2509e860752e23812bb408e331c02c918aadd54 Mon Sep 17 00:00:00 2001 From: André Oliveira <37463445+SpecialAro@users.noreply.github.com> Date: Thu, 4 Aug 2022 00:12:03 +0100 Subject: chore: change values inside mobx actions to fix console warnings (#532) --- src/models/Service.ts | 80 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 28 deletions(-) (limited to 'src/models') diff --git a/src/models/Service.ts b/src/models/Service.ts index 21fa65f69..fb594a6f4 100644 --- a/src/models/Service.ts +++ b/src/models/Service.ts @@ -1,4 +1,4 @@ -import { autorun, computed, makeObservable, observable } from 'mobx'; +import { autorun, action, computed, makeObservable, observable } from 'mobx'; import { ipcRenderer } from 'electron'; import { webContents } from '@electron/remote'; import normalizeUrl from 'normalize-url'; @@ -124,6 +124,19 @@ export default class Service { @observable proxy: string | null = null; + @action _setAutoRun() { + if (!this.isEnabled) { + this.webview = null; + this.isAttached = false; + this.unreadDirectMessageCount = 0; + this.unreadIndirectMessageCount = 0; + } + + if (this.recipe.hasCustomUrl && this.customUrl) { + this.isUsingCustomUrl = true; + } + } + constructor(data, recipe: IRecipe) { if (!data) { throw new Error('Service config not valid'); @@ -212,19 +225,42 @@ export default class Service { } autorun((): void => { - if (!this.isEnabled) { - this.webview = null; - this.isAttached = false; - this.unreadDirectMessageCount = 0; - this.unreadIndirectMessageCount = 0; - } - - if (this.recipe.hasCustomUrl && this.customUrl) { - this.isUsingCustomUrl = true; - } + this._setAutoRun(); }); } + @action _didStartLoading(): void { + this.hasCrashed = false; + this.isLoading = true; + this.isLoadingPage = true; + this.isError = false; + } + + @action _didStopLoading(): void { + this.isLoading = false; + this.isLoadingPage = false; + } + + @action _didLoad(): void { + this.isLoading = false; + this.isLoadingPage = false; + + if (!this.isError) { + this.isFirstLoad = false; + } + } + + @action _didFailLoad(event: { errorDescription: string }): void { + this.isError = false; + this.errorMessage = event.errorDescription; + this.isLoading = false; + this.isLoadingPage = false; + } + + @action _hasCrashed(): void { + this.hasCrashed = true; + } + @computed get shareWithWebview(): object { return { id: this.id, @@ -420,27 +456,18 @@ export default class Service { this.webview.addEventListener('did-start-loading', event => { debug('Did start load', this.name, event); - this.hasCrashed = false; - this.isLoading = true; - this.isLoadingPage = true; - this.isError = false; + this._didStartLoading(); }); this.webview.addEventListener('did-stop-loading', event => { debug('Did stop load', this.name, event); - this.isLoading = false; - this.isLoadingPage = false; + this._didStopLoading(); }); // eslint-disable-next-line unicorn/consistent-function-scoping const didLoad = () => { - this.isLoading = false; - this.isLoadingPage = false; - - if (!this.isError) { - this.isFirstLoad = false; - } + this._didLoad(); }; this.webview.addEventListener('did-frame-finish-load', didLoad.bind(this)); @@ -453,16 +480,13 @@ export default class Service { event.errorCode !== -21 && event.errorCode !== -3 ) { - this.isError = true; - this.errorMessage = event.errorDescription; - this.isLoading = false; - this.isLoadingPage = false; + this._didFailLoad(event); } }); this.webview.addEventListener('crashed', () => { debug('Service crashed', this.name); - this.hasCrashed = true; + this._hasCrashed(); }); this.webview.addEventListener('found-in-page', ({ result }) => { -- cgit v1.2.3-70-g09d2