feat: handle basic routes (#1784)

This commit is contained in:
Mo
2022-10-13 09:08:03 -05:00
committed by GitHub
parent 794ed7f7d4
commit 3cb016ab1f
27 changed files with 391 additions and 140 deletions

View File

@@ -0,0 +1,50 @@
import {
AbstractService,
ApplicationEvent,
ApplicationInterface,
InternalEventBusInterface,
} from '@standardnotes/services'
import { RouteParser } from './RouteParser'
import { RouteServiceEvent } from './RouteServiceEvent'
export class RouteService extends AbstractService<RouteServiceEvent, RouteParser> {
private unsubApp!: () => void
constructor(
private application: ApplicationInterface,
protected override internalEventBus: InternalEventBusInterface,
) {
super(internalEventBus)
this.addAppEventObserver()
}
override deinit() {
super.deinit()
;(this.application as unknown) = undefined
this.unsubApp()
}
public getRoute(): RouteParser {
return new RouteParser(window.location.href)
}
public removeSettingsFromURLQueryParameters() {
const urlSearchParams = new URLSearchParams(window.location.search)
urlSearchParams.delete('settings')
const newUrl = `${window.location.origin}${window.location.pathname}${urlSearchParams.toString()}`
window.history.replaceState(null, document.title, newUrl)
}
private addAppEventObserver() {
this.unsubApp = this.application.addEventObserver(async (event: ApplicationEvent) => {
if (event === ApplicationEvent.LocalDataLoaded) {
void this.notifyRouteChange()
}
})
}
private notifyRouteChange() {
void this.notifyEvent(RouteServiceEvent.RouteChanged, this.getRoute())
}
}