Files
standardnotes-app-web/packages/snjs/lib/Services/AppService/ApplicationService.ts
2022-07-06 14:04:30 +02:00

76 lines
1.9 KiB
TypeScript

import { ApplicationEvent } from '@Lib/Application/Event'
import { AbstractService, InternalEventBusInterface } from '@standardnotes/services'
import { SNApplication } from '../../Application/Application'
export class ApplicationService extends AbstractService {
private unsubApp!: () => void
constructor(protected application: SNApplication, 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 */
}
}