feat: persist selected tag & note locally (#1851)

This commit is contained in:
Aman Harwara
2022-10-20 18:56:59 +05:30
committed by GitHub
parent 6c47f95748
commit 4432f1cb4c
20 changed files with 421 additions and 116 deletions

View File

@@ -0,0 +1,60 @@
import { WebApplication } from '@/Application/Application'
import { ShouldPersistNoteStateKey } from '@/Components/Preferences/Panes/General/Persistence'
import { ApplicationEvent, InternalEventBus } from '@standardnotes/snjs'
import { CrossControllerEvent } from '../CrossControllerEvent'
const MasterPersistenceKey = 'master-persistence-key'
export enum PersistenceKey {
SelectedItemsController = 'selected-items-controller',
NavigationController = 'navigation-controller',
ItemListController = 'item-list-controller',
}
export type MasterPersistedValue = Record<PersistenceKey, unknown>
export class PersistenceService {
private unsubAppEventObserver: () => void
constructor(private application: WebApplication, private eventBus: InternalEventBus) {
this.unsubAppEventObserver = this.application.addEventObserver(async (eventName) => {
if (!this.application) {
return
}
void this.onAppEvent(eventName)
})
}
async onAppEvent(eventName: ApplicationEvent) {
if (eventName === ApplicationEvent.LocalDataLoaded) {
let shouldHydrateState = this.application.getValue(ShouldPersistNoteStateKey)
if (typeof shouldHydrateState === 'undefined') {
this.application.setValue(ShouldPersistNoteStateKey, true)
shouldHydrateState = true
}
this.eventBus.publish({
type: CrossControllerEvent.HydrateFromPersistedValues,
payload: shouldHydrateState ? this.getPersistedValues() : undefined,
})
}
}
persistValues(values: MasterPersistedValue): void {
if (!this.application.isDatabaseLoaded()) {
return
}
this.application.setValue(MasterPersistenceKey, values)
}
getPersistedValues(): MasterPersistedValue {
return this.application.getValue(MasterPersistenceKey) as MasterPersistedValue
}
deinit() {
this.unsubAppEventObserver()
}
}