@ersbeth/picoflow 1.1.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +3 -3
- package/CHANGELOG.md +43 -0
- package/README.md +2 -18
- package/biome.json +45 -35
- package/dist/picoflow.js +857 -1528
- package/dist/types/api/base/flowDisposable.d.ts +41 -0
- package/dist/types/api/base/flowDisposable.d.ts.map +1 -0
- package/dist/types/api/base/flowObservable.d.ts +27 -0
- package/dist/types/api/base/flowObservable.d.ts.map +1 -0
- package/dist/types/api/base/flowSubscribable.d.ts +79 -0
- package/dist/types/api/base/flowSubscribable.d.ts.map +1 -0
- package/dist/types/api/base/flowTracker.d.ts +8 -0
- package/dist/types/api/base/flowTracker.d.ts.map +1 -0
- package/dist/types/api/base/index.d.ts +5 -0
- package/dist/types/api/base/index.d.ts.map +1 -0
- package/dist/types/api/index.d.ts +3 -0
- package/dist/types/api/index.d.ts.map +1 -0
- package/dist/types/api/nodes/async/flowConstantAsync.d.ts +31 -0
- package/dist/types/api/nodes/async/flowConstantAsync.d.ts.map +1 -0
- package/dist/types/api/nodes/async/flowDerivationAsync.d.ts +37 -0
- package/dist/types/api/nodes/async/flowDerivationAsync.d.ts.map +1 -0
- package/dist/types/api/nodes/async/flowStateAsync.d.ts +41 -0
- package/dist/types/api/nodes/async/flowStateAsync.d.ts.map +1 -0
- package/dist/types/api/nodes/async/flowWritableDerivationAsync.d.ts +30 -0
- package/dist/types/api/nodes/async/flowWritableDerivationAsync.d.ts.map +1 -0
- package/dist/types/{flow → api}/nodes/async/index.d.ts +1 -2
- package/dist/types/api/nodes/async/index.d.ts.map +1 -0
- package/dist/types/api/nodes/collections/flowArray.d.ts +134 -0
- package/dist/types/api/nodes/collections/flowArray.d.ts.map +1 -0
- package/dist/types/api/nodes/collections/flowMap.d.ts +98 -0
- package/dist/types/api/nodes/collections/flowMap.d.ts.map +1 -0
- package/dist/types/api/nodes/collections/index.d.ts.map +1 -0
- package/dist/types/api/nodes/flowEffect.d.ts +28 -0
- package/dist/types/api/nodes/flowEffect.d.ts.map +1 -0
- package/dist/types/api/nodes/flowSignal.d.ts +25 -0
- package/dist/types/api/nodes/flowSignal.d.ts.map +1 -0
- package/dist/types/api/nodes/flowValue.d.ts +35 -0
- package/dist/types/api/nodes/flowValue.d.ts.map +1 -0
- package/dist/types/api/nodes/index.d.ts +8 -0
- package/dist/types/api/nodes/index.d.ts.map +1 -0
- package/dist/types/api/nodes/sync/flowConstant.d.ts +29 -0
- package/dist/types/api/nodes/sync/flowConstant.d.ts.map +1 -0
- package/dist/types/api/nodes/sync/flowDerivation.d.ts +36 -0
- package/dist/types/api/nodes/sync/flowDerivation.d.ts.map +1 -0
- package/dist/types/api/nodes/sync/flowState.d.ts +39 -0
- package/dist/types/api/nodes/sync/flowState.d.ts.map +1 -0
- package/dist/types/api/nodes/sync/flowWritableDerivation.d.ts +28 -0
- package/dist/types/api/nodes/sync/flowWritableDerivation.d.ts.map +1 -0
- package/dist/types/{flow → api}/nodes/sync/index.d.ts +1 -2
- package/dist/types/api/nodes/sync/index.d.ts.map +1 -0
- package/dist/types/api/nodes/utils.d.ts +22 -0
- package/dist/types/api/nodes/utils.d.ts.map +1 -0
- package/dist/types/base/disposable.d.ts +11 -0
- package/dist/types/base/disposable.d.ts.map +1 -0
- package/dist/types/base/executionStack.d.ts +14 -0
- package/dist/types/base/executionStack.d.ts.map +1 -0
- package/dist/types/base/index.d.ts +6 -0
- package/dist/types/base/index.d.ts.map +1 -0
- package/dist/types/base/node.d.ts +27 -0
- package/dist/types/base/node.d.ts.map +1 -0
- package/dist/types/base/observable.d.ts +37 -0
- package/dist/types/base/observable.d.ts.map +1 -0
- package/dist/types/base/observer.d.ts +25 -0
- package/dist/types/base/observer.d.ts.map +1 -0
- package/dist/types/converters/index.d.ts +2 -0
- package/dist/types/converters/index.d.ts.map +1 -0
- package/dist/types/converters/solid.d.ts +46 -0
- package/dist/types/converters/solid.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -63
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/nodes/arrayNode.d.ts +2 -0
- package/dist/types/nodes/arrayNode.d.ts.map +1 -0
- package/dist/types/nodes/effectNode.d.ts +2 -0
- package/dist/types/nodes/effectNode.d.ts.map +1 -0
- package/dist/types/nodes/index.d.ts +9 -0
- package/dist/types/nodes/index.d.ts.map +1 -0
- package/dist/types/nodes/mapNode.d.ts +2 -0
- package/dist/types/nodes/mapNode.d.ts.map +1 -0
- package/dist/types/nodes/signalNode.d.ts +2 -0
- package/dist/types/nodes/signalNode.d.ts.map +1 -0
- package/dist/types/nodes/valueAsyncNode.d.ts +2 -0
- package/dist/types/nodes/valueAsyncNode.d.ts.map +1 -0
- package/dist/types/nodes/valueNode.d.ts +2 -0
- package/dist/types/nodes/valueNode.d.ts.map +1 -0
- package/dist/types/nodes/valueSyncNode.d.ts +2 -0
- package/dist/types/nodes/valueSyncNode.d.ts.map +1 -0
- package/dist/types/schedulers/asyncResolver.d.ts +2 -0
- package/dist/types/schedulers/asyncResolver.d.ts.map +1 -0
- package/dist/types/schedulers/asyncScheduler.d.ts +2 -0
- package/dist/types/schedulers/asyncScheduler.d.ts.map +1 -0
- package/dist/types/schedulers/index.d.ts +5 -0
- package/dist/types/schedulers/index.d.ts.map +1 -0
- package/dist/types/schedulers/pendingError.d.ts +2 -0
- package/dist/types/schedulers/pendingError.d.ts.map +1 -0
- package/dist/types/schedulers/scheduler.d.ts +2 -0
- package/dist/types/schedulers/scheduler.d.ts.map +1 -0
- package/dist/types/schedulers/syncResolver.d.ts +2 -0
- package/dist/types/schedulers/syncResolver.d.ts.map +1 -0
- package/dist/types/schedulers/syncScheduler.d.ts +2 -0
- package/dist/types/schedulers/syncScheduler.d.ts.map +1 -0
- package/docs/.vitepress/config.mts +128 -93
- package/docs/api/functions/array.md +14 -37
- package/docs/api/functions/constant.md +13 -25
- package/docs/api/functions/constantAsync.md +69 -0
- package/docs/api/functions/derivation.md +14 -33
- package/docs/api/functions/derivationAsync.md +34 -0
- package/docs/api/functions/from.md +62 -153
- package/docs/api/functions/isDisposable.md +8 -30
- package/docs/api/functions/map.md +15 -36
- package/docs/api/functions/signal.md +8 -23
- package/docs/api/functions/state.md +43 -23
- package/docs/api/functions/stateAsync.md +69 -0
- package/docs/api/functions/subscribe.md +40 -0
- package/docs/api/functions/writableDerivation.md +33 -0
- package/docs/api/functions/writableDerivationAsync.md +34 -0
- package/docs/api/index.md +45 -102
- package/docs/api/interfaces/FlowArray.md +439 -0
- package/docs/api/interfaces/FlowConstant.md +220 -0
- package/docs/api/interfaces/FlowConstantAsync.md +221 -0
- package/docs/api/interfaces/FlowDerivation.md +241 -0
- package/docs/api/interfaces/FlowDerivationAsync.md +242 -0
- package/docs/api/interfaces/FlowDisposable.md +32 -38
- package/docs/api/interfaces/FlowEffect.md +64 -0
- package/docs/api/interfaces/FlowMap.md +374 -0
- package/docs/api/interfaces/FlowObservable.md +155 -0
- package/docs/api/interfaces/FlowSignal.md +156 -0
- package/docs/api/interfaces/FlowState.md +269 -0
- package/docs/api/interfaces/FlowStateAsync.md +268 -0
- package/docs/api/interfaces/FlowSubscribable.md +55 -0
- package/docs/api/interfaces/FlowTracker.md +61 -0
- package/docs/api/interfaces/FlowValue.md +222 -0
- package/docs/api/interfaces/FlowWritableDerivation.md +292 -0
- package/docs/api/interfaces/FlowWritableDerivationAsync.md +293 -0
- package/docs/api/type-aliases/DerivationFunction.md +28 -0
- package/docs/api/type-aliases/DerivationFunctionAsync.md +28 -0
- package/docs/api/type-aliases/FlowArrayAction.md +19 -8
- package/docs/api/type-aliases/FlowDataTracker.md +33 -0
- package/docs/api/type-aliases/FlowMapAction.md +48 -0
- package/docs/api/type-aliases/FlowOnDataListener.md +33 -0
- package/docs/api/type-aliases/FlowOnErrorListener.md +27 -0
- package/docs/api/type-aliases/FlowOnPendingListener.md +21 -0
- package/docs/api/type-aliases/FlowReadonly.md +22 -0
- package/docs/api/type-aliases/InitFunction.md +21 -0
- package/docs/api/type-aliases/InitFunctionAsync.md +21 -0
- package/docs/api/type-aliases/NotPromise.md +6 -3
- package/docs/api/type-aliases/UpdateFunction.md +27 -0
- package/docs/api/type-aliases/UpdateFunctionAsync.md +27 -0
- package/docs/api/typedoc-sidebar.json +1 -81
- package/docs/examples/examples.md +0 -2
- package/docs/guide/advanced/architecture.md +1234 -0
- package/docs/guide/advanced/migration-v2.md +204 -0
- package/docs/guide/advanced/solidjs.md +2 -88
- package/docs/guide/introduction/concepts.md +4 -3
- package/docs/guide/introduction/conventions.md +2 -33
- package/docs/guide/introduction/getting-started.md +28 -23
- package/docs/guide/introduction/lifecycle.md +16 -19
- package/docs/guide/primitives/array.md +102 -216
- package/docs/guide/primitives/constant.md +39 -212
- package/docs/guide/primitives/derivations.md +55 -122
- package/docs/guide/primitives/effects.md +155 -241
- package/docs/guide/primitives/map.md +64 -186
- package/docs/guide/primitives/overview.md +45 -128
- package/docs/guide/primitives/signal.md +51 -88
- package/docs/guide/primitives/state.md +34 -130
- package/package.json +56 -60
- package/src/api/base/flowDisposable.ts +44 -0
- package/src/api/base/flowObservable.ts +28 -0
- package/src/api/base/flowSubscribable.ts +87 -0
- package/src/api/base/flowTracker.ts +7 -0
- package/src/api/base/index.ts +4 -0
- package/src/{flow → api}/index.ts +0 -1
- package/src/api/nodes/async/flowConstantAsync.ts +36 -0
- package/src/api/nodes/async/flowDerivationAsync.ts +42 -0
- package/src/api/nodes/async/flowStateAsync.ts +47 -0
- package/src/api/nodes/async/flowWritableDerivationAsync.ts +33 -0
- package/src/{flow → api}/nodes/async/index.ts +1 -2
- package/src/api/nodes/collections/flowArray.ts +155 -0
- package/src/api/nodes/collections/flowMap.ts +115 -0
- package/src/api/nodes/flowEffect.ts +42 -0
- package/src/api/nodes/flowSignal.ts +28 -0
- package/src/api/nodes/flowValue.ts +36 -0
- package/src/api/nodes/index.ts +7 -0
- package/src/api/nodes/sync/flowConstant.ts +33 -0
- package/src/api/nodes/sync/flowDerivation.ts +41 -0
- package/src/api/nodes/sync/flowState.ts +45 -0
- package/src/api/nodes/sync/flowWritableDerivation.ts +31 -0
- package/src/{flow → api}/nodes/sync/index.ts +1 -2
- package/src/api/nodes/utils.ts +22 -0
- package/src/base/disposable.ts +18 -0
- package/src/base/executionStack.ts +42 -0
- package/src/base/index.ts +5 -0
- package/src/base/node.ts +98 -0
- package/src/base/observable.ts +87 -0
- package/src/base/observer.ts +51 -0
- package/src/converters/index.ts +1 -0
- package/src/converters/solid.ts +109 -0
- package/src/index.ts +2 -64
- package/src/nodes/arrayNode.ts +172 -0
- package/src/nodes/effectNode.ts +59 -0
- package/src/nodes/index.ts +8 -0
- package/src/nodes/mapNode.ts +127 -0
- package/src/nodes/signalNode.ts +21 -0
- package/src/nodes/valueAsyncNode.ts +88 -0
- package/src/nodes/valueNode.ts +144 -0
- package/src/nodes/valueSyncNode.ts +128 -0
- package/src/schedulers/asyncResolver.ts +78 -0
- package/src/schedulers/asyncScheduler.ts +66 -0
- package/src/schedulers/index.ts +4 -0
- package/src/schedulers/pendingError.ts +13 -0
- package/src/schedulers/scheduler.ts +9 -0
- package/src/schedulers/syncResolver.ts +69 -0
- package/src/schedulers/syncScheduler.ts +55 -0
- package/test/base/pendingError.test.ts +67 -0
- package/test/converters/solid.derivation.browser.test.tsx +69 -0
- package/test/converters/solid.node.test.ts +654 -0
- package/test/converters/solid.state.browser.test.tsx +1592 -0
- package/test/reactivity/flowSignal.test.ts +226 -0
- package/test/reactivity/nodes/async/asyncScheduler/asyncResolver.test.ts +593 -0
- package/test/reactivity/nodes/async/asyncScheduler/asyncScheduler.test.ts +317 -0
- package/test/reactivity/nodes/async/flowConstantAsync.test.ts +652 -0
- package/test/reactivity/nodes/async/flowDerivation.test.ts +898 -0
- package/test/reactivity/nodes/async/flowDerivationAsync.test.ts +1716 -0
- package/test/reactivity/nodes/async/flowStateAsync.test.ts +708 -0
- package/test/reactivity/nodes/async/flowWritableDerivationAsync.test.ts +614 -0
- package/test/reactivity/nodes/collections/flowArray.asyncStates.test.ts +1289 -0
- package/test/reactivity/nodes/collections/flowArray.scalars.test.ts +961 -0
- package/test/reactivity/nodes/collections/flowArray.states.test.ts +1035 -0
- package/test/reactivity/nodes/collections/flowMap.asyncStates.test.ts +960 -0
- package/test/reactivity/nodes/collections/flowMap.scalars.test.ts +775 -0
- package/test/reactivity/nodes/collections/flowMap.states.test.ts +958 -0
- package/test/reactivity/nodes/sync/flowConstant.test.ts +377 -0
- package/test/reactivity/nodes/sync/flowDerivation.test.ts +896 -0
- package/test/reactivity/nodes/sync/flowState.test.ts +341 -0
- package/test/reactivity/nodes/sync/flowWritableDerivation.test.ts +603 -0
- package/test/vitest.d.ts +10 -0
- package/tsconfig.json +31 -20
- package/typedoc.json +35 -35
- package/vite.config.ts +25 -23
- package/vitest.browser.config.ts +21 -0
- package/vitest.config.ts +12 -12
- package/.cursor/plans/unifier-flowresource-avec-flowderivation-c9506e24.plan.md +0 -372
- package/.cursor/plans/update-js-e795d61b.plan.md +0 -567
- package/dist/types/flow/base/flowDisposable.d.ts +0 -67
- package/dist/types/flow/base/flowDisposable.d.ts.map +0 -1
- package/dist/types/flow/base/flowEffect.d.ts +0 -127
- package/dist/types/flow/base/flowEffect.d.ts.map +0 -1
- package/dist/types/flow/base/flowGraph.d.ts +0 -97
- package/dist/types/flow/base/flowGraph.d.ts.map +0 -1
- package/dist/types/flow/base/flowSignal.d.ts +0 -134
- package/dist/types/flow/base/flowSignal.d.ts.map +0 -1
- package/dist/types/flow/base/flowTracker.d.ts +0 -15
- package/dist/types/flow/base/flowTracker.d.ts.map +0 -1
- package/dist/types/flow/base/index.d.ts +0 -7
- package/dist/types/flow/base/index.d.ts.map +0 -1
- package/dist/types/flow/base/utils.d.ts +0 -20
- package/dist/types/flow/base/utils.d.ts.map +0 -1
- package/dist/types/flow/collections/flowArray.d.ts +0 -148
- package/dist/types/flow/collections/flowArray.d.ts.map +0 -1
- package/dist/types/flow/collections/flowMap.d.ts +0 -224
- package/dist/types/flow/collections/flowMap.d.ts.map +0 -1
- package/dist/types/flow/collections/index.d.ts.map +0 -1
- package/dist/types/flow/index.d.ts +0 -4
- package/dist/types/flow/index.d.ts.map +0 -1
- package/dist/types/flow/nodes/async/flowConstantAsync.d.ts +0 -137
- package/dist/types/flow/nodes/async/flowConstantAsync.d.ts.map +0 -1
- package/dist/types/flow/nodes/async/flowDerivationAsync.d.ts +0 -137
- package/dist/types/flow/nodes/async/flowDerivationAsync.d.ts.map +0 -1
- package/dist/types/flow/nodes/async/flowNodeAsync.d.ts +0 -343
- package/dist/types/flow/nodes/async/flowNodeAsync.d.ts.map +0 -1
- package/dist/types/flow/nodes/async/flowReadonlyAsync.d.ts +0 -81
- package/dist/types/flow/nodes/async/flowReadonlyAsync.d.ts.map +0 -1
- package/dist/types/flow/nodes/async/flowStateAsync.d.ts +0 -111
- package/dist/types/flow/nodes/async/flowStateAsync.d.ts.map +0 -1
- package/dist/types/flow/nodes/async/index.d.ts.map +0 -1
- package/dist/types/flow/nodes/index.d.ts +0 -3
- package/dist/types/flow/nodes/index.d.ts.map +0 -1
- package/dist/types/flow/nodes/sync/flowConstant.d.ts +0 -108
- package/dist/types/flow/nodes/sync/flowConstant.d.ts.map +0 -1
- package/dist/types/flow/nodes/sync/flowDerivation.d.ts +0 -100
- package/dist/types/flow/nodes/sync/flowDerivation.d.ts.map +0 -1
- package/dist/types/flow/nodes/sync/flowNode.d.ts +0 -314
- package/dist/types/flow/nodes/sync/flowNode.d.ts.map +0 -1
- package/dist/types/flow/nodes/sync/flowReadonly.d.ts +0 -57
- package/dist/types/flow/nodes/sync/flowReadonly.d.ts.map +0 -1
- package/dist/types/flow/nodes/sync/flowState.d.ts +0 -96
- package/dist/types/flow/nodes/sync/flowState.d.ts.map +0 -1
- package/dist/types/flow/nodes/sync/index.d.ts.map +0 -1
- package/dist/types/solid/converters.d.ts +0 -53
- package/dist/types/solid/converters.d.ts.map +0 -1
- package/dist/types/solid/index.d.ts +0 -3
- package/dist/types/solid/index.d.ts.map +0 -1
- package/dist/types/solid/primitives.d.ts +0 -181
- package/dist/types/solid/primitives.d.ts.map +0 -1
- package/docs/api/classes/FlowArray.md +0 -489
- package/docs/api/classes/FlowConstant.md +0 -350
- package/docs/api/classes/FlowDerivation.md +0 -334
- package/docs/api/classes/FlowEffect.md +0 -100
- package/docs/api/classes/FlowMap.md +0 -512
- package/docs/api/classes/FlowObservable.md +0 -306
- package/docs/api/classes/FlowResource.md +0 -380
- package/docs/api/classes/FlowResourceAsync.md +0 -362
- package/docs/api/classes/FlowSignal.md +0 -160
- package/docs/api/classes/FlowState.md +0 -368
- package/docs/api/classes/FlowStream.md +0 -367
- package/docs/api/classes/FlowStreamAsync.md +0 -364
- package/docs/api/classes/SolidDerivation.md +0 -75
- package/docs/api/classes/SolidResource.md +0 -91
- package/docs/api/classes/SolidState.md +0 -71
- package/docs/api/classes/TrackingContext.md +0 -33
- package/docs/api/functions/effect.md +0 -49
- package/docs/api/functions/resource.md +0 -52
- package/docs/api/functions/resourceAsync.md +0 -50
- package/docs/api/functions/stream.md +0 -53
- package/docs/api/functions/streamAsync.md +0 -50
- package/docs/api/interfaces/SolidObservable.md +0 -19
- package/docs/api/type-aliases/FlowStreamDisposer.md +0 -15
- package/docs/api/type-aliases/FlowStreamSetter.md +0 -27
- package/docs/api/type-aliases/FlowStreamUpdater.md +0 -32
- package/docs/api/type-aliases/SolidGetter.md +0 -17
- package/docs/guide/primitives/resources.md +0 -858
- package/docs/guide/primitives/streams.md +0 -931
- package/src/flow/base/flowDisposable.ts +0 -71
- package/src/flow/base/flowEffect.ts +0 -171
- package/src/flow/base/flowGraph.ts +0 -288
- package/src/flow/base/flowSignal.ts +0 -207
- package/src/flow/base/flowTracker.ts +0 -17
- package/src/flow/base/index.ts +0 -6
- package/src/flow/base/utils.ts +0 -19
- package/src/flow/collections/flowArray.ts +0 -409
- package/src/flow/collections/flowMap.ts +0 -398
- package/src/flow/nodes/async/flowConstantAsync.ts +0 -142
- package/src/flow/nodes/async/flowDerivationAsync.ts +0 -143
- package/src/flow/nodes/async/flowNodeAsync.ts +0 -474
- package/src/flow/nodes/async/flowReadonlyAsync.ts +0 -81
- package/src/flow/nodes/async/flowStateAsync.ts +0 -116
- package/src/flow/nodes/await/advanced/index.ts +0 -5
- package/src/flow/nodes/await/advanced/resource.ts +0 -134
- package/src/flow/nodes/await/advanced/resourceAsync.ts +0 -109
- package/src/flow/nodes/await/advanced/stream.ts +0 -188
- package/src/flow/nodes/await/advanced/streamAsync.ts +0 -176
- package/src/flow/nodes/await/flowConstantAwait.ts +0 -154
- package/src/flow/nodes/await/flowDerivationAwait.ts +0 -154
- package/src/flow/nodes/await/flowNodeAwait.ts +0 -508
- package/src/flow/nodes/await/flowReadonlyAwait.ts +0 -89
- package/src/flow/nodes/await/flowStateAwait.ts +0 -130
- package/src/flow/nodes/await/index.ts +0 -5
- package/src/flow/nodes/index.ts +0 -3
- package/src/flow/nodes/sync/flowConstant.ts +0 -111
- package/src/flow/nodes/sync/flowDerivation.ts +0 -105
- package/src/flow/nodes/sync/flowNode.ts +0 -439
- package/src/flow/nodes/sync/flowReadonly.ts +0 -57
- package/src/flow/nodes/sync/flowState.ts +0 -101
- package/src/solid/converters.ts +0 -139
- package/src/solid/index.ts +0 -2
- package/src/solid/primitives.ts +0 -215
- package/test/base/flowEffect.test.ts +0 -108
- package/test/base/flowGraph.test.ts +0 -485
- package/test/base/flowSignal.test.ts +0 -372
- package/test/collections/flowArray.asyncStates.test.ts +0 -1553
- package/test/collections/flowArray.scalars.test.ts +0 -1129
- package/test/collections/flowArray.states.test.ts +0 -1365
- package/test/collections/flowMap.asyncStates.test.ts +0 -1105
- package/test/collections/flowMap.scalars.test.ts +0 -877
- package/test/collections/flowMap.states.test.ts +0 -1097
- package/test/nodes/async/flowConstantAsync.test.ts +0 -860
- package/test/nodes/async/flowDerivationAsync.test.ts +0 -1517
- package/test/nodes/async/flowStateAsync.test.ts +0 -1387
- package/test/nodes/await/advanced/resource.test.ts +0 -129
- package/test/nodes/await/advanced/resourceAsync.test.ts +0 -108
- package/test/nodes/await/advanced/stream.test.ts +0 -198
- package/test/nodes/await/advanced/streamAsync.test.ts +0 -196
- package/test/nodes/await/flowConstantAwait.test.ts +0 -643
- package/test/nodes/await/flowDerivationAwait.test.ts +0 -1583
- package/test/nodes/await/flowStateAwait.test.ts +0 -999
- package/test/nodes/mixed/derivation.test.ts +0 -1527
- package/test/nodes/sync/flowConstant.test.ts +0 -620
- package/test/nodes/sync/flowDerivation.test.ts +0 -1373
- package/test/nodes/sync/flowState.test.ts +0 -945
- package/test/solid/converters.test.ts +0 -721
- package/test/solid/primitives.test.ts +0 -1031
- /package/dist/types/{flow → api/nodes}/collections/index.d.ts +0 -0
- /package/docs/guide/advanced/{upgrading.md → migration-v1.md} +0 -0
- /package/src/{flow → api/nodes}/collections/index.ts +0 -0
|
@@ -1,439 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
FlowEffect,
|
|
3
|
-
FlowGraph,
|
|
4
|
-
FlowSignal,
|
|
5
|
-
type FlowTracker,
|
|
6
|
-
type NotPromise,
|
|
7
|
-
} from "../../base";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* A versatile reactive node that can operate as mutable state or computed derivation.
|
|
11
|
-
*
|
|
12
|
-
* @typeParam T - The type of the value held by this node.
|
|
13
|
-
*
|
|
14
|
-
* @remarks
|
|
15
|
-
* FlowNode is a flexible reactive primitive that adapts its behavior based on how it's initialized.
|
|
16
|
-
* It extends {@link FlowSignal} and provides reactive value access with automatic dependency tracking.
|
|
17
|
-
*
|
|
18
|
-
* **Operating Modes:**
|
|
19
|
-
*
|
|
20
|
-
* - **Mutable State Mode**: When constructed with a constant value, FlowNode acts as a mutable
|
|
21
|
-
* reactive state. You can update its value using `set()`, and all dependents are automatically
|
|
22
|
-
* notified when the value changes.
|
|
23
|
-
*
|
|
24
|
-
* - **Computed Derivation Mode**: When constructed with a compute function, FlowNode acts as a
|
|
25
|
-
* computed value that automatically tracks its dependencies. The compute function runs lazily
|
|
26
|
-
* (only when the value is first accessed) and recomputes when any tracked dependency changes.
|
|
27
|
-
* The computed value can be temporarily overridden using `set()`, but the override is cleared
|
|
28
|
-
* on the next recomputation (triggered by dependency changes or `refresh()`).
|
|
29
|
-
*
|
|
30
|
-
* **Lazy Computation:**
|
|
31
|
-
* When using a compute function, the computation doesn't run immediately upon construction. It
|
|
32
|
-
* executes only when:
|
|
33
|
-
* - The value is first read via `get()` or `pick()`
|
|
34
|
-
* - The node is watched via `watch()`
|
|
35
|
-
*
|
|
36
|
-
* **Dirty Tracking:**
|
|
37
|
-
* When a tracked dependency changes, the node is marked as "dirty" but doesn't recompute
|
|
38
|
-
* immediately. Recomputation happens lazily on the next value access, preventing unnecessary
|
|
39
|
-
* computations when multiple dependencies change in quick succession.
|
|
40
|
-
*
|
|
41
|
-
* **Dynamic Dependencies:**
|
|
42
|
-
* Dependencies are tracked dynamically during each computation. If the compute function
|
|
43
|
-
* conditionally tracks different observables, the dependency graph updates automatically.
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* ```typescript
|
|
47
|
-
* // Mutable state mode
|
|
48
|
-
* const $count = new FlowNode(0);
|
|
49
|
-
* $count.set(1); // Updates the value
|
|
50
|
-
*
|
|
51
|
-
* // Computed derivation mode
|
|
52
|
-
* const $firstName = new FlowNode('John');
|
|
53
|
-
* const $lastName = new FlowNode('Doe');
|
|
54
|
-
* const $fullName = new FlowNode((t) => {
|
|
55
|
-
* return `${$firstName.get(t)} ${$lastName.get(t)}`;
|
|
56
|
-
* });
|
|
57
|
-
*
|
|
58
|
-
* // Compute function hasn't run yet (lazy)
|
|
59
|
-
* const name = await $fullName.pick(); // Now it computes: "John Doe"
|
|
60
|
-
*
|
|
61
|
-
* // Temporarily override computed value
|
|
62
|
-
* $fullName.set('Override');
|
|
63
|
-
* console.log(await $fullName.pick()); // "Override"
|
|
64
|
-
*
|
|
65
|
-
* // Trigger recomputation (clears override)
|
|
66
|
-
* $firstName.set('Jane');
|
|
67
|
-
* console.log(await $fullName.pick()); // "Jane Doe" (override cleared)
|
|
68
|
-
* ```
|
|
69
|
-
*
|
|
70
|
-
* @public
|
|
71
|
-
*/
|
|
72
|
-
export class FlowNode<T extends NotPromise<unknown>> extends FlowSignal {
|
|
73
|
-
protected _value?: T;
|
|
74
|
-
private _dirty = true;
|
|
75
|
-
private _compute?: (t: FlowTracker) => T;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Creates a new FlowNode.
|
|
79
|
-
*
|
|
80
|
-
* @param compute - Either a constant value or a compute function that derives the value.
|
|
81
|
-
*
|
|
82
|
-
* @remarks
|
|
83
|
-
* The constructor accepts two different initialization modes:
|
|
84
|
-
*
|
|
85
|
-
* - **Constant value**: Creates a mutable reactive node that can be updated via `set()`.
|
|
86
|
-
* The value is stored immediately and can be changed at any time.
|
|
87
|
-
*
|
|
88
|
-
* - **Compute function**: Creates a computed reactive node that automatically tracks dependencies
|
|
89
|
-
* and recomputes when they change. The compute function receives a tracking context (`t`)
|
|
90
|
-
* that should be used to access dependencies via `.get(t)`. The function is not executed
|
|
91
|
-
* immediately; it runs lazily on first access. The computed value can be temporarily
|
|
92
|
-
* overridden using `set()`, but the override is cleared on the next recomputation
|
|
93
|
-
* (triggered by dependency changes or `refresh()`).
|
|
94
|
-
*
|
|
95
|
-
* @example
|
|
96
|
-
* ```typescript
|
|
97
|
-
* // Mutable state with constant value
|
|
98
|
-
* const $count = new FlowNode(0);
|
|
99
|
-
* $count.set(5);
|
|
100
|
-
*
|
|
101
|
-
* // Computed derivation with function
|
|
102
|
-
* const $a = new FlowNode(10);
|
|
103
|
-
* const $b = new FlowNode(20);
|
|
104
|
-
* const $sum = new FlowNode((t) => $a.get(t) + $b.get(t));
|
|
105
|
-
*
|
|
106
|
-
* // Lazy evaluation - compute function hasn't run yet
|
|
107
|
-
* console.log(await $sum.pick()); // Now it computes: 30
|
|
108
|
-
* ```
|
|
109
|
-
*
|
|
110
|
-
* @public
|
|
111
|
-
*/
|
|
112
|
-
constructor(compute: NotPromise<T> | ((t: FlowTracker) => NotPromise<T>)) {
|
|
113
|
-
super();
|
|
114
|
-
if (typeof compute === "function") {
|
|
115
|
-
this._compute = compute as (t: FlowTracker) => T;
|
|
116
|
-
} else {
|
|
117
|
-
this._value = compute;
|
|
118
|
-
this._dirty = false;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
override watch(tracker: FlowTracker): void {
|
|
123
|
-
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
124
|
-
this._computeValue();
|
|
125
|
-
super.watch(tracker);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Gets the current value with dependency tracking.
|
|
130
|
-
*
|
|
131
|
-
* @param tracker - The tracking context for reactive tracking. This observable is registered
|
|
132
|
-
* as a dependency when accessed through this method.
|
|
133
|
-
*
|
|
134
|
-
* @returns The current value of type T.
|
|
135
|
-
*
|
|
136
|
-
* @remarks
|
|
137
|
-
* Use `get(t)` within effects and derivations to create reactive dependencies. The tracker
|
|
138
|
-
* parameter must be provided from the reactive context (typically the `t` parameter in effect
|
|
139
|
-
* or derivation callbacks).
|
|
140
|
-
*
|
|
141
|
-
* To read a value without creating a dependency, use `pick()` instead.
|
|
142
|
-
*
|
|
143
|
-
* @example
|
|
144
|
-
* ```typescript
|
|
145
|
-
* effect((t) => {
|
|
146
|
-
* const tracked = $state.get(t); // Dependency registered
|
|
147
|
-
* const untracked = await $other.pick(); // No dependency
|
|
148
|
-
* });
|
|
149
|
-
* ```
|
|
150
|
-
*
|
|
151
|
-
* @throws Error if the node has been disposed.
|
|
152
|
-
*
|
|
153
|
-
* @public
|
|
154
|
-
*/
|
|
155
|
-
get(tracker: FlowTracker | null): T {
|
|
156
|
-
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
157
|
-
this._computeValue();
|
|
158
|
-
if (tracker) super.watch(tracker);
|
|
159
|
-
return this._value as T;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Updates the node with a new value.
|
|
164
|
-
*
|
|
165
|
-
* @param value - A new value or a callback function that computes a new value based on the current value.
|
|
166
|
-
*
|
|
167
|
-
* @returns A promise that resolves after the update is processed and all dependent effects have been notified.
|
|
168
|
-
*
|
|
169
|
-
* @remarks
|
|
170
|
-
* This method can be used in two ways:
|
|
171
|
-
*
|
|
172
|
-
* **For mutable state nodes** (constructed with a constant value):
|
|
173
|
-
* - Updates the stored value directly
|
|
174
|
-
* - All dependents are notified of the change
|
|
175
|
-
*
|
|
176
|
-
* **For computed nodes** (constructed with a compute function):
|
|
177
|
-
* - Temporarily overrides the computed value
|
|
178
|
-
* - The override persists until the next recomputation, which occurs when:
|
|
179
|
-
* - A tracked dependency changes, or
|
|
180
|
-
* - `refresh()` is called
|
|
181
|
-
* - This allows temporarily overriding computed values for testing or manual control
|
|
182
|
-
*
|
|
183
|
-
* **Value Comparison:**
|
|
184
|
-
* If the new value is strictly equal (`===`) to the current value, no update occurs
|
|
185
|
-
* and subscribers are not notified. This prevents unnecessary re-renders and effect
|
|
186
|
-
* executions.
|
|
187
|
-
*
|
|
188
|
-
* **Asynchronous Processing:**
|
|
189
|
-
* The update is processed asynchronously through the reactive graph, ensuring proper
|
|
190
|
-
* ordering of updates and effect execution.
|
|
191
|
-
*
|
|
192
|
-
* @throws Error if the node has been disposed.
|
|
193
|
-
*
|
|
194
|
-
* @example
|
|
195
|
-
* ```typescript
|
|
196
|
-
* // Mutable state usage
|
|
197
|
-
* const $count = new FlowNode(0);
|
|
198
|
-
* await $count.set(5);
|
|
199
|
-
* await $count.set(current => current + 1); // 6
|
|
200
|
-
*
|
|
201
|
-
* // Temporary override of computed value
|
|
202
|
-
* const $source = new FlowNode(10);
|
|
203
|
-
* const $doubled = new FlowNode((t) => $source.get(t) * 2);
|
|
204
|
-
*
|
|
205
|
-
* console.log(await $doubled.pick()); // 20
|
|
206
|
-
*
|
|
207
|
-
* // Temporarily override
|
|
208
|
-
* await $doubled.set(50);
|
|
209
|
-
* console.log(await $doubled.pick()); // 50 (override active)
|
|
210
|
-
*
|
|
211
|
-
* // Dependency change clears override
|
|
212
|
-
* await $source.set(15);
|
|
213
|
-
* console.log(await $doubled.pick()); // 30 (recomputed, override cleared)
|
|
214
|
-
* ```
|
|
215
|
-
*
|
|
216
|
-
* @public
|
|
217
|
-
*/
|
|
218
|
-
set(value: NotPromise<T> | ((current: T) => NotPromise<T>)): Promise<void> {
|
|
219
|
-
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
220
|
-
|
|
221
|
-
const update = () => {
|
|
222
|
-
// compute current value
|
|
223
|
-
this._computeValue();
|
|
224
|
-
const currentValue = this._value as T;
|
|
225
|
-
|
|
226
|
-
// compute new value
|
|
227
|
-
const next =
|
|
228
|
-
typeof value === "function"
|
|
229
|
-
? (value as (current: T) => T)(currentValue)
|
|
230
|
-
: value;
|
|
231
|
-
|
|
232
|
-
// assign new value
|
|
233
|
-
this._value = next;
|
|
234
|
-
|
|
235
|
-
// return value changed check
|
|
236
|
-
return next !== currentValue;
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
const notify = () => {
|
|
240
|
-
// call super method to avoid setting dirty flag back to true
|
|
241
|
-
super._notify();
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
return FlowGraph.requestWrite(notify, update);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* Forces recomputation of the value, even if it's not marked as dirty.
|
|
249
|
-
*
|
|
250
|
-
* @returns A promise that resolves after the recomputation is complete and all
|
|
251
|
-
* dependent effects have been notified.
|
|
252
|
-
*
|
|
253
|
-
* @remarks
|
|
254
|
-
* This method is useful when you need to force a recomputation of a computed value,
|
|
255
|
-
* for example when the computation depends on external data that has changed outside
|
|
256
|
-
* the reactive system.
|
|
257
|
-
*
|
|
258
|
-
* **Behavior:**
|
|
259
|
-
* - For nodes with a compute function: Forces the compute function to run again,
|
|
260
|
-
* even if no dependencies have changed. This effectively clears any temporary
|
|
261
|
-
* override that was set using `set()`.
|
|
262
|
-
* - For nodes without a compute function (mutable state): Recomputes the current
|
|
263
|
-
* value (which is just the stored value), useful for consistency but typically
|
|
264
|
-
* not necessary.
|
|
265
|
-
*
|
|
266
|
-
* The recomputation happens asynchronously through the reactive graph, ensuring
|
|
267
|
-
* proper ordering of updates and effect execution.
|
|
268
|
-
*
|
|
269
|
-
* @throws Error if the node has been disposed.
|
|
270
|
-
*
|
|
271
|
-
* @example
|
|
272
|
-
* ```typescript
|
|
273
|
-
* const $externalData = new FlowNode(() => fetchExternalData());
|
|
274
|
-
*
|
|
275
|
-
* // Some external event occurs that changes the data source
|
|
276
|
-
* externalDataChanged();
|
|
277
|
-
*
|
|
278
|
-
* // Force recomputation to get the new value
|
|
279
|
-
* await $externalData.refresh();
|
|
280
|
-
*
|
|
281
|
-
* // For computed nodes with temporary overrides
|
|
282
|
-
* const $computed = new FlowNode((t) => $source.get(t) * 2);
|
|
283
|
-
* $computed.set(100); // Temporary override
|
|
284
|
-
* await $computed.refresh(); // Clears override, recomputes from source
|
|
285
|
-
* ```
|
|
286
|
-
*
|
|
287
|
-
* @public
|
|
288
|
-
*/
|
|
289
|
-
async refresh(): Promise<void> {
|
|
290
|
-
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
291
|
-
|
|
292
|
-
const update = () => {
|
|
293
|
-
// compute current value
|
|
294
|
-
this._computeValue();
|
|
295
|
-
const currentValue = this._value;
|
|
296
|
-
|
|
297
|
-
// compute new value
|
|
298
|
-
this._computeValue({ force: true });
|
|
299
|
-
const nextValue = this._value;
|
|
300
|
-
|
|
301
|
-
// return value changed check
|
|
302
|
-
return nextValue !== currentValue;
|
|
303
|
-
};
|
|
304
|
-
|
|
305
|
-
const notify = () => {
|
|
306
|
-
// call super method to avoid setting dirty flag back to true
|
|
307
|
-
super._notify();
|
|
308
|
-
};
|
|
309
|
-
|
|
310
|
-
return await FlowGraph.requestWrite(notify, update);
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* Gets the current value without any dependency tracking.
|
|
315
|
-
*
|
|
316
|
-
* @returns A promise that resolves with the current value of type T.
|
|
317
|
-
*
|
|
318
|
-
* @remarks
|
|
319
|
-
* This method reads the value asynchronously through the reactive graph, ensuring proper ordering of
|
|
320
|
-
* read operations. Unlike `get(t)`, this method does not create a reactive dependency.
|
|
321
|
-
*
|
|
322
|
-
* Use `pick()` when you want to read a snapshot of the current value without creating a reactive
|
|
323
|
-
* dependency. This is useful for:
|
|
324
|
-
* - Reading initial values outside reactive contexts
|
|
325
|
-
* - Accessing configuration that shouldn't trigger updates
|
|
326
|
-
* - Mixing tracked and untracked reads in the same effect
|
|
327
|
-
*
|
|
328
|
-
* **Note:** This is an async method. Always use `await` when calling it.
|
|
329
|
-
*
|
|
330
|
-
* @example
|
|
331
|
-
* ```typescript
|
|
332
|
-
* // Read a snapshot outside reactive context
|
|
333
|
-
* const currentValue = await $state.pick();
|
|
334
|
-
*
|
|
335
|
-
* // Mix tracked and untracked reads
|
|
336
|
-
* effect(async (t) => {
|
|
337
|
-
* const tracked = $reactive.get(t); // Triggers re-runs
|
|
338
|
-
* const snapshot = await $config.pick(); // Doesn't trigger re-runs
|
|
339
|
-
* processData(tracked, snapshot);
|
|
340
|
-
* });
|
|
341
|
-
* ```
|
|
342
|
-
*
|
|
343
|
-
* @throws Error if the node has been disposed.
|
|
344
|
-
*
|
|
345
|
-
* @public
|
|
346
|
-
*/
|
|
347
|
-
async pick(): Promise<T> {
|
|
348
|
-
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
349
|
-
await FlowGraph.requestRead(() => this._computeValue());
|
|
350
|
-
return this._value as T;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* Subscribes a listener function to changes of this node.
|
|
355
|
-
*
|
|
356
|
-
* @param listener - A callback function that receives the new value whenever it changes.
|
|
357
|
-
*
|
|
358
|
-
* @returns A disposer function that cancels the subscription when called.
|
|
359
|
-
*
|
|
360
|
-
* @remarks
|
|
361
|
-
* This method creates a reactive subscription that automatically tracks this node as a dependency.
|
|
362
|
-
* The listener is executed:
|
|
363
|
-
* - Immediately with the current value when the subscription is created
|
|
364
|
-
* - Automatically whenever the value changes
|
|
365
|
-
*
|
|
366
|
-
* The subscription uses a {@link FlowEffect} internally to manage the reactive tracking and
|
|
367
|
-
* automatic re-execution. When the value changes, the listener is called with the new value
|
|
368
|
-
* after the update is processed through the reactive graph.
|
|
369
|
-
*
|
|
370
|
-
* **Cleanup:**
|
|
371
|
-
* Always call the returned disposer function when you no longer need the subscription to
|
|
372
|
-
* prevent memory leaks and unnecessary computations.
|
|
373
|
-
*
|
|
374
|
-
* @example
|
|
375
|
-
* ```typescript
|
|
376
|
-
* const $count = new FlowNode(0);
|
|
377
|
-
*
|
|
378
|
-
* // Subscribe to changes
|
|
379
|
-
* const unsubscribe = $count.subscribe((value) => {
|
|
380
|
-
* console.log(`Count is now: ${value}`);
|
|
381
|
-
* });
|
|
382
|
-
* // Logs immediately: "Count is now: 0"
|
|
383
|
-
*
|
|
384
|
-
* await $count.set(5);
|
|
385
|
-
* // Logs: "Count is now: 5"
|
|
386
|
-
*
|
|
387
|
-
* // Clean up when done
|
|
388
|
-
* unsubscribe();
|
|
389
|
-
* ```
|
|
390
|
-
*
|
|
391
|
-
* @throws Error if the node has been disposed.
|
|
392
|
-
*
|
|
393
|
-
* @public
|
|
394
|
-
*/
|
|
395
|
-
subscribe(listener: (value: T) => void): () => void {
|
|
396
|
-
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
397
|
-
const effect = new FlowEffect((t) => {
|
|
398
|
-
listener(this.get(t));
|
|
399
|
-
});
|
|
400
|
-
return () => effect.dispose();
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
/* INTERNAL --------------------------------------------------------- */
|
|
404
|
-
|
|
405
|
-
/* @internal */ override _notify(): void {
|
|
406
|
-
if (this._dirty) {
|
|
407
|
-
return;
|
|
408
|
-
}
|
|
409
|
-
this._dirty = true;
|
|
410
|
-
super._notify();
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
private _computeValue(options?: { force?: boolean }): void {
|
|
414
|
-
if (!this._dirty && !options?.force) return;
|
|
415
|
-
|
|
416
|
-
if (!this._compute) {
|
|
417
|
-
this._dirty = false;
|
|
418
|
-
return;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
this._dirty = false;
|
|
422
|
-
|
|
423
|
-
// Store current dependencies
|
|
424
|
-
const dependencies = [...this._dependencies];
|
|
425
|
-
|
|
426
|
-
// Clear current dependencies, compute and retrack dependencies
|
|
427
|
-
this._dependencies.clear();
|
|
428
|
-
|
|
429
|
-
this._value = this._compute(this);
|
|
430
|
-
|
|
431
|
-
// Unsubscribe from dependencies that are no longer needed
|
|
432
|
-
const dependenciesToRemove = dependencies.filter(
|
|
433
|
-
(dependency) => !this._dependencies.has(dependency),
|
|
434
|
-
);
|
|
435
|
-
dependenciesToRemove.forEach((dependency) => {
|
|
436
|
-
dependency._unregisterDependency(this);
|
|
437
|
-
});
|
|
438
|
-
}
|
|
439
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import type { FlowNode } from "./flowNode";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A generic utility type for creating read-only views of reactive nodes.
|
|
5
|
-
*
|
|
6
|
-
* @typeParam T - The type of the value held by the reactive node.
|
|
7
|
-
*
|
|
8
|
-
* @remarks
|
|
9
|
-
* `FlowReadonly` is a type alias based on {@link FlowNode} with the `set()` method removed,
|
|
10
|
-
* making it a read-only view. It provides all the reactive capabilities of `FlowNode`
|
|
11
|
-
* (dependency tracking, lazy computation, caching, reading values) but prevents mutation.
|
|
12
|
-
*
|
|
13
|
-
* **Difference from FlowConstant and FlowDerivation:**
|
|
14
|
-
* - {@link FlowConstant}: Specialized type for immutable constants that compute once and never change
|
|
15
|
-
* - {@link FlowDerivation}: Specialized type for computed values that automatically recompute when dependencies change
|
|
16
|
-
* - `FlowReadonly`: Generic utility type that can be used with any `FlowNode` to create a read-only view
|
|
17
|
-
*
|
|
18
|
-
* **Use Cases:**
|
|
19
|
-
* - **Type Safety**: Ensure functions don't accidentally mutate state by accepting `FlowReadonly` instead of `FlowState`
|
|
20
|
-
* - **API Design**: Expose read-only views of internal state to prevent external mutation
|
|
21
|
-
* - **Function Parameters**: Accept any reactive node (state, derivation, constant) but prevent mutation
|
|
22
|
-
* - **Composition**: Create read-only wrappers around mutable states for safer sharing
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```typescript
|
|
26
|
-
* // Type safety: Function that only reads state
|
|
27
|
-
* function displayCount(count: FlowReadonly<number>) {
|
|
28
|
-
* // count.set(100); // TypeScript error: Property 'set' does not exist
|
|
29
|
-
* effect((t) => {
|
|
30
|
-
* console.log(count.get(t)); // OK: Reading is allowed
|
|
31
|
-
* });
|
|
32
|
-
* }
|
|
33
|
-
*
|
|
34
|
-
* const $count = state(0);
|
|
35
|
-
* displayCount($count); // OK: FlowState is assignable to FlowReadonly
|
|
36
|
-
*
|
|
37
|
-
* // API design: Expose read-only view
|
|
38
|
-
* class Counter {
|
|
39
|
-
* private _count = state(0);
|
|
40
|
-
*
|
|
41
|
-
* get count(): FlowReadonly<number> {
|
|
42
|
-
* return this._count; // FlowState is assignable to FlowReadonly
|
|
43
|
-
* }
|
|
44
|
-
*
|
|
45
|
-
* increment() {
|
|
46
|
-
* this._count.set(n => n + 1); // Internal mutation allowed
|
|
47
|
-
* }
|
|
48
|
-
* }
|
|
49
|
-
*
|
|
50
|
-
* const counter = new Counter();
|
|
51
|
-
* // counter.count.set(100); // TypeScript error: Cannot mutate read-only view
|
|
52
|
-
* const current = await counter.count.pick(); // OK: Reading is allowed
|
|
53
|
-
* ```
|
|
54
|
-
*
|
|
55
|
-
* @public
|
|
56
|
-
*/
|
|
57
|
-
export type FlowReadonly<T> = Omit<FlowNode<T>, "set" | "refresh">;
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import type { NotPromise } from "../../base";
|
|
2
|
-
import { FlowNode } from "./flowNode";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Represents a reactive state that holds a mutable value.
|
|
6
|
-
*
|
|
7
|
-
* @typeParam T - The type of the state value.
|
|
8
|
-
*
|
|
9
|
-
* @remarks
|
|
10
|
-
* `FlowState` is a type alias of {@link FlowNode}, providing a mutable reactive state that can be
|
|
11
|
-
* updated using the `set()` method. It provides all the reactive capabilities of `FlowNode`
|
|
12
|
-
* (lazy computation, caching) with the ability to mutate the value.
|
|
13
|
-
*
|
|
14
|
-
* Unlike {@link FlowConstant}, which is immutable and computes once, states can be updated at any
|
|
15
|
-
* time. Unlike {@link FlowDerivation}, which recomputes when dependencies change, states are
|
|
16
|
-
* updated explicitly via `set()`.
|
|
17
|
-
*
|
|
18
|
-
* **Reading Values:**
|
|
19
|
-
* You can read the state value using `get(t)` for tracked access (creates reactive dependency)
|
|
20
|
-
* or `pick()` for untracked access (reads without creating dependency).
|
|
21
|
-
*
|
|
22
|
-
* **Updating Values:**
|
|
23
|
-
* When the state is updated with a new value that differs from the current value (strict equality),
|
|
24
|
-
* all dependent effects and derivations are automatically notified and re-executed. If the new value
|
|
25
|
-
* is strictly equal to the current value, no notification occurs.
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```typescript
|
|
29
|
-
* const $count = state(0);
|
|
30
|
-
*
|
|
31
|
-
* // Read with tracking
|
|
32
|
-
* effect((t) => {
|
|
33
|
-
* console.log($count.get(t)); // Effect re-runs when $count changes
|
|
34
|
-
* });
|
|
35
|
-
*
|
|
36
|
-
* // Read without tracking
|
|
37
|
-
* const snapshot = await $count.pick();
|
|
38
|
-
*
|
|
39
|
-
* // Update the value
|
|
40
|
-
* await $count.set(1);
|
|
41
|
-
* await $count.set(current => current + 1);
|
|
42
|
-
* ```
|
|
43
|
-
*
|
|
44
|
-
* @public
|
|
45
|
-
*/
|
|
46
|
-
export type FlowState<T> = Omit<FlowNode<T>, "refresh">;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Creates a new reactive state holding a mutable value.
|
|
50
|
-
*
|
|
51
|
-
* @typeParam T - The type of the state value.
|
|
52
|
-
*
|
|
53
|
-
* @param value - The initial value for the state, or a function that returns the initial value.
|
|
54
|
-
* If a function is provided, it is called lazily only when the value is first accessed via
|
|
55
|
-
* `get()` or `pick()`. If a direct value is provided, it is stored immediately.
|
|
56
|
-
*
|
|
57
|
-
* @returns A new instance of {@link FlowState} that provides reactive access to the mutable value.
|
|
58
|
-
*
|
|
59
|
-
* @remarks
|
|
60
|
-
* State is the most common reactive primitive - a mutable container that notifies dependents
|
|
61
|
-
* when its value changes. Use `set()` to update the value (returns `Promise<void>`) and `get(t)`
|
|
62
|
-
* or `pick()` to read it.
|
|
63
|
-
*
|
|
64
|
-
* **Lazy Initialization:**
|
|
65
|
-
* When a function is provided, the initialization is lazy - the function is not called immediately
|
|
66
|
-
* upon creation. It executes only when:
|
|
67
|
-
* - The value is first read via `get(t)` or `pick()`
|
|
68
|
-
* - The state is watched via `watch()`
|
|
69
|
-
*
|
|
70
|
-
* This is useful for expensive initializations that may not be needed immediately.
|
|
71
|
-
*
|
|
72
|
-
* **When to Use States:**
|
|
73
|
-
* - Use `state()` for mutable values that can be updated directly
|
|
74
|
-
* - Use `constant()` for values that compute once and never change
|
|
75
|
-
* - Use `derivation()` for values that recompute when dependencies change
|
|
76
|
-
*
|
|
77
|
-
* @example
|
|
78
|
-
* ```typescript
|
|
79
|
-
* // Direct value - initialized immediately
|
|
80
|
-
* const $count = state(0);
|
|
81
|
-
*
|
|
82
|
-
* // Lazy initialization - computed on first access
|
|
83
|
-
* const $config = state(() => {
|
|
84
|
-
* return loadConfiguration();
|
|
85
|
-
* });
|
|
86
|
-
*
|
|
87
|
-
* effect((t) => {
|
|
88
|
-
* console.log($count.get(t));
|
|
89
|
-
* });
|
|
90
|
-
*
|
|
91
|
-
* await $count.set(1); // Logs: 1
|
|
92
|
-
* await $count.set(n => n + 1); // Logs: 2
|
|
93
|
-
* ```
|
|
94
|
-
*
|
|
95
|
-
* @public
|
|
96
|
-
*/
|
|
97
|
-
export function state<T>(
|
|
98
|
-
value: NotPromise<T> | (() => NotPromise<T>),
|
|
99
|
-
): FlowState<T> {
|
|
100
|
-
return new FlowNode<T>(value);
|
|
101
|
-
}
|