@ersbeth/picoflow 1.1.2 → 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 +856 -1530
- 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 -57
- 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 -148
- 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,474 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
FlowEffect,
|
|
3
|
-
FlowGraph,
|
|
4
|
-
FlowSignal,
|
|
5
|
-
type FlowTracker,
|
|
6
|
-
} from "../../base";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* A versatile reactive node that can operate as mutable state or computed derivation, with asynchronous values.
|
|
10
|
-
*
|
|
11
|
-
* @typeParam T - The type of the value held by this node (not the Promise itself).
|
|
12
|
-
*
|
|
13
|
-
* @remarks
|
|
14
|
-
* FlowNodeAsync is a flexible reactive primitive that adapts its behavior based on how it's initialized.
|
|
15
|
-
* It extends {@link FlowSignal} and provides reactive value access with automatic dependency tracking.
|
|
16
|
-
* Unlike its synchronous counterpart {@link FlowNode}, FlowNodeAsync always works with `Promise<T>` values,
|
|
17
|
-
* making it suitable for asynchronous computations and data fetching.
|
|
18
|
-
*
|
|
19
|
-
* **Operating Modes:**
|
|
20
|
-
*
|
|
21
|
-
* - **Mutable State Mode**: When constructed with a constant Promise, FlowNodeAsync acts as a mutable
|
|
22
|
-
* reactive state. You can update its value using `set()`, and all dependents are automatically
|
|
23
|
-
* notified when the value changes. The value is always a Promise that resolves to type T.
|
|
24
|
-
*
|
|
25
|
-
* - **Computed Derivation Mode**: When constructed with a compute function, FlowNodeAsync acts as a
|
|
26
|
-
* computed value that automatically tracks its dependencies. The compute function runs lazily
|
|
27
|
-
* (only when the value is first accessed) and recomputes when any tracked dependency changes.
|
|
28
|
-
* The compute function must return a `Promise<T>`. The computed value can be temporarily overridden
|
|
29
|
-
* using `set()`, but the override is cleared on the next recomputation (triggered by dependency changes).
|
|
30
|
-
*
|
|
31
|
-
* **Asynchronous Nature:**
|
|
32
|
-
* All values in FlowNodeAsync are Promises. When you call `get()` or `pick()`, you receive a `Promise<T>`
|
|
33
|
-
* that you must await to get the actual value. This allows for seamless integration with async/await
|
|
34
|
-
* patterns and asynchronous data sources.
|
|
35
|
-
*
|
|
36
|
-
* **Lazy Computation:**
|
|
37
|
-
* When using a compute function, the computation doesn't run immediately upon construction. It
|
|
38
|
-
* executes only when:
|
|
39
|
-
* - The value is first read via `get()` or `pick()`
|
|
40
|
-
* - The node is watched via `watch()`
|
|
41
|
-
*
|
|
42
|
-
* **Dirty Tracking:**
|
|
43
|
-
* When a tracked dependency changes, the node is marked as "dirty" but doesn't recompute
|
|
44
|
-
* immediately. Recomputation happens lazily on the next value access, preventing unnecessary
|
|
45
|
-
* computations when multiple dependencies change in quick succession. The Promise is cached until
|
|
46
|
-
* dependencies change, ensuring efficient access patterns.
|
|
47
|
-
*
|
|
48
|
-
* **Dynamic Dependencies:**
|
|
49
|
-
* Dependencies are tracked dynamically during each computation. If the compute function
|
|
50
|
-
* conditionally tracks different observables, the dependency graph updates automatically.
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* ```typescript
|
|
54
|
-
* // Mutable state mode
|
|
55
|
-
* const $count = new FlowNodeAsync(Promise.resolve(0));
|
|
56
|
-
* await $count.set(Promise.resolve(1)); // Updates the value
|
|
57
|
-
*
|
|
58
|
-
* // Computed derivation mode
|
|
59
|
-
* const $firstName = new FlowNodeAsync(Promise.resolve('John'));
|
|
60
|
-
* const $lastName = new FlowNodeAsync(Promise.resolve('Doe'));
|
|
61
|
-
* const $fullName = new FlowNodeAsync(async (t) => {
|
|
62
|
-
* const first = await $firstName.get(t);
|
|
63
|
-
* const last = await $lastName.get(t);
|
|
64
|
-
* return `${first} ${last}`;
|
|
65
|
-
* });
|
|
66
|
-
*
|
|
67
|
-
* // Compute function hasn't run yet (lazy)
|
|
68
|
-
* const name = await $fullName.pick(); // Now it computes: "John Doe"
|
|
69
|
-
*
|
|
70
|
-
* // Temporarily override computed value
|
|
71
|
-
* await $fullName.set(Promise.resolve('Override'));
|
|
72
|
-
* console.log(await $fullName.pick()); // "Override"
|
|
73
|
-
*
|
|
74
|
-
* // Trigger recomputation (clears override)
|
|
75
|
-
* await $firstName.set(Promise.resolve('Jane'));
|
|
76
|
-
* console.log(await $fullName.pick()); // "Jane Doe" (override cleared)
|
|
77
|
-
* ```
|
|
78
|
-
*
|
|
79
|
-
* @public
|
|
80
|
-
*/
|
|
81
|
-
export class FlowNodeAsync<T> extends FlowSignal {
|
|
82
|
-
protected _promise?: Promise<T>;
|
|
83
|
-
private _dirty = true;
|
|
84
|
-
private _compute?: (t: FlowTracker) => Promise<T>;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Creates a new FlowNodeAsync.
|
|
88
|
-
*
|
|
89
|
-
* @param compute - Either a constant Promise or a compute function that derives the value.
|
|
90
|
-
*
|
|
91
|
-
* @remarks
|
|
92
|
-
* The constructor accepts two different initialization modes:
|
|
93
|
-
*
|
|
94
|
-
* - **Constant Promise**: Creates a mutable reactive node that can be updated via `set()`.
|
|
95
|
-
* The Promise is stored immediately and can be changed at any time. The Promise resolves
|
|
96
|
-
* to the value of type T.
|
|
97
|
-
*
|
|
98
|
-
* - **Compute function**: Creates a computed reactive node that automatically tracks dependencies
|
|
99
|
-
* and recomputes when they change. The compute function receives a tracking context (`t`)
|
|
100
|
-
* that should be used to access dependencies via `.get(t)`. The function must return a
|
|
101
|
-
* `Promise<T>`. The function is not executed immediately; it runs lazily on first access.
|
|
102
|
-
* The computed value can be temporarily overridden using `set()`, but the override is cleared
|
|
103
|
-
* on the next recomputation (triggered by dependency changes).
|
|
104
|
-
*
|
|
105
|
-
* @example
|
|
106
|
-
* ```typescript
|
|
107
|
-
* // Mutable state with constant Promise
|
|
108
|
-
* const $count = new FlowNodeAsync(Promise.resolve(0));
|
|
109
|
-
* await $count.set(Promise.resolve(5));
|
|
110
|
-
*
|
|
111
|
-
* // Computed derivation with async function
|
|
112
|
-
* const $a = new FlowNodeAsync(Promise.resolve(10));
|
|
113
|
-
* const $b = new FlowNodeAsync(Promise.resolve(20));
|
|
114
|
-
* const $sum = new FlowNodeAsync(async (t) => {
|
|
115
|
-
* const a = await $a.get(t);
|
|
116
|
-
* const b = await $b.get(t);
|
|
117
|
-
* return a + b;
|
|
118
|
-
* });
|
|
119
|
-
*
|
|
120
|
-
* // Lazy evaluation - compute function hasn't run yet
|
|
121
|
-
* console.log(await $sum.pick()); // Now it computes: 30
|
|
122
|
-
* ```
|
|
123
|
-
*
|
|
124
|
-
* @public
|
|
125
|
-
*/
|
|
126
|
-
constructor(compute: Promise<T> | ((t: FlowTracker) => Promise<T>)) {
|
|
127
|
-
super();
|
|
128
|
-
if (typeof compute === "function") {
|
|
129
|
-
this._compute = compute as (t: FlowTracker) => Promise<T>;
|
|
130
|
-
} else {
|
|
131
|
-
this._promise = compute;
|
|
132
|
-
this._dirty = false;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
get settled() {
|
|
137
|
-
return this._promise;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
override async watch(tracker: FlowTracker): Promise<void> {
|
|
141
|
-
if (this._disposed)
|
|
142
|
-
return Promise.reject(new Error("[PicoFlow] Primitive is disposed"));
|
|
143
|
-
super.watch(tracker);
|
|
144
|
-
return this._computeValue();
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Gets the current value with dependency tracking.
|
|
149
|
-
*
|
|
150
|
-
* @param tracker - The tracking context for reactive tracking. This observable is registered
|
|
151
|
-
* as a dependency when accessed through this method.
|
|
152
|
-
*
|
|
153
|
-
* @returns A Promise that resolves to the current value of type T.
|
|
154
|
-
*
|
|
155
|
-
* @remarks
|
|
156
|
-
* Use `get(t)` within effects and derivations to create reactive dependencies. The tracker
|
|
157
|
-
* parameter must be provided from the reactive context (typically the `t` parameter in effect
|
|
158
|
-
* or derivation callbacks).
|
|
159
|
-
*
|
|
160
|
-
* **Important:** This method returns a `Promise<T>`, not `T`. You must await the Promise to
|
|
161
|
-
* get the actual value. The Promise is cached until dependencies change, ensuring efficient
|
|
162
|
-
* access patterns.
|
|
163
|
-
*
|
|
164
|
-
* To read a value without creating a dependency, use `pick()` instead.
|
|
165
|
-
*
|
|
166
|
-
* @example
|
|
167
|
-
* ```typescript
|
|
168
|
-
* effect(async (t) => {
|
|
169
|
-
* const tracked = await $state.get(t); // Dependency registered, await the Promise
|
|
170
|
-
* const untracked = await $other.pick(); // No dependency
|
|
171
|
-
* });
|
|
172
|
-
* ```
|
|
173
|
-
*
|
|
174
|
-
* @throws Error if the node has been disposed.
|
|
175
|
-
*
|
|
176
|
-
* @public
|
|
177
|
-
*/
|
|
178
|
-
async get(tracker: FlowTracker | null): Promise<T> {
|
|
179
|
-
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
180
|
-
await this._computeValue();
|
|
181
|
-
if (tracker) super.watch(tracker);
|
|
182
|
-
return this._promise as Promise<T>;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Updates the node with a new value.
|
|
187
|
-
*
|
|
188
|
-
* @param value - A new Promise or a callback function that computes a new Promise based on the current value.
|
|
189
|
-
*
|
|
190
|
-
* @returns A promise that resolves after the update is processed and all dependent effects have been notified.
|
|
191
|
-
*
|
|
192
|
-
* @remarks
|
|
193
|
-
* This method can be used in two ways:
|
|
194
|
-
*
|
|
195
|
-
* **For mutable state nodes** (constructed with a constant Promise):
|
|
196
|
-
* - Updates the stored Promise directly
|
|
197
|
-
* - All dependents are notified of the change
|
|
198
|
-
*
|
|
199
|
-
* **For computed nodes** (constructed with a compute function):
|
|
200
|
-
* - Temporarily overrides the computed value
|
|
201
|
-
* - The override persists until the next recomputation, which occurs when:
|
|
202
|
-
* - A tracked dependency changes, or
|
|
203
|
-
* - The node is refreshed
|
|
204
|
-
* - This allows temporarily overriding computed values for testing or manual control
|
|
205
|
-
*
|
|
206
|
-
* **Value Comparison:**
|
|
207
|
-
* The Promises are resolved and their values are compared. If the resolved new value is strictly
|
|
208
|
-
* equal (`===`) to the current resolved value, no update occurs and subscribers are not notified.
|
|
209
|
-
* This prevents unnecessary re-renders and effect executions.
|
|
210
|
-
*
|
|
211
|
-
* **Asynchronous Processing:**
|
|
212
|
-
* The update is processed asynchronously through the reactive graph, ensuring proper
|
|
213
|
-
* ordering of updates and effect execution. The method returns a Promise that resolves
|
|
214
|
-
* after the update is complete.
|
|
215
|
-
*
|
|
216
|
-
* @throws Error if the node has been disposed.
|
|
217
|
-
*
|
|
218
|
-
* @example
|
|
219
|
-
* ```typescript
|
|
220
|
-
* // Mutable state usage
|
|
221
|
-
* const $count = new FlowNodeAsync(Promise.resolve(0));
|
|
222
|
-
* await $count.set(Promise.resolve(5));
|
|
223
|
-
* await $count.set(async (current) => Promise.resolve(current + 1)); // 6
|
|
224
|
-
*
|
|
225
|
-
* // Temporary override of computed value
|
|
226
|
-
* const $source = new FlowNodeAsync(Promise.resolve(10));
|
|
227
|
-
* const $doubled = new FlowNodeAsync(async (t) => {
|
|
228
|
-
* const val = await $source.get(t);
|
|
229
|
-
* return val * 2;
|
|
230
|
-
* });
|
|
231
|
-
*
|
|
232
|
-
* console.log(await $doubled.pick()); // 20
|
|
233
|
-
*
|
|
234
|
-
* // Temporarily override
|
|
235
|
-
* await $doubled.set(Promise.resolve(50));
|
|
236
|
-
* console.log(await $doubled.pick()); // 50 (override active)
|
|
237
|
-
*
|
|
238
|
-
* // Dependency change clears override
|
|
239
|
-
* await $source.set(Promise.resolve(15));
|
|
240
|
-
* console.log(await $doubled.pick()); // 30 (recomputed, override cleared)
|
|
241
|
-
* ```
|
|
242
|
-
*
|
|
243
|
-
* @public
|
|
244
|
-
*/
|
|
245
|
-
async set(value: Promise<T> | ((current: T) => Promise<T>)): Promise<void> {
|
|
246
|
-
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
247
|
-
|
|
248
|
-
const update = async () => {
|
|
249
|
-
// compute current value
|
|
250
|
-
await this._computeValue();
|
|
251
|
-
const currentValue = (await this._promise) as T;
|
|
252
|
-
|
|
253
|
-
// compute new value
|
|
254
|
-
const nextPromise =
|
|
255
|
-
typeof value === "function" ? value(currentValue) : value;
|
|
256
|
-
const nextValue = await nextPromise;
|
|
257
|
-
|
|
258
|
-
// assign new promise
|
|
259
|
-
this._promise = nextPromise;
|
|
260
|
-
|
|
261
|
-
// return value changed check
|
|
262
|
-
return nextValue !== currentValue;
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
const notify = () => {
|
|
266
|
-
// call super method to avoid setting dirty flag back to true
|
|
267
|
-
super._notify();
|
|
268
|
-
};
|
|
269
|
-
|
|
270
|
-
return FlowGraph.requestWrite(notify, update);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Forces recomputation of the value, even if it's not marked as dirty.
|
|
275
|
-
*
|
|
276
|
-
* @returns A promise that resolves after the recomputation is complete and all
|
|
277
|
-
* dependent effects have been notified.
|
|
278
|
-
*
|
|
279
|
-
* @remarks
|
|
280
|
-
* This method is useful when you need to force a recomputation of a computed value,
|
|
281
|
-
* for example when the computation depends on external data that has changed outside
|
|
282
|
-
* the reactive system.
|
|
283
|
-
*
|
|
284
|
-
* **Behavior:**
|
|
285
|
-
* - For nodes with a compute function: Forces the compute function to run again,
|
|
286
|
-
* even if no dependencies have changed. This effectively clears any temporary
|
|
287
|
-
* override that was set using `set()`.
|
|
288
|
-
* - For nodes without a compute function (mutable state): Recomputes the current
|
|
289
|
-
* value (which is just the stored value), useful for consistency but typically
|
|
290
|
-
* not necessary.
|
|
291
|
-
*
|
|
292
|
-
* The recomputation happens asynchronously through the reactive graph, ensuring
|
|
293
|
-
* proper ordering of updates and effect execution.
|
|
294
|
-
*
|
|
295
|
-
* @throws Error if the node has been disposed.
|
|
296
|
-
*
|
|
297
|
-
* @example
|
|
298
|
-
* ```typescript
|
|
299
|
-
* const $externalData = new FlowNode(() => fetchExternalData());
|
|
300
|
-
*
|
|
301
|
-
* // Some external event occurs that changes the data source
|
|
302
|
-
* externalDataChanged();
|
|
303
|
-
*
|
|
304
|
-
* // Force recomputation to get the new value
|
|
305
|
-
* await $externalData.refresh();
|
|
306
|
-
*
|
|
307
|
-
* // For computed nodes with temporary overrides
|
|
308
|
-
* const $computed = new FlowNode((t) => $source.get(t) * 2);
|
|
309
|
-
* $computed.set(100); // Temporary override
|
|
310
|
-
* await $computed.refresh(); // Clears override, recomputes from source
|
|
311
|
-
* ```
|
|
312
|
-
*
|
|
313
|
-
* @public
|
|
314
|
-
*/
|
|
315
|
-
async refresh(): Promise<void> {
|
|
316
|
-
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
317
|
-
|
|
318
|
-
const update = async () => {
|
|
319
|
-
// compute current value
|
|
320
|
-
await this._computeValue();
|
|
321
|
-
const currentValue = await (this._promise as Promise<T>);
|
|
322
|
-
|
|
323
|
-
// compute new value
|
|
324
|
-
await this._computeValue({ force: true });
|
|
325
|
-
const nextValue = await (this._promise as Promise<T>);
|
|
326
|
-
|
|
327
|
-
// return value changed check
|
|
328
|
-
return nextValue !== currentValue;
|
|
329
|
-
};
|
|
330
|
-
|
|
331
|
-
const notify = () => {
|
|
332
|
-
// call super method to avoid setting dirty flag back to true
|
|
333
|
-
super._notify();
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
return await FlowGraph.requestWrite(notify, update);
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* Gets the current value without any dependency tracking.
|
|
341
|
-
*
|
|
342
|
-
* @returns A promise that resolves with the current value of type T.
|
|
343
|
-
*
|
|
344
|
-
* @remarks
|
|
345
|
-
* This method reads the value asynchronously through the reactive graph, ensuring proper ordering of
|
|
346
|
-
* read operations. Unlike `get(t)`, this method does not create a reactive dependency.
|
|
347
|
-
*
|
|
348
|
-
* **Important:** This is an async method that returns `Promise<T>`. Always use `await` when calling it.
|
|
349
|
-
*
|
|
350
|
-
* Use `pick()` when you want to read a snapshot of the current value without creating a reactive
|
|
351
|
-
* dependency. This is useful for:
|
|
352
|
-
* - Reading initial values outside reactive contexts
|
|
353
|
-
* - Accessing configuration that shouldn't trigger updates
|
|
354
|
-
* - Mixing tracked and untracked reads in the same effect
|
|
355
|
-
*
|
|
356
|
-
* @example
|
|
357
|
-
* ```typescript
|
|
358
|
-
* // Read a snapshot outside reactive context
|
|
359
|
-
* const currentValue = await $state.pick();
|
|
360
|
-
*
|
|
361
|
-
* // Mix tracked and untracked reads
|
|
362
|
-
* effect(async (t) => {
|
|
363
|
-
* const tracked = await $reactive.get(t); // Triggers re-runs
|
|
364
|
-
* const snapshot = await $config.pick(); // Doesn't trigger re-runs
|
|
365
|
-
* processData(tracked, snapshot);
|
|
366
|
-
* });
|
|
367
|
-
* ```
|
|
368
|
-
*
|
|
369
|
-
* @throws Error if the node has been disposed.
|
|
370
|
-
*
|
|
371
|
-
* @public
|
|
372
|
-
*/
|
|
373
|
-
async pick(): Promise<T> {
|
|
374
|
-
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
375
|
-
await FlowGraph.requestRead(() => this._computeValue());
|
|
376
|
-
return this._promise as Promise<T>;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
/**
|
|
380
|
-
* Subscribes a listener function to changes of this node.
|
|
381
|
-
*
|
|
382
|
-
* @param listener - A callback function that receives the new value whenever it changes.
|
|
383
|
-
*
|
|
384
|
-
* @returns A disposer function that cancels the subscription when called.
|
|
385
|
-
*
|
|
386
|
-
* @remarks
|
|
387
|
-
* This method creates a reactive subscription that automatically tracks this node as a dependency.
|
|
388
|
-
* The listener is executed:
|
|
389
|
-
* - Immediately with the current value when the subscription is created
|
|
390
|
-
* - Automatically whenever the value changes
|
|
391
|
-
*
|
|
392
|
-
* **Important:** The listener receives a `Promise<T>`, not `T`. You must await the Promise
|
|
393
|
-
* within the listener to access the actual value. The Promise is the same one returned by `get()`,
|
|
394
|
-
* so it's cached until dependencies change.
|
|
395
|
-
*
|
|
396
|
-
* The subscription uses a {@link FlowEffect} internally to manage the reactive tracking and
|
|
397
|
-
* automatic re-execution. When the value changes, the listener is called with the new Promise
|
|
398
|
-
* after the update is processed through the reactive graph.
|
|
399
|
-
*
|
|
400
|
-
* **Cleanup:**
|
|
401
|
-
* Always call the returned disposer function when you no longer need the subscription to
|
|
402
|
-
* prevent memory leaks and unnecessary computations.
|
|
403
|
-
*
|
|
404
|
-
* @example
|
|
405
|
-
* ```typescript
|
|
406
|
-
* const $count = new FlowNodeAsync(Promise.resolve(0));
|
|
407
|
-
*
|
|
408
|
-
* // Subscribe to changes
|
|
409
|
-
* const unsubscribe = $count.subscribe(async (valuePromise) => {
|
|
410
|
-
* const value = await valuePromise;
|
|
411
|
-
* console.log(`Count is now: ${value}`);
|
|
412
|
-
* });
|
|
413
|
-
* // Logs immediately: "Count is now: 0"
|
|
414
|
-
*
|
|
415
|
-
* await $count.set(Promise.resolve(5));
|
|
416
|
-
* // Logs: "Count is now: 5"
|
|
417
|
-
*
|
|
418
|
-
* // Clean up when done
|
|
419
|
-
* unsubscribe();
|
|
420
|
-
* ```
|
|
421
|
-
*
|
|
422
|
-
* @throws Error if the node has been disposed.
|
|
423
|
-
*
|
|
424
|
-
* @public
|
|
425
|
-
*/
|
|
426
|
-
subscribe(listener: (value: Promise<T>) => void): () => void {
|
|
427
|
-
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
428
|
-
const effect = new FlowEffect((t) => {
|
|
429
|
-
listener(this.get(t));
|
|
430
|
-
});
|
|
431
|
-
return () => effect.dispose();
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
/* INTERNAL --------------------------------------------------------- */
|
|
435
|
-
|
|
436
|
-
/* @internal */ override _notify(): void {
|
|
437
|
-
if (this._dirty) {
|
|
438
|
-
return;
|
|
439
|
-
}
|
|
440
|
-
this._dirty = true;
|
|
441
|
-
super._notify();
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
private async _computeValue(options?: { force?: boolean }): Promise<void> {
|
|
445
|
-
if (!this._dirty && !options?.force) return;
|
|
446
|
-
|
|
447
|
-
if (!this._compute) {
|
|
448
|
-
this._dirty = false;
|
|
449
|
-
return;
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
this._dirty = false;
|
|
453
|
-
|
|
454
|
-
// Store current dependencies
|
|
455
|
-
const dependencies = [...this._dependencies];
|
|
456
|
-
|
|
457
|
-
// Clear current dependencies, compute and retrack dependencies
|
|
458
|
-
this._dependencies.clear();
|
|
459
|
-
|
|
460
|
-
// Compute the value
|
|
461
|
-
this._promise = this._compute(this);
|
|
462
|
-
|
|
463
|
-
// Wait until all dependencies are computed
|
|
464
|
-
await this._promise;
|
|
465
|
-
|
|
466
|
-
// Unsubscribe from dependencies that are no longer needed
|
|
467
|
-
const dependenciesToRemove = dependencies.filter(
|
|
468
|
-
(dependency) => !this._dependencies.has(dependency),
|
|
469
|
-
);
|
|
470
|
-
dependenciesToRemove.forEach((dependency) => {
|
|
471
|
-
dependency._unregisterDependency(this);
|
|
472
|
-
});
|
|
473
|
-
}
|
|
474
|
-
}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import type { FlowNodeAsync } from "./flowNodeAsync";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A generic utility type for creating read-only views of reactive nodes, with asynchronous values.
|
|
5
|
-
*
|
|
6
|
-
* @typeParam T - The type of the value held by the reactive node (not the Promise itself).
|
|
7
|
-
*
|
|
8
|
-
* @remarks
|
|
9
|
-
* `FlowReadonlyAsync` is a type alias based on {@link FlowNodeAsync} with the `set()` and `refresh()`
|
|
10
|
-
* methods removed, making it a read-only view. It provides all the reactive capabilities of `FlowNodeAsync`
|
|
11
|
-
* (dependency tracking, lazy computation, caching, reading values) but prevents mutation.
|
|
12
|
-
*
|
|
13
|
-
* **Asynchronous Nature:**
|
|
14
|
-
* All values in FlowReadonlyAsync are Promises. When you call `get()` or `pick()`, you receive a
|
|
15
|
-
* `Promise<T>` that you must await to get the actual value. This allows for seamless integration
|
|
16
|
-
* with async/await patterns and asynchronous data sources.
|
|
17
|
-
*
|
|
18
|
-
* **Difference from FlowConstantAsync and FlowDerivationAsync:**
|
|
19
|
-
* - {@link FlowConstantAsync}: Specialized type for immutable constants that compute once and never change
|
|
20
|
-
* - {@link FlowDerivationAsync}: Specialized type for computed values that automatically recompute when dependencies change
|
|
21
|
-
* - `FlowReadonlyAsync`: Generic utility type that can be used with any `FlowNodeAsync` to create a read-only view
|
|
22
|
-
*
|
|
23
|
-
* **Use Cases:**
|
|
24
|
-
* - **Type Safety**: Ensure functions don't accidentally mutate state by accepting `FlowReadonlyAsync` instead of `FlowStateAsync`
|
|
25
|
-
* - **API Design**: Expose read-only views of internal state to prevent external mutation
|
|
26
|
-
* - **Function Parameters**: Accept any reactive node (state, derivation, constant) but prevent mutation
|
|
27
|
-
* - **Composition**: Create read-only wrappers around mutable states for safer sharing
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* ```typescript
|
|
31
|
-
* // Type safety: Function that only reads state
|
|
32
|
-
* function displayCount(count: FlowReadonlyAsync<number>) {
|
|
33
|
-
* // count.set(Promise.resolve(100)); // TypeScript error: Property 'set' does not exist
|
|
34
|
-
* effect(async (t) => {
|
|
35
|
-
* const value = await count.get(t); // OK: Reading is allowed
|
|
36
|
-
* console.log(value);
|
|
37
|
-
* });
|
|
38
|
-
* }
|
|
39
|
-
*
|
|
40
|
-
* const $count = stateAsync(Promise.resolve(0));
|
|
41
|
-
* displayCount($count); // OK: FlowStateAsync is assignable to FlowReadonlyAsync
|
|
42
|
-
*
|
|
43
|
-
* // API design: Expose read-only view
|
|
44
|
-
* class Counter {
|
|
45
|
-
* private _count = stateAsync(Promise.resolve(0));
|
|
46
|
-
*
|
|
47
|
-
* get count(): FlowReadonlyAsync<number> {
|
|
48
|
-
* return this._count; // FlowStateAsync is assignable to FlowReadonlyAsync
|
|
49
|
-
* }
|
|
50
|
-
*
|
|
51
|
-
* async increment() {
|
|
52
|
-
* await this._count.set(async (current) => Promise.resolve(current + 1)); // Internal mutation allowed
|
|
53
|
-
* }
|
|
54
|
-
* }
|
|
55
|
-
*
|
|
56
|
-
* const counter = new Counter();
|
|
57
|
-
* // counter.count.set(Promise.resolve(100)); // TypeScript error: Cannot mutate read-only view
|
|
58
|
-
* const current = await counter.count.pick(); // OK: Reading is allowed
|
|
59
|
-
*
|
|
60
|
-
* // Works with derivations too
|
|
61
|
-
* const $firstName = stateAsync(Promise.resolve('John'));
|
|
62
|
-
* const $lastName = stateAsync(Promise.resolve('Doe'));
|
|
63
|
-
* const $fullName = derivationAsync(async (t) => {
|
|
64
|
-
* const first = await $firstName.get(t);
|
|
65
|
-
* const last = await $lastName.get(t);
|
|
66
|
-
* return `${first} ${last}`;
|
|
67
|
-
* });
|
|
68
|
-
*
|
|
69
|
-
* function displayName(name: FlowReadonlyAsync<string>) {
|
|
70
|
-
* effect(async (t) => {
|
|
71
|
-
* const fullName = await name.get(t);
|
|
72
|
-
* console.log(fullName);
|
|
73
|
-
* });
|
|
74
|
-
* }
|
|
75
|
-
*
|
|
76
|
-
* displayName($fullName); // OK: FlowDerivationAsync is assignable to FlowReadonlyAsync
|
|
77
|
-
* ```
|
|
78
|
-
*
|
|
79
|
-
* @public
|
|
80
|
-
*/
|
|
81
|
-
export type FlowReadonlyAsync<T> = Omit<FlowNodeAsync<T>, "set" | "refresh">;
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { FlowNodeAsync } from "./flowNodeAsync";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Represents a reactive state that holds a mutable value, with asynchronous values.
|
|
5
|
-
*
|
|
6
|
-
* @typeParam T - The type of the state value (not the Promise itself).
|
|
7
|
-
*
|
|
8
|
-
* @remarks
|
|
9
|
-
* `FlowStateAsync` is a type alias of {@link FlowNodeAsync}, providing a mutable reactive state that can be
|
|
10
|
-
* updated using the `set()` method. It provides all the reactive capabilities of `FlowNodeAsync`
|
|
11
|
-
* (lazy computation, caching) with the ability to mutate the value.
|
|
12
|
-
*
|
|
13
|
-
* Unlike {@link FlowConstantAsync}, which is immutable and computes once, states can be updated at any
|
|
14
|
-
* time. Unlike {@link FlowDerivationAsync}, which recomputes when dependencies change, states are
|
|
15
|
-
* updated explicitly via `set()`.
|
|
16
|
-
*
|
|
17
|
-
* **Asynchronous Nature:**
|
|
18
|
-
* All values in FlowStateAsync are Promises. When you call `get()` or `pick()`, you receive a
|
|
19
|
-
* `Promise<T>` that you must await to get the actual value. The `set()` method returns `Promise<void>`
|
|
20
|
-
* and must be awaited. This allows for seamless integration with async/await patterns and asynchronous
|
|
21
|
-
* data sources.
|
|
22
|
-
*
|
|
23
|
-
* **Reading Values:**
|
|
24
|
-
* You can read the state value using `get(t)` for tracked access (creates reactive dependency)
|
|
25
|
-
* or `pick()` for untracked access (reads without creating dependency). Both methods return `Promise<T>`
|
|
26
|
-
* that must be awaited.
|
|
27
|
-
*
|
|
28
|
-
* **Updating Values:**
|
|
29
|
-
* When the state is updated with a new value that differs from the current value (strict equality),
|
|
30
|
-
* all dependent effects and derivations are automatically notified and re-executed. If the new value
|
|
31
|
-
* is strictly equal to the current value, no notification occurs. The `set()` method accepts either
|
|
32
|
-
* a `Promise<T>` or a function `(current: T) => Promise<T>` and returns `Promise<void>`.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```typescript
|
|
36
|
-
* const $count = stateAsync(Promise.resolve(0));
|
|
37
|
-
*
|
|
38
|
-
* // Read with tracking
|
|
39
|
-
* effect(async (t) => {
|
|
40
|
-
* const value = await $count.get(t); // Effect re-runs when $count changes
|
|
41
|
-
* console.log(value);
|
|
42
|
-
* });
|
|
43
|
-
*
|
|
44
|
-
* // Read without tracking
|
|
45
|
-
* const snapshot = await $count.pick();
|
|
46
|
-
*
|
|
47
|
-
* // Update the value
|
|
48
|
-
* await $count.set(Promise.resolve(1));
|
|
49
|
-
* await $count.set(async (current) => Promise.resolve(current + 1));
|
|
50
|
-
* ```
|
|
51
|
-
*
|
|
52
|
-
* @public
|
|
53
|
-
*/
|
|
54
|
-
export type FlowStateAsync<T> = Omit<FlowNodeAsync<T>, "refresh">;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Creates a new reactive state holding a mutable value.
|
|
58
|
-
*
|
|
59
|
-
* @typeParam T - The type of the state value (not the Promise itself).
|
|
60
|
-
*
|
|
61
|
-
* @param value - The initial value for the state, or a function that returns the initial value.
|
|
62
|
-
* If a function is provided, it is called lazily only when the value is first accessed via
|
|
63
|
-
* `get()` or `pick()`. If a direct Promise is provided, it is stored immediately.
|
|
64
|
-
*
|
|
65
|
-
* @returns A new instance of {@link FlowStateAsync} that provides reactive access to the mutable value.
|
|
66
|
-
*
|
|
67
|
-
* @remarks
|
|
68
|
-
* State is the most common reactive primitive - a mutable container that notifies dependents
|
|
69
|
-
* when its value changes. Use `set()` to update the value (returns `Promise<void>`) and `get(t)`
|
|
70
|
-
* or `pick()` to read it (both return `Promise<T>`).
|
|
71
|
-
*
|
|
72
|
-
* **Asynchronous Nature:**
|
|
73
|
-
* All values are Promises. When you access the state via `get(t)` or `pick()`, you receive a
|
|
74
|
-
* `Promise<T>` that you must await to get the actual value. The `set()` method also returns
|
|
75
|
-
* `Promise<void>` and must be awaited. This allows for seamless integration with async/await
|
|
76
|
-
* patterns and asynchronous data sources.
|
|
77
|
-
*
|
|
78
|
-
* **Lazy Initialization:**
|
|
79
|
-
* When a function is provided, the initialization is lazy - the function is not called immediately
|
|
80
|
-
* upon creation. It executes only when:
|
|
81
|
-
* - The value is first read via `get(t)` or `pick()`
|
|
82
|
-
* - The state is watched via `watch()`
|
|
83
|
-
*
|
|
84
|
-
* This is useful for expensive async initializations that may not be needed immediately.
|
|
85
|
-
*
|
|
86
|
-
* **When to Use States:**
|
|
87
|
-
* - Use `stateAsync()` for mutable values that can be updated directly
|
|
88
|
-
* - Use `constantAsync()` for values that compute once and never change
|
|
89
|
-
* - Use `derivationAsync()` for values that recompute when dependencies change
|
|
90
|
-
*
|
|
91
|
-
* @example
|
|
92
|
-
* ```typescript
|
|
93
|
-
* // Direct Promise - initialized immediately
|
|
94
|
-
* const $count = stateAsync(Promise.resolve(0));
|
|
95
|
-
*
|
|
96
|
-
* // Lazy initialization - computed on first access
|
|
97
|
-
* const $config = stateAsync(async () => {
|
|
98
|
-
* return await loadConfiguration();
|
|
99
|
-
* });
|
|
100
|
-
*
|
|
101
|
-
* effect(async (t) => {
|
|
102
|
-
* const value = await $count.get(t);
|
|
103
|
-
* console.log(value);
|
|
104
|
-
* });
|
|
105
|
-
*
|
|
106
|
-
* await $count.set(Promise.resolve(1)); // Logs: 1
|
|
107
|
-
* await $count.set(async (current) => Promise.resolve(current + 1)); // Logs: 2
|
|
108
|
-
* ```
|
|
109
|
-
*
|
|
110
|
-
* @public
|
|
111
|
-
*/
|
|
112
|
-
export function stateAsync<T>(
|
|
113
|
-
value: Promise<T> | (() => Promise<T>),
|
|
114
|
-
): FlowStateAsync<T> {
|
|
115
|
-
return new FlowNodeAsync(value);
|
|
116
|
-
}
|