-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat(definedWatch): new filter #504
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
Conversation
Have you tried http://vueuse.org/whenever ? |
Okay, some things are rushing to my head now 🙂, so everything below could be a little bit chaotic 😄 So at first: No, I didn't saw BUT thinking my idea a bit further (over night and with help of sleep) some new ideas came up in my mind 😃 I have some sections in my codebase where I setup watchers that looks like this: watch(source, (val, oldVal) => {
if (oldVal && val.id !== oldVal.id) {
// update stuff
}
}) I'm thinking about that export function definedFilter() {
let lastDefinedValue
const filter: EventFilter = (invoke, options) => {
if (options.args[0] != null) {
lastDefinedValue = options.args[0]
invoke() // find out how to pass `lastDefinedValue` as `oldVal` :thinking:
}
}
return filter
} The benefit over
whenever(
() => typeof source.value === 'boolean',
() => console.log('It's definitiv a boolean!'),
) But you need to now that Please let me now what you are thinking about my ideas and tell me everything you can think off which what I could improve this This PR was originally intended to get a first real contact with the codebase of VueUse 🙂 |
Maybe it could be split into two filters, as there are two different features: one similar to whenever and a new one |
But "has changed" is already the default behavior of watchers 😅 |
You are right, now I have to recall why I thought that each assignment will still trigger the watch. Ignore the noise then 😅 |
You can still use |
Btw, I don't think we use stories anymore, so you probably don't have to include those files. |
Could you explain a little bit what these files are for? Cause I just copied over Also, do you all think |
I don't want to get too off topic, but
Don't have any strong opinions here as I'm not the best at naming things to start with 😅 |
I think this is a to special case, and also it can so quickly be solved by just using an if inside the normal watcher 🤷 |
This is a new filter / watcher that only triggers when the source is currently
not null
ornot undefined
It should also be combinable with other filters so that in example a
debounceFilter
works together with adefinedFilter
and therefore only triggers after some debounce time but only when the value was definedThis is useful for watching a stream of data but only pass the defined values instead of wrapping the body of the filter with an if statement like
I'm thinking this further and we could create a filter where you can pass a
predicate
🤔Maybe