fix(smartstate): Fix StateAction trigger method to properly return Promise
This commit is contained in:
63
readme.md
63
readme.md
@@ -1,5 +1,5 @@
|
||||
# @push.rocks/smartstate
|
||||
a package that handles state in a good way
|
||||
A package for handling and managing state in applications
|
||||
|
||||
## Install
|
||||
|
||||
@@ -44,13 +44,17 @@ When creating state parts, you can specify different initialization modes:
|
||||
|
||||
State parts represent separable sections of your state, making it easier to manage and modularize. For example, you may have a state part for user data and another for application settings.
|
||||
|
||||
Define an enum for state part names for better management:
|
||||
Define state part names using either enums or string literal types:
|
||||
|
||||
```typescript
|
||||
// Option 1: Using enums
|
||||
enum AppStateParts {
|
||||
UserState,
|
||||
SettingsState
|
||||
UserState = 'UserState',
|
||||
SettingsState = 'SettingsState'
|
||||
}
|
||||
|
||||
// Option 2: Using string literal types (simpler approach)
|
||||
type AppStateParts = 'UserState' | 'SettingsState';
|
||||
```
|
||||
|
||||
Now, let's create a state part within our `myAppSmartState` instance:
|
||||
@@ -67,10 +71,7 @@ const userStatePart = await myAppSmartState.getStatePart<IUserState>(
|
||||
'soft' // Init mode (optional, defaults to 'soft')
|
||||
);
|
||||
|
||||
// For persistent state parts, you must call init()
|
||||
if (mode === 'persistent') {
|
||||
await userStatePart.init();
|
||||
}
|
||||
// Note: Persistent state parts are automatically initialized internally
|
||||
```
|
||||
|
||||
### Subscribing to State Changes
|
||||
@@ -107,9 +108,27 @@ const loginUserAction = userStatePart.createAction<ILoginPayload>(async (statePa
|
||||
});
|
||||
|
||||
// Dispatch the action to update the state
|
||||
await loginUserAction.trigger({ username: 'johnDoe' });
|
||||
loginUserAction.trigger({ username: 'johnDoe' });
|
||||
// or await the result
|
||||
const newState = await loginUserAction.trigger({ username: 'johnDoe' });
|
||||
```
|
||||
|
||||
### Dispatching Actions
|
||||
|
||||
There are two ways to dispatch actions:
|
||||
|
||||
```typescript
|
||||
// Method 1: Using trigger on the action (returns promise)
|
||||
const newState = await loginUserAction.trigger({ username: 'johnDoe' });
|
||||
// or fire and forget
|
||||
loginUserAction.trigger({ username: 'johnDoe' });
|
||||
|
||||
// Method 2: Using dispatchAction on the state part (returns promise)
|
||||
const newState = await userStatePart.dispatchAction(loginUserAction, { username: 'johnDoe' });
|
||||
```
|
||||
|
||||
Both methods return a Promise with the new state, giving you flexibility in how you handle the result.
|
||||
|
||||
### Additional State Methods
|
||||
|
||||
`StatePart` provides several useful methods for state management:
|
||||
@@ -118,17 +137,18 @@ await loginUserAction.trigger({ username: 'johnDoe' });
|
||||
// Wait for a specific state condition
|
||||
await userStatePart.waitUntilPresent();
|
||||
|
||||
// Wait for a specific property to be present
|
||||
await userStatePart.waitUntilPresent(state => state.username);
|
||||
|
||||
// Setup initial state with async operations
|
||||
await userStatePart.stateSetup(async (state) => {
|
||||
await userStatePart.stateSetup(async (statePart) => {
|
||||
// Perform async initialization
|
||||
const userData = await fetchUserData();
|
||||
return { ...state, ...userData };
|
||||
return { ...statePart.getState(), ...userData };
|
||||
});
|
||||
|
||||
// Batch multiple state changes for cumulative notification
|
||||
userStatePart.notifyChangeCumulative(() => {
|
||||
// Multiple state changes here will result in a single notification
|
||||
});
|
||||
// Defer notification to end of call stack
|
||||
userStatePart.notifyChangeCumulative();
|
||||
```
|
||||
|
||||
### Persistent State with WebStore
|
||||
@@ -142,8 +162,7 @@ const settingsStatePart = await myAppSmartState.getStatePart<ISettingsState>(
|
||||
'persistent' // Mode
|
||||
);
|
||||
|
||||
// Initialize the persistent state (required for persistent mode)
|
||||
await settingsStatePart.init();
|
||||
// Note: init() is called automatically for persistent mode
|
||||
```
|
||||
|
||||
Persistent state automatically:
|
||||
@@ -155,7 +174,7 @@ Persistent state automatically:
|
||||
|
||||
`Smartstate` includes built-in performance optimizations:
|
||||
|
||||
- **State Hash Detection**: Uses SHA256 hashing to detect actual state changes, preventing unnecessary notifications when state values haven't truly changed
|
||||
- **State Change Detection**: Detects actual state changes to prevent unnecessary notifications when state values haven't truly changed
|
||||
- **Cumulative Notifications**: Batch multiple state changes into a single notification using `notifyChangeCumulative()`
|
||||
- **Selective Subscriptions**: Use selectors to subscribe only to specific state properties
|
||||
|
||||
@@ -205,13 +224,13 @@ This repository contains open-source code that is licensed under the MIT License
|
||||
|
||||
### Trademarks
|
||||
|
||||
This project is owned and maintained by Lossless GmbH. The names and logos associated with Lossless GmbH and any related products or services are trademarks of Lossless GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Lossless GmbH's Trademark Guidelines, and any usage must be approved in writing by Lossless GmbH.
|
||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||
|
||||
### Company Information
|
||||
|
||||
Lossless GmbH
|
||||
Task Venture Capital GmbH
|
||||
Registered at District court Bremen HRB 35230 HB, Germany
|
||||
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@lossless.com.
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||
|
||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Lossless GmbH of any derivative works.
|
||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
||||
|
Reference in New Issue
Block a user