simontonsoftware / s-libs

A collection of libraries for any of javascript, rxjs, or angular.
MIT License
43 stars 5 forks source link

[app-state]: Feature request: `PersistedStore` #67

Closed eric-simonton-sama closed 2 years ago

eric-simonton-sama commented 2 years ago

It's a common pattern to combine a RootStore, Persistence and MigrationManager. It would be nice to reduce a little boilerplate. Something like:

export class PersistedStore<T extends VersionedObject> extends RootStore<T> {
  constructor(
    persistenceKey: string,
    defaultState: T,
    migrator: MigrationManager<T>,
  ) {
    const persistence = new Persistence<T>(persistenceKey);
    super(migrator.run(persistence, defaultState));

    this.$.subscribe((state) => persistence.put(state));
  }
}
ersimont commented 2 years ago

Added. Will be available in the next release.