fix: improve deinit logic and flow

This commit is contained in:
Mo
2022-02-08 21:35:31 -06:00
parent 1dd70364e7
commit e43c8a6f07
6 changed files with 82 additions and 51 deletions

View File

@@ -39,8 +39,8 @@ export type WebEventObserver = (event: WebAppEvent) => void;
export class WebApplication extends SNApplication {
private webServices!: WebServices;
public noteControllerGroup: NoteGroupController;
private webEventObservers: WebEventObserver[] = [];
public noteControllerGroup: NoteGroupController;
public iconsController: IconsController;
constructor(
@@ -70,28 +70,26 @@ export class WebApplication extends SNApplication {
this.iconsController = new IconsController();
}
/** @override */
deinit(source: DeinitSource): void {
for (const service of Object.values(this.webServices)) {
if ('deinit' in service) {
service.deinit?.(source);
super.deinit(source);
try {
for (const service of Object.values(this.webServices)) {
if ('deinit' in service) {
service.deinit?.(source);
}
(service as any).application = undefined;
}
(service as any).application = undefined;
}
this.webServices = {} as WebServices;
this.noteControllerGroup.deinit();
this.iconsController.deinit();
this.webEventObservers.length = 0;
/**
* Allow any pending renders to complete before destroying the global
* application instance and all its services
*/
setTimeout(() => {
super.deinit(source);
this.webServices = {} as WebServices;
this.noteControllerGroup.deinit();
this.iconsController.deinit();
this.webEventObservers.length = 0;
if (source === DeinitSource.SignOut) {
this.bridge.onSignOut();
}
}, 0);
} catch (error) {
console.error('Error while deiniting application', error);
}
}
setWebServices(services: WebServices): void {