Compare commits
91 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d45e1188b1 | |||
| 9312b8908c | |||
| 2f0b39ae41 | |||
| 575477df09 | |||
| 39aa63bdb3 | |||
| c1aa4eae5e | |||
| d8decdb3e5 | |||
| 03cfee2003 | |||
| f6a3e71f0a | |||
| 6436370abc | |||
| eb1c48bee4 | |||
| 05417ed4c3 | |||
| 1d74a7f465 | |||
| 81ca32cdef | |||
| 07bfbfd393 | |||
| aa411072f2 | |||
| 02575e8baf | |||
| 09fc53aaff | |||
| bcb58dd012 | |||
| f0064bd94b | |||
| e9c527a9dc | |||
| a47d8bb3c7 | |||
| aa6766ef36 | |||
| b0442e1227 | |||
| 0f1eb6eb27 | |||
| dd18ef94bd | |||
| a0189921a5 | |||
| b409f1aa55 | |||
| 57553aa7c8 | |||
| c302cbdae4 | |||
| fd6e3ba8b1 | |||
| 8c3a7aa637 | |||
| 8c0125a8c0 | |||
| 40f3ffbdfb | |||
| d4e01a73d3 | |||
| 641e77892a | |||
| 0c1211778b | |||
| 024f7f4f8f | |||
| 8f1cba5078 | |||
| 188f8057bf | |||
| 99cb86258e | |||
| 83976fa3f4 | |||
| fe81307ca6 | |||
| 3a119b50a2 | |||
| d3332ccb3f | |||
| 776eba09e9 | |||
| b41ff5d495 | |||
| 5f5f9db884 | |||
| 876042b446 | |||
| df2924577b | |||
| 4abaea84f8 | |||
| de454b4c8d | |||
| 961685b5bd | |||
| c622396d50 | |||
| d130c1c6fd | |||
| f8a967aeac | |||
| abee699af8 | |||
| a21131eaf6 | |||
| c4b214a308 | |||
| 6db03eee83 | |||
| efc5b54d2e | |||
| 9860e43398 | |||
| 234117c8dd | |||
| 371c6553cd | |||
| b6b738916f | |||
| fad7e0bc6b | |||
| fc4fb911ef | |||
| 7e0dad1c10 | |||
| 0fb50714b9 | |||
| 7e1821f37d | |||
| 592228fc51 | |||
| 431089e23f | |||
| 5b64733d91 | |||
| 2da2a43a09 | |||
| f7887a6663 | |||
| be505ee915 | |||
| 9584f403b1 | |||
| 993a100eae | |||
| 29f0307ef8 | |||
| 9413c5bcdc | |||
| 1d5fb320f4 | |||
| 69e905ad76 | |||
| 7b9ed9ee78 | |||
| f884807f33 | |||
| f167800113 | |||
| c549d06a6b | |||
| 23061c86eb | |||
| 4d0567a95d | |||
| 8524c95440 | |||
| b1fc60fc2e | |||
| 8d296cf08d |
66
.gitea/workflows/default_nottags.yaml
Normal file
66
.gitea/workflows/default_nottags.yaml
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
name: Default (not tags)
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags-ignore:
|
||||||
|
- '**'
|
||||||
|
|
||||||
|
env:
|
||||||
|
IMAGE: code.foss.global/host.today/ht-docker-node:npmci
|
||||||
|
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_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||||
|
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||||
|
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
security:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
continue-on-error: true
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Install pnpm and npmci
|
||||||
|
run: |
|
||||||
|
pnpm install -g pnpm
|
||||||
|
pnpm install -g @ship.zone/npmci
|
||||||
|
|
||||||
|
- name: Run npm prepare
|
||||||
|
run: npmci npm prepare
|
||||||
|
|
||||||
|
- name: Audit production dependencies
|
||||||
|
run: |
|
||||||
|
npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
npmci command pnpm audit --audit-level=high --prod
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
|
- name: Audit development dependencies
|
||||||
|
run: |
|
||||||
|
npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
npmci command pnpm audit --audit-level=high --dev
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
|
test:
|
||||||
|
if: ${{ always() }}
|
||||||
|
needs: security
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Test stable
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm install
|
||||||
|
npmci npm test
|
||||||
|
|
||||||
|
- name: Test build
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm install
|
||||||
|
npmci npm build
|
||||||
124
.gitea/workflows/default_tags.yaml
Normal file
124
.gitea/workflows/default_tags.yaml
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
name: Default (tags)
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- '*'
|
||||||
|
|
||||||
|
env:
|
||||||
|
IMAGE: code.foss.global/host.today/ht-docker-node:npmci
|
||||||
|
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_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||||
|
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||||
|
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
security:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
continue-on-error: true
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
run: |
|
||||||
|
pnpm install -g pnpm
|
||||||
|
pnpm install -g @ship.zone/npmci
|
||||||
|
npmci npm prepare
|
||||||
|
|
||||||
|
- name: Audit production dependencies
|
||||||
|
run: |
|
||||||
|
npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
npmci command pnpm audit --audit-level=high --prod
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
|
- name: Audit development dependencies
|
||||||
|
run: |
|
||||||
|
npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
npmci command pnpm audit --audit-level=high --dev
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
|
test:
|
||||||
|
if: ${{ always() }}
|
||||||
|
needs: security
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
run: |
|
||||||
|
pnpm install -g pnpm
|
||||||
|
pnpm install -g @ship.zone/npmci
|
||||||
|
npmci npm prepare
|
||||||
|
|
||||||
|
- name: Test stable
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm install
|
||||||
|
npmci npm test
|
||||||
|
|
||||||
|
- name: Test build
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm install
|
||||||
|
npmci npm build
|
||||||
|
|
||||||
|
release:
|
||||||
|
needs: test
|
||||||
|
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
run: |
|
||||||
|
pnpm install -g pnpm
|
||||||
|
pnpm install -g @ship.zone/npmci
|
||||||
|
npmci npm prepare
|
||||||
|
|
||||||
|
- name: Release
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm publish
|
||||||
|
|
||||||
|
metadata:
|
||||||
|
needs: test
|
||||||
|
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
run: |
|
||||||
|
pnpm install -g pnpm
|
||||||
|
pnpm install -g @ship.zone/npmci
|
||||||
|
npmci npm prepare
|
||||||
|
|
||||||
|
- name: Code quality
|
||||||
|
run: |
|
||||||
|
npmci command npm install -g typescript
|
||||||
|
npmci npm install
|
||||||
|
|
||||||
|
- name: Trigger
|
||||||
|
run: npmci trigger
|
||||||
|
|
||||||
|
- name: Build docs and upload artifacts
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm install
|
||||||
|
pnpm install -g @git.zone/tsdoc
|
||||||
|
npmci command tsdoc
|
||||||
|
continue-on-error: true
|
||||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -15,8 +15,6 @@ node_modules/
|
|||||||
|
|
||||||
# builds
|
# builds
|
||||||
dist/
|
dist/
|
||||||
dist_web/
|
dist_*/
|
||||||
dist_serve/
|
|
||||||
dist_ts_web/
|
|
||||||
|
|
||||||
# custom
|
# custom
|
||||||
119
.gitlab-ci.yml
119
.gitlab-ci.yml
@@ -1,119 +0,0 @@
|
|||||||
# gitzone ci_default
|
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
|
||||||
|
|
||||||
cache:
|
|
||||||
paths:
|
|
||||||
- .npmci_cache/
|
|
||||||
key: "$CI_BUILD_STAGE"
|
|
||||||
|
|
||||||
stages:
|
|
||||||
- security
|
|
||||||
- test
|
|
||||||
- release
|
|
||||||
- metadata
|
|
||||||
|
|
||||||
# ====================
|
|
||||||
# security stage
|
|
||||||
# ====================
|
|
||||||
mirror:
|
|
||||||
stage: security
|
|
||||||
script:
|
|
||||||
- npmci git mirror
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
snyk:
|
|
||||||
stage: security
|
|
||||||
script:
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci command npm install -g snyk
|
|
||||||
- npmci command npm install --ignore-scripts
|
|
||||||
- npmci command snyk test
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
# ====================
|
|
||||||
# test stage
|
|
||||||
# ====================
|
|
||||||
|
|
||||||
testStable:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci node install stable
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- priv
|
|
||||||
|
|
||||||
testBuild:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci node install stable
|
|
||||||
- npmci npm install
|
|
||||||
- npmci command npm run build
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
release:
|
|
||||||
stage: release
|
|
||||||
script:
|
|
||||||
- npmci node install stable
|
|
||||||
- npmci npm publish
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
# ====================
|
|
||||||
# metadata stage
|
|
||||||
# ====================
|
|
||||||
codequality:
|
|
||||||
stage: metadata
|
|
||||||
allow_failure: true
|
|
||||||
script:
|
|
||||||
- npmci command npm install -g tslint typescript
|
|
||||||
- npmci npm install
|
|
||||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- priv
|
|
||||||
|
|
||||||
trigger:
|
|
||||||
stage: metadata
|
|
||||||
script:
|
|
||||||
- npmci trigger
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
pages:
|
|
||||||
image: hosttoday/ht-docker-dbase:npmci
|
|
||||||
services:
|
|
||||||
- docker:stable-dind
|
|
||||||
stage: metadata
|
|
||||||
script:
|
|
||||||
- npmci command npm install -g @gitzone/tsdoc
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci npm install
|
|
||||||
- npmci command tsdoc
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
artifacts:
|
|
||||||
expire_in: 1 week
|
|
||||||
paths:
|
|
||||||
- public
|
|
||||||
allow_failure: true
|
|
||||||
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"command": "npm test",
|
||||||
|
"name": "Run npm test",
|
||||||
|
"request": "launch",
|
||||||
|
"type": "node-terminal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"json.schemas": [
|
||||||
|
{
|
||||||
|
"fileMatch": ["/npmextra.json"],
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"npmci": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for npmci"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for gitzone",
|
||||||
|
"properties": {
|
||||||
|
"projectType": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
274
changelog.md
Normal file
274
changelog.md
Normal file
@@ -0,0 +1,274 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
## 2026-02-28 - 2.1.1 - fix(core)
|
||||||
|
serialize state mutations, fix batch flushing/reentrancy, handle falsy initial values, dispose old StatePart on force, and improve notification/error handling
|
||||||
|
|
||||||
|
- Serialize setState() and dispatchAction() using an internal mutation queue to prevent lost updates and race conditions.
|
||||||
|
- Prevent batch flush deadlocks by introducing isFlushing and draining pending notifications iteratively.
|
||||||
|
- Force initMode now disposes the previous StatePart so the Subject completes and resources are cleaned up.
|
||||||
|
- Treat falsy but non-null values (0, false) as present: getStatePart accepts 0 as initial value and waitUntilPresent resolves for false/0.
|
||||||
|
- Improve notifyChange: use a stable snapshot, catch and log hash computation errors, and avoid duplicate notifications; notifyChangeCumulative now safely catches async errors.
|
||||||
|
- Add StatePart.dispose() to complete the Subject and clear pending timers/middlewares.
|
||||||
|
- Add/adjust tests for concurrent dispatches, concurrent setState, disposal behavior, falsy state handling, batch re-entrancy, force-mode disposal, and zero initialization.
|
||||||
|
- Documentation and README improvements (examples, clearer descriptions, persistence notes) and minor code cleanup (remove unused import).
|
||||||
|
|
||||||
|
## 2026-02-27 - 2.1.0 - feat(smartstate)
|
||||||
|
Add middleware, computed, batching, selector memoization, AbortSignal support, and Web Component Context Protocol provider
|
||||||
|
|
||||||
|
- Introduce StatePart middleware API (addMiddleware) — middleware runs sequentially before validation/persistence and can transform or reject a state change.
|
||||||
|
- Add computed derived observables: standalone computed(sources, fn) and Smartstate.computed to derive values from multiple state parts (lazy subscription).
|
||||||
|
- Add batching support via Smartstate.batch(fn), isBatching flag, and deferred notifications to batch multiple updates and flush only at the outermost level.
|
||||||
|
- Enhance select() with selector memoization (WeakMap cache and shareReplay) and optional AbortSignal support (auto-unsubscribe).
|
||||||
|
- Extend waitUntilPresent() to accept timeout and AbortSignal options and maintain backward-compatible numeric timeout argument.
|
||||||
|
- Add attachContextProvider(element, options) to bridge state parts to Web Component Context Protocol (context-request events) with subscribe/unsubscribe handling.
|
||||||
|
- Update StatePart.setState to run middleware, persist processed state atomically, and defer notifications to batching when applicable.
|
||||||
|
- Tests and README updated to document new features, behaviors, and examples.
|
||||||
|
|
||||||
|
## 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)
|
||||||
|
Fix StateAction trigger method to properly return Promise
|
||||||
|
|
||||||
|
- Fixed StateAction.trigger() to return Promise<TStateType> as expected
|
||||||
|
- Updated readme with improved documentation and examples
|
||||||
|
- Replaced outdated legal information with Task Venture Capital GmbH details
|
||||||
|
- Added implementation notes in readme.hints.md
|
||||||
|
|
||||||
|
## 2025-06-19 - 2.0.21 - maintenance
|
||||||
|
General updates and improvements
|
||||||
|
|
||||||
|
## 2025-06-19 - 2.0.20 - fix(smartstate)
|
||||||
|
Update build scripts and dependency versions; replace isohash with smarthashWeb for state hash generation
|
||||||
|
|
||||||
|
- Adjusted package.json scripts to include verbose testing and modified build command
|
||||||
|
- Bumped development dependencies (tsbuild, tsbundle, tsrun, tstest, tapbundle) to newer versions
|
||||||
|
- Updated production dependencies (lik, smarthash, smartpromise, smartrx) with minor version bumps
|
||||||
|
- Replaced import of isohash with smarthashWeb in state hash generation, ensuring consistency across modules
|
||||||
|
|
||||||
|
## 2024-10-02 - 2.0.19 - fix(dependencies)
|
||||||
|
Update dependencies to latest versions
|
||||||
|
|
||||||
|
- Updated @git.zone/tsbuild to version ^2.1.84
|
||||||
|
- Updated @git.zone/tsbundle to version ^2.0.15
|
||||||
|
- Updated @git.zone/tsrun to version ^1.2.49
|
||||||
|
- Updated @git.zone/tstest to version ^1.0.90
|
||||||
|
- Updated @push.rocks/tapbundle to version ^5.3.0
|
||||||
|
- Updated @types/node to version ^22.7.4
|
||||||
|
- Updated @push.rocks/lik to version ^6.0.15
|
||||||
|
- Updated @push.rocks/smartjson to version ^5.0.20
|
||||||
|
- Updated @push.rocks/smartpromise to version ^4.0.4
|
||||||
|
- Updated @push.rocks/smartrx to version ^3.0.7
|
||||||
|
- Updated @push.rocks/webstore to version ^2.0.20
|
||||||
|
|
||||||
|
## 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
|
||||||
2
license
2
license
@@ -1,4 +1,4 @@
|
|||||||
Copyright (c) 2019 Lossless GmbH (hello@lossless.com)
|
Copyright (c) 2019 Task Venture Capital GmbH (hello@task.vc)
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
@@ -1,16 +1,55 @@
|
|||||||
{
|
{
|
||||||
"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",
|
||||||
"module": {
|
"module": {
|
||||||
"githost": "gitlab.com",
|
"githost": "code.foss.global",
|
||||||
"gitscope": "pushrocks",
|
"gitscope": "push.rocks",
|
||||||
"gitrepo": "smartstate",
|
"gitrepo": "smartstate",
|
||||||
"shortDescription": "a package that handles state in a good way",
|
"description": "A TypeScript-first reactive state management library with middleware, computed state, batching, persistence, and Web Component Context Protocol support.",
|
||||||
"npmPackagename": "@pushrocks/smartstate",
|
"npmPackagename": "@push.rocks/smartstate",
|
||||||
"license": "MIT"
|
"license": "MIT",
|
||||||
|
"keywords": [
|
||||||
|
"state management",
|
||||||
|
"reactive programming",
|
||||||
|
"TypeScript",
|
||||||
|
"observables",
|
||||||
|
"web storage",
|
||||||
|
"state action",
|
||||||
|
"state selection",
|
||||||
|
"state notification",
|
||||||
|
"asynchronous state",
|
||||||
|
"cumulative notification",
|
||||||
|
"middleware",
|
||||||
|
"computed state",
|
||||||
|
"batch updates",
|
||||||
|
"context protocol",
|
||||||
|
"web components",
|
||||||
|
"AbortSignal"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"release": {
|
||||||
|
"registries": [
|
||||||
|
"https://verdaccio.lossless.digital",
|
||||||
|
"https://registry.npmjs.org"
|
||||||
|
],
|
||||||
|
"accessLevel": "public"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"@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"
|
||||||
|
},
|
||||||
|
"@ship.zone/szci": {
|
||||||
|
"npmGlobalTools": []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
17940
package-lock.json
generated
17940
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
79
package.json
79
package.json
@@ -1,39 +1,70 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartstate",
|
"name": "@push.rocks/smartstate",
|
||||||
"version": "1.0.13",
|
"version": "2.1.1",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "a package that handles state in a good way",
|
"description": "A TypeScript-first reactive state management library with middleware, computed state, batching, persistence, and Web Component Context Protocol support.",
|
||||||
"main": "dist/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
|
"type": "module",
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/)",
|
"test": "(tstest test/ --verbose)",
|
||||||
"build": "(tsbuild)",
|
"build": "(tsbuild tsfolders --allowimplicitany && tsbundle npm)",
|
||||||
"format": "(gitzone format)"
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.17",
|
"@git.zone/tsbuild": "^4.1.2",
|
||||||
"@gitzone/tstest": "^1.0.24",
|
"@git.zone/tsbundle": "^2.9.0",
|
||||||
"@pushrocks/tapbundle": "^3.0.13",
|
"@git.zone/tsrun": "^2.0.1",
|
||||||
"@types/node": "^12.7.7",
|
"@git.zone/tstest": "^3.1.8",
|
||||||
"tslint": "^5.20.0",
|
"@push.rocks/tapbundle": "^6.0.3",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"@types/node": "^25.3.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/lik": "^3.0.11",
|
"@push.rocks/smarthash": "^3.2.6",
|
||||||
"@pushrocks/smartpromise": "^3.0.5",
|
"@push.rocks/smartjson": "^6.0.0",
|
||||||
"rxjs": "^6.5.3"
|
"@push.rocks/smartpromise": "^4.2.3",
|
||||||
|
"@push.rocks/smartrx": "^3.0.10",
|
||||||
|
"@push.rocks/webstore": "^2.0.20"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/*",
|
"ts/**/*",
|
||||||
"ts_web/*",
|
"ts_web/**/*",
|
||||||
"dist/*",
|
"dist/**/*",
|
||||||
"dist_web/*",
|
"dist_*/**/*",
|
||||||
"dist_ts_web/*",
|
"dist_ts/**/*",
|
||||||
"assets/*",
|
"dist_ts_web/**/*",
|
||||||
|
"assets/**/*",
|
||||||
"cli.js",
|
"cli.js",
|
||||||
"npmextra.json",
|
"npmextra.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
]
|
],
|
||||||
|
"browserslist": [
|
||||||
|
"last 1 chrome versions"
|
||||||
|
],
|
||||||
|
"keywords": [
|
||||||
|
"state management",
|
||||||
|
"reactive programming",
|
||||||
|
"TypeScript",
|
||||||
|
"observables",
|
||||||
|
"web storage",
|
||||||
|
"state action",
|
||||||
|
"state selection",
|
||||||
|
"state notification",
|
||||||
|
"asynchronous state",
|
||||||
|
"cumulative notification",
|
||||||
|
"middleware",
|
||||||
|
"computed state",
|
||||||
|
"batch updates",
|
||||||
|
"context protocol",
|
||||||
|
"web components",
|
||||||
|
"AbortSignal"
|
||||||
|
],
|
||||||
|
"homepage": "https://code.foss.global/push.rocks/smartstate",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://code.foss.global/push.rocks/smartstate.git"
|
||||||
|
},
|
||||||
|
"packageManager": "pnpm@10.11.0+sha512.6540583f41cc5f628eb3d9773ecee802f4f9ef9923cc45b69890fb47991d4b092964694ec3a4f738a420c918a333062c8b925d312f42e4f0c263eb603551f977"
|
||||||
}
|
}
|
||||||
|
|||||||
9915
pnpm-lock.yaml
generated
Normal file
9915
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
77
readme.hints.md
Normal file
77
readme.hints.md
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
# Smartstate Implementation Notes
|
||||||
|
|
||||||
|
## Current API (as of v2.0.31)
|
||||||
|
|
||||||
|
### State Part Initialization
|
||||||
|
- 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
|
||||||
|
- State merge order fixed: initial state takes precedence over stored state
|
||||||
|
|
||||||
|
### Actions
|
||||||
|
- Actions are created with `createAction()` method
|
||||||
|
- Two ways to dispatch: `stateAction.trigger(payload)` or `statePart.dispatchAction(stateAction, payload)`
|
||||||
|
- Both return Promise<TStatePayload>
|
||||||
|
|
||||||
|
### State Management Methods
|
||||||
|
- `select(fn?, { signal? })` - returns Observable, memoized by selector fn ref, supports AbortSignal
|
||||||
|
- `waitUntilPresent(fn?, number | { timeoutMs?, signal? })` - waits for state condition, backward compat with number arg
|
||||||
|
- `stateSetup()` - async state initialization with cumulative defer
|
||||||
|
- `notifyChangeCumulative()` - defers notification to end of call stack
|
||||||
|
- `getState()` - returns current state or undefined
|
||||||
|
- `setState()` - runs middleware, validates, persists, notifies
|
||||||
|
- `addMiddleware(fn)` - intercepts setState, returns removal function
|
||||||
|
|
||||||
|
### Middleware
|
||||||
|
- Type: `(newState, oldState) => newState | Promise<newState>`
|
||||||
|
- Runs sequentially in insertion order before validation/persistence
|
||||||
|
- Throw to reject state changes (atomic — state unchanged on error)
|
||||||
|
- Does NOT run during initial createStatePart() hydration
|
||||||
|
|
||||||
|
### Selector Memoization
|
||||||
|
- Uses WeakMap<Function, Observable> for fn-keyed cache
|
||||||
|
- `defaultSelectObservable` for no-arg select()
|
||||||
|
- Wrapped in `shareReplay({ bufferSize: 1, refCount: true })`
|
||||||
|
- NOT cached when AbortSignal is provided
|
||||||
|
|
||||||
|
### Batch Updates
|
||||||
|
- `smartstate.batch(async () => {...})` — defers notifications until batch completes
|
||||||
|
- Supports nesting — only flushes at outermost level
|
||||||
|
- StatePart has `smartstateRef` set by `createStatePart()` for batch awareness
|
||||||
|
- State parts created via `new StatePart()` directly work without batching
|
||||||
|
|
||||||
|
### Computed State
|
||||||
|
- `computed(sources, fn)` — standalone function using `combineLatest` + `map`
|
||||||
|
- Also available as `smartstate.computed(sources, fn)`
|
||||||
|
- Lazy — only subscribes when subscribed to
|
||||||
|
|
||||||
|
### Context Protocol Bridge
|
||||||
|
- `attachContextProvider(element, { context, statePart, selectorFn? })` — returns cleanup fn
|
||||||
|
- Listens for `context-request` CustomEvent on element
|
||||||
|
- Supports one-shot and subscription modes
|
||||||
|
- Works with Lit @consume(), FAST, or any Context Protocol consumer
|
||||||
|
|
||||||
|
### State Hash Detection
|
||||||
|
- Uses SHA256 hash to detect actual state changes
|
||||||
|
- Hash comparison properly awaits async hash calculation
|
||||||
|
- Prevents duplicate notifications for identical state values
|
||||||
|
|
||||||
|
### State Validation
|
||||||
|
- Basic validation ensures state is not null/undefined
|
||||||
|
- `validateState()` can be overridden in subclasses
|
||||||
|
|
||||||
|
### Key Notes
|
||||||
|
- `smartstateRef` creates circular ref between StatePart and Smartstate
|
||||||
|
- Use `===` not deep equality for StatePart comparison in tests
|
||||||
|
- Direct rxjs imports used for: Observable, shareReplay, takeUntil, combineLatest, map
|
||||||
|
|
||||||
|
## Dependency Versions (v2.0.31)
|
||||||
|
- @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
|
||||||
416
readme.md
416
readme.md
@@ -1,26 +1,404 @@
|
|||||||
# @pushrocks/smartstate
|
# @push.rocks/smartstate
|
||||||
a package that handles state in a good way
|
|
||||||
|
|
||||||
## Availabililty and Links
|
A TypeScript-first reactive state management library with middleware, computed state, batching, persistence, and Web Component Context Protocol support 🚀
|
||||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartstate)
|
|
||||||
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartstate)
|
|
||||||
* [github.com (source mirror)](https://github.com/pushrocks/smartstate)
|
|
||||||
* [docs (typedoc)](https://pushrocks.gitlab.io/smartstate/)
|
|
||||||
|
|
||||||
## Status for master
|
## Issue Reporting and Security
|
||||||
[](https://gitlab.com/pushrocks/smartstate/commits/master)
|
|
||||||
[](https://gitlab.com/pushrocks/smartstate/commits/master)
|
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.
|
||||||
[](https://www.npmjs.com/package/@pushrocks/smartstate)
|
|
||||||
[](https://snyk.io/test/npm/@pushrocks/smartstate)
|
## Install
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
```bash
|
||||||
[](https://prettier.io/)
|
pnpm install @push.rocks/smartstate --save
|
||||||
|
```
|
||||||
|
|
||||||
|
Or with npm:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install @push.rocks/smartstate --save
|
||||||
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
For further information read the linked docs at the top of this readme.
|
### Quick Start
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
```typescript
|
||||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
import { Smartstate } from '@push.rocks/smartstate';
|
||||||
|
|
||||||
[](https://maintainedby.lossless.com)
|
// 1. Define your state part names
|
||||||
|
type AppParts = 'user' | 'settings';
|
||||||
|
|
||||||
|
// 2. Create the root instance
|
||||||
|
const state = new Smartstate<AppParts>();
|
||||||
|
|
||||||
|
// 3. Create state parts with initial values
|
||||||
|
const userState = await state.getStatePart<{ name: string; loggedIn: boolean }>('user', {
|
||||||
|
name: '',
|
||||||
|
loggedIn: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 4. Subscribe to changes
|
||||||
|
userState.select((s) => s.name).subscribe((name) => {
|
||||||
|
console.log('Name changed:', name);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 5. Update state
|
||||||
|
await userState.setState({ name: 'Alice', loggedIn: true });
|
||||||
|
```
|
||||||
|
|
||||||
|
### 🧩 State Parts & Init Modes
|
||||||
|
|
||||||
|
State parts are isolated, typed units of state. They are the building blocks of your application's state tree. Create them via `getStatePart()`:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const part = await state.getStatePart<IMyState>(name, initialState, initMode);
|
||||||
|
```
|
||||||
|
|
||||||
|
| Init Mode | Behavior |
|
||||||
|
|-----------|----------|
|
||||||
|
| `'soft'` (default) | Returns existing if found, creates new otherwise |
|
||||||
|
| `'mandatory'` | Throws if state part already exists — useful for ensuring single-initialization |
|
||||||
|
| `'force'` | Always creates a new state part, overwriting any existing one |
|
||||||
|
| `'persistent'` | Like `'soft'` but automatically persists state to IndexedDB via WebStore |
|
||||||
|
|
||||||
|
You can use either enums or string literal types for state part names:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// String literal types (simpler)
|
||||||
|
type AppParts = 'user' | 'settings' | 'cart';
|
||||||
|
|
||||||
|
// Enums (more explicit)
|
||||||
|
enum AppParts {
|
||||||
|
User = 'user',
|
||||||
|
Settings = 'settings',
|
||||||
|
Cart = 'cart',
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 💾 Persistent State
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const settings = await state.getStatePart('settings', { theme: 'dark', fontSize: 14 }, 'persistent');
|
||||||
|
|
||||||
|
// ✅ Automatically saved to IndexedDB on every setState()
|
||||||
|
// ✅ On next app load, persisted values override defaults
|
||||||
|
// ✅ Persistence writes complete before in-memory updates (atomic)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 🔭 Selecting State
|
||||||
|
|
||||||
|
`select()` returns an RxJS Observable that emits the current value immediately and on every subsequent change:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Full state
|
||||||
|
userState.select().subscribe((state) => console.log(state));
|
||||||
|
|
||||||
|
// Derived value via selector function
|
||||||
|
userState.select((s) => s.name).subscribe((name) => console.log(name));
|
||||||
|
```
|
||||||
|
|
||||||
|
Selectors are **memoized** — calling `select(fn)` with the same function reference returns the same cached Observable, shared across all subscribers via `shareReplay`. This means you can call `select(mySelector)` in multiple places without creating duplicate subscriptions.
|
||||||
|
|
||||||
|
#### ✂️ AbortSignal Support
|
||||||
|
|
||||||
|
Clean up subscriptions without manual `.unsubscribe()` — the modern way:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
userState.select((s) => s.name, { signal: controller.signal }).subscribe((name) => {
|
||||||
|
console.log(name); // automatically stops receiving when aborted
|
||||||
|
});
|
||||||
|
|
||||||
|
// Later: clean up all subscriptions tied to this signal
|
||||||
|
controller.abort();
|
||||||
|
```
|
||||||
|
|
||||||
|
### ⚡ Actions
|
||||||
|
|
||||||
|
Actions provide controlled, named state mutations with full async support:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
interface ILoginPayload {
|
||||||
|
username: string;
|
||||||
|
email: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const loginAction = userState.createAction<ILoginPayload>(async (statePart, payload) => {
|
||||||
|
// You have access to the current state via statePart.getState()
|
||||||
|
const current = statePart.getState();
|
||||||
|
return { ...current, name: payload.username, loggedIn: true };
|
||||||
|
});
|
||||||
|
|
||||||
|
// Two equivalent ways to dispatch:
|
||||||
|
await loginAction.trigger({ username: 'Alice', email: 'alice@example.com' });
|
||||||
|
// or
|
||||||
|
await userState.dispatchAction(loginAction, { username: 'Alice', email: 'alice@example.com' });
|
||||||
|
```
|
||||||
|
|
||||||
|
Both `trigger()` and `dispatchAction()` return a Promise with the new state.
|
||||||
|
|
||||||
|
### 🛡️ Middleware
|
||||||
|
|
||||||
|
Intercept every `setState()` call to transform, validate, log, or reject state changes:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Logging middleware
|
||||||
|
userState.addMiddleware((newState, oldState) => {
|
||||||
|
console.log('State changing:', oldState, '→', newState);
|
||||||
|
return newState;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Validation middleware — throw to reject the change
|
||||||
|
userState.addMiddleware((newState) => {
|
||||||
|
if (!newState.name) throw new Error('Name is required');
|
||||||
|
return newState;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Transform middleware
|
||||||
|
userState.addMiddleware((newState) => {
|
||||||
|
return { ...newState, name: newState.name.trim() };
|
||||||
|
});
|
||||||
|
|
||||||
|
// Async middleware
|
||||||
|
userState.addMiddleware(async (newState, oldState) => {
|
||||||
|
await auditLog('state-change', { from: oldState, to: newState });
|
||||||
|
return newState;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Removal — addMiddleware() returns a dispose function
|
||||||
|
const remove = userState.addMiddleware(myMiddleware);
|
||||||
|
remove(); // middleware no longer runs
|
||||||
|
```
|
||||||
|
|
||||||
|
Middleware runs **sequentially** in insertion order. If any middleware throws, the state remains unchanged — the operation is **atomic**.
|
||||||
|
|
||||||
|
### 🧮 Computed / Derived State
|
||||||
|
|
||||||
|
Derive reactive values from one or more state parts using `combineLatest` under the hood:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { computed } from '@push.rocks/smartstate';
|
||||||
|
|
||||||
|
const userState = await state.getStatePart('user', { firstName: 'Jane', lastName: 'Doe' });
|
||||||
|
const settingsState = await state.getStatePart('settings', { locale: 'en' });
|
||||||
|
|
||||||
|
// Standalone function
|
||||||
|
const greeting$ = computed(
|
||||||
|
[userState, settingsState],
|
||||||
|
(user, settings) => `Hello, ${user.firstName} (${settings.locale})`,
|
||||||
|
);
|
||||||
|
|
||||||
|
greeting$.subscribe((msg) => console.log(msg));
|
||||||
|
// => "Hello, Jane (en)"
|
||||||
|
|
||||||
|
// Also available as a convenience method on the Smartstate instance:
|
||||||
|
const greeting2$ = state.computed(
|
||||||
|
[userState, settingsState],
|
||||||
|
(user, settings) => `${user.firstName} - ${settings.locale}`,
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
Computed observables are **lazy** — they only subscribe to their sources when someone subscribes to them, and they automatically unsubscribe when all subscribers disconnect.
|
||||||
|
|
||||||
|
### 📦 Batch Updates
|
||||||
|
|
||||||
|
Update multiple state parts at once while deferring all notifications until the entire batch completes:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const partA = await state.getStatePart('a', { value: 1 });
|
||||||
|
const partB = await state.getStatePart('b', { value: 2 });
|
||||||
|
|
||||||
|
await state.batch(async () => {
|
||||||
|
await partA.setState({ value: 10 });
|
||||||
|
await partB.setState({ value: 20 });
|
||||||
|
// No notifications fire inside the batch
|
||||||
|
});
|
||||||
|
// Both subscribers now fire with their new values simultaneously
|
||||||
|
|
||||||
|
// Nested batches are supported — flush happens at the outermost level only
|
||||||
|
await state.batch(async () => {
|
||||||
|
await partA.setState({ value: 100 });
|
||||||
|
await state.batch(async () => {
|
||||||
|
await partB.setState({ value: 200 });
|
||||||
|
});
|
||||||
|
// Still deferred — inner batch doesn't trigger flush
|
||||||
|
});
|
||||||
|
// Now both fire
|
||||||
|
```
|
||||||
|
|
||||||
|
### ⏳ Waiting for State
|
||||||
|
|
||||||
|
Wait for a specific state condition to be met before proceeding:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Wait for any truthy state
|
||||||
|
const currentState = await userState.waitUntilPresent();
|
||||||
|
|
||||||
|
// Wait for a specific condition
|
||||||
|
const name = await userState.waitUntilPresent((s) => s.name || undefined);
|
||||||
|
|
||||||
|
// With timeout (milliseconds)
|
||||||
|
const name = await userState.waitUntilPresent((s) => s.name || undefined, 5000);
|
||||||
|
|
||||||
|
// With AbortSignal and/or timeout via options object
|
||||||
|
const controller = new AbortController();
|
||||||
|
try {
|
||||||
|
const name = await userState.waitUntilPresent(
|
||||||
|
(s) => s.name || undefined,
|
||||||
|
{ timeoutMs: 5000, signal: controller.signal },
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
// e.message is 'Aborted' or 'waitUntilPresent timed out after 5000ms'
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 🌐 Context Protocol Bridge (Web Components)
|
||||||
|
|
||||||
|
Expose state parts to web components via the [W3C Context Protocol](https://github.com/webcomponents-cg/community-protocols/blob/main/proposals/context.md). This lets any web component framework (Lit, FAST, Stencil, or vanilla) consume your state without coupling:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { attachContextProvider } from '@push.rocks/smartstate';
|
||||||
|
|
||||||
|
// Define a context key (use Symbol for uniqueness)
|
||||||
|
const themeContext = Symbol('theme');
|
||||||
|
|
||||||
|
// Attach a provider to a DOM element — any descendant can consume it
|
||||||
|
const cleanup = attachContextProvider(document.body, {
|
||||||
|
context: themeContext,
|
||||||
|
statePart: settingsState,
|
||||||
|
selectorFn: (s) => s.theme, // optional: provide a derived value instead of full state
|
||||||
|
});
|
||||||
|
|
||||||
|
// A consumer dispatches a context-request event:
|
||||||
|
myComponent.dispatchEvent(
|
||||||
|
new CustomEvent('context-request', {
|
||||||
|
bubbles: true,
|
||||||
|
composed: true,
|
||||||
|
detail: {
|
||||||
|
context: themeContext,
|
||||||
|
callback: (theme) => console.log('Got theme:', theme),
|
||||||
|
subscribe: true, // receive updates whenever the state changes
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Works seamlessly with Lit's @consume() decorator, FAST's context, etc.
|
||||||
|
|
||||||
|
// Cleanup when the provider is no longer needed
|
||||||
|
cleanup();
|
||||||
|
```
|
||||||
|
|
||||||
|
### ✅ State Validation
|
||||||
|
|
||||||
|
Built-in validation prevents `null` and `undefined` from being set as state. For custom validation, extend `StatePart`:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { StatePart } from '@push.rocks/smartstate';
|
||||||
|
|
||||||
|
class ValidatedUserPart extends StatePart<string, IUserState> {
|
||||||
|
protected validateState(stateArg: any): stateArg is IUserState {
|
||||||
|
return (
|
||||||
|
super.validateState(stateArg) &&
|
||||||
|
typeof stateArg.name === 'string' &&
|
||||||
|
typeof stateArg.loggedIn === 'boolean'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
If validation fails, `setState()` throws and the state remains unchanged.
|
||||||
|
|
||||||
|
### ⚙️ Async State Setup
|
||||||
|
|
||||||
|
Initialize state with async operations while ensuring actions wait for setup to complete:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
await userState.stateSetup(async (statePart) => {
|
||||||
|
const userData = await fetchUserFromAPI();
|
||||||
|
return { ...statePart.getState(), ...userData };
|
||||||
|
});
|
||||||
|
|
||||||
|
// Any dispatchAction() calls will automatically wait for stateSetup() to finish
|
||||||
|
```
|
||||||
|
|
||||||
|
### 🏎️ Performance
|
||||||
|
|
||||||
|
Smartstate is built with performance in mind:
|
||||||
|
|
||||||
|
- **🔒 SHA256 Change Detection** — Uses content hashing to detect actual changes. Identical state values don't trigger notifications, even with different object references.
|
||||||
|
- **♻️ Selector Memoization** — `select(fn)` caches observables by function reference and shares them via `shareReplay({ refCount: true })`. Multiple subscribers share one upstream subscription.
|
||||||
|
- **📦 Cumulative Notifications** — `notifyChangeCumulative()` debounces rapid changes into a single notification at the end of the call stack.
|
||||||
|
- **🔐 Concurrent Safety** — Simultaneous `getStatePart()` calls for the same name return the same promise, preventing duplicate creation or race conditions.
|
||||||
|
- **💾 Atomic Persistence** — WebStore writes complete before in-memory state updates, ensuring consistency even if the process crashes mid-write.
|
||||||
|
- **⏸️ Batch Deferred Notifications** — `batch()` suppresses all subscriber notifications until every update in the batch completes.
|
||||||
|
|
||||||
|
## API Reference
|
||||||
|
|
||||||
|
### `Smartstate<T>`
|
||||||
|
|
||||||
|
| Method / Property | Description |
|
||||||
|
|-------------------|-------------|
|
||||||
|
| `getStatePart(name, initial?, initMode?)` | Get or create a typed state part |
|
||||||
|
| `batch(fn)` | Batch state updates, defer all notifications until complete |
|
||||||
|
| `computed(sources, fn)` | Create a computed observable from multiple state parts |
|
||||||
|
| `isBatching` | `boolean` — whether a batch is currently active |
|
||||||
|
| `statePartMap` | Registry of all created state parts |
|
||||||
|
|
||||||
|
### `StatePart<TName, TPayload>`
|
||||||
|
|
||||||
|
| Method | Description |
|
||||||
|
|--------|-------------|
|
||||||
|
| `getState()` | Get current state (returns `TPayload \| undefined`) |
|
||||||
|
| `setState(newState)` | Set state — runs middleware → validates → persists → notifies |
|
||||||
|
| `select(selectorFn?, options?)` | Returns an Observable of state or derived values. Options: `{ signal?: AbortSignal }` |
|
||||||
|
| `createAction(actionDef)` | Create a reusable, typed state action |
|
||||||
|
| `dispatchAction(action, payload)` | Dispatch an action and return the new state |
|
||||||
|
| `addMiddleware(fn)` | Add a middleware interceptor. Returns a removal function |
|
||||||
|
| `waitUntilPresent(selectorFn?, opts?)` | Wait for a state condition. Opts: `number` (timeout) or `{ timeoutMs?, signal? }` |
|
||||||
|
| `notifyChange()` | Manually trigger a change notification (with hash dedup) |
|
||||||
|
| `notifyChangeCumulative()` | Debounced notification — fires at end of call stack |
|
||||||
|
| `stateSetup(fn)` | Async state initialization with action serialization |
|
||||||
|
|
||||||
|
### `StateAction<TState, TPayload>`
|
||||||
|
|
||||||
|
| Method | Description |
|
||||||
|
|--------|-------------|
|
||||||
|
| `trigger(payload)` | Dispatch the action on its associated state part |
|
||||||
|
|
||||||
|
### Standalone Functions
|
||||||
|
|
||||||
|
| Function | Description |
|
||||||
|
|----------|-------------|
|
||||||
|
| `computed(sources, fn)` | Create a computed observable from multiple state parts |
|
||||||
|
| `attachContextProvider(element, options)` | Bridge a state part to the W3C Context Protocol |
|
||||||
|
|
||||||
|
### Exported Types
|
||||||
|
|
||||||
|
| Type | Description |
|
||||||
|
|------|-------------|
|
||||||
|
| `TInitMode` | `'soft' \| 'mandatory' \| 'force' \| 'persistent'` |
|
||||||
|
| `TMiddleware<TPayload>` | `(newState, oldState) => TPayload \| Promise<TPayload>` |
|
||||||
|
| `IActionDef<TState, TPayload>` | Action definition function signature |
|
||||||
|
| `IContextProviderOptions<TPayload>` | Options for `attachContextProvider` |
|
||||||
|
|
||||||
|
## License and Legal Information
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
### 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 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
|
||||||
|
|
||||||
|
Task Venture Capital GmbH
|
||||||
|
Registered at District Court Bremen HRB 35230 HB, Germany
|
||||||
|
|
||||||
|
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.
|
||||||
|
|||||||
767
test/test.initialization.ts
Normal file
767
test/test.initialization.ts
Normal file
@@ -0,0 +1,767 @@
|
|||||||
|
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;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================
|
||||||
|
// Init mode tests
|
||||||
|
// ============================
|
||||||
|
|
||||||
|
tap.test('should handle soft init mode (default)', async () => {
|
||||||
|
const state = new smartstate.Smartstate<TTestStateParts>();
|
||||||
|
|
||||||
|
const statePart1 = await state.getStatePart<ITestState>('initTest', {
|
||||||
|
value: 1,
|
||||||
|
nested: { data: 'initial' }
|
||||||
|
});
|
||||||
|
expect(statePart1.getState()).toEqual({
|
||||||
|
value: 1,
|
||||||
|
nested: { data: 'initial' }
|
||||||
|
});
|
||||||
|
|
||||||
|
const statePart2 = await state.getStatePart<ITestState>('initTest');
|
||||||
|
expect(statePart1 === statePart2).toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should handle mandatory init mode', async () => {
|
||||||
|
const state = new smartstate.Smartstate<TTestStateParts>();
|
||||||
|
|
||||||
|
const statePart1 = await state.getStatePart<ITestState>('initTest', {
|
||||||
|
value: 1,
|
||||||
|
nested: { data: 'initial' }
|
||||||
|
}, 'mandatory');
|
||||||
|
expect(statePart1).toBeInstanceOf(smartstate.StatePart);
|
||||||
|
|
||||||
|
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>();
|
||||||
|
|
||||||
|
const statePart1 = await state.getStatePart<ITestState>('forceTest', {
|
||||||
|
value: 1,
|
||||||
|
nested: { data: 'initial' }
|
||||||
|
});
|
||||||
|
expect(statePart1.getState()?.value).toEqual(1);
|
||||||
|
|
||||||
|
const statePart2 = await state.getStatePart<ITestState>('forceTest', {
|
||||||
|
value: 2,
|
||||||
|
nested: { data: 'forced' }
|
||||||
|
}, 'force');
|
||||||
|
expect(statePart2.getState()?.value).toEqual(2);
|
||||||
|
expect(statePart1 === statePart2).toBeFalse();
|
||||||
|
});
|
||||||
|
|
||||||
|
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' }
|
||||||
|
});
|
||||||
|
|
||||||
|
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');
|
||||||
|
|
||||||
|
const values: (ITestState | undefined)[] = [];
|
||||||
|
statePart.select().subscribe(val => values.push(val));
|
||||||
|
|
||||||
|
expect(values).toHaveLength(0);
|
||||||
|
|
||||||
|
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;
|
||||||
|
statePart.select().subscribe(() => notificationCount++);
|
||||||
|
|
||||||
|
expect(notificationCount).toEqual(1);
|
||||||
|
|
||||||
|
await statePart.setState({ value: 1, nested: { data: 'initial' } });
|
||||||
|
await statePart.setState({ value: 1, nested: { data: 'initial' } });
|
||||||
|
await statePart.setState({ value: 1, nested: { data: 'initial' } });
|
||||||
|
|
||||||
|
expect(notificationCount).toEqual(1);
|
||||||
|
|
||||||
|
await statePart.setState({ value: 2, nested: { data: 'changed' } });
|
||||||
|
expect(notificationCount).toEqual(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
// ============================
|
||||||
|
// AbortSignal tests
|
||||||
|
// ============================
|
||||||
|
|
||||||
|
tap.test('select should complete when AbortSignal fires', async () => {
|
||||||
|
const state = new smartstate.Smartstate<TTestStateParts>();
|
||||||
|
const statePart = await state.getStatePart<ITestState>('initTest', {
|
||||||
|
value: 1,
|
||||||
|
nested: { data: 'initial' }
|
||||||
|
});
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
const values: any[] = [];
|
||||||
|
let completed = false;
|
||||||
|
|
||||||
|
statePart.select(undefined, { signal: controller.signal }).subscribe({
|
||||||
|
next: (v) => values.push(v),
|
||||||
|
complete: () => { completed = true; },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(values.length).toBeGreaterThanOrEqual(1);
|
||||||
|
|
||||||
|
controller.abort();
|
||||||
|
// Give microtask time
|
||||||
|
await new Promise<void>((r) => setTimeout(r, 10));
|
||||||
|
|
||||||
|
expect(completed).toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('select with pre-aborted signal should complete immediately', async () => {
|
||||||
|
const state = new smartstate.Smartstate<TTestStateParts>();
|
||||||
|
const statePart = await state.getStatePart<ITestState>('initTest', {
|
||||||
|
value: 1,
|
||||||
|
nested: { data: 'initial' }
|
||||||
|
});
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
controller.abort();
|
||||||
|
|
||||||
|
let completed = false;
|
||||||
|
statePart.select(undefined, { signal: controller.signal }).subscribe({
|
||||||
|
complete: () => { completed = true; },
|
||||||
|
});
|
||||||
|
|
||||||
|
await new Promise<void>((r) => setTimeout(r, 10));
|
||||||
|
expect(completed).toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('waitUntilPresent should reject when AbortSignal fires', async () => {
|
||||||
|
const state = new smartstate.Smartstate<TTestStateParts>();
|
||||||
|
const statePart = await state.getStatePart<ITestState>('initTest', {
|
||||||
|
value: 0,
|
||||||
|
nested: { data: '' }
|
||||||
|
}, 'force');
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
const promise = statePart.waitUntilPresent(
|
||||||
|
(s) => s.value > 100 ? s : undefined as any,
|
||||||
|
{ signal: controller.signal }
|
||||||
|
);
|
||||||
|
|
||||||
|
// Abort before the condition can be met
|
||||||
|
setTimeout(() => controller.abort(), 20);
|
||||||
|
|
||||||
|
let error: Error | null = null;
|
||||||
|
try {
|
||||||
|
await promise;
|
||||||
|
} catch (e) {
|
||||||
|
error = e as Error;
|
||||||
|
}
|
||||||
|
expect(error).not.toBeNull();
|
||||||
|
expect(error?.message).toEqual('Aborted');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('waitUntilPresent should still work with numeric timeout (backward compat)', async () => {
|
||||||
|
const state = new smartstate.Smartstate<TTestStateParts>();
|
||||||
|
const statePart = await state.getStatePart<ITestState>('initTest', {
|
||||||
|
value: 42,
|
||||||
|
nested: { data: 'present' }
|
||||||
|
}, 'force');
|
||||||
|
|
||||||
|
const result = await statePart.waitUntilPresent(undefined, 5000);
|
||||||
|
expect(result.value).toEqual(42);
|
||||||
|
});
|
||||||
|
|
||||||
|
// ============================
|
||||||
|
// Middleware tests
|
||||||
|
// ============================
|
||||||
|
|
||||||
|
tap.test('middleware should transform state', async () => {
|
||||||
|
const state = new smartstate.Smartstate<TTestStateParts>();
|
||||||
|
const statePart = await state.getStatePart<ITestState>('initTest', {
|
||||||
|
value: 1,
|
||||||
|
nested: { data: 'initial' }
|
||||||
|
}, 'force');
|
||||||
|
|
||||||
|
statePart.addMiddleware((newState, oldState) => {
|
||||||
|
return { ...newState, nested: { data: newState.nested.data.toUpperCase() } };
|
||||||
|
});
|
||||||
|
|
||||||
|
await statePart.setState({ value: 2, nested: { data: 'hello' } });
|
||||||
|
expect(statePart.getState().nested.data).toEqual('HELLO');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('middleware should reject state changes on throw', async () => {
|
||||||
|
const state = new smartstate.Smartstate<TTestStateParts>();
|
||||||
|
const statePart = await state.getStatePart<ITestState>('initTest', {
|
||||||
|
value: 1,
|
||||||
|
nested: { data: 'initial' }
|
||||||
|
}, 'force');
|
||||||
|
|
||||||
|
statePart.addMiddleware((newState) => {
|
||||||
|
if (newState.value < 0) {
|
||||||
|
throw new Error('Value must be non-negative');
|
||||||
|
}
|
||||||
|
return newState;
|
||||||
|
});
|
||||||
|
|
||||||
|
let error: Error | null = null;
|
||||||
|
try {
|
||||||
|
await statePart.setState({ value: -1, nested: { data: 'bad' } });
|
||||||
|
} catch (e) {
|
||||||
|
error = e as Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(error).not.toBeNull();
|
||||||
|
expect(error?.message).toEqual('Value must be non-negative');
|
||||||
|
// State should be unchanged
|
||||||
|
expect(statePart.getState().value).toEqual(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('multiple middlewares should run in order', async () => {
|
||||||
|
const state = new smartstate.Smartstate<TTestStateParts>();
|
||||||
|
const statePart = await state.getStatePart<ITestState>('initTest', {
|
||||||
|
value: 1,
|
||||||
|
nested: { data: 'initial' }
|
||||||
|
}, 'force');
|
||||||
|
|
||||||
|
const order: number[] = [];
|
||||||
|
|
||||||
|
statePart.addMiddleware((newState) => {
|
||||||
|
order.push(1);
|
||||||
|
return { ...newState, value: newState.value + 10 };
|
||||||
|
});
|
||||||
|
|
||||||
|
statePart.addMiddleware((newState) => {
|
||||||
|
order.push(2);
|
||||||
|
return { ...newState, value: newState.value * 2 };
|
||||||
|
});
|
||||||
|
|
||||||
|
await statePart.setState({ value: 5, nested: { data: 'test' } });
|
||||||
|
expect(order).toEqual([1, 2]);
|
||||||
|
// (5 + 10) * 2 = 30
|
||||||
|
expect(statePart.getState().value).toEqual(30);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('middleware removal should work', async () => {
|
||||||
|
const state = new smartstate.Smartstate<TTestStateParts>();
|
||||||
|
const statePart = await state.getStatePart<ITestState>('initTest', {
|
||||||
|
value: 1,
|
||||||
|
nested: { data: 'initial' }
|
||||||
|
}, 'force');
|
||||||
|
|
||||||
|
const remove = statePart.addMiddleware((newState) => {
|
||||||
|
return { ...newState, value: newState.value * 100 };
|
||||||
|
});
|
||||||
|
|
||||||
|
await statePart.setState({ value: 2, nested: { data: 'test' } });
|
||||||
|
expect(statePart.getState().value).toEqual(200);
|
||||||
|
|
||||||
|
remove();
|
||||||
|
|
||||||
|
await statePart.setState({ value: 3, nested: { data: 'test' } });
|
||||||
|
expect(statePart.getState().value).toEqual(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
// ============================
|
||||||
|
// Selector memoization tests
|
||||||
|
// ============================
|
||||||
|
|
||||||
|
tap.test('select with same selector fn should return cached observable', async () => {
|
||||||
|
const state = new smartstate.Smartstate<TTestStateParts>();
|
||||||
|
const statePart = await state.getStatePart<ITestState>('initTest', {
|
||||||
|
value: 1,
|
||||||
|
nested: { data: 'initial' }
|
||||||
|
}, 'force');
|
||||||
|
|
||||||
|
const selector = (s: ITestState) => s.value;
|
||||||
|
const obs1 = statePart.select(selector);
|
||||||
|
const obs2 = statePart.select(selector);
|
||||||
|
expect(obs1).toEqual(obs2);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('select with no args should return cached observable', async () => {
|
||||||
|
const state = new smartstate.Smartstate<TTestStateParts>();
|
||||||
|
const statePart = await state.getStatePart<ITestState>('initTest', {
|
||||||
|
value: 1,
|
||||||
|
nested: { data: 'initial' }
|
||||||
|
}, 'force');
|
||||||
|
|
||||||
|
const obs1 = statePart.select();
|
||||||
|
const obs2 = statePart.select();
|
||||||
|
expect(obs1).toEqual(obs2);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('select with different selectors should return different observables', async () => {
|
||||||
|
const state = new smartstate.Smartstate<TTestStateParts>();
|
||||||
|
const statePart = await state.getStatePart<ITestState>('initTest', {
|
||||||
|
value: 1,
|
||||||
|
nested: { data: 'initial' }
|
||||||
|
}, 'force');
|
||||||
|
|
||||||
|
const obs1 = statePart.select((s) => s.value);
|
||||||
|
const obs2 = statePart.select((s) => s.nested);
|
||||||
|
expect(obs1).not.toEqual(obs2);
|
||||||
|
});
|
||||||
|
|
||||||
|
// ============================
|
||||||
|
// Batch update tests
|
||||||
|
// ============================
|
||||||
|
|
||||||
|
tap.test('batch should defer notifications until complete', async () => {
|
||||||
|
type TBatchParts = 'partA' | 'partB';
|
||||||
|
const state = new smartstate.Smartstate<TBatchParts>();
|
||||||
|
const partA = await state.getStatePart<ITestState>('partA', {
|
||||||
|
value: 1,
|
||||||
|
nested: { data: 'a' }
|
||||||
|
});
|
||||||
|
const partB = await state.getStatePart<ITestState>('partB', {
|
||||||
|
value: 2,
|
||||||
|
nested: { data: 'b' }
|
||||||
|
});
|
||||||
|
|
||||||
|
const notificationsA: number[] = [];
|
||||||
|
const notificationsB: number[] = [];
|
||||||
|
|
||||||
|
partA.select((s) => s.value).subscribe((v) => notificationsA.push(v));
|
||||||
|
partB.select((s) => s.value).subscribe((v) => notificationsB.push(v));
|
||||||
|
|
||||||
|
// Reset after initial notifications
|
||||||
|
notificationsA.length = 0;
|
||||||
|
notificationsB.length = 0;
|
||||||
|
|
||||||
|
await state.batch(async () => {
|
||||||
|
await partA.setState({ value: 10, nested: { data: 'aa' } });
|
||||||
|
await partB.setState({ value: 20, nested: { data: 'bb' } });
|
||||||
|
|
||||||
|
// During batch, no notifications yet
|
||||||
|
expect(notificationsA).toHaveLength(0);
|
||||||
|
expect(notificationsB).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
// After batch, both should have notified
|
||||||
|
expect(notificationsA).toContain(10);
|
||||||
|
expect(notificationsB).toContain(20);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('nested batches should only flush at outermost level', async () => {
|
||||||
|
type TBatchParts = 'nested';
|
||||||
|
const state = new smartstate.Smartstate<TBatchParts>();
|
||||||
|
const part = await state.getStatePart<ITestState>('nested', {
|
||||||
|
value: 0,
|
||||||
|
nested: { data: 'start' }
|
||||||
|
});
|
||||||
|
|
||||||
|
const values: number[] = [];
|
||||||
|
part.select((s) => s.value).subscribe((v) => values.push(v));
|
||||||
|
values.length = 0;
|
||||||
|
|
||||||
|
await state.batch(async () => {
|
||||||
|
await part.setState({ value: 1, nested: { data: 'a' } });
|
||||||
|
|
||||||
|
await state.batch(async () => {
|
||||||
|
await part.setState({ value: 2, nested: { data: 'b' } });
|
||||||
|
// Still inside outer batch
|
||||||
|
expect(values).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Inner batch ended but outer batch still active
|
||||||
|
expect(values).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Now outer batch is done — should see final notification
|
||||||
|
expect(values.length).toBeGreaterThanOrEqual(1);
|
||||||
|
expect(values[values.length - 1]).toEqual(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
// ============================
|
||||||
|
// Computed state tests
|
||||||
|
// ============================
|
||||||
|
|
||||||
|
tap.test('computed should derive from multiple state parts', async () => {
|
||||||
|
type TComputedParts = 'first' | 'second';
|
||||||
|
const state = new smartstate.Smartstate<TComputedParts>();
|
||||||
|
const first = await state.getStatePart<{ count: number }>('first', { count: 5 });
|
||||||
|
const second = await state.getStatePart<{ count: number }>('second', { count: 10 });
|
||||||
|
|
||||||
|
const derived$ = state.computed(
|
||||||
|
[first, second],
|
||||||
|
(a, b) => a.count + b.count,
|
||||||
|
);
|
||||||
|
|
||||||
|
const values: number[] = [];
|
||||||
|
derived$.subscribe((v) => values.push(v));
|
||||||
|
|
||||||
|
expect(values).toContain(15);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('computed should update when a source changes', async () => {
|
||||||
|
type TComputedParts = 'x' | 'y';
|
||||||
|
const state = new smartstate.Smartstate<TComputedParts>();
|
||||||
|
const x = await state.getStatePart<{ n: number }>('x', { n: 1 });
|
||||||
|
const y = await state.getStatePart<{ n: number }>('y', { n: 2 });
|
||||||
|
|
||||||
|
const derived$ = state.computed(
|
||||||
|
[x, y],
|
||||||
|
(xState, yState) => xState.n * yState.n,
|
||||||
|
);
|
||||||
|
|
||||||
|
const values: number[] = [];
|
||||||
|
derived$.subscribe((v) => values.push(v));
|
||||||
|
|
||||||
|
// Initial: 1 * 2 = 2
|
||||||
|
expect(values[0]).toEqual(2);
|
||||||
|
|
||||||
|
await x.setState({ n: 5 });
|
||||||
|
|
||||||
|
// After update: 5 * 2 = 10
|
||||||
|
expect(values[values.length - 1]).toEqual(10);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('standalone computed function should work', async () => {
|
||||||
|
type TParts = 'a' | 'b';
|
||||||
|
const state = new smartstate.Smartstate<TParts>();
|
||||||
|
const a = await state.getStatePart<{ val: string }>('a', { val: 'hello' });
|
||||||
|
const b = await state.getStatePart<{ val: string }>('b', { val: 'world' });
|
||||||
|
|
||||||
|
const derived$ = smartstate.computed(
|
||||||
|
[a, b],
|
||||||
|
(aState, bState) => `${aState.val} ${bState.val}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
const values: string[] = [];
|
||||||
|
derived$.subscribe((v) => values.push(v));
|
||||||
|
|
||||||
|
expect(values[0]).toEqual('hello world');
|
||||||
|
|
||||||
|
await a.setState({ val: 'hi' });
|
||||||
|
expect(values[values.length - 1]).toEqual('hi world');
|
||||||
|
});
|
||||||
|
|
||||||
|
// ============================
|
||||||
|
// Context Protocol tests
|
||||||
|
// ============================
|
||||||
|
|
||||||
|
tap.test('attachContextProvider should respond to context-request events', async () => {
|
||||||
|
// EventTarget and CustomEvent are available in Node 18+
|
||||||
|
if (typeof EventTarget === 'undefined') {
|
||||||
|
console.log('Skipping context test — EventTarget not available');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
type TParts = 'ctx';
|
||||||
|
const state = new smartstate.Smartstate<TParts>();
|
||||||
|
const statePart = await state.getStatePart<{ theme: string }>('ctx', { theme: 'dark' });
|
||||||
|
|
||||||
|
const myContext = Symbol('test-context');
|
||||||
|
|
||||||
|
// Use an EventTarget as a mock element
|
||||||
|
const element = new EventTarget() as any as HTMLElement;
|
||||||
|
|
||||||
|
const cleanup = smartstate.attachContextProvider(element, {
|
||||||
|
context: myContext,
|
||||||
|
statePart,
|
||||||
|
});
|
||||||
|
|
||||||
|
let receivedValue: any = null;
|
||||||
|
|
||||||
|
// Dispatch a context-request event
|
||||||
|
const event = new CustomEvent('context-request', {
|
||||||
|
detail: {
|
||||||
|
context: myContext,
|
||||||
|
callback: (value: any) => { receivedValue = value; },
|
||||||
|
subscribe: false,
|
||||||
|
},
|
||||||
|
bubbles: true,
|
||||||
|
composed: true,
|
||||||
|
});
|
||||||
|
(element as any).dispatchEvent(event);
|
||||||
|
|
||||||
|
expect(receivedValue).toEqual({ theme: 'dark' });
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('attachContextProvider should support subscriptions', async () => {
|
||||||
|
if (typeof EventTarget === 'undefined') {
|
||||||
|
console.log('Skipping context subscription test — EventTarget not available');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
type TParts = 'ctxSub';
|
||||||
|
const state = new smartstate.Smartstate<TParts>();
|
||||||
|
const statePart = await state.getStatePart<{ count: number }>('ctxSub', { count: 0 });
|
||||||
|
|
||||||
|
const myContext = Symbol('sub-context');
|
||||||
|
const element = new EventTarget() as any as HTMLElement;
|
||||||
|
|
||||||
|
const cleanup = smartstate.attachContextProvider(element, {
|
||||||
|
context: myContext,
|
||||||
|
statePart,
|
||||||
|
});
|
||||||
|
|
||||||
|
const receivedValues: any[] = [];
|
||||||
|
let unsubFn: (() => void) | undefined;
|
||||||
|
|
||||||
|
const event = new CustomEvent('context-request', {
|
||||||
|
detail: {
|
||||||
|
context: myContext,
|
||||||
|
callback: (value: any, unsub?: () => void) => {
|
||||||
|
receivedValues.push(value);
|
||||||
|
if (unsub) unsubFn = unsub;
|
||||||
|
},
|
||||||
|
subscribe: true,
|
||||||
|
},
|
||||||
|
bubbles: true,
|
||||||
|
composed: true,
|
||||||
|
});
|
||||||
|
(element as any).dispatchEvent(event);
|
||||||
|
|
||||||
|
expect(receivedValues).toHaveLength(1);
|
||||||
|
expect(receivedValues[0]).toEqual({ count: 0 });
|
||||||
|
|
||||||
|
// Update state — should trigger subscription callback
|
||||||
|
await statePart.setState({ count: 42 });
|
||||||
|
|
||||||
|
// Give a tick for the subscription to fire
|
||||||
|
await new Promise<void>((r) => setTimeout(r, 10));
|
||||||
|
|
||||||
|
expect(receivedValues.length).toBeGreaterThanOrEqual(2);
|
||||||
|
expect(receivedValues[receivedValues.length - 1]).toEqual({ count: 42 });
|
||||||
|
|
||||||
|
// Unsubscribe
|
||||||
|
expect(unsubFn).toBeDefined();
|
||||||
|
unsubFn!();
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('attachContextProvider should ignore non-matching contexts', async () => {
|
||||||
|
if (typeof EventTarget === 'undefined') {
|
||||||
|
console.log('Skipping context mismatch test — EventTarget not available');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
type TParts = 'ctxMismatch';
|
||||||
|
const state = new smartstate.Smartstate<TParts>();
|
||||||
|
const statePart = await state.getStatePart<{ v: number }>('ctxMismatch', { v: 1 });
|
||||||
|
|
||||||
|
const myContext = Symbol('my-context');
|
||||||
|
const otherContext = Symbol('other-context');
|
||||||
|
const element = new EventTarget() as any as HTMLElement;
|
||||||
|
|
||||||
|
const cleanup = smartstate.attachContextProvider(element, {
|
||||||
|
context: myContext,
|
||||||
|
statePart,
|
||||||
|
});
|
||||||
|
|
||||||
|
let called = false;
|
||||||
|
const event = new CustomEvent('context-request', {
|
||||||
|
detail: {
|
||||||
|
context: otherContext,
|
||||||
|
callback: () => { called = true; },
|
||||||
|
subscribe: false,
|
||||||
|
},
|
||||||
|
bubbles: true,
|
||||||
|
composed: true,
|
||||||
|
});
|
||||||
|
(element as any).dispatchEvent(event);
|
||||||
|
|
||||||
|
expect(called).toBeFalse();
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
});
|
||||||
|
|
||||||
|
// ============================
|
||||||
|
// Enterprise hardening tests
|
||||||
|
// ============================
|
||||||
|
|
||||||
|
tap.test('concurrent dispatchAction should serialize (counter reaches exactly 10)', async () => {
|
||||||
|
type TParts = 'counter';
|
||||||
|
const state = new smartstate.Smartstate<TParts>();
|
||||||
|
const counter = await state.getStatePart<{ count: number }>('counter', { count: 0 });
|
||||||
|
|
||||||
|
const increment = counter.createAction<void>(async (statePart) => {
|
||||||
|
const current = statePart.getState();
|
||||||
|
return { count: current.count + 1 };
|
||||||
|
});
|
||||||
|
|
||||||
|
// Fire 10 concurrent increments (no await)
|
||||||
|
const promises: Promise<any>[] = [];
|
||||||
|
for (let i = 0; i < 10; i++) {
|
||||||
|
promises.push(counter.dispatchAction(increment, undefined));
|
||||||
|
}
|
||||||
|
await Promise.all(promises);
|
||||||
|
|
||||||
|
expect(counter.getState().count).toEqual(10);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('concurrent setState should serialize (no lost updates)', async () => {
|
||||||
|
type TParts = 'concurrent';
|
||||||
|
const state = new smartstate.Smartstate<TParts>();
|
||||||
|
const part = await state.getStatePart<{ values: number[] }>('concurrent', { values: [] });
|
||||||
|
|
||||||
|
const promises: Promise<any>[] = [];
|
||||||
|
for (let i = 0; i < 5; i++) {
|
||||||
|
promises.push(
|
||||||
|
part.setState({ values: [...(part.getState()?.values || []), i] })
|
||||||
|
);
|
||||||
|
}
|
||||||
|
await Promise.all(promises);
|
||||||
|
|
||||||
|
// At minimum, the final state should have been set 5 times without error
|
||||||
|
// The exact values depend on serialization timing, but state should be valid
|
||||||
|
expect(part.getState()).toBeTruthy();
|
||||||
|
expect(part.getState().values).toBeInstanceOf(Array);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('dispose should complete the Subject and notify subscribers', async () => {
|
||||||
|
type TParts = 'disposable';
|
||||||
|
const state = new smartstate.Smartstate<TParts>();
|
||||||
|
const part = await state.getStatePart<{ v: number }>('disposable', { v: 1 });
|
||||||
|
|
||||||
|
let completed = false;
|
||||||
|
part.select().subscribe({
|
||||||
|
complete: () => { completed = true; },
|
||||||
|
});
|
||||||
|
|
||||||
|
part.dispose();
|
||||||
|
|
||||||
|
expect(completed).toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('falsy state {count: 0} should trigger notification', async () => {
|
||||||
|
type TParts = 'falsy';
|
||||||
|
const state = new smartstate.Smartstate<TParts>();
|
||||||
|
const part = await state.getStatePart<{ count: number }>('falsy', { count: 0 });
|
||||||
|
|
||||||
|
const values: number[] = [];
|
||||||
|
part.select((s) => s.count).subscribe((v) => values.push(v));
|
||||||
|
|
||||||
|
// Initial value should include 0
|
||||||
|
expect(values).toContain(0);
|
||||||
|
|
||||||
|
await part.setState({ count: 0 });
|
||||||
|
// Should not duplicate since hash is the same, but the initial notification should have fired
|
||||||
|
expect(values.length).toBeGreaterThanOrEqual(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('waitUntilPresent should resolve for falsy non-null values like false', async () => {
|
||||||
|
type TParts = 'falsyWait';
|
||||||
|
const state = new smartstate.Smartstate<TParts>();
|
||||||
|
const part = await state.getStatePart<{ flag: boolean }>('falsyWait', { flag: false });
|
||||||
|
|
||||||
|
const result = await part.waitUntilPresent((s) => s.flag as any, 2000);
|
||||||
|
// false is not null/undefined, so it should resolve
|
||||||
|
// Actually false IS falsy for `value !== undefined && value !== null` — false passes that check
|
||||||
|
expect(result).toBeFalse();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('batch re-entrancy: setState during flush should not deadlock', async () => {
|
||||||
|
type TParts = 'reentrant';
|
||||||
|
const state = new smartstate.Smartstate<TParts>();
|
||||||
|
const part = await state.getStatePart<{ v: number }>('reentrant', { v: 0 });
|
||||||
|
|
||||||
|
let flushSetStateDone = false;
|
||||||
|
|
||||||
|
// Subscribe and trigger a setState during notification flush
|
||||||
|
part.select((s) => s.v).subscribe((v) => {
|
||||||
|
if (v === 1 && !flushSetStateDone) {
|
||||||
|
flushSetStateDone = true;
|
||||||
|
// Fire-and-forget setState during notification — should not deadlock
|
||||||
|
part.setState({ v: 2 });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await state.batch(async () => {
|
||||||
|
await part.setState({ v: 1 });
|
||||||
|
});
|
||||||
|
|
||||||
|
// Wait for the fire-and-forget setState to complete
|
||||||
|
await new Promise<void>((r) => setTimeout(r, 50));
|
||||||
|
|
||||||
|
expect(part.getState().v).toEqual(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('force mode should dispose old StatePart (Subject completes)', async () => {
|
||||||
|
type TParts = 'forceDispose';
|
||||||
|
const state = new smartstate.Smartstate<TParts>();
|
||||||
|
const old = await state.getStatePart<{ v: number }>('forceDispose', { v: 1 });
|
||||||
|
|
||||||
|
let oldCompleted = false;
|
||||||
|
old.select().subscribe({
|
||||||
|
complete: () => { oldCompleted = true; },
|
||||||
|
});
|
||||||
|
|
||||||
|
await state.getStatePart<{ v: number }>('forceDispose', { v: 2 }, 'force');
|
||||||
|
|
||||||
|
expect(oldCompleted).toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('getStatePart should accept 0 as initial value', async () => {
|
||||||
|
type TParts = 'zeroInit';
|
||||||
|
const state = new smartstate.Smartstate<TParts>();
|
||||||
|
|
||||||
|
// 0 is falsy but should be accepted as a valid initial value
|
||||||
|
const part = await state.getStatePart<number>('zeroInit', 0);
|
||||||
|
expect(part.getState()).toEqual(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
export default tap.start();
|
||||||
41
test/test.ts
41
test/test.ts
@@ -1,5 +1,5 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@push.rocks/tapbundle';
|
||||||
import * as smartstate from '../ts/index';
|
import * as smartstate from '../ts/index.js';
|
||||||
|
|
||||||
type TMyStateParts = 'testStatePart';
|
type TMyStateParts = 'testStatePart';
|
||||||
interface TStatePartPayload {
|
interface TStatePartPayload {
|
||||||
@@ -14,36 +14,47 @@ let testStatePart: smartstate.StatePart<TMyStateParts, TStatePartPayload>;
|
|||||||
|
|
||||||
tap.test('should create a new SmartState', async () => {
|
tap.test('should create a new SmartState', async () => {
|
||||||
testState = new smartstate.Smartstate<TMyStateParts>();
|
testState = new smartstate.Smartstate<TMyStateParts>();
|
||||||
expect(testState).to.be.instanceOf(smartstate.Smartstate);
|
expect(testState).toBeInstanceOf(smartstate.Smartstate);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should create a new StatePart', async () => {
|
tap.test('should create a new StatePart', async () => {
|
||||||
testStatePart = testState.getStatePart<TStatePartPayload>('testStatePart', {
|
testStatePart = await testState.getStatePart<TStatePartPayload>('testStatePart', {
|
||||||
currentFavorites: [],
|
currentFavorites: [],
|
||||||
deep: {
|
deep: {
|
||||||
hi: 2
|
hi: 2,
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
expect(testStatePart).to.be.instanceOf(smartstate.StatePart);
|
expect(testStatePart).toBeInstanceOf(smartstate.StatePart);
|
||||||
console.log(testStatePart);
|
console.log(testStatePart);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should select something', async () => {
|
tap.test('should select something', async () => {
|
||||||
testStatePart
|
testStatePart
|
||||||
.select(state => state.deep.hi)
|
.select((state) => state.deep.hi)
|
||||||
.subscribe(substate => {
|
.subscribe((substate) => {
|
||||||
expect(substate).to.equal(2);
|
expect(substate).toEqual(2);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should dispatch a state action', async () => {
|
tap.test('should dispatch a state action', async (tools) => {
|
||||||
|
const done = tools.defer();
|
||||||
const addFavourite = testStatePart.createAction<string>(async (statePart, payload) => {
|
const addFavourite = testStatePart.createAction<string>(async (statePart, payload) => {
|
||||||
const currentState = statePart.getState();
|
const currentState = statePart.getState();
|
||||||
currentState.currentFavorites.push(payload);
|
return {
|
||||||
return currentState;
|
...currentState,
|
||||||
|
currentFavorites: [...currentState.currentFavorites, payload],
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
testStatePart
|
||||||
|
.waitUntilPresent((state) => {
|
||||||
|
return state.currentFavorites[0];
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
done.resolve();
|
||||||
|
});
|
||||||
await testStatePart.dispatchAction(addFavourite, 'my favourite things');
|
await testStatePart.dispatchAction(addFavourite, 'my favourite things');
|
||||||
expect(testStatePart.getState().currentFavorites).to.include('my favourite things');
|
expect(testStatePart.getState().currentFavorites).toContain('my favourite things');
|
||||||
|
await done.promise;
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
export default tap.start();
|
||||||
|
|||||||
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitinfo by @push.rocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@push.rocks/smartstate',
|
||||||
|
version: '2.1.1',
|
||||||
|
description: 'A TypeScript-first reactive state management library with middleware, computed state, batching, persistence, and Web Component Context Protocol support.'
|
||||||
|
}
|
||||||
10
ts/index.ts
10
ts/index.ts
@@ -1,5 +1,5 @@
|
|||||||
export * from './smartstate.classes.smartstate';
|
export * from './smartstate.classes.smartstate.js';
|
||||||
export * from './smartstate.classes.statepart';
|
export * from './smartstate.classes.statepart.js';
|
||||||
export * from './smartstate.classes.statecollection';
|
export * from './smartstate.classes.stateaction.js';
|
||||||
export * from './smartstate.classes.stateaction';
|
export * from './smartstate.classes.computed.js';
|
||||||
export * from './smartstate.classes.stateobservable';
|
export * from './smartstate.contextprovider.js';
|
||||||
|
|||||||
16
ts/smartstate.classes.computed.ts
Normal file
16
ts/smartstate.classes.computed.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import * as plugins from './smartstate.plugins.js';
|
||||||
|
import { combineLatest, map } from 'rxjs';
|
||||||
|
import type { StatePart } from './smartstate.classes.statepart.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* creates a computed observable derived from multiple state parts.
|
||||||
|
* the observable is lazy — it only subscribes to sources when subscribed to.
|
||||||
|
*/
|
||||||
|
export function computed<TResult>(
|
||||||
|
sources: StatePart<any, any>[],
|
||||||
|
computeFn: (...states: any[]) => TResult,
|
||||||
|
): plugins.smartrx.rxjs.Observable<TResult> {
|
||||||
|
return combineLatest(sources.map((sp) => sp.select())).pipe(
|
||||||
|
map((states) => computeFn(...states)),
|
||||||
|
) as plugins.smartrx.rxjs.Observable<TResult>;
|
||||||
|
}
|
||||||
@@ -1,52 +1,154 @@
|
|||||||
import * as plugins from './smartstate.plugins';
|
import * as plugins from './smartstate.plugins.js';
|
||||||
import { StatePart } from './smartstate.classes.statepart';
|
import { StatePart } from './smartstate.classes.statepart.js';
|
||||||
|
import { computed } from './smartstate.classes.computed.js';
|
||||||
|
|
||||||
|
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> } = {};
|
||||||
|
|
||||||
|
private pendingStatePartCreation: Map<string, Promise<StatePart<StatePartNameType, any>>> = new Map();
|
||||||
|
|
||||||
|
// Batch support
|
||||||
|
private batchDepth = 0;
|
||||||
|
private isFlushing = false;
|
||||||
|
private pendingNotifications = new Set<StatePart<any, any>>();
|
||||||
|
|
||||||
constructor() {}
|
constructor() {}
|
||||||
|
|
||||||
public getStatePart<PayloadType>(
|
/**
|
||||||
statePartNameArg: StatePartNameType,
|
* whether state changes are currently being batched
|
||||||
initialArg?: PayloadType
|
*/
|
||||||
): StatePart<StatePartNameType, PayloadType> {
|
public get isBatching(): boolean {
|
||||||
if (this.statePartMap[statePartNameArg as any]) {
|
return this.batchDepth > 0;
|
||||||
if (initialArg) {
|
}
|
||||||
throw new Error(
|
|
||||||
`${statePartNameArg} already exists, yet you try to set an initial state again`
|
/**
|
||||||
);
|
* registers a state part for deferred notification during a batch
|
||||||
|
*/
|
||||||
|
public registerPendingNotification(statePart: StatePart<any, any>): void {
|
||||||
|
this.pendingNotifications.add(statePart);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* batches multiple state updates so subscribers are only notified once all updates complete
|
||||||
|
*/
|
||||||
|
public async batch(updateFn: () => Promise<void> | void): Promise<void> {
|
||||||
|
this.batchDepth++;
|
||||||
|
try {
|
||||||
|
await updateFn();
|
||||||
|
} finally {
|
||||||
|
this.batchDepth--;
|
||||||
|
if (this.batchDepth === 0 && !this.isFlushing) {
|
||||||
|
this.isFlushing = true;
|
||||||
|
try {
|
||||||
|
while (this.pendingNotifications.size > 0) {
|
||||||
|
const pending = [...this.pendingNotifications];
|
||||||
|
this.pendingNotifications.clear();
|
||||||
|
for (const sp of pending) {
|
||||||
|
await sp.notifyChange();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
this.isFlushing = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return this.statePartMap[statePartNameArg as any];
|
|
||||||
} 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates a statepart
|
* creates a computed observable derived from multiple state parts
|
||||||
* @param statePartName
|
|
||||||
* @param initialPayloadArg
|
|
||||||
*/
|
*/
|
||||||
private createStatePart<PayloadType>(
|
public computed<TResult>(
|
||||||
statePartName: StatePartNameType,
|
sources: StatePart<StatePartNameType, any>[],
|
||||||
initialPayloadArg: PayloadType
|
computeFn: (...states: any[]) => TResult,
|
||||||
): StatePart<StatePartNameType, PayloadType> {
|
): plugins.smartrx.rxjs.Observable<TResult> {
|
||||||
const newState = new StatePart<StatePartNameType, PayloadType>(statePartName);
|
return computed(sources, computeFn);
|
||||||
newState.setState(initialPayloadArg);
|
|
||||||
this.statePartMap[statePartName as any] = newState;
|
|
||||||
return newState;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* dispatches an action on the main level
|
* Allows getting and initializing a new statepart
|
||||||
*/
|
*/
|
||||||
public dispatch() {}
|
public async getStatePart<PayloadType>(
|
||||||
|
statePartNameArg: StatePartNameType,
|
||||||
|
initialArg?: PayloadType,
|
||||||
|
initMode: TInitMode = 'soft'
|
||||||
|
): Promise<StatePart<StatePartNameType, PayloadType>> {
|
||||||
|
// Return pending creation if one exists to prevent duplicate state parts
|
||||||
|
const pending = this.pendingStatePartCreation.get(statePartNameArg);
|
||||||
|
if (pending) {
|
||||||
|
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':
|
||||||
|
existingStatePart.dispose();
|
||||||
|
break;
|
||||||
|
case 'soft':
|
||||||
|
case 'persistent':
|
||||||
|
default:
|
||||||
|
return existingStatePart as StatePart<StatePartNameType, PayloadType>;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (initialArg === undefined) {
|
||||||
|
throw new Error(
|
||||||
|
`State part '${statePartNameArg}' does not exist and no initial state provided`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a statepart
|
||||||
|
*/
|
||||||
|
private async createStatePart<PayloadType>(
|
||||||
|
statePartName: StatePartNameType,
|
||||||
|
initialPayloadArg: PayloadType,
|
||||||
|
initMode: TInitMode = 'soft'
|
||||||
|
): Promise<StatePart<StatePartNameType, PayloadType>> {
|
||||||
|
const newState = new StatePart<StatePartNameType, PayloadType>(
|
||||||
|
statePartName,
|
||||||
|
initMode === 'persistent'
|
||||||
|
? {
|
||||||
|
dbName: 'smartstate',
|
||||||
|
storeName: statePartName,
|
||||||
|
}
|
||||||
|
: null
|
||||||
|
);
|
||||||
|
newState.smartstateRef = this;
|
||||||
|
await newState.init();
|
||||||
|
const currentState = newState.getState();
|
||||||
|
|
||||||
|
if (initMode === 'persistent' && currentState !== undefined) {
|
||||||
|
await newState.setState({
|
||||||
|
...initialPayloadArg,
|
||||||
|
...currentState,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await newState.setState(initialPayloadArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.statePartMap[statePartName] = newState;
|
||||||
|
return newState;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import * as plugins from './smartstate.plugins';
|
import { StatePart } from './smartstate.classes.statepart.js';
|
||||||
import { StatePart } from './smartstate.classes.statepart';
|
|
||||||
|
|
||||||
export interface IActionDef<TStateType, TActionPayloadType> {
|
export interface IActionDef<TStateType, TActionPayloadType> {
|
||||||
(stateArg: StatePart<any, TStateType>, actionPayload: TActionPayloadType): Promise<TStateType>;
|
(stateArg: StatePart<any, TStateType>, actionPayload: TActionPayloadType): Promise<TStateType>;
|
||||||
@@ -9,9 +8,12 @@ export interface IActionDef<TStateType, TActionPayloadType> {
|
|||||||
* an actionmodifier for the state
|
* an actionmodifier for the state
|
||||||
*/
|
*/
|
||||||
export class StateAction<TStateType, TActionPayloadType> {
|
export class StateAction<TStateType, TActionPayloadType> {
|
||||||
constructor(public statePartRef: StatePart<any, any>, public actionDef: IActionDef<TStateType, TActionPayloadType>) {}
|
constructor(
|
||||||
|
public statePartRef: StatePart<any, any>,
|
||||||
|
public actionDef: IActionDef<TStateType, TActionPayloadType>
|
||||||
|
) {}
|
||||||
|
|
||||||
public trigger(payload: TActionPayloadType) {
|
public trigger(payload: TActionPayloadType): Promise<TStateType> {
|
||||||
this.statePartRef.dispatchAction(this, payload);
|
return this.statePartRef.dispatchAction(this, payload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
import * as plugins from './smartstate.plugins';
|
|
||||||
import { StatePart } from './smartstate.classes.statepart';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A StatePartCollection is a collection of StateParts.
|
|
||||||
* It can be used for expressing interest in a certain set of StateParts.
|
|
||||||
*/
|
|
||||||
export class StatePartCollection<StatePartNameType, T> extends StatePart<StatePartNameType, T> {
|
|
||||||
constructor(nameArg: StatePartNameType) {
|
|
||||||
super(nameArg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
import * as plugins from './smartstate.plugins';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* State observable observes a StatePart and notifies everyone interested
|
|
||||||
*/
|
|
||||||
export class StateObservable {
|
|
||||||
/**
|
|
||||||
* creates an observable from a StateCollection
|
|
||||||
*/
|
|
||||||
public static fromStatePartCollection(filterArg?: () => any) {}
|
|
||||||
|
|
||||||
constructor() {}
|
|
||||||
}
|
|
||||||
@@ -1,62 +1,243 @@
|
|||||||
import * as plugins from './smartstate.plugins';
|
import * as plugins from './smartstate.plugins.js';
|
||||||
|
import { Observable, shareReplay, takeUntil } from 'rxjs';
|
||||||
|
import { StateAction, type IActionDef } from './smartstate.classes.stateaction.js';
|
||||||
|
import type { Smartstate } from './smartstate.classes.smartstate.js';
|
||||||
|
|
||||||
import { Observable, Subject } from 'rxjs';
|
export type TMiddleware<TPayload> = (
|
||||||
import { startWith, takeUntil, map } from 'rxjs/operators';
|
newState: TPayload,
|
||||||
|
oldState: TPayload | undefined,
|
||||||
|
) => TPayload | Promise<TPayload>;
|
||||||
|
|
||||||
import { StateAction, IActionDef } from './smartstate.classes.stateaction';
|
/**
|
||||||
|
* creates an Observable that emits once when the given AbortSignal fires
|
||||||
|
*/
|
||||||
|
function fromAbortSignal(signal: AbortSignal): Observable<void> {
|
||||||
|
return new Observable<void>((subscriber) => {
|
||||||
|
if (signal.aborted) {
|
||||||
|
subscriber.next();
|
||||||
|
subscriber.complete();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const handler = () => {
|
||||||
|
subscriber.next();
|
||||||
|
subscriber.complete();
|
||||||
|
};
|
||||||
|
signal.addEventListener('abort', handler);
|
||||||
|
return () => signal.removeEventListener('abort', handler);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export class StatePart<TStatePartName, TStatePayload> {
|
export class StatePart<TStatePartName, TStatePayload> {
|
||||||
public name: TStatePartName;
|
public name: TStatePartName;
|
||||||
public state = new Subject<TStatePayload>();
|
public state = new plugins.smartrx.rxjs.Subject<TStatePayload>();
|
||||||
public stateStore: TStatePayload;
|
public stateStore: TStatePayload | undefined;
|
||||||
|
public smartstateRef?: Smartstate<any>;
|
||||||
|
private cumulativeDeferred = plugins.smartpromise.cumulativeDefer();
|
||||||
|
|
||||||
constructor(nameArg: TStatePartName) {
|
private mutationQueue: Promise<any> = Promise.resolve();
|
||||||
|
private pendingCumulativeNotification: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
|
||||||
|
private webStoreOptions: plugins.webstore.IWebStoreOptions;
|
||||||
|
private webStore: plugins.webstore.WebStore<TStatePayload> | null = null;
|
||||||
|
|
||||||
|
private middlewares: TMiddleware<TStatePayload>[] = [];
|
||||||
|
|
||||||
|
// Selector memoization
|
||||||
|
private selectorCache = new WeakMap<Function, plugins.smartrx.rxjs.Observable<any>>();
|
||||||
|
private defaultSelectObservable: plugins.smartrx.rxjs.Observable<TStatePayload> | null = null;
|
||||||
|
|
||||||
|
constructor(nameArg: TStatePartName, webStoreOptionsArg?: plugins.webstore.IWebStoreOptions) {
|
||||||
this.name = nameArg;
|
this.name = nameArg;
|
||||||
|
|
||||||
|
if (webStoreOptionsArg) {
|
||||||
|
this.webStoreOptions = webStoreOptionsArg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* initializes the webstore
|
||||||
|
*/
|
||||||
|
public async init() {
|
||||||
|
if (this.webStoreOptions) {
|
||||||
|
this.webStore = new plugins.webstore.WebStore<TStatePayload>(this.webStoreOptions);
|
||||||
|
await this.webStore.init();
|
||||||
|
const storedState = await this.webStore.get(String(this.name));
|
||||||
|
if (storedState && this.validateState(storedState)) {
|
||||||
|
this.stateStore = storedState;
|
||||||
|
await this.notifyChange();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sets the stateStore to the new state
|
* adds a middleware that intercepts setState calls.
|
||||||
* @param newStateArg
|
* middleware can transform the state or throw to reject it.
|
||||||
|
* returns a removal function.
|
||||||
*/
|
*/
|
||||||
public setState(newStateArg: TStatePayload) {
|
public addMiddleware(middleware: TMiddleware<TStatePayload>): () => void {
|
||||||
this.stateStore = newStateArg;
|
this.middlewares.push(middleware);
|
||||||
this.notifyChange();
|
return () => {
|
||||||
|
const idx = this.middlewares.indexOf(middleware);
|
||||||
|
if (idx !== -1) {
|
||||||
|
this.middlewares.splice(idx, 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sets the stateStore to the new state (serialized via mutation queue)
|
||||||
|
*/
|
||||||
|
public async setState(newStateArg: TStatePayload): Promise<TStatePayload> {
|
||||||
|
return this.mutationQueue = this.mutationQueue.then(
|
||||||
|
() => this.applyState(newStateArg),
|
||||||
|
() => this.applyState(newStateArg),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* applies the state change (middleware → validate → persist → notify)
|
||||||
|
*/
|
||||||
|
private async applyState(newStateArg: TStatePayload): Promise<TStatePayload> {
|
||||||
|
// Run middleware chain
|
||||||
|
let processedState = newStateArg;
|
||||||
|
for (const mw of this.middlewares) {
|
||||||
|
processedState = await mw(processedState, this.stateStore);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate state structure
|
||||||
|
if (!this.validateState(processedState)) {
|
||||||
|
throw new Error(`Invalid state structure for state part '${this.name}'`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save to WebStore first to ensure atomicity
|
||||||
|
if (this.webStore) {
|
||||||
|
await this.webStore.set(String(this.name), processedState);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update in-memory state after successful persistence
|
||||||
|
this.stateStore = processedState;
|
||||||
|
await this.notifyChange();
|
||||||
|
|
||||||
|
return this.stateStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates state structure - can be overridden for custom validation
|
||||||
|
*/
|
||||||
|
protected validateState(stateArg: any): stateArg is TStatePayload {
|
||||||
|
return stateArg !== null && stateArg !== undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* notifies of a change on the state
|
* notifies of a change on the state
|
||||||
*/
|
*/
|
||||||
public notifyChange() {
|
public async notifyChange() {
|
||||||
this.state.next(this.stateStore);
|
const snapshot = this.stateStore;
|
||||||
|
if (snapshot === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If inside a batch, defer the notification
|
||||||
|
if (this.smartstateRef?.isBatching) {
|
||||||
|
this.smartstateRef.registerPendingNotification(this);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const createStateHash = async (stateArg: any) => {
|
||||||
|
return await plugins.smarthashWeb.sha256FromString(plugins.smartjson.stableOneWayStringify(stateArg));
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
const currentHash = await createStateHash(snapshot);
|
||||||
|
if (
|
||||||
|
this.lastStateNotificationPayloadHash &&
|
||||||
|
currentHash === this.lastStateNotificationPayloadHash
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.lastStateNotificationPayloadHash = currentHash;
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`State hash computation failed for '${this.name}':`, err);
|
||||||
|
}
|
||||||
|
this.state.next(snapshot);
|
||||||
|
}
|
||||||
|
private lastStateNotificationPayloadHash: any;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* creates a cumulative notification by adding a change notification at the end of the call stack
|
||||||
|
*/
|
||||||
|
public notifyChangeCumulative() {
|
||||||
|
if (this.pendingCumulativeNotification) {
|
||||||
|
clearTimeout(this.pendingCumulativeNotification);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.pendingCumulativeNotification = setTimeout(() => {
|
||||||
|
this.pendingCumulativeNotification = null;
|
||||||
|
if (this.stateStore !== undefined) {
|
||||||
|
this.notifyChange().catch((err) => {
|
||||||
|
console.error(`notifyChangeCumulative failed for '${this.name}':`, err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* selects a state or a substate
|
* selects a state or a substate.
|
||||||
|
* supports an optional AbortSignal for automatic unsubscription.
|
||||||
|
* memoizes observables by selector function reference.
|
||||||
*/
|
*/
|
||||||
public select<T = TStatePayload>(selectorFn?: (state: TStatePayload) => T): Observable<T> {
|
public select<T = TStatePayload>(
|
||||||
if (!selectorFn) {
|
selectorFn?: (state: TStatePayload) => T,
|
||||||
selectorFn = (state: TStatePayload) => <T>(<any>state);
|
options?: { signal?: AbortSignal }
|
||||||
|
): plugins.smartrx.rxjs.Observable<T> {
|
||||||
|
const hasSignal = options?.signal != null;
|
||||||
|
|
||||||
|
// Check memoization cache (only for non-signal selects)
|
||||||
|
if (!hasSignal) {
|
||||||
|
if (!selectorFn) {
|
||||||
|
if (this.defaultSelectObservable) {
|
||||||
|
return this.defaultSelectObservable as unknown as plugins.smartrx.rxjs.Observable<T>;
|
||||||
|
}
|
||||||
|
} else if (this.selectorCache.has(selectorFn)) {
|
||||||
|
return this.selectorCache.get(selectorFn)!;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const mapped = this.state.pipe(
|
const effectiveSelectorFn = selectorFn || ((state: TStatePayload) => <T>(<any>state));
|
||||||
startWith(this.getState()),
|
|
||||||
map((stateArg) => {
|
let mapped = this.state.pipe(
|
||||||
|
plugins.smartrx.rxjs.ops.startWith(this.getState()),
|
||||||
|
plugins.smartrx.rxjs.ops.filter((stateArg): stateArg is TStatePayload => stateArg !== undefined),
|
||||||
|
plugins.smartrx.rxjs.ops.map((stateArg) => {
|
||||||
try {
|
try {
|
||||||
return selectorFn(stateArg);
|
return effectiveSelectorFn(stateArg);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Nothing here
|
console.error(`Selector error in state part '${this.name}':`, e);
|
||||||
|
return undefined;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
return mapped;
|
if (hasSignal) {
|
||||||
|
mapped = mapped.pipe(takeUntil(fromAbortSignal(options.signal)));
|
||||||
|
return mapped;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply shareReplay for caching and store in memo cache
|
||||||
|
const shared = mapped.pipe(shareReplay({ bufferSize: 1, refCount: true }));
|
||||||
|
if (!selectorFn) {
|
||||||
|
this.defaultSelectObservable = shared as unknown as plugins.smartrx.rxjs.Observable<TStatePayload>;
|
||||||
|
} else {
|
||||||
|
this.selectorCache.set(selectorFn, shared);
|
||||||
|
}
|
||||||
|
|
||||||
|
return shared;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -71,24 +252,115 @@ export class StatePart<TStatePartName, TStatePayload> {
|
|||||||
/**
|
/**
|
||||||
* dispatches an action on the statepart level
|
* dispatches an action on the statepart level
|
||||||
*/
|
*/
|
||||||
public async dispatchAction<T>(stateAction: StateAction<TStatePayload, T>, actionPayload: T) {
|
public async dispatchAction<T>(stateAction: StateAction<TStatePayload, T>, actionPayload: T): Promise<TStatePayload> {
|
||||||
const newState = await stateAction.actionDef(this, actionPayload);
|
await this.cumulativeDeferred.promise;
|
||||||
this.setState(newState);
|
return this.mutationQueue = this.mutationQueue.then(
|
||||||
|
async () => {
|
||||||
|
const newState = await stateAction.actionDef(this, actionPayload);
|
||||||
|
return this.applyState(newState);
|
||||||
|
},
|
||||||
|
async () => {
|
||||||
|
const newState = await stateAction.actionDef(this, actionPayload);
|
||||||
|
return this.applyState(newState);
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* waits until a certain part of the state becomes available
|
* waits until a certain part of the state becomes available.
|
||||||
* @param selectorFn
|
* supports optional timeout and AbortSignal.
|
||||||
*/
|
*/
|
||||||
public async waitUntilPresent<T = TStatePayload>(selectorFn?: (state: TStatePayload) => T): Promise<T> {
|
public async waitUntilPresent<T = TStatePayload>(
|
||||||
|
selectorFn?: (state: TStatePayload) => T,
|
||||||
|
optionsOrTimeout?: number | { timeoutMs?: number; signal?: AbortSignal }
|
||||||
|
): Promise<T> {
|
||||||
|
// Parse backward-compatible args
|
||||||
|
let timeoutMs: number | undefined;
|
||||||
|
let signal: AbortSignal | undefined;
|
||||||
|
if (typeof optionsOrTimeout === 'number') {
|
||||||
|
timeoutMs = optionsOrTimeout;
|
||||||
|
} else if (optionsOrTimeout) {
|
||||||
|
timeoutMs = optionsOrTimeout.timeoutMs;
|
||||||
|
signal = optionsOrTimeout.signal;
|
||||||
|
}
|
||||||
|
|
||||||
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(value => {
|
let resolved = false;
|
||||||
if (value) {
|
|
||||||
subscription.unsubscribe();
|
// Check if already aborted
|
||||||
|
if (signal?.aborted) {
|
||||||
|
throw new Error('Aborted');
|
||||||
|
}
|
||||||
|
|
||||||
|
const subscription = selectedObservable.subscribe((value) => {
|
||||||
|
if (value !== undefined && value !== null && !resolved) {
|
||||||
|
resolved = true;
|
||||||
done.resolve(value);
|
done.resolve(value);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return await done.promise;
|
|
||||||
|
let timeoutId: ReturnType<typeof setTimeout> | undefined;
|
||||||
|
if (timeoutMs) {
|
||||||
|
timeoutId = setTimeout(() => {
|
||||||
|
if (!resolved) {
|
||||||
|
resolved = true;
|
||||||
|
subscription.unsubscribe();
|
||||||
|
done.reject(new Error(`waitUntilPresent timed out after ${timeoutMs}ms`));
|
||||||
|
}
|
||||||
|
}, timeoutMs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle abort signal
|
||||||
|
const abortHandler = signal ? () => {
|
||||||
|
if (!resolved) {
|
||||||
|
resolved = true;
|
||||||
|
subscription.unsubscribe();
|
||||||
|
if (timeoutId) clearTimeout(timeoutId);
|
||||||
|
done.reject(new Error('Aborted'));
|
||||||
|
}
|
||||||
|
} : undefined;
|
||||||
|
|
||||||
|
if (signal && abortHandler) {
|
||||||
|
signal.addEventListener('abort', abortHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await done.promise;
|
||||||
|
return result;
|
||||||
|
} finally {
|
||||||
|
subscription.unsubscribe();
|
||||||
|
if (timeoutId) clearTimeout(timeoutId);
|
||||||
|
if (signal && abortHandler) {
|
||||||
|
signal.removeEventListener('abort', abortHandler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* is executed
|
||||||
|
*/
|
||||||
|
public async stateSetup(
|
||||||
|
funcArg: (statePartArg?: StatePart<any, TStatePayload>) => Promise<TStatePayload>
|
||||||
|
) {
|
||||||
|
const resultPromise = funcArg(this);
|
||||||
|
this.cumulativeDeferred.addPromise(resultPromise);
|
||||||
|
await this.setState(await resultPromise);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* disposes the state part, completing the Subject and cleaning up resources
|
||||||
|
*/
|
||||||
|
public dispose(): void {
|
||||||
|
this.state.complete();
|
||||||
|
if (this.pendingCumulativeNotification) {
|
||||||
|
clearTimeout(this.pendingCumulativeNotification);
|
||||||
|
this.pendingCumulativeNotification = null;
|
||||||
|
}
|
||||||
|
this.middlewares.length = 0;
|
||||||
|
this.selectorCache = new WeakMap();
|
||||||
|
this.defaultSelectObservable = null;
|
||||||
|
this.webStore = null;
|
||||||
|
this.smartstateRef = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
61
ts/smartstate.contextprovider.ts
Normal file
61
ts/smartstate.contextprovider.ts
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
import type { StatePart } from './smartstate.classes.statepart.js';
|
||||||
|
|
||||||
|
export interface IContextProviderOptions<TPayload> {
|
||||||
|
/** the context key (compared by strict equality) */
|
||||||
|
context: unknown;
|
||||||
|
/** the state part to provide */
|
||||||
|
statePart: StatePart<any, TPayload>;
|
||||||
|
/** optional selector to provide a derived value instead of the full state */
|
||||||
|
selectorFn?: (state: TPayload) => any;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* attaches a Context Protocol provider to an HTML element.
|
||||||
|
* listens for `context-request` events and responds with the state part's value.
|
||||||
|
* if subscribe=true, retains the callback and invokes it on every state change.
|
||||||
|
* returns a cleanup function that removes the listener and unsubscribes.
|
||||||
|
*/
|
||||||
|
export function attachContextProvider<TPayload>(
|
||||||
|
element: HTMLElement,
|
||||||
|
options: IContextProviderOptions<TPayload>,
|
||||||
|
): () => void {
|
||||||
|
const { context, statePart, selectorFn } = options;
|
||||||
|
const subscribers = new Set<(value: any, unsubscribe?: () => void) => void>();
|
||||||
|
|
||||||
|
const subscription = statePart.select(selectorFn).subscribe((value) => {
|
||||||
|
for (const cb of subscribers) {
|
||||||
|
cb(value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const getValue = (): any => {
|
||||||
|
const state = statePart.getState();
|
||||||
|
if (state === undefined) return undefined;
|
||||||
|
return selectorFn ? selectorFn(state) : state;
|
||||||
|
};
|
||||||
|
|
||||||
|
const handler = (event: Event) => {
|
||||||
|
const e = event as CustomEvent;
|
||||||
|
const detail = e.detail;
|
||||||
|
if (!detail || detail.context !== context) return;
|
||||||
|
|
||||||
|
e.stopPropagation();
|
||||||
|
|
||||||
|
if (detail.subscribe) {
|
||||||
|
const cb = detail.callback;
|
||||||
|
subscribers.add(cb);
|
||||||
|
const unsubscribe = () => subscribers.delete(cb);
|
||||||
|
cb(getValue(), unsubscribe);
|
||||||
|
} else {
|
||||||
|
detail.callback(getValue());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
element.addEventListener('context-request', handler);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
element.removeEventListener('context-request', handler);
|
||||||
|
subscription.unsubscribe();
|
||||||
|
subscribers.clear();
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,13 +1,7 @@
|
|||||||
import { Observable } from 'rxjs';
|
import * as smarthashWeb from '@push.rocks/smarthash/web';
|
||||||
|
import * as smartjson from '@push.rocks/smartjson';
|
||||||
|
import * as smartpromise from '@push.rocks/smartpromise';
|
||||||
|
import * as smartrx from '@push.rocks/smartrx';
|
||||||
|
import * as webstore from '@push.rocks/webstore';
|
||||||
|
|
||||||
const rxjsPart = {
|
export { smarthashWeb, smartjson, smartpromise, smartrx, webstore };
|
||||||
Observable
|
|
||||||
};
|
|
||||||
|
|
||||||
export { rxjsPart };
|
|
||||||
|
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
|
||||||
|
|
||||||
export {
|
|
||||||
smartpromise
|
|
||||||
};
|
|
||||||
|
|||||||
14
tsconfig.json
Normal file
14
tsconfig.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "NodeNext",
|
||||||
|
"moduleResolution": "NodeNext",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"verbatimModuleSyntax": true
|
||||||
|
},
|
||||||
|
"exclude": [
|
||||||
|
"dist_*/**/*.d.ts"
|
||||||
|
]
|
||||||
|
}
|
||||||
17
tslint.json
17
tslint.json
@@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
|
||||||
"rules": {
|
|
||||||
"semicolon": [true, "always"],
|
|
||||||
"no-console": false,
|
|
||||||
"ordered-imports": false,
|
|
||||||
"object-literal-sort-keys": false,
|
|
||||||
"member-ordering": {
|
|
||||||
"options":{
|
|
||||||
"order": [
|
|
||||||
"static-method"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"defaultSeverity": "warning"
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user