Allow custom behavior for reactive objects #364
Replies: 4 comments 1 reply
-
I wanted something like this before too but then I resolved to use a |
Beta Was this translation helpful? Give feedback.
-
Repost from vuejs/core#4189 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 |
Beta Was this translation helpful? Give feedback.
-
It will be very Good ! It will be very useful with grpc-web !!! Because of |
Beta Was this translation helpful? Give feedback.
-
Hello, |
Beta Was this translation helpful? Give feedback.
-
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 and obviously there are alot of alternatives regarding input validation although 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
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
Beta Was this translation helpful? Give feedback.
All reactions