Files
standardnotes-app-web/packages/snjs/lib/Services/AppService/ApplicationService.ts
2022-08-04 15:13:30 +02:00

82 lines
1.9 KiB
TypeScript

import {
AbstractService,
ApplicationEvent,
ApplicationInterface,
InternalEventBusInterface,
} from '@standardnotes/services'
export class ApplicationService extends AbstractService {
private unsubApp!: () => void
constructor(
protected application: ApplicationInterface,
protected override internalEventBus: InternalEventBusInterface,
) {
super(internalEventBus)
this.addAppEventObserverAfterSubclassesFinishConstructing()
}
override deinit() {
;(this.application as unknown) = undefined
this.unsubApp()
;(this.unsubApp as unknown) = undefined
super.deinit()
}
addAppEventObserverAfterSubclassesFinishConstructing() {
setTimeout(() => {
this.addAppEventObserver()
}, 0)
}
addAppEventObserver() {
if (this.application.isStarted()) {
void this.onAppStart()
}
if (this.application.isLaunched()) {
void this.onAppLaunch()
}
this.unsubApp = this.application.addEventObserver(async (event: ApplicationEvent) => {
await this.onAppEvent(event)
if (event === ApplicationEvent.Started) {
void this.onAppStart()
} else if (event === ApplicationEvent.Launched) {
void this.onAppLaunch()
} else if (event === ApplicationEvent.CompletedFullSync) {
this.onAppFullSync()
} else if (event === ApplicationEvent.CompletedIncrementalSync) {
this.onAppIncrementalSync()
} else if (event === ApplicationEvent.KeyStatusChanged) {
void this.onAppKeyChange()
}
})
}
async onAppEvent(_event: ApplicationEvent) {
/** Optional override */
}
async onAppStart() {
/** Optional override */
}
async onAppLaunch() {
/** Optional override */
}
async onAppKeyChange() {
/** Optional override */
}
onAppIncrementalSync() {
/** Optional override */
}
onAppFullSync() {
/** Optional override */
}
}