fix(core): update

This commit is contained in:
Philipp Kunz 2023-10-03 13:19:38 +02:00
parent 776eba09e9
commit d3332ccb3f
2 changed files with 14 additions and 5 deletions

View File

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

View File

@ -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
*/ */
@ -21,7 +23,7 @@ export class Smartstate<StatePartNameType> {
public getStatePart<PayloadType>( public getStatePart<PayloadType>(
statePartNameArg: string & StatePartNameType, statePartNameArg: string & StatePartNameType,
initialArg?: PayloadType, initialArg?: PayloadType,
initMode?: 'soft' | 'mandatory' | 'force' | 'persistent' initMode?: TInitMode
): StatePart<StatePartNameType, PayloadType> { ): 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')) {
@ -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);
} }
} }
@ -47,9 +49,16 @@ export class Smartstate<StatePartNameType> {
*/ */
private createStatePart<PayloadType>( private createStatePart<PayloadType>(
statePartName: StatePartNameType, statePartName: StatePartNameType,
initialPayloadArg: PayloadType initialPayloadArg: PayloadType,
initMode?: TInitMode
): StatePart<StatePartNameType, PayloadType> { ): StatePart<StatePartNameType, PayloadType> {
const newState = new StatePart<StatePartNameType, PayloadType>(statePartName); const newState = new StatePart<StatePartNameType, PayloadType>(
statePartName,
initMode === 'persistent' ? {
dbName: 'smartstate',
storeName: statePartName as any,
} : null
);
newState.setState(initialPayloadArg); newState.setState(initialPayloadArg);
this.statePartMap[statePartName as any] = newState; this.statePartMap[statePartName as any] = newState;
return newState; return newState;