diff options
Diffstat (limited to 'src/models')
-rw-r--r-- | src/models/Service.ts | 80 |
1 files changed, 52 insertions, 28 deletions
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 @@ | |||
1 | import { autorun, computed, makeObservable, observable } from 'mobx'; | 1 | import { autorun, action, computed, makeObservable, observable } from 'mobx'; |
2 | import { ipcRenderer } from 'electron'; | 2 | import { ipcRenderer } from 'electron'; |
3 | import { webContents } from '@electron/remote'; | 3 | import { webContents } from '@electron/remote'; |
4 | import normalizeUrl from 'normalize-url'; | 4 | import normalizeUrl from 'normalize-url'; |
@@ -124,6 +124,19 @@ export default class Service { | |||
124 | 124 | ||
125 | @observable proxy: string | null = null; | 125 | @observable proxy: string | null = null; |
126 | 126 | ||
127 | @action _setAutoRun() { | ||
128 | if (!this.isEnabled) { | ||
129 | this.webview = null; | ||
130 | this.isAttached = false; | ||
131 | this.unreadDirectMessageCount = 0; | ||
132 | this.unreadIndirectMessageCount = 0; | ||
133 | } | ||
134 | |||
135 | if (this.recipe.hasCustomUrl && this.customUrl) { | ||
136 | this.isUsingCustomUrl = true; | ||
137 | } | ||
138 | } | ||
139 | |||
127 | constructor(data, recipe: IRecipe) { | 140 | constructor(data, recipe: IRecipe) { |
128 | if (!data) { | 141 | if (!data) { |
129 | throw new Error('Service config not valid'); | 142 | throw new Error('Service config not valid'); |
@@ -212,19 +225,42 @@ export default class Service { | |||
212 | } | 225 | } |
213 | 226 | ||
214 | autorun((): void => { | 227 | autorun((): void => { |
215 | if (!this.isEnabled) { | 228 | this._setAutoRun(); |
216 | this.webview = null; | ||
217 | this.isAttached = false; | ||
218 | this.unreadDirectMessageCount = 0; | ||
219 | this.unreadIndirectMessageCount = 0; | ||
220 | } | ||
221 | |||
222 | if (this.recipe.hasCustomUrl && this.customUrl) { | ||
223 | this.isUsingCustomUrl = true; | ||
224 | } | ||
225 | }); | 229 | }); |
226 | } | 230 | } |
227 | 231 | ||
232 | @action _didStartLoading(): void { | ||
233 | this.hasCrashed = false; | ||
234 | this.isLoading = true; | ||
235 | this.isLoadingPage = true; | ||
236 | this.isError = false; | ||
237 | } | ||
238 | |||
239 | @action _didStopLoading(): void { | ||
240 | this.isLoading = false; | ||
241 | this.isLoadingPage = false; | ||
242 | } | ||
243 | |||
244 | @action _didLoad(): void { | ||
245 | this.isLoading = false; | ||
246 | this.isLoadingPage = false; | ||
247 | |||
248 | if (!this.isError) { | ||
249 | this.isFirstLoad = false; | ||
250 | } | ||
251 | } | ||
252 | |||
253 | @action _didFailLoad(event: { errorDescription: string }): void { | ||
254 | this.isError = false; | ||
255 | this.errorMessage = event.errorDescription; | ||
256 | this.isLoading = false; | ||
257 | this.isLoadingPage = false; | ||
258 | } | ||
259 | |||
260 | @action _hasCrashed(): void { | ||
261 | this.hasCrashed = true; | ||
262 | } | ||
263 | |||
228 | @computed get shareWithWebview(): object { | 264 | @computed get shareWithWebview(): object { |
229 | return { | 265 | return { |
230 | id: this.id, | 266 | id: this.id, |
@@ -420,27 +456,18 @@ export default class Service { | |||
420 | this.webview.addEventListener('did-start-loading', event => { | 456 | this.webview.addEventListener('did-start-loading', event => { |
421 | debug('Did start load', this.name, event); | 457 | debug('Did start load', this.name, event); |
422 | 458 | ||
423 | this.hasCrashed = false; | 459 | this._didStartLoading(); |
424 | this.isLoading = true; | ||
425 | this.isLoadingPage = true; | ||
426 | this.isError = false; | ||
427 | }); | 460 | }); |
428 | 461 | ||
429 | this.webview.addEventListener('did-stop-loading', event => { | 462 | this.webview.addEventListener('did-stop-loading', event => { |
430 | debug('Did stop load', this.name, event); | 463 | debug('Did stop load', this.name, event); |
431 | 464 | ||
432 | this.isLoading = false; | 465 | this._didStopLoading(); |
433 | this.isLoadingPage = false; | ||
434 | }); | 466 | }); |
435 | 467 | ||
436 | // eslint-disable-next-line unicorn/consistent-function-scoping | 468 | // eslint-disable-next-line unicorn/consistent-function-scoping |
437 | const didLoad = () => { | 469 | const didLoad = () => { |
438 | this.isLoading = false; | 470 | this._didLoad(); |
439 | this.isLoadingPage = false; | ||
440 | |||
441 | if (!this.isError) { | ||
442 | this.isFirstLoad = false; | ||
443 | } | ||
444 | }; | 471 | }; |
445 | 472 | ||
446 | this.webview.addEventListener('did-frame-finish-load', didLoad.bind(this)); | 473 | this.webview.addEventListener('did-frame-finish-load', didLoad.bind(this)); |
@@ -453,16 +480,13 @@ export default class Service { | |||
453 | event.errorCode !== -21 && | 480 | event.errorCode !== -21 && |
454 | event.errorCode !== -3 | 481 | event.errorCode !== -3 |
455 | ) { | 482 | ) { |
456 | this.isError = true; | 483 | this._didFailLoad(event); |
457 | this.errorMessage = event.errorDescription; | ||
458 | this.isLoading = false; | ||
459 | this.isLoadingPage = false; | ||
460 | } | 484 | } |
461 | }); | 485 | }); |
462 | 486 | ||
463 | this.webview.addEventListener('crashed', () => { | 487 | this.webview.addEventListener('crashed', () => { |
464 | debug('Service crashed', this.name); | 488 | debug('Service crashed', this.name); |
465 | this.hasCrashed = true; | 489 | this._hasCrashed(); |
466 | }); | 490 | }); |
467 | 491 | ||
468 | this.webview.addEventListener('found-in-page', ({ result }) => { | 492 | this.webview.addEventListener('found-in-page', ({ result }) => { |