Skip to content

Commit

Permalink
Fix observers in 2.3.0+ (#487)
Browse files Browse the repository at this point in the history
* example

* Reinstate _setupEmberKVO
  • Loading branch information
machty authored Sep 2, 2022
1 parent 91c4525 commit 34e1a61
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion addon/-private/external/task-decorators.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TaskFactory } from './task-factory';

function taskFromPropertyDescriptor(
_target,
target,
key,
descriptor,
params = [],
Expand All @@ -23,6 +23,7 @@ function taskFromPropertyDescriptor(
let tasks = new WeakMap();
let options = params[0] || {};
let factory = new factoryClass(key, taskFn, options);
factory._setupEmberKVO(target);

return {
get() {
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/decorators-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { module } from 'qunit';
import { run } from '@ember/runloop';
import EmberObject from '@ember/object';
import { setOwner } from '@ember/application';
import {
task,
Expand All @@ -9,6 +10,7 @@ import {
enqueueTask,
} from 'ember-concurrency';
import { decoratorTest } from '../helpers/helpers';
import { settled } from '@ember/test-helpers';

module('Unit | decorators', function () {
decoratorTest('Basic decorators functionality', function (assert) {
Expand Down Expand Up @@ -86,4 +88,39 @@ module('Unit | decorators', function () {
});
assert.equal(subject.encapsulated.last.value, 56);
});

decoratorTest(
'`observes` re-performs the task every time the observed property changes in a coalesced manner',
async function (assert) {
assert.expect(2);

let values = [];
class Obj extends EmberObject {
foo = 0;

@task({ observes: 'foo' })
*observingTask() {
values.push(this.foo);
}
}

let obj = Obj.create();
await settled();

obj.set('foo', 1);
obj.set('foo', 2);
obj.set('foo', 3);
await settled();

assert.deepEqual(values, [3]);
values = [];

obj.set('foo', 4);
obj.set('foo', 5);
obj.set('foo', 6);
await settled();

assert.deepEqual(values, [6]);
}
);
});

0 comments on commit 34e1a61

Please sign in to comment.