fix(core): Fix type errors and typos in Smartstate class

This commit is contained in:
Philipp Kunz 2024-10-02 15:59:41 +02:00
parent fd6e3ba8b1
commit c302cbdae4
3 changed files with 151 additions and 15 deletions

134
changelog.md Normal file
View File

@ -0,0 +1,134 @@
# Changelog
## 2024-10-02 - 2.0.18 - fix(core)
Fix type errors and typos in Smartstate class
- Updated type annotation in Smartstate class to ensure StatePartNameType extends string.
- Fixed a typo in the JSDoc comment: 'existing' instead of 'exiting'.
- Corrected improper type casting in the Smartstate class.
## 2024-05-29 - 2.0.17 - Maintenance
General updates and improvements.
- Updated project description
- Multiple updates to `tsconfig`
- Updated `npmextra.json` to include `githost`
## 2023-10-07 - 2.0.16 - Maintenance
General updates and improvements.
- Core update
## 2023-10-04 - 2.0.15 - Maintenance
General updates and improvements.
- Core update
## 2023-10-03 - 2.0.14 to 2.0.10 - Maintenance
General updates and improvements.
- Core updates
## 2023-09-11 - 2.0.9 - Maintenance
General updates and improvements.
- Core update
## 2023-09-11 - 2.0.8 - Maintenance
General updates and improvements.
- Core update
## 2023-07-27 - 2.0.7 - Maintenance
General updates and improvements.
- Core update
## 2023-07-27 - 2.0.6 - Maintenance
General updates and improvements.
- Core update
## 2023-04-13 - 2.0.5 - Maintenance
General updates and improvements.
- Core update
## 2023-04-12 - 2.0.4 - Maintenance
General updates and improvements.
- Core update
## 2023-04-04 - 2.0.3 to 2.0.1 - Maintenance
General updates and improvements.
- Core updates
## 2023-03-15 - 2.0.0 - Major Update
Core update with significant changes.
## 2022-03-25 - 1.0.23 - Major Update
Breaking changes and major updates.
- SWITCH TO ESM
## 2022-01-24 - 1.0.22 - Maintenance
General updates and improvements.
- Core updates
## 2020-11-30 - 1.0.21 to 1.0.20 - Maintenance
General updates and improvements.
- Core updates
## 2020-11-30 - 1.0.19 to 1.0.18 - Maintenance
General updates and improvements.
- Core updates
## 2020-07-27 - 1.0.17 to 1.0.16 - Maintenance
General updates and improvements.
- Core updates
## 2020-05-27 - 1.0.15 - Maintenance
General updates and improvements.
- Core update
## 2020-05-27 - 1.0.14 - Maintenance
General updates and improvements.
- Core update
## 2019-09-25 - 1.0.13 - Maintenance
General updates and improvements.
- Core update
## 2019-09-25 - 1.0.12 - Maintenance
General updates and improvements.
- Core updates
## 2019-04-30 - 1.0.11 to 1.0.10 - Maintenance
General updates and improvements.
- Core updates
## 2019-03-22 - 1.0.9 - Maintenance
General updates and improvements.
- Core update
## 2019-02-27 - 1.0.8 - Minor Update
Minor updates and improvements.
- Updated action generation
- Core update
## 2019-02-21 - 1.0.7 - Initial Release
Initial release of the project.
- Initial core implementation

View File

@ -1,8 +1,8 @@
/** /**
* autocreated commitinfo by @pushrocks/commitinfo * autocreated commitinfo by @push.rocks/commitinfo
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartstate', name: '@push.rocks/smartstate',
version: '2.0.17', version: '2.0.18',
description: 'a package that handles state in a good way' description: 'A package for handling and managing state in applications.'
} }

View File

@ -6,26 +6,26 @@ export type TInitMode = 'soft' | 'mandatory' | 'force' | 'persistent';
/** /**
* Smartstate takes care of providing state * Smartstate takes care of providing state
*/ */
export class Smartstate<StatePartNameType> { export class Smartstate<StatePartNameType extends string> {
public statePartMap: { [key: string]: StatePart<StatePartNameType, any> } = {}; public statePartMap: { [key in StatePartNameType]?: StatePart<StatePartNameType, any> } = {};
constructor() {} constructor() {}
/** /**
* Allows getting and initializing a new statepart * Allows getting and initializing a new statepart
* initMode === 'soft' it will allow existing stateparts * initMode === 'soft' it will allow existing stateparts
* initMode === 'mandatory' will fail if there is an exiting statepart * initMode === 'mandatory' will fail if there is an existing statepart
* initMode === 'force' will overwrite any existing statepart * initMode === 'force' will overwrite any existing statepart
* @param statePartNameArg * @param statePartNameArg
* @param initialArg * @param initialArg
* @param initMode * @param initMode
*/ */
public async getStatePart<PayloadType>( public async getStatePart<PayloadType>(
statePartNameArg: string & StatePartNameType, statePartNameArg: StatePartNameType,
initialArg?: PayloadType, initialArg?: PayloadType,
initMode?: TInitMode initMode?: TInitMode
): Promise<StatePart<StatePartNameType, PayloadType>> { ): Promise<StatePart<StatePartNameType, PayloadType>> {
if (this.statePartMap[statePartNameArg as any]) { if (this.statePartMap[statePartNameArg]) {
if (initialArg && (!initMode || initMode !== 'soft')) { if (initialArg && (!initMode || initMode !== 'soft')) {
throw new Error( throw new Error(
`${statePartNameArg} already exists, yet you try to set an initial state again` `${statePartNameArg} already exists, yet you try to set an initial state again`
@ -43,7 +43,7 @@ export class Smartstate<StatePartNameType> {
} }
/** /**
* creates a statepart * Creates a statepart
* @param statePartName * @param statePartName
* @param initialPayloadArg * @param initialPayloadArg
*/ */
@ -54,10 +54,12 @@ export class Smartstate<StatePartNameType> {
): Promise<StatePart<StatePartNameType, PayloadType>> { ): Promise<StatePart<StatePartNameType, PayloadType>> {
const newState = new StatePart<StatePartNameType, PayloadType>( const newState = new StatePart<StatePartNameType, PayloadType>(
statePartName, statePartName,
initMode === 'persistent' ? { initMode === 'persistent'
? {
dbName: 'smartstate', dbName: 'smartstate',
storeName: statePartName as any, storeName: statePartName,
} : null }
: null
); );
await newState.init(); await newState.init();
const currentState = newState.getState(); const currentState = newState.getState();
@ -65,7 +67,7 @@ export class Smartstate<StatePartNameType> {
...initialPayloadArg, ...initialPayloadArg,
...currentState, ...currentState,
}); });
this.statePartMap[statePartName as any] = newState; this.statePartMap[statePartName] = newState;
return newState; return newState;
} }
} }