Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
024f7f4f8f | |||
8f1cba5078 | |||
188f8057bf | |||
99cb86258e | |||
83976fa3f4 | |||
fe81307ca6 | |||
3a119b50a2 | |||
d3332ccb3f | |||
776eba09e9 | |||
b41ff5d495 | |||
5f5f9db884 | |||
876042b446 |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartstate",
|
"name": "@push.rocks/smartstate",
|
||||||
"version": "2.0.10",
|
"version": "2.0.16",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "a package that handles state in a good way",
|
"description": "a package that handles state in a good way",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
|
@ -18,7 +18,7 @@ tap.test('should create a new SmartState', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should create a new StatePart', async () => {
|
tap.test('should create a new StatePart', async () => {
|
||||||
testStatePart = testState.getStatePart<TStatePartPayload>('testStatePart', {
|
testStatePart = await testState.getStatePart<TStatePartPayload>('testStatePart', {
|
||||||
currentFavorites: [],
|
currentFavorites: [],
|
||||||
deep: {
|
deep: {
|
||||||
hi: 2,
|
hi: 2,
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartstate',
|
name: '@push.rocks/smartstate',
|
||||||
version: '2.0.10',
|
version: '2.0.16',
|
||||||
description: 'a package that handles state in a good way'
|
description: 'a package that handles state in a good way'
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import * as plugins from './smartstate.plugins.js';
|
import * as plugins from './smartstate.plugins.js';
|
||||||
import { StatePart } from './smartstate.classes.statepart.js';
|
import { StatePart } from './smartstate.classes.statepart.js';
|
||||||
|
|
||||||
|
export type TInitMode = 'soft' | 'mandatory' | 'force' | 'persistent';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Smartstate takes care of providing state
|
* Smartstate takes care of providing state
|
||||||
*/
|
*/
|
||||||
@ -18,11 +20,11 @@ export class Smartstate<StatePartNameType> {
|
|||||||
* @param initialArg
|
* @param initialArg
|
||||||
* @param initMode
|
* @param initMode
|
||||||
*/
|
*/
|
||||||
public getStatePart<PayloadType>(
|
public async getStatePart<PayloadType>(
|
||||||
statePartNameArg: string & StatePartNameType,
|
statePartNameArg: string & StatePartNameType,
|
||||||
initialArg?: PayloadType,
|
initialArg?: PayloadType,
|
||||||
initMode?: 'soft' | 'mandatory' | 'force' | 'persistent'
|
initMode?: TInitMode
|
||||||
): StatePart<StatePartNameType, PayloadType> {
|
): Promise<StatePart<StatePartNameType, PayloadType>> {
|
||||||
if (this.statePartMap[statePartNameArg as any]) {
|
if (this.statePartMap[statePartNameArg as any]) {
|
||||||
if (initialArg && (!initMode || initMode !== 'soft')) {
|
if (initialArg && (!initMode || initMode !== 'soft')) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@ -36,7 +38,7 @@ export class Smartstate<StatePartNameType> {
|
|||||||
`${statePartNameArg} does not yet exist, yet you don't provide an initial state`
|
`${statePartNameArg} does not yet exist, yet you don't provide an initial state`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return this.createStatePart<PayloadType>(statePartNameArg, initialArg);
|
return this.createStatePart<PayloadType>(statePartNameArg, initialArg, initMode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,12 +47,24 @@ export class Smartstate<StatePartNameType> {
|
|||||||
* @param statePartName
|
* @param statePartName
|
||||||
* @param initialPayloadArg
|
* @param initialPayloadArg
|
||||||
*/
|
*/
|
||||||
private createStatePart<PayloadType>(
|
private async createStatePart<PayloadType>(
|
||||||
statePartName: StatePartNameType,
|
statePartName: StatePartNameType,
|
||||||
initialPayloadArg: PayloadType
|
initialPayloadArg: PayloadType,
|
||||||
): StatePart<StatePartNameType, PayloadType> {
|
initMode?: TInitMode
|
||||||
const newState = new StatePart<StatePartNameType, PayloadType>(statePartName);
|
): Promise<StatePart<StatePartNameType, PayloadType>> {
|
||||||
newState.setState(initialPayloadArg);
|
const newState = new StatePart<StatePartNameType, PayloadType>(
|
||||||
|
statePartName,
|
||||||
|
initMode === 'persistent' ? {
|
||||||
|
dbName: 'smartstate',
|
||||||
|
storeName: statePartName as any,
|
||||||
|
} : null
|
||||||
|
);
|
||||||
|
await newState.init();
|
||||||
|
const currentState = newState.getState();
|
||||||
|
await newState.setState({
|
||||||
|
...initialPayloadArg,
|
||||||
|
...currentState,
|
||||||
|
});
|
||||||
this.statePartMap[statePartName as any] = newState;
|
this.statePartMap[statePartName as any] = newState;
|
||||||
return newState;
|
return newState;
|
||||||
}
|
}
|
||||||
|
@ -7,23 +7,24 @@ export class StatePart<TStatePartName, TStatePayload> {
|
|||||||
public stateStore: TStatePayload;
|
public stateStore: TStatePayload;
|
||||||
private cumulativeDeferred = plugins.smartpromise.cumulativeDefer();
|
private cumulativeDeferred = plugins.smartpromise.cumulativeDefer();
|
||||||
|
|
||||||
|
private webStoreOptions: plugins.webstore.IWebStoreOptions;
|
||||||
private webStore: plugins.webstore.WebStore<TStatePayload> | null = null; // Add WebStore instance
|
private webStore: plugins.webstore.WebStore<TStatePayload> | null = null; // Add WebStore instance
|
||||||
|
|
||||||
constructor(nameArg: TStatePartName, webStoreOptions?: plugins.webstore.IWebStoreOptions) {
|
constructor(nameArg: TStatePartName, webStoreOptionsArg?: plugins.webstore.IWebStoreOptions) {
|
||||||
this.name = nameArg;
|
this.name = nameArg;
|
||||||
|
|
||||||
// Initialize WebStore if webStoreOptions are provided
|
// Initialize WebStore if webStoreOptions are provided
|
||||||
if (webStoreOptions) {
|
if (webStoreOptionsArg) {
|
||||||
this.webStore = new plugins.webstore.WebStore<TStatePayload>(webStoreOptions);
|
this.webStoreOptions = webStoreOptionsArg;
|
||||||
this.initWebStore();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* initializes the webstore
|
* initializes the webstore
|
||||||
*/
|
*/
|
||||||
private async initWebStore() {
|
public async init() {
|
||||||
if (this.webStore) {
|
if (this.webStoreOptions) {
|
||||||
|
this.webStore = new plugins.webstore.WebStore<TStatePayload>(this.webStoreOptions);
|
||||||
await this.webStore.init();
|
await this.webStore.init();
|
||||||
const storedState = await this.webStore.get(String(this.name));
|
const storedState = await this.webStore.get(String(this.name));
|
||||||
if (storedState) {
|
if (storedState) {
|
||||||
@ -116,10 +117,11 @@ export class StatePart<TStatePartName, TStatePayload> {
|
|||||||
/**
|
/**
|
||||||
* dispatches an action on the statepart level
|
* dispatches an action on the statepart level
|
||||||
*/
|
*/
|
||||||
public async dispatchAction<T>(stateAction: StateAction<TStatePayload, T>, actionPayload: T) {
|
public async dispatchAction<T>(stateAction: StateAction<TStatePayload, T>, actionPayload: T): Promise<TStatePayload> {
|
||||||
await this.cumulativeDeferred.promise;
|
await this.cumulativeDeferred.promise;
|
||||||
const newState = await stateAction.actionDef(this, actionPayload);
|
const newState = await stateAction.actionDef(this, actionPayload);
|
||||||
this.setState(newState);
|
await this.setState(newState);
|
||||||
|
return this.getState();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user