Compare commits

...

17 Commits

Author SHA1 Message Date
39aa63bdb3 v2.0.31
Some checks failed
Default (tags) / security (push) Successful in 33s
Default (tags) / test (push) Failing after 1m21s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-02-27 10:18:18 +00:00
c1aa4eae5e fix(deps): bump devDependencies and fix README license path 2026-02-27 10:18:18 +00:00
d8decdb3e5 v2.0.30
Some checks failed
Default (tags) / security (push) Successful in 24s
Default (tags) / test (push) Failing after 39s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-02-02 01:07:38 +00:00
03cfee2003 fix(config): update npmextra configuration and improve README: rename package keys, add release registry config, clarify waitUntilPresent timeout and notification/persistence behavior 2026-02-02 01:07:38 +00:00
f6a3e71f0a v2.0.29
Some checks failed
Default (tags) / security (push) Successful in 29s
Default (tags) / test (push) Failing after 41s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-02-02 01:05:57 +00:00
6436370abc fix(smartstate): prevent duplicate statepart creation and fix persistence/notification race conditions 2026-02-02 01:05:57 +00:00
eb1c48bee4 v2.0.28
Some checks failed
Default (tags) / security (push) Successful in 29s
Default (tags) / test (push) Failing after 41s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-02-02 00:52:23 +00:00
05417ed4c3 fix(deps): bump devDependencies and dependencies, add tsbundle build config, update docs, and reorganize tests 2026-02-02 00:52:23 +00:00
1d74a7f465 2.0.27
Some checks failed
Default (tags) / security (push) Successful in 44s
Default (tags) / test (push) Successful in 1m13s
Default (tags) / release (push) Failing after 59s
Default (tags) / metadata (push) Successful in 1m15s
2025-09-12 22:08:35 +00:00
81ca32cdef fix(StatePart): Use stable JSON stringify for state hashing; update dependencies and tooling 2025-09-12 22:08:35 +00:00
07bfbfd393 2.0.26
Some checks failed
Default (tags) / security (push) Successful in 40s
Default (tags) / test (push) Successful in 1m6s
Default (tags) / release (push) Failing after 58s
Default (tags) / metadata (push) Successful in 1m15s
2025-08-16 13:09:13 +00:00
aa411072f2 fix(ci): checksum 2025-08-16 13:09:13 +00:00
02575e8baf fix(core): Fix state initialization, hash detection, and validation - v2.0.25
Some checks failed
Default (tags) / security (push) Successful in 42s
Default (tags) / test (push) Successful in 1m8s
Default (tags) / release (push) Failing after 59s
Default (tags) / metadata (push) Successful in 1m8s
2025-07-29 19:26:03 +00:00
09fc53aaff fix(core): Multiple fixes and improvements for version 2.0.24
Some checks failed
Default (tags) / security (push) Successful in 43s
Default (tags) / test (push) Successful in 1m1s
Default (tags) / release (push) Failing after 58s
Default (tags) / metadata (push) Successful in 1m9s
2025-07-19 08:19:59 +00:00
bcb58dd012 chore(workspace): Add pnpm workspace configuration for built-only dependencies 2025-07-19 08:16:36 +00:00
f0064bd94b 2.0.23
Some checks failed
Default (tags) / security (push) Successful in 46s
Default (tags) / test (push) Successful in 51s
Default (tags) / release (push) Failing after 45s
Default (tags) / metadata (push) Successful in 55s
2025-07-19 07:30:55 +00:00
e9c527a9dc fix(ci): Update CI workflows to use new container registry and npmci package name 2025-07-19 07:30:55 +00:00
14 changed files with 20200 additions and 3368 deletions

View File

@@ -6,8 +6,8 @@ on:
- '**' - '**'
env: env:
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci IMAGE: code.foss.global/host.today/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@code.foss.global/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}} NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}} NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}} NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
@@ -26,7 +26,7 @@ jobs:
- name: Install pnpm and npmci - name: Install pnpm and npmci
run: | run: |
pnpm install -g pnpm pnpm install -g pnpm
pnpm install -g @shipzone/npmci pnpm install -g @ship.zone/npmci
- name: Run npm prepare - name: Run npm prepare
run: npmci npm prepare run: npmci npm prepare

View File

@@ -6,8 +6,8 @@ on:
- '*' - '*'
env: env:
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci IMAGE: code.foss.global/host.today/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@code.foss.global/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}} NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}} NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}} NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
@@ -26,7 +26,7 @@ jobs:
- name: Prepare - name: Prepare
run: | run: |
pnpm install -g pnpm pnpm install -g pnpm
pnpm install -g @shipzone/npmci pnpm install -g @ship.zone/npmci
npmci npm prepare npmci npm prepare
- name: Audit production dependencies - name: Audit production dependencies
@@ -54,7 +54,7 @@ jobs:
- name: Prepare - name: Prepare
run: | run: |
pnpm install -g pnpm pnpm install -g pnpm
pnpm install -g @shipzone/npmci pnpm install -g @ship.zone/npmci
npmci npm prepare npmci npm prepare
- name: Test stable - name: Test stable
@@ -82,7 +82,7 @@ jobs:
- name: Prepare - name: Prepare
run: | run: |
pnpm install -g pnpm pnpm install -g pnpm
pnpm install -g @shipzone/npmci pnpm install -g @ship.zone/npmci
npmci npm prepare npmci npm prepare
- name: Release - name: Release
@@ -104,7 +104,7 @@ jobs:
- name: Prepare - name: Prepare
run: | run: |
pnpm install -g pnpm pnpm install -g pnpm
pnpm install -g @shipzone/npmci pnpm install -g @ship.zone/npmci
npmci npm prepare npmci npm prepare
- name: Code quality - name: Code quality

View File

@@ -1,5 +1,87 @@
# Changelog # Changelog
## 2026-02-27 - 2.0.31 - fix(deps)
bump devDependencies and fix README license path
- Bump @git.zone/tsbundle from ^2.8.3 to ^2.9.0
- Bump @types/node from ^25.2.0 to ^25.3.2
- Update documented dependency set/version to v2.0.30 in readme.hints.md
- Fix README license file path from LICENSE to license in readme.md
## 2026-02-02 - 2.0.30 - fix(config)
update npmextra configuration and improve README: rename package keys, add release registry config, clarify waitUntilPresent timeout and notification/persistence behavior
- Renamed npmextra keys: 'gitzone' → '@git.zone/cli' and 'tsdoc' → '@git.zone/tsdoc'
- Added release configuration for @git.zone/cli including registries (verdaccio and npm) and accessLevel
- Removed top-level 'npmci' section
- Added new '@ship.zone/szci' entry with npmGlobalTools
- README: added waitUntilPresent timeout example with error handling
- README: clarified notifyChangeCumulative is debounced and documented persistence behavior (merge with defaults, atomic writes)
- README: documented concurrency/race-condition safety and timeout support for waitUntilPresent
## 2026-02-02 - 2.0.29 - fix(smartstate)
prevent duplicate statepart creation and fix persistence/notification race conditions
- Add pendingStatePartCreation map to deduplicate concurrent createStatePart calls
- Adjust init handling so 'force' falls through to creation and concurrent creations are serialized
- Merge persisted state with initial payload in 'persistent' initMode, with persisted values taking precedence
- Persist to WebStore before updating in-memory state to ensure atomicity
- Debounce cumulative notifications via pendingCumulativeNotification to avoid duplicate notifications
- Log selector errors instead of silently swallowing exceptions
- Add optional timeout to waitUntilPresent and ensure subscriptions and timeouts are cleaned up to avoid indefinite waits
- Await setState when performing chained state updates to ensure ordering and avoid race conditions
## 2026-02-02 - 2.0.28 - fix(deps)
bump devDependencies and dependencies, add tsbundle build config, update docs, and reorganize tests
- Bumped @git.zone/tsbuild to ^4.1.2, @git.zone/tsbundle to ^2.8.3, @git.zone/tsrun to ^2.0.1, @git.zone/tstest to ^3.1.8, and @types/node to ^25.2.0
- Upgraded @push.rocks/smartjson to ^6.0.0
- Added @git.zone/tsbundle bundle configuration to npmextra.json for building a dist bundle
- Removed pnpm-workspace.yaml entries (cleaned workspace constraints)
- Updated readme and readme.hints (docs formatting, version bumped to v2.0.28, issue reporting/security section and dependency list)
- Reorganized tests: removed *.both.ts variants and added consolidated test files under test/ (test.ts, test.initialization.ts)
## 2025-09-12 - 2.0.27 - fix(StatePart)
Use stable JSON stringify for state hashing; update dependencies and tooling
- Replace smartjson.stringify with smartjson.stableOneWayStringify when creating SHA256 state hashes to ensure deterministic hashing and avoid duplicate notifications for semantically identical states.
- Bump runtime dependencies: @push.rocks/smarthash -> ^3.2.6, @push.rocks/smartjson -> ^5.2.0.
- Bump dev tooling versions: @git.zone/tsbuild -> ^2.6.8, @git.zone/tsbundle -> ^2.5.1, @git.zone/tstest -> ^2.3.8.
- Add local .claude/settings.local.json configuration for allowed permissions (local tooling/settings file).
## 2025-08-16 - 2.0.26 - fix(ci)
Add local Claude settings file to allow helper permissions for common local commands
- Added .claude/settings.local.json to grant local helper permissions for tooling
- Allowed commands: Bash(tsx:*), Bash(tstest test:*), Bash(git add:*), Bash(git tag:*)
- No changes to source code or runtime behavior; tooling/config only
## 2025-07-29 - 2.0.25 - fix(core)
Major state initialization and validation improvements
- Fixed state hash bug: Now properly compares hash values instead of storing state objects
- Fixed state initialization merge order: Initial state now correctly takes precedence over stored state
- Improved type safety: stateStore properly typed as potentially undefined
- Simplified init mode logic with clear behavior for 'soft', 'mandatory', 'force', and 'persistent'
- Added state validation with extensible validateState() method
- Made notifyChange() async to support proper hash comparison
- Enhanced select() to filter undefined states automatically
- Added comprehensive test suite for state initialization scenarios
- Updated documentation with clearer examples and improved readme
## 2025-07-19 - 2.0.24 - fix(core)
Multiple fixes and improvements
- Fixed StateAction trigger method to properly return Promise<TStateType>
- Updated CI workflows to use new container registry and npmci package name
- Added pnpm workspace configuration for built-only dependencies
## 2025-07-19 - 2.0.23 - fix(ci)
Update CI workflows to use new container registry and npmci package name
- Changed CI image from 'registry.gitlab.com/hosttoday/ht-docker-node:npmci' to 'code.foss.global/host.today/ht-docker-node:npmci'
- Replaced npmci installation command from '@shipzone/npmci' to '@ship.zone/npmci' in workflow configurations
## 2025-07-19 - 2.0.22 - fix(smartstate) ## 2025-07-19 - 2.0.22 - fix(smartstate)
Fix StateAction trigger method to properly return Promise Fix StateAction trigger method to properly return Promise

View File

@@ -1,9 +1,16 @@
{ {
"npmci": { "@git.zone/tsbundle": {
"npmGlobalTools": [], "bundles": [
"npmAccessLevel": "public" {
"from": "./ts/index.ts",
"to": "./dist_bundle/bundle.js",
"outputMode": "bundle",
"bundler": "esbuild",
"production": true
}
]
}, },
"gitzone": { "@git.zone/cli": {
"projectType": "npm", "projectType": "npm",
"module": { "module": {
"githost": "code.foss.global", "githost": "code.foss.global",
@@ -24,9 +31,19 @@
"asynchronous state", "asynchronous state",
"cumulative notification" "cumulative notification"
] ]
},
"release": {
"registries": [
"https://verdaccio.lossless.digital",
"https://registry.npmjs.org"
],
"accessLevel": "public"
} }
}, },
"tsdoc": { "@git.zone/tsdoc": {
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis 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.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy 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.\n" "legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis 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.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy 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.\n"
},
"@ship.zone/szci": {
"npmGlobalTools": []
} }
} }

16523
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartstate", "name": "@push.rocks/smartstate",
"version": "2.0.22", "version": "2.0.31",
"private": false, "private": false,
"description": "A package for handling and managing state in applications.", "description": "A package for handling and managing state in applications.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
@@ -14,17 +14,17 @@
"buildDocs": "tsdoc" "buildDocs": "tsdoc"
}, },
"devDependencies": { "devDependencies": {
"@git.zone/tsbuild": "^2.6.4", "@git.zone/tsbuild": "^4.1.2",
"@git.zone/tsbundle": "^2.4.0", "@git.zone/tsbundle": "^2.9.0",
"@git.zone/tsrun": "^1.3.3", "@git.zone/tsrun": "^2.0.1",
"@git.zone/tstest": "^2.3.1", "@git.zone/tstest": "^3.1.8",
"@push.rocks/tapbundle": "^6.0.3", "@push.rocks/tapbundle": "^6.0.3",
"@types/node": "^22.7.4" "@types/node": "^25.3.2"
}, },
"dependencies": { "dependencies": {
"@push.rocks/lik": "^6.2.2", "@push.rocks/lik": "^6.2.2",
"@push.rocks/smarthash": "^3.2.0", "@push.rocks/smarthash": "^3.2.6",
"@push.rocks/smartjson": "^5.0.20", "@push.rocks/smartjson": "^6.0.0",
"@push.rocks/smartpromise": "^4.2.3", "@push.rocks/smartpromise": "^4.2.3",
"@push.rocks/smartrx": "^3.0.10", "@push.rocks/smartrx": "^3.0.10",
"@push.rocks/webstore": "^2.0.20" "@push.rocks/webstore": "^2.0.20"

6319
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,39 +1,60 @@
# Smartstate Implementation Notes # Smartstate Implementation Notes
## Current API (as of analysis) ## Current API (as of v2.0.28+)
### State Part Initialization ### State Part Initialization
- State parts can be created with different init modes: 'soft', 'mandatory', 'force', 'persistent' - State parts can be created with different init modes: 'soft' (default), 'mandatory', 'force', 'persistent'
- 'soft' - returns existing state part if exists, creates new if not
- 'mandatory' - requires state part to not exist, fails if it does
- 'force' - always creates new state part, overwriting any existing
- 'persistent' - like 'soft' but with WebStore persistence (IndexedDB)
- Persistent mode automatically calls init() internally - no need to call it manually - Persistent mode automatically calls init() internally - no need to call it manually
- WebStore integration for persistent state uses IndexedDB - State merge order fixed: initial state takes precedence over stored state
### Actions ### Actions
- Actions are created with `createAction()` method - Actions are created with `createAction()` method
- Two ways to dispatch actions: - Two ways to dispatch actions:
1. `stateAction.trigger(payload)` - returns Promise<TStatePayload> 1. `stateAction.trigger(payload)` - returns Promise<TStatePayload>
2. `await statePart.dispatchAction(stateAction, payload)` - returns Promise<TStatePayload> 2. `await statePart.dispatchAction(stateAction, payload)` - returns Promise<TStatePayload>
- Both methods now return the same Promise, providing flexibility in usage - Both methods return the same Promise, providing flexibility in usage
### State Management Methods ### State Management Methods
- `select()` - returns Observable with startWith current state - `select()` - returns Observable with startWith current state, filters undefined states
- `waitUntilPresent()` - waits for specific state condition - `waitUntilPresent()` - waits for specific state condition
- `stateSetup()` - async state initialization with cumulative defer - `stateSetup()` - async state initialization with cumulative defer
- `notifyChangeCumulative()` - defers notification to end of call stack (no callback parameter) - `notifyChangeCumulative()` - defers notification to end of call stack
- `getState()` - returns current state or undefined
- `setState()` - validates state before setting, notifies only on actual changes
### State Hash Detection ### State Hash Detection
- Uses SHA256 hash to detect actual state changes - Uses SHA256 hash to detect actual state changes
- Bug: Currently stores the state object itself as hash instead of the actual hash - Fixed: Hash comparison now properly awaits async hash calculation
- This prevents proper duplicate notification prevention - Prevents duplicate notifications for identical state values
- `notifyChange()` is now async to support proper hash comparison
### State Validation
- Basic validation ensures state is not null/undefined
- `validateState()` method can be overridden in subclasses for custom validation
- Validation runs on both setState() and when loading from persistent storage
### Type System ### Type System
- Can use either enums or string literal types for state part names - Can use either enums or string literal types for state part names
- Test uses simple string types: `type TMyStateParts = 'testStatePart'` - Test uses simple string types: `type TMyStateParts = 'testStatePart'`
- State can be undefined initially, handled properly in select() and other methods
## Fixed Issues in Documentation ## Recent Fixes (v2.0.24+)
1. Updated trigger() to return Promise (API enhancement) 1. Fixed state hash bug - now properly compares hash values instead of promises
2. Added dispatchAction as alternative method 2. Fixed state initialization merge order - initial state now takes precedence
3. Corrected notifyChangeCumulative usage 3. Ensured stateStore is properly typed as potentially undefined
4. Clarified persistent mode auto-init 4. Simplified init mode logic with clear behavior for each mode
5. Added stateSetup documentation 5. Added state validation with extensible validateState() method
6. Fixed state hash detection description 6. Made notifyChange() async to support proper hash comparison
7. Both trigger() and dispatchAction() now return Promise for consistency 7. Updated select() to filter undefined states
## Dependency Versions (v2.0.30)
- @git.zone/tsbuild: ^4.1.2
- @git.zone/tsbundle: ^2.9.0
- @git.zone/tsrun: ^2.0.1
- @git.zone/tstest: ^3.1.8
- @push.rocks/smartjson: ^6.0.0
- @types/node: ^25.3.2

195
readme.md
View File

@@ -1,23 +1,33 @@
# @push.rocks/smartstate # @push.rocks/smartstate
A package for handling and managing state in applications
A powerful TypeScript library for elegant state management using RxJS and reactive programming patterns 🚀
## Issue Reporting and Security
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
## Install ## Install
To install `@push.rocks/smartstate`, you can use pnpm (Performant Node Package Manager). Run the following command in your terminal: To install `@push.rocks/smartstate`, you can use pnpm, npm, or yarn:
```bash ```bash
# Using pnpm (recommended)
pnpm install @push.rocks/smartstate --save pnpm install @push.rocks/smartstate --save
```
This will add `@push.rocks/smartstate` to your project's dependencies. # Using npm
npm install @push.rocks/smartstate --save
# Using yarn
yarn add @push.rocks/smartstate
```
## Usage ## Usage
The `@push.rocks/smartstate` library provides an elegant way to handle state within your JavaScript or TypeScript projects, leveraging the power of Reactive Extensions (RxJS) and a structured state management strategy. In the following sections, we will explore the comprehensive capabilities of this package and how to effectively use them in various scenarios, ensuring a robust state management pattern in your applications. The `@push.rocks/smartstate` library provides an elegant way to handle state within your JavaScript or TypeScript projects, leveraging the power of Reactive Extensions (RxJS) and a structured state management strategy.
### Getting Started ### Getting Started
First, let's import the necessary components from the library: Import the necessary components from the library:
```typescript ```typescript
import { Smartstate, StatePart, StateAction } from '@push.rocks/smartstate'; import { Smartstate, StatePart, StateAction } from '@push.rocks/smartstate';
@@ -25,7 +35,7 @@ import { Smartstate, StatePart, StateAction } from '@push.rocks/smartstate';
### Creating a SmartState Instance ### Creating a SmartState Instance
`Smartstate` acts as the container for your state parts. You can consider it as the root of your state management structure. `Smartstate` acts as the container for your state parts. Think of it as the root of your state management structure:
```typescript ```typescript
const myAppSmartState = new Smartstate<YourStatePartNamesEnum>(); const myAppSmartState = new Smartstate<YourStatePartNamesEnum>();
@@ -35,16 +45,16 @@ const myAppSmartState = new Smartstate<YourStatePartNamesEnum>();
When creating state parts, you can specify different initialization modes: When creating state parts, you can specify different initialization modes:
- **`'soft'`** - Allows existing state parts to remain (default behavior) | Mode | Description |
- **`'mandatory'`** - Fails if there's an existing state part with the same name |------|-------------|
- **`'force'`** - Overwrites any existing state part | `'soft'` | Default. Returns existing state part if it exists, creates new if not |
- **`'persistent'`** - Enables WebStore persistence using IndexedDB | `'mandatory'` | Requires state part to not exist, throws error if it does |
| `'force'` | Always creates new state part, overwriting any existing one |
| `'persistent'` | Like 'soft' but with WebStore persistence using IndexedDB |
### Defining State Parts ### Defining State Parts
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. State parts represent separable sections of your state, making it easier to manage and modularize. Define state part names using either enums or string literal types:
Define state part names using either enums or string literal types:
```typescript ```typescript
// Option 1: Using enums // Option 1: Using enums
@@ -57,7 +67,7 @@ enum AppStateParts {
type AppStateParts = 'UserState' | 'SettingsState'; type AppStateParts = 'UserState' | 'SettingsState';
``` ```
Now, let's create a state part within our `myAppSmartState` instance: Create a state part within your `Smartstate` instance:
```typescript ```typescript
interface IUserState { interface IUserState {
@@ -70,21 +80,20 @@ const userStatePart = await myAppSmartState.getStatePart<IUserState>(
{ isLoggedIn: false }, // Initial state { isLoggedIn: false }, // Initial state
'soft' // Init mode (optional, defaults to 'soft') 'soft' // Init mode (optional, defaults to 'soft')
); );
// Note: Persistent state parts are automatically initialized internally
``` ```
### Subscribing to State Changes ### Subscribing to State Changes
You can subscribe to changes in a state part to perform actions accordingly: Subscribe to changes in a state part to perform actions accordingly:
```typescript ```typescript
// The select() method automatically filters out undefined states
userStatePart.select().subscribe((currentState) => { userStatePart.select().subscribe((currentState) => {
console.log(`User Logged In: ${currentState.isLoggedIn}`); console.log(`User Logged In: ${currentState.isLoggedIn}`);
}); });
``` ```
If you need to select a specific part of your state, you can pass a selector function: Select a specific part of your state with a selector function:
```typescript ```typescript
userStatePart.select(state => state.username).subscribe((username) => { userStatePart.select(state => state.username).subscribe((username) => {
@@ -108,8 +117,6 @@ const loginUserAction = userStatePart.createAction<ILoginPayload>(async (statePa
}); });
// Dispatch the action to update the state // Dispatch the action to update the state
loginUserAction.trigger({ username: 'johnDoe' });
// or await the result
const newState = await loginUserAction.trigger({ username: 'johnDoe' }); const newState = await loginUserAction.trigger({ username: 'johnDoe' });
``` ```
@@ -120,34 +127,44 @@ There are two ways to dispatch actions:
```typescript ```typescript
// Method 1: Using trigger on the action (returns promise) // Method 1: Using trigger on the action (returns promise)
const newState = await loginUserAction.trigger({ username: 'johnDoe' }); 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) // Method 2: Using dispatchAction on the state part (returns promise)
const newState = await userStatePart.dispatchAction(loginUserAction, { username: 'johnDoe' }); 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. Both methods return a Promise with the new state payload.
### Additional State Methods ### Additional State Methods
`StatePart` provides several useful methods for state management: `StatePart` provides several useful methods for state management:
```typescript ```typescript
// Wait for a specific state condition // Get current state (may be undefined initially)
const currentState = userStatePart.getState();
if (currentState) {
console.log('Current user:', currentState.username);
}
// Wait for state to be present
await userStatePart.waitUntilPresent(); await userStatePart.waitUntilPresent();
// Wait for a specific property to be present // Wait for a specific property to be present
await userStatePart.waitUntilPresent(state => state.username); await userStatePart.waitUntilPresent(state => state.username);
// Wait with a timeout (throws error if condition not met within timeout)
try {
await userStatePart.waitUntilPresent(state => state.username, 5000); // 5 second timeout
} catch (error) {
console.error('Timed out waiting for username');
}
// Setup initial state with async operations // Setup initial state with async operations
await userStatePart.stateSetup(async (statePart) => { await userStatePart.stateSetup(async (statePart) => {
// Perform async initialization
const userData = await fetchUserData(); const userData = await fetchUserData();
return { ...statePart.getState(), ...userData }; return { ...statePart.getState(), ...userData };
}); });
// Defer notification to end of call stack // Defer notification to end of call stack (debounced)
userStatePart.notifyChangeCumulative(); userStatePart.notifyChangeCumulative();
``` ```
@@ -158,25 +175,43 @@ userStatePart.notifyChangeCumulative();
```typescript ```typescript
const settingsStatePart = await myAppSmartState.getStatePart<ISettingsState>( const settingsStatePart = await myAppSmartState.getStatePart<ISettingsState>(
AppStateParts.SettingsState, AppStateParts.SettingsState,
{ theme: 'light' }, // Initial state { theme: 'light' }, // Initial/default state
'persistent' // Mode 'persistent' // Mode
); );
// Note: init() is called automatically for persistent mode
``` ```
Persistent state automatically: Persistent state automatically:
- Saves state changes to IndexedDB - Saves state changes to IndexedDB
- Restores state on application restart - Restores state on application restart
- Manages storage with configurable database and store names - Merges persisted values with defaults (persisted values take precedence)
- Ensures atomic writes (persistence happens before memory update)
### State Validation
`Smartstate` includes built-in state validation to ensure data integrity:
```typescript
// Basic validation (built-in) ensures state is not null or undefined
await userStatePart.setState(null); // Throws error: Invalid state structure
// Custom validation by extending StatePart
class ValidatedStatePart<T> extends StatePart<string, T> {
protected validateState(stateArg: any): stateArg is T {
return super.validateState(stateArg) && /* your validation */;
}
}
```
### Performance Optimization ### Performance Optimization
`Smartstate` includes built-in performance optimizations: `Smartstate` includes advanced performance optimizations:
- **State Change Detection**: Detects actual state changes to prevent unnecessary notifications when state values haven't truly changed - **🔒 Async State Hash Detection**: Uses SHA256 hashing to detect actual state changes, preventing unnecessary notifications when state values haven't truly changed
- **Cumulative Notifications**: Batch multiple state changes into a single notification using `notifyChangeCumulative()` - **🚫 Duplicate Prevention**: Identical state updates are automatically filtered out
- **Selective Subscriptions**: Use selectors to subscribe only to specific state properties - **📦 Cumulative Notifications**: Batch multiple state changes into a single notification using `notifyChangeCumulative()` with automatic debouncing
- **🎯 Selective Subscriptions**: Use selectors to subscribe only to specific state properties
- **✨ Undefined State Filtering**: The `select()` method automatically filters out undefined states
- **⚡ Concurrent Access Safety**: Prevents race conditions when multiple calls request the same state part simultaneously
### RxJS Integration ### RxJS Integration
@@ -202,35 +237,95 @@ userStatePart.select(state => state.username)
}); });
``` ```
### Comprehensive Usage ### Complete Example
Putting it all together, `@push.rocks/smartstate` offers a flexible and powerful pattern for managing application state. By modularizing state parts, subscribing to state changes, and controlling state modifications through actions, developers can maintain a clean and scalable architecture. Combining these strategies with persistent states unlocks the full potential for creating dynamic and user-friendly applications. Here's a comprehensive example showcasing the power of `@push.rocks/smartstate`:
Key features: ```typescript
- **Type-safe state management** with full TypeScript support import { Smartstate, StatePart, StateAction } from '@push.rocks/smartstate';
- **Reactive state updates** using RxJS observables
- **Persistent state** with IndexedDB storage
- **Performance optimized** with state hash detection
- **Modular architecture** with separate state parts
- **Action-based updates** for predictable state modifications
For more complex scenarios, consider combining multiple state parts, creating hierarchical state structures, and integrating with other state management solutions as needed. With `@push.rocks/smartstate`, the possibilities are vast, empowering you to tailor the state management approach to fit the unique requirements of your project. // Define your state structure
type AppStateParts = 'user' | 'settings' | 'cart';
interface IUserState {
isLoggedIn: boolean;
username?: string;
email?: string;
}
interface ICartState {
items: Array<{ id: string; quantity: number }>;
total: number;
}
// Create the smartstate instance
const appState = new Smartstate<AppStateParts>();
// Initialize state parts
const userState = await appState.getStatePart<IUserState>('user', {
isLoggedIn: false
});
const cartState = await appState.getStatePart<ICartState>('cart', {
items: [],
total: 0
}, 'persistent'); // Persists across sessions
// Create actions
const loginAction = userState.createAction<{ username: string; email: string }>(
async (statePart, payload) => {
// Simulate API call
await new Promise(resolve => setTimeout(resolve, 1000));
return {
isLoggedIn: true,
username: payload.username,
email: payload.email
};
}
);
// Subscribe to changes
userState.select(state => state.isLoggedIn).subscribe(isLoggedIn => {
console.log('Login status changed:', isLoggedIn);
});
// Dispatch actions
await loginAction.trigger({ username: 'john', email: 'john@example.com' });
```
## Key Features
| Feature | Description |
|---------|-------------|
| 🎯 **Type-safe** | Full TypeScript support with intelligent type inference |
| ⚡ **Performance optimized** | Async state hash detection prevents unnecessary re-renders |
| 💾 **Persistent state** | Built-in IndexedDB support for state persistence |
| 🔄 **Reactive** | Powered by RxJS for elegant async handling |
| 🧩 **Modular** | Organize state into logical, reusable parts |
| ✅ **Validated** | Built-in state validation with extensible validation logic |
| 🎭 **Flexible init modes** | Choose how state parts are initialized |
| 📦 **Zero config** | Works out of the box with sensible defaults |
| 🛡️ **Race condition safe** | Concurrent state part creation is handled safely |
| ⏱️ **Timeout support** | `waitUntilPresent` supports optional timeouts |
## License and Legal Information ## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [license](./license) file.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file. **Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks ### Trademarks
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. 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 or third parties, 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 or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
### Company Information ### Company Information
Task Venture Capital GmbH Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany 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@task.vc. For any legal inquiries or 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 Task Venture Capital 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.

157
test/test.initialization.ts Normal file
View File

@@ -0,0 +1,157 @@
import { expect, tap } from '@push.rocks/tapbundle';
import * as smartstate from '../ts/index.js';
type TTestStateParts = 'initTest' | 'persistTest' | 'forceTest';
interface ITestState {
value: number;
nested: {
data: string;
};
}
tap.test('should handle soft init mode (default)', async () => {
const state = new smartstate.Smartstate<TTestStateParts>();
// First creation
const statePart1 = await state.getStatePart<ITestState>('initTest', {
value: 1,
nested: { data: 'initial' }
});
expect(statePart1.getState()).toEqual({
value: 1,
nested: { data: 'initial' }
});
// Second call should return existing
const statePart2 = await state.getStatePart<ITestState>('initTest');
expect(statePart1).toEqual(statePart2);
});
tap.test('should handle mandatory init mode', async () => {
const state = new smartstate.Smartstate<TTestStateParts>();
// First creation should succeed
const statePart1 = await state.getStatePart<ITestState>('initTest', {
value: 1,
nested: { data: 'initial' }
}, 'mandatory');
expect(statePart1).toBeInstanceOf(smartstate.StatePart);
// Second call with mandatory should fail
let error: Error | null = null;
try {
await state.getStatePart<ITestState>('initTest', {
value: 2,
nested: { data: 'second' }
}, 'mandatory');
} catch (e) {
error = e as Error;
}
expect(error).not.toBeNull();
expect(error?.message).toMatch(/already exists.*mandatory/);
});
tap.test('should handle force init mode', async () => {
const state = new smartstate.Smartstate<TTestStateParts>();
// First creation
const statePart1 = await state.getStatePart<ITestState>('forceTest', {
value: 1,
nested: { data: 'initial' }
});
expect(statePart1.getState()?.value).toEqual(1);
// Force should create new state part
const statePart2 = await state.getStatePart<ITestState>('forceTest', {
value: 2,
nested: { data: 'forced' }
}, 'force');
expect(statePart2.getState()?.value).toEqual(2);
expect(statePart1).not.toEqual(statePart2);
});
tap.test('should handle missing initial state error', async () => {
const state = new smartstate.Smartstate<TTestStateParts>();
let error: Error | null = null;
try {
await state.getStatePart<ITestState>('initTest');
} catch (e) {
error = e as Error;
}
expect(error).not.toBeNull();
expect(error?.message).toMatch(/does not exist.*no initial state/);
});
tap.test('should handle state validation', async () => {
const state = new smartstate.Smartstate<TTestStateParts>();
const statePart = await state.getStatePart<ITestState>('initTest', {
value: 1,
nested: { data: 'initial' }
});
// Setting null should fail validation
let error: Error | null = null;
try {
await statePart.setState(null as any);
} catch (e) {
error = e as Error;
}
expect(error).not.toBeNull();
expect(error?.message).toMatch(/Invalid state structure/);
});
tap.test('should handle undefined state in select', async () => {
const state = new smartstate.Smartstate<TTestStateParts>();
const statePart = new smartstate.StatePart<TTestStateParts, ITestState>('initTest');
// Select should filter out undefined states
const values: (ITestState | undefined)[] = [];
statePart.select().subscribe(val => values.push(val));
// Initially undefined, should not emit
expect(values).toHaveLength(0);
// After setting state, should emit
await statePart.setState({
value: 1,
nested: { data: 'test' }
});
expect(values).toHaveLength(1);
expect(values[0]).toEqual({
value: 1,
nested: { data: 'test' }
});
});
tap.test('should not notify on duplicate state', async () => {
const state = new smartstate.Smartstate<TTestStateParts>();
const statePart = await state.getStatePart<ITestState>('initTest', {
value: 1,
nested: { data: 'initial' }
});
let notificationCount = 0;
// Use select() to get initial value + changes
statePart.select().subscribe(() => notificationCount++);
// Should have received initial state
expect(notificationCount).toEqual(1);
// Set same state multiple times
await statePart.setState({ value: 1, nested: { data: 'initial' } });
await statePart.setState({ value: 1, nested: { data: 'initial' } });
await statePart.setState({ value: 1, nested: { data: 'initial' } });
// Should still be 1 (no new notifications for duplicate state)
expect(notificationCount).toEqual(1);
// Change state should notify
await statePart.setState({ value: 2, nested: { data: 'changed' } });
expect(notificationCount).toEqual(2);
});
export default tap.start();

View File

@@ -55,4 +55,4 @@ tap.test('should dispatch a state action', async (tools) => {
await done.promise; await done.promise;
}); });
tap.start(); export default tap.start();

View File

@@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartstate', name: '@push.rocks/smartstate',
version: '2.0.20', version: '2.0.31',
description: 'A package for handling and managing state in applications.' description: 'A package for handling and managing state in applications.'
} }

View File

@@ -9,13 +9,16 @@ export type TInitMode = 'soft' | 'mandatory' | 'force' | 'persistent';
export class Smartstate<StatePartNameType extends string> { export class Smartstate<StatePartNameType extends string> {
public statePartMap: { [key in StatePartNameType]?: StatePart<StatePartNameType, any> } = {}; public statePartMap: { [key in StatePartNameType]?: StatePart<StatePartNameType, any> } = {};
private pendingStatePartCreation: Map<string, Promise<StatePart<StatePartNameType, any>>> = new Map();
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' (default) - returns existing statepart if exists, creates new if not
* initMode === 'mandatory' will fail if there is an existing statepart * initMode === 'mandatory' - requires statepart to not exist, fails if it does
* initMode === 'force' will overwrite any existing statepart * initMode === 'force' - always creates new statepart, overwriting any existing
* initMode === 'persistent' - like 'soft' but with webstore persistence
* @param statePartNameArg * @param statePartNameArg
* @param initialArg * @param initialArg
* @param initMode * @param initMode
@@ -23,22 +26,48 @@ export class Smartstate<StatePartNameType extends string> {
public async getStatePart<PayloadType>( public async getStatePart<PayloadType>(
statePartNameArg: StatePartNameType, statePartNameArg: StatePartNameType,
initialArg?: PayloadType, initialArg?: PayloadType,
initMode?: TInitMode initMode: TInitMode = 'soft'
): Promise<StatePart<StatePartNameType, PayloadType>> { ): Promise<StatePart<StatePartNameType, PayloadType>> {
if (this.statePartMap[statePartNameArg]) { // Return pending creation if one exists to prevent duplicate state parts
if (initialArg && (!initMode || initMode !== 'soft')) { const pending = this.pendingStatePartCreation.get(statePartNameArg);
throw new Error( if (pending) {
`${statePartNameArg} already exists, yet you try to set an initial state again` return pending as Promise<StatePart<StatePartNameType, PayloadType>>;
); }
const existingStatePart = this.statePartMap[statePartNameArg];
if (existingStatePart) {
switch (initMode) {
case 'mandatory':
throw new Error(
`State part '${statePartNameArg}' already exists, but initMode is 'mandatory'`
);
case 'force':
// Force mode: create new state part
break; // Fall through to creation
case 'soft':
case 'persistent':
default:
// Return existing state part
return existingStatePart as StatePart<StatePartNameType, PayloadType>;
} }
return this.statePartMap[statePartNameArg] as StatePart<StatePartNameType, PayloadType>;
} else { } else {
// State part doesn't exist
if (!initialArg) { if (!initialArg) {
throw new Error( throw new Error(
`${statePartNameArg} does not yet exist, yet you don't provide an initial state` `State part '${statePartNameArg}' does not exist and no initial state provided`
); );
} }
return this.createStatePart<PayloadType>(statePartNameArg, initialArg, initMode); }
const creationPromise = this.createStatePart<PayloadType>(statePartNameArg, initialArg, initMode);
this.pendingStatePartCreation.set(statePartNameArg, creationPromise);
try {
const result = await creationPromise;
return result;
} finally {
this.pendingStatePartCreation.delete(statePartNameArg);
} }
} }
@@ -46,11 +75,12 @@ export class Smartstate<StatePartNameType extends string> {
* Creates a statepart * Creates a statepart
* @param statePartName * @param statePartName
* @param initialPayloadArg * @param initialPayloadArg
* @param initMode
*/ */
private async createStatePart<PayloadType>( private async createStatePart<PayloadType>(
statePartName: StatePartNameType, statePartName: StatePartNameType,
initialPayloadArg: PayloadType, initialPayloadArg: PayloadType,
initMode?: TInitMode initMode: TInitMode = 'soft'
): Promise<StatePart<StatePartNameType, PayloadType>> { ): Promise<StatePart<StatePartNameType, PayloadType>> {
const newState = new StatePart<StatePartNameType, PayloadType>( const newState = new StatePart<StatePartNameType, PayloadType>(
statePartName, statePartName,
@@ -63,10 +93,18 @@ export class Smartstate<StatePartNameType extends string> {
); );
await newState.init(); await newState.init();
const currentState = newState.getState(); const currentState = newState.getState();
await newState.setState({
...initialPayloadArg, if (initMode === 'persistent' && currentState !== undefined) {
...currentState, // Persisted state exists - merge with defaults, persisted values take precedence
}); await newState.setState({
...initialPayloadArg,
...currentState,
});
} else {
// No persisted state or non-persistent mode
await newState.setState(initialPayloadArg);
}
this.statePartMap[statePartName] = newState; this.statePartMap[statePartName] = newState;
return newState; return newState;
} }

View File

@@ -4,9 +4,11 @@ import { StateAction, type IActionDef } from './smartstate.classes.stateaction.j
export class StatePart<TStatePartName, TStatePayload> { export class StatePart<TStatePartName, TStatePayload> {
public name: TStatePartName; public name: TStatePartName;
public state = new plugins.smartrx.rxjs.Subject<TStatePayload>(); public state = new plugins.smartrx.rxjs.Subject<TStatePayload>();
public stateStore: TStatePayload; public stateStore: TStatePayload | undefined;
private cumulativeDeferred = plugins.smartpromise.cumulativeDefer(); private cumulativeDeferred = plugins.smartpromise.cumulativeDefer();
private pendingCumulativeNotification: ReturnType<typeof setTimeout> | null = null;
private webStoreOptions: plugins.webstore.IWebStoreOptions; private webStoreOptions: plugins.webstore.IWebStoreOptions;
private webStore: plugins.webstore.WebStore<TStatePayload> | null = null; // Add WebStore instance private webStore: plugins.webstore.WebStore<TStatePayload> | null = null; // Add WebStore instance
@@ -27,9 +29,9 @@ export class StatePart<TStatePartName, TStatePayload> {
this.webStore = new plugins.webstore.WebStore<TStatePayload>(this.webStoreOptions); this.webStore = new plugins.webstore.WebStore<TStatePayload>(this.webStoreOptions);
await this.webStore.init(); await this.webStore.init();
const storedState = await this.webStore.get(String(this.name)); const storedState = await this.webStore.get(String(this.name));
if (storedState) { if (storedState && this.validateState(storedState)) {
this.stateStore = storedState; this.stateStore = storedState;
this.notifyChange(); await this.notifyChange();
} }
} }
} }
@@ -37,7 +39,7 @@ export class StatePart<TStatePartName, TStatePayload> {
/** /**
* gets the state from the state store * gets the state from the state store
*/ */
public getState(): TStatePayload { public getState(): TStatePayload | undefined {
return this.stateStore; return this.stateStore;
} }
@@ -46,31 +48,51 @@ export class StatePart<TStatePartName, TStatePayload> {
* @param newStateArg * @param newStateArg
*/ */
public async setState(newStateArg: TStatePayload) { public async setState(newStateArg: TStatePayload) {
this.stateStore = newStateArg; // Validate state structure
this.notifyChange(); if (!this.validateState(newStateArg)) {
throw new Error(`Invalid state structure for state part '${this.name}'`);
// Save state to WebStore if initialized }
// Save to WebStore first to ensure atomicity - if save fails, memory state remains unchanged
if (this.webStore) { if (this.webStore) {
await this.webStore.set(String(this.name), newStateArg); await this.webStore.set(String(this.name), newStateArg);
} }
// Update in-memory state after successful persistence
this.stateStore = newStateArg;
await this.notifyChange();
return this.stateStore; return this.stateStore;
} }
/**
* Validates state structure - can be overridden for custom validation
* @param stateArg
*/
protected validateState(stateArg: any): stateArg is TStatePayload {
// Basic validation - ensure state is not null/undefined
// Subclasses can override for more specific validation
return stateArg !== null && stateArg !== undefined;
}
/** /**
* notifies of a change on the state * notifies of a change on the state
*/ */
public notifyChange() { public async notifyChange() {
const createStateHash = (stateArg: any) => { if (!this.stateStore) {
return plugins.smarthashWeb.sha256FromString(plugins.smartjson.stringify(stateArg)); return;
}
const createStateHash = async (stateArg: any) => {
return await plugins.smarthashWeb.sha256FromString(plugins.smartjson.stableOneWayStringify(stateArg));
}; };
const currentHash = await createStateHash(this.stateStore);
if ( if (
this.stateStore &&
this.lastStateNotificationPayloadHash && this.lastStateNotificationPayloadHash &&
createStateHash(this.stateStore) === this.lastStateNotificationPayloadHash currentHash === this.lastStateNotificationPayloadHash
) { ) {
return; return;
} else { } else {
this.lastStateNotificationPayloadHash = this.stateStore; this.lastStateNotificationPayloadHash = currentHash;
} }
this.state.next(this.stateStore); this.state.next(this.stateStore);
} }
@@ -80,8 +102,17 @@ export class StatePart<TStatePartName, TStatePayload> {
* creates a cumulative notification by adding a change notification at the end of the call stack; * creates a cumulative notification by adding a change notification at the end of the call stack;
*/ */
public notifyChangeCumulative() { public notifyChangeCumulative() {
// TODO: check viability // Debounce: clear any pending notification
setTimeout(() => this.state.next(this.stateStore), 0); if (this.pendingCumulativeNotification) {
clearTimeout(this.pendingCumulativeNotification);
}
this.pendingCumulativeNotification = setTimeout(async () => {
this.pendingCumulativeNotification = null;
if (this.stateStore) {
await this.notifyChange();
}
}, 0);
} }
/** /**
@@ -95,11 +126,13 @@ export class StatePart<TStatePartName, TStatePayload> {
} }
const mapped = this.state.pipe( const mapped = this.state.pipe(
plugins.smartrx.rxjs.ops.startWith(this.getState()), plugins.smartrx.rxjs.ops.startWith(this.getState()),
plugins.smartrx.rxjs.ops.filter((stateArg): stateArg is TStatePayload => stateArg !== undefined),
plugins.smartrx.rxjs.ops.map((stateArg) => { plugins.smartrx.rxjs.ops.map((stateArg) => {
try { try {
return selectorFn(stateArg); return selectorFn(stateArg);
} catch (e) { } catch (e) {
// Nothing here console.error(`Selector error in state part '${this.name}':`, e);
return undefined;
} }
}) })
); );
@@ -128,20 +161,41 @@ export class StatePart<TStatePartName, TStatePayload> {
/** /**
* waits until a certain part of the state becomes available * waits until a certain part of the state becomes available
* @param selectorFn * @param selectorFn
* @param timeoutMs - optional timeout in milliseconds to prevent indefinite waiting
*/ */
public async waitUntilPresent<T = TStatePayload>( public async waitUntilPresent<T = TStatePayload>(
selectorFn?: (state: TStatePayload) => T selectorFn?: (state: TStatePayload) => T,
timeoutMs?: number
): Promise<T> { ): Promise<T> {
const done = plugins.smartpromise.defer<T>(); const done = plugins.smartpromise.defer<T>();
const selectedObservable = this.select(selectorFn); const selectedObservable = this.select(selectorFn);
const subscription = selectedObservable.subscribe(async (value) => { let resolved = false;
if (value) {
const subscription = selectedObservable.subscribe((value) => {
if (value && !resolved) {
resolved = true;
done.resolve(value); done.resolve(value);
} }
}); });
const result = await done.promise;
subscription.unsubscribe(); let timeoutId: ReturnType<typeof setTimeout> | undefined;
return result; if (timeoutMs) {
timeoutId = setTimeout(() => {
if (!resolved) {
resolved = true;
subscription.unsubscribe();
done.reject(new Error(`waitUntilPresent timed out after ${timeoutMs}ms`));
}
}, timeoutMs);
}
try {
const result = await done.promise;
return result;
} finally {
subscription.unsubscribe();
if (timeoutId) clearTimeout(timeoutId);
}
} }
/** /**
@@ -152,6 +206,6 @@ export class StatePart<TStatePartName, TStatePayload> {
) { ) {
const resultPromise = funcArg(this); const resultPromise = funcArg(this);
this.cumulativeDeferred.addPromise(resultPromise); this.cumulativeDeferred.addPromise(resultPromise);
this.setState(await resultPromise); await this.setState(await resultPromise);
} }
} }