feat(reactivity): public contract for ref, reactive (fix vuejs/rfcs#129) #834
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 adds two new public functions:
markRef
andmarkReactive
.They are meant for advanced users or library authors and are only exported by
@vue/reactivity
, notvue
itself.The use-case here is to allow libraries to provide alternative, specialized implementations of the
ref
(observable value) andreactive
(observable object) concepts.Example use cases
One could want to create a ref that debounces its notifications:
On the reactive side, one could want to create:
hideRefs({ a: ref(4) })
that only automatically unwrap refs.Alternatives
It is possible to mark alternative implementations with
markNonReactive
but it doesn't integrate as well with Vue and some scenarios are broken.For example, you can't make a
Ref<T>
in Typescript because the interface uses a private symbol to prevent that. This can only be worked around with<any>
casts.On the
reactive
side, some operations usetoRaw
(notably the templateref
feature, e.g.<Component ref='x'>
) and this is not exposed publicly so there is no way to tap into it.RFC
Closes vuejs/rfcs#129