73 lines
2.3 KiB
TypeScript
73 lines
2.3 KiB
TypeScript
import * as plugins from './smartstate.plugins.js';
|
|
import { StatePart } from './smartstate.classes.statepart.js';
|
|
|
|
export type TInitMode = 'soft' | 'mandatory' | 'force' | 'persistent';
|
|
|
|
/**
|
|
* Smartstate takes care of providing state
|
|
*/
|
|
export class Smartstate<StatePartNameType extends string> {
|
|
public statePartMap: { [key in StatePartNameType]?: StatePart<StatePartNameType, any> } = {};
|
|
|
|
constructor() {}
|
|
|
|
/**
|
|
* Allows getting and initializing a new statepart
|
|
* initMode === 'soft' it will allow existing stateparts
|
|
* initMode === 'mandatory' will fail if there is an existing statepart
|
|
* initMode === 'force' will overwrite any existing statepart
|
|
* @param statePartNameArg
|
|
* @param initialArg
|
|
* @param initMode
|
|
*/
|
|
public async getStatePart<PayloadType>(
|
|
statePartNameArg: StatePartNameType,
|
|
initialArg?: PayloadType,
|
|
initMode?: TInitMode
|
|
): Promise<StatePart<StatePartNameType, PayloadType>> {
|
|
if (this.statePartMap[statePartNameArg]) {
|
|
if (initialArg && (!initMode || initMode !== 'soft')) {
|
|
throw new Error(
|
|
`${statePartNameArg} already exists, yet you try to set an initial state again`
|
|
);
|
|
}
|
|
return this.statePartMap[statePartNameArg] as StatePart<StatePartNameType, PayloadType>;
|
|
} else {
|
|
if (!initialArg) {
|
|
throw new Error(
|
|
`${statePartNameArg} does not yet exist, yet you don't provide an initial state`
|
|
);
|
|
}
|
|
return this.createStatePart<PayloadType>(statePartNameArg, initialArg, initMode);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Creates a statepart
|
|
* @param statePartName
|
|
* @param initialPayloadArg
|
|
*/
|
|
private async createStatePart<PayloadType>(
|
|
statePartName: StatePartNameType,
|
|
initialPayloadArg: PayloadType,
|
|
initMode?: TInitMode
|
|
): Promise<StatePart<StatePartNameType, PayloadType>> {
|
|
const newState = new StatePart<StatePartNameType, PayloadType>(
|
|
statePartName,
|
|
initMode === 'persistent'
|
|
? {
|
|
dbName: 'smartstate',
|
|
storeName: statePartName,
|
|
}
|
|
: null
|
|
);
|
|
await newState.init();
|
|
const currentState = newState.getState();
|
|
await newState.setState({
|
|
...initialPayloadArg,
|
|
...currentState,
|
|
});
|
|
this.statePartMap[statePartName] = newState;
|
|
return newState;
|
|
}
|
|
} |