fix(core): update

This commit is contained in:
Philipp Kunz 2023-10-03 16:20:34 +02:00
parent 3a119b50a2
commit fe81307ca6
3 changed files with 11 additions and 7 deletions

View File

@ -18,7 +18,7 @@ tap.test('should create a new SmartState', async () => {
});
tap.test('should create a new StatePart', async () => {
testStatePart = testState.getStatePart<TStatePartPayload>('testStatePart', {
testStatePart = await testState.getStatePart<TStatePartPayload>('testStatePart', {
currentFavorites: [],
deep: {
hi: 2,

View File

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

View File

@ -20,11 +20,11 @@ export class Smartstate<StatePartNameType> {
* @param initialArg
* @param initMode
*/
public getStatePart<PayloadType>(
public async getStatePart<PayloadType>(
statePartNameArg: string & StatePartNameType,
initialArg?: PayloadType,
initMode?: TInitMode
): StatePart<StatePartNameType, PayloadType> {
): Promise<StatePart<StatePartNameType, PayloadType>> {
if (this.statePartMap[statePartNameArg as any]) {
if (initialArg && (!initMode || initMode !== 'soft')) {
throw new Error(
@ -47,11 +47,11 @@ export class Smartstate<StatePartNameType> {
* @param statePartName
* @param initialPayloadArg
*/
private createStatePart<PayloadType>(
private async createStatePart<PayloadType>(
statePartName: StatePartNameType,
initialPayloadArg: PayloadType,
initMode?: TInitMode
): StatePart<StatePartNameType, PayloadType> {
): Promise<StatePart<StatePartNameType, PayloadType>> {
const newState = new StatePart<StatePartNameType, PayloadType>(
statePartName,
initMode === 'persistent' ? {
@ -59,7 +59,11 @@ export class Smartstate<StatePartNameType> {
storeName: statePartName as any,
} : null
);
newState.setState(initialPayloadArg);
const currentState = newState.getState();
await newState.setState({
...initialPayloadArg,
...currentState,
});
this.statePartMap[statePartName as any] = newState;
return newState;
}