Skip to content

Commit 34e1a61

Browse files
authored
Fix observers in 2.3.0+ (#487)
* example * Reinstate _setupEmberKVO
1 parent 91c4525 commit 34e1a61

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

addon/-private/external/task-decorators.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TaskFactory } from './task-factory';
22

33
function taskFromPropertyDescriptor(
4-
_target,
4+
target,
55
key,
66
descriptor,
77
params = [],
@@ -23,6 +23,7 @@ function taskFromPropertyDescriptor(
2323
let tasks = new WeakMap();
2424
let options = params[0] || {};
2525
let factory = new factoryClass(key, taskFn, options);
26+
factory._setupEmberKVO(target);
2627

2728
return {
2829
get() {

tests/unit/decorators-test.js

+37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { module } from 'qunit';
22
import { run } from '@ember/runloop';
3+
import EmberObject from '@ember/object';
34
import { setOwner } from '@ember/application';
45
import {
56
task,
@@ -9,6 +10,7 @@ import {
910
enqueueTask,
1011
} from 'ember-concurrency';
1112
import { decoratorTest } from '../helpers/helpers';
13+
import { settled } from '@ember/test-helpers';
1214

1315
module('Unit | decorators', function () {
1416
decoratorTest('Basic decorators functionality', function (assert) {
@@ -86,4 +88,39 @@ module('Unit | decorators', function () {
8688
});
8789
assert.equal(subject.encapsulated.last.value, 56);
8890
});
91+
92+
decoratorTest(
93+
'`observes` re-performs the task every time the observed property changes in a coalesced manner',
94+
async function (assert) {
95+
assert.expect(2);
96+
97+
let values = [];
98+
class Obj extends EmberObject {
99+
foo = 0;
100+
101+
@task({ observes: 'foo' })
102+
*observingTask() {
103+
values.push(this.foo);
104+
}
105+
}
106+
107+
let obj = Obj.create();
108+
await settled();
109+
110+
obj.set('foo', 1);
111+
obj.set('foo', 2);
112+
obj.set('foo', 3);
113+
await settled();
114+
115+
assert.deepEqual(values, [3]);
116+
values = [];
117+
118+
obj.set('foo', 4);
119+
obj.set('foo', 5);
120+
obj.set('foo', 6);
121+
await settled();
122+
123+
assert.deepEqual(values, [6]);
124+
}
125+
);
89126
});

0 commit comments

Comments
 (0)