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

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartstate',
version: '2.0.9',
version: '2.0.10',
description: 'a package that handles state in a good way'
}

View File

@ -21,7 +21,7 @@ export class Smartstate<StatePartNameType> {
public getStatePart<PayloadType>(
statePartNameArg: string & StatePartNameType,
initialArg?: PayloadType,
initMode?: 'soft' | 'mandatory' | 'force'
initMode?: 'soft' | 'mandatory' | 'force' | 'persistent'
): StatePart<StatePartNameType, PayloadType> {
if (this.statePartMap[statePartNameArg as any]) {
if (initialArg && (!initMode || initMode !== 'soft')) {

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);
}
}
/**

View File

@ -2,5 +2,6 @@ import * as isohash from '@push.rocks/isohash';
import * as smartjson from '@push.rocks/smartjson';
import * as smartpromise from '@push.rocks/smartpromise';
import * as smartrx from '@push.rocks/smartrx';
import * as webstore from '@push.rocks/webstore';
export { isohash, smartjson, smartpromise, smartrx };
export { isohash, smartjson, smartpromise, smartrx, webstore };