ember-concurrency icon indicating copy to clipboard operation
ember-concurrency copied to clipboard

Feature Request: waitForPropertyChange

Open BryanCrotaz opened this issue 6 years ago • 6 comments

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.

BryanCrotaz avatar Feb 25 '18 19:02 BryanCrotaz

waitforProperty could be refactored to use this new feature internally.

BryanCrotaz avatar Feb 25 '18 19:02 BryanCrotaz

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')

machty avatar Feb 25 '18 20:02 machty

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')

BryanCrotaz avatar Feb 26 '18 00:02 BryanCrotaz

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 avatar Mar 27 '19 12:03 fran-worley

@fran-worley have you tried my sample implementation of waitForPropertyChange (and does it work well)?

machty avatar Mar 27 '19 12:03 machty

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.

fran-worley avatar Mar 28 '19 12:03 fran-worley