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