overhaul reactive system #21
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces two new primitives:
createEffectandcreateMemo. It also overhauls howcreateComputedandAccessortracking works. It also adds support for nested properties usingcreateBinding.Changes
Accessor dependency tracking
Accessor tracking is now builtin to the Accessor implementation and does not require other code to manage it so
createComputedsyntax is replaced withcreateComputed(() => dep1() + dep2()).Note that calling an
Accessoras a function previously required passing a transform function which returns a newAccessor. This behavior is not changed, but now its possible to omit it, which simply returns the value and tracks it in reactive scopes as a dependency.State custom equals
createStatecan now take in a customequalsfunction to customize when updates trigger.With the changes to
createComputeddependencies are no longer aggressively cached.the previous behavior can still be achieved with a memo
Memo
New primitive:
createMemo.Its signature is identical to
createComputedbut instead of only invalidating and only computing when accessedcreateMemoalways re-runs the computation but will not trigger effect/computation updates that depend on itEffects
New primitive:
createEffectInstead of avoiding it, the docs simply warn users when not to use it. It probably won't stop users to write bad code but at least resources are automatically managed for them.
Nested bindings
createBindingnow finally supports nested propertiesTypeScript annotations only support up to 4 levels for now which should be plenty enough for any usecase, but the runtime supports an unlimited number of deepness so if 4 levels is not enough you can slap a
@ts-expect-erroron it.Deprecations
createComputedsyntaxWith this PR the previous two
createComputedsyntax is deprecated.createComputed([dep1, dep2], (v1, v2) => v1 + v2)andcreateComputed((get) => get(dep1) + get(dep2))Accessor.get
To reflect its usecase better it has been renamed to
.peek()