ember-concurrency
ember-concurrency copied to clipboard
Feature Request: waitForPropertyChange
waitForProperty
waits for a property to have a certain value, yielding immediately if it already has that value.
A separate use case is to wait for the property to change to any new value.
yield waitForPropertyChange(obj, path)
will always return an unresolved promise, resolving next time there is a set
to that property and the value changes.
waitforProperty
could be refactored to use this new feature internally.
I don't know how useful this is as a feature in general, will wait for feedback from other people, but one thing you can do is pass a callback that returns false the first time it's called and returns true after that, e.g.
function waitForPropertyChange(obj, key) {
let hasRun = false;
return waitForProperty(obj, key, () => {
if (hasRun) {
return true;
} else {
hasRun = true;
return false;
}
});
}
// in a task:
yield waitForPropertyChange(this, 'prop')
It's useful as a direct replacement for observer
with all the auto destruction niceties of ember-concurrency that you've already given us.
e.g.
ddauOnValidChange: task(function*() {
while (true) {
yield waitForPropertyChange(this, 'validator.isValid');
this.onValidChange(get(this, 'validator.isValid'));
}
}).on('init')
We've bumped into needing a feature like this.
We have several components which perform tasks on init
/ didInsertElement
lifecycle hooks. These tasks are usually dependent on an attribute value (i.e. model.id
) and we end up having to add an observer to ensure that the task is re-performed when model.id
changes.
It strikes me that a waitForPropertyChange would be perfect for this.
@fran-worley have you tried my sample implementation of waitForPropertyChange
(and does it work well)?
I haven't yet @machty, I wasn't entirely sure where to put it as I'm still learning the ember framework and how ember-concurrency fits into it! Any ideas? Then I'll be sure to test it and let you know how I get on.