-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Allow for custom reactive behavior #4189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
doesn't a |
I would also say a watcher is more adapted for validation. In any case, this needs to go through an RFC. If you still think this is worth pursuing, follw the RFC proccess in the rfcs repo |
I second the request of @mVIII, to have a What problem does this feature solve?For ref's we have In other words, it's currently not possible (that I know of) to delay triggering effects on changes in a reactive object. The only workaround so far would be building/using a custom Proxy implementation on values assigned to a My end goalI'd like to build a transactional higher-level API on top of Vue's native reactivity system: Batching a bunch of async changes without running any effects until the end of the transaction. Other reactivity API's have a concept of transactions, namely async 'actions' (Vuex, Redux, MobX, ...). No effect will be run until the outer-most action has finished, guaranteeing that intermediate or incomplete values produced during an action are not visible to the rest of the application until the action has completed. But without a What does the proposed API look like?Creates a customized reactive object with explicit control over its dependency tracking and updates triggering. It expects a factory function, which receives track and trigger functions as arguments and should return an object with get and set. function useDebouncedReactive(value, delay = 200) {
let timeout
return customReactive((track, trigger) => {
return {
get(target, type, key) {
track(target, type, key)
if (type === 'has') return key in target
if (type === 'iterate') return Object.getOwnPropertyNames(target[key])
return target[key]
},
set(target, type, key, newValue) {
clearTimeout(timeout)
timeout = setTimeout(() => {
if (type === 'set' || type === 'add') {
target.key = newValue
} else if (type === 'delete') {
delete target.key
}
trigger(target, type, key)
}, delay)
},
}
})
}
export default {
setup() {
return {
user: useDebouncedReactive({ name: 'John', age: 42 })
}
}
} When dealing with Map or Set inside the reactive value, then the example would look more complex of course (also handling set |
What problem does this feature solve?
Context
Trying to create property validations for my objects through their accessors,I stumbled up the following problem. Throwing an error from a setter method would simply break reactivity.More specifically in the set method when my objects setter is invoked.Ideally I would like to catch the error without breaking the reactivity chain.While my use case may be too specific ,allowing for a customized behavior for reactive object like the already existing CustomRef would make the api more extensive and probably cover a variety of other use cases
What does the proposed API look like?
Custom reactive
I propose either one of the following
Exposing a custom reactive method
Re export methods track/trigger through the user-facing renderer
Another solution would be re-exporting the method trigger/track from the reactivity package through the vue-runtime dom as implied here @vue-reactivity
The text was updated successfully, but these errors were encountered: