Reactivity Interoperability #423
Replies: 3 comments 20 replies
-
A similar implementation (not in vue), for reference: solidjs/solid#739 |
Beta Was this translation helpful? Give feedback.
-
Wouldn't that problem be solved more simply by making As a bonus side-effect it would enable creating custom/more efficient reactive objects/primitives; and it would allow other reactive systems such as MobX to integrate into Vue reactivity. I have proposed this in the past and Evan was opposed to making (Note: |
Beta Was this translation helpful? Give feedback.
-
There are two things that I don't find elegant in your proposal:
Sadly, I don't see how to improve those points without having the 3rd party lib cooperate :( |
Beta Was this translation helpful? Give feedback.
-
Summary
Introducing a new
addReactivityInterop()
API for@vue/reactivity
. This feature adds support for (one-way) reactivity interoperability with third party libraries.Basic example
In vue component:
Motivation
It adds a lot of convinient, eliminates annoying wrappers (e.g. convert to a
ref
) and we can use third party reactive objects as if they are first-class citizen in vue.This feature is primarily for library authors. Normal users do not need to worry about this.
Detailed design
API Summary
Implementation
Currently
addReactivityInterop
will perform irreversible side effect: setting the currentInteropSourceFactory
(which should be a global variable).Whenever a
ReactiveEffect
is constructed, the currentInteropSourceFactory
should be called with the originalfn
passed inReactiveEffect.constructor
and atrigger
function which trigger theReactiveEffect
(to be scheduled/re-run). The return value is aInteropSource
, whose.track
should replace.fn
of currentReactiveEffect
and.dispose()
should be called whenever currentReactiveEffect
will be cleaned up. Essentially there are two steps:ReactiveEffect.fn
, so third party libraries get the control to perform their own dependency tracking logic.Note this feature implies the (external) dependencies should be transparently collected (by external) and the control should be NOT inversed by external (it's vue taking control at first, calling the third party code to perform their dependency tracking). However, not all reactivity implementations give user the control (usually the control is inversed, e.g.
computed(getter)
whosegetter
is called by framework, not user directly, while mobx'sReaction.track(fn)
is called by user, and runs immediately) or support auto (transparent/implicit) dependency tracking.Workaround
In vue 3.2+ there is a work-around by patching
ReactiveEffect
Composed
InteropSourceFactory
If
addReactivityInterop
is called multiple times, the currentInteropSourceFactory
should be composed.Drawbacks
ReactiveEffect
Alternatives
N/A
Adoption strategy
This is a new API and should not affect the existing code. It's also a low-level API only intended for advanced users and library authors.
Unresolved questions
Beta Was this translation helpful? Give feedback.
All reactions