fix(core): update

This commit is contained in:
2023-10-03 07:53:28 +02:00
parent de454b4c8d
commit 4abaea84f8
7 changed files with 94 additions and 74 deletions

View File

@ -7,8 +7,30 @@ export class StatePart<TStatePartName, TStatePayload> {
public stateStore: TStatePayload;
private cumulativeDeferred = plugins.smartpromise.cumulativeDefer();
constructor(nameArg: TStatePartName) {
private webStore: plugins.webstore.WebStore<TStatePayload> | null = null; // Add WebStore instance
constructor(nameArg: TStatePartName, webStoreOptions?: plugins.webstore.IWebStoreOptions) {
this.name = nameArg;
// Initialize WebStore if webStoreOptions are provided
if (webStoreOptions) {
this.webStore = new plugins.webstore.WebStore<TStatePayload>(webStoreOptions);
this.initWebStore();
}
}
/**
* initializes the webstore
*/
private async initWebStore() {
if (this.webStore) {
await this.webStore.init();
const storedState = await this.webStore.get(String(this.name));
if (storedState) {
this.stateStore = storedState;
this.notifyChange();
}
}
}
/**
@ -22,9 +44,14 @@ export class StatePart<TStatePartName, TStatePayload> {
* sets the stateStore to the new state
* @param newStateArg
*/
public setState(newStateArg: TStatePayload) {
public async setState(newStateArg: TStatePayload) {
this.stateStore = newStateArg;
this.notifyChange();
// Save state to WebStore if initialized
if (this.webStore) {
await this.webStore.set(String(this.name), newStateArg);
}
}
/**