Skip to content

Commit 22ffbe3

Browse files
author
Chris Garrett
committed
[BUGFIX] Updates internals to Decorator from Descriptor
Updates the usage of the ComputedProperty APIs for TaskProperty to use the Decorator API in the latest versions of Ember. Uses `ember-compatibility-helpers` to remain compatible with older versions of Ember.
1 parent ce38e83 commit 22ffbe3

File tree

5 files changed

+176
-89
lines changed

5 files changed

+176
-89
lines changed

addon/-task-group.js

+4-23
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import { or, bool } from '@ember/object/computed';
22
import EmberObject from '@ember/object';
3-
import { objectAssign, _ComputedProperty } from './utils';
3+
import { objectAssign } from './utils';
44
import TaskStateMixin from './-task-state-mixin';
5-
import {
6-
propertyModifiers,
7-
resolveScheduler
8-
} from './-property-modifiers-mixin';
9-
5+
import { propertyModifiers } from './-property-modifiers-mixin';
106

117
export const TaskGroup = EmberObject.extend(TaskStateMixin, {
128
isTaskGroup: true,
@@ -17,24 +13,9 @@ export const TaskGroup = EmberObject.extend(TaskStateMixin, {
1713

1814
_numRunningOrNumQueued: or('numRunning', 'numQueued'),
1915
isRunning: bool('_numRunningOrNumQueued'),
20-
isQueued: false
16+
isQueued: false,
2117
});
2218

23-
export class TaskGroupProperty extends _ComputedProperty {
24-
constructor(taskFn) {
25-
let tp;
26-
super(function(_propertyName) {
27-
return TaskGroup.create({
28-
fn: taskFn,
29-
context: this,
30-
_origin: this,
31-
_taskGroupPath: tp._taskGroupPath,
32-
_scheduler: resolveScheduler(tp, this, TaskGroup),
33-
_propertyName,
34-
});
35-
});
36-
tp = this;
37-
}
38-
}
19+
export class TaskGroupProperty {}
3920

4021
objectAssign(TaskGroupProperty.prototype, propertyModifiers);

addon/-task-property.js

+81-54
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,35 @@ import { addObserver } from '@ember/object/observers';
33
import { addListener } from '@ember/object/events';
44
import EmberObject from '@ember/object';
55
import { getOwner } from '@ember/application';
6-
import {
7-
default as TaskInstance,
8-
getRunningInstance
9-
} from './-task-instance';
6+
import { default as TaskInstance, getRunningInstance } from './-task-instance';
107
import {
118
PERFORM_TYPE_DEFAULT,
129
PERFORM_TYPE_UNLINKED,
13-
PERFORM_TYPE_LINKED
10+
PERFORM_TYPE_LINKED,
1411
} from './-task-instance';
1512
import TaskStateMixin from './-task-state-mixin';
16-
import { TaskGroup } from './-task-group';
17-
import {
18-
propertyModifiers,
19-
resolveScheduler
20-
} from './-property-modifiers-mixin';
13+
import { propertyModifiers } from './-property-modifiers-mixin';
2114
import {
2215
objectAssign,
2316
INVOKE,
2417
_cleanupOnDestroy,
25-
_ComputedProperty
18+
_ComputedProperty,
2619
} from './utils';
2720
import EncapsulatedTask from './-encapsulated-task';
2821
import { deprecate } from '@ember/debug';
22+
import { gte } from 'ember-compatibility-helpers';
2923

3024
const PerformProxy = EmberObject.extend({
3125
_task: null,
3226
_performType: null,
3327
_linkedObject: null,
3428

3529
perform(...args) {
36-
return this._task._performShared(args, this._performType, this._linkedObject);
30+
return this._task._performShared(
31+
args,
32+
this._performType,
33+
this._linkedObject
34+
);
3735
},
3836
});
3937

@@ -184,10 +182,15 @@ export const Task = EmberObject.extend(TaskStateMixin, {
184182
if (typeof this.fn === 'object') {
185183
let owner = getOwner(this.context);
186184
let ownerInjection = owner ? owner.ownerInjection() : {};
187-
this._taskInstanceFactory = EncapsulatedTask.extend(ownerInjection, this.fn);
185+
this._taskInstanceFactory = EncapsulatedTask.extend(
186+
ownerInjection,
187+
this.fn
188+
);
188189
}
189190

190-
_cleanupOnDestroy(this.context, this, 'cancelAll', { reason: 'the object it lives on was destroyed or unrendered' });
191+
_cleanupOnDestroy(this.context, this, 'cancelAll', {
192+
reason: 'the object it lives on was destroyed or unrendered',
193+
});
191194
},
192195

193196
_curry(...args) {
@@ -315,7 +318,6 @@ export const Task = EmberObject.extend(TaskStateMixin, {
315318
* @readOnly
316319
*/
317320

318-
319321
/**
320322
* The current number of active running task instances. This
321323
* number will never exceed maxConcurrency.
@@ -413,42 +415,56 @@ export const Task = EmberObject.extend(TaskStateMixin, {
413415
414416
@class TaskProperty
415417
*/
416-
export class TaskProperty extends _ComputedProperty {
417-
constructor(taskFn) {
418-
let tp;
419-
super(function(_propertyName) {
420-
taskFn.displayName = `${_propertyName} (task)`;
421-
return Task.create({
422-
fn: tp.taskFn,
423-
context: this,
424-
_origin: this,
425-
_taskGroupPath: tp._taskGroupPath,
426-
_scheduler: resolveScheduler(tp, this, TaskGroup),
427-
_propertyName,
428-
_debug: tp._debug,
429-
_hasEnabledEvents: tp._hasEnabledEvents
430-
});
431-
});
432-
tp = this;
433-
this.taskFn = taskFn;
434-
this.eventNames = null;
435-
this.cancelEventNames = null;
436-
this._observes = null;
437-
}
418+
export let TaskProperty;
419+
420+
if (gte('3.10.0')) {
421+
TaskProperty = class {};
422+
} else {
423+
// Prior to the 3.10.0 refactors, we had to extend the _ComputedProprety class
424+
// for a classic decorator/descriptor to run correctly.
425+
TaskProperty = class extends _ComputedProperty {};
426+
}
438427

428+
objectAssign(TaskProperty.prototype, {
439429
setup(proto, taskName) {
440430
if (super.setup) {
441431
super.setup(...arguments);
442432
}
433+
443434
if (this._maxConcurrency !== Infinity && !this._hasSetBufferPolicy) {
444435
// eslint-disable-next-line no-console
445-
console.warn(`The use of maxConcurrency() without a specified task modifier is deprecated and won't be supported in future versions of ember-concurrency. Please specify a task modifier instead, e.g. \`${taskName}: task(...).enqueue().maxConcurrency(${this._maxConcurrency})\``);
436+
console.warn(
437+
`The use of maxConcurrency() without a specified task modifier is deprecated and won't be supported in future versions of ember-concurrency. Please specify a task modifier instead, e.g. \`${taskName}: task(...).enqueue().maxConcurrency(${
438+
this._maxConcurrency
439+
})\``
440+
);
446441
}
447442

448-
registerOnPrototype(addListener, proto, this.eventNames, taskName, 'perform', false);
449-
registerOnPrototype(addListener, proto, this.cancelEventNames, taskName, 'cancelAll', false);
450-
registerOnPrototype(addObserver, proto, this._observes, taskName, 'perform', true);
451-
}
443+
registerOnPrototype(
444+
addListener,
445+
proto,
446+
this.eventNames,
447+
taskName,
448+
'perform',
449+
false
450+
);
451+
registerOnPrototype(
452+
addListener,
453+
proto,
454+
this.cancelEventNames,
455+
taskName,
456+
'cancelAll',
457+
false
458+
);
459+
registerOnPrototype(
460+
addObserver,
461+
proto,
462+
this._observes,
463+
taskName,
464+
'perform',
465+
true
466+
);
467+
},
452468

453469
/**
454470
* Calling `task(...).on(eventName)` configures the task to be
@@ -485,7 +501,7 @@ export class TaskProperty extends _ComputedProperty {
485501
this.eventNames = this.eventNames || [];
486502
this.eventNames.push.apply(this.eventNames, arguments);
487503
return this;
488-
}
504+
},
489505

490506
/**
491507
* This behaves like the {@linkcode TaskProperty#on task(...).on() modifier},
@@ -503,12 +519,12 @@ export class TaskProperty extends _ComputedProperty {
503519
this.cancelEventNames = this.cancelEventNames || [];
504520
this.cancelEventNames.push.apply(this.cancelEventNames, arguments);
505521
return this;
506-
}
522+
},
507523

508524
observes(...properties) {
509525
this._observes = properties;
510526
return this;
511-
}
527+
},
512528

513529
/**
514530
* Configures the task to cancel old currently task instances
@@ -630,25 +646,34 @@ export class TaskProperty extends _ComputedProperty {
630646
*/
631647

632648
perform() {
633-
deprecate(`[DEPRECATED] An ember-concurrency task property was not set on its object via 'defineProperty'.
634-
You probably used 'set(obj, "myTask", task(function* () { ... }) )'.
649+
deprecate(
650+
`[DEPRECATED] An ember-concurrency task property was not set on its object via 'defineProperty'.
651+
You probably used 'set(obj, "myTask", task(function* () { ... }) )'.
635652
Unfortunately due to this we can't tell you the name of the task.`,
636653
false,
637654
{
638655
id: 'ember-meta.descriptor-on-object',
639656
until: '3.5.0',
640-
url: 'https://emberjs.com/deprecations/v3.x#toc_use-defineProperty-to-define-computed-properties',
657+
url:
658+
'https://emberjs.com/deprecations/v3.x#toc_use-defineProperty-to-define-computed-properties',
641659
}
642660
);
643-
throw new Error("An ember-concurrency task property was not set on its object via 'defineProperty'. See deprecation warning for details.");
644-
}
645-
}
661+
throw new Error(
662+
"An ember-concurrency task property was not set on its object via 'defineProperty'. See deprecation warning for details."
663+
);
664+
},
665+
});
646666

647667
objectAssign(TaskProperty.prototype, propertyModifiers);
648668

649-
let handlerCounter = 0;
650-
651-
function registerOnPrototype(addListenerOrObserver, proto, names, taskName, taskMethod, once) {
669+
function registerOnPrototype(
670+
addListenerOrObserver,
671+
proto,
672+
names,
673+
taskName,
674+
taskMethod,
675+
once
676+
) {
652677
if (names) {
653678
for (let i = 0; i < names.length; ++i) {
654679
let name = names[i];
@@ -671,3 +696,5 @@ function makeTaskCallback(taskName, method, once) {
671696
}
672697
};
673698
}
699+
700+
let handlerCounter = 0;

addon/index.js

+64-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
1+
import Ember from 'ember';
2+
import { computed } from '@ember/object';
13
import { timeout, forever } from './utils';
2-
import { TaskProperty } from './-task-property';
4+
import { Task, TaskProperty } from './-task-property';
35
import { didCancel } from './-task-instance';
4-
import { TaskGroupProperty } from './-task-group';
6+
import { TaskGroup, TaskGroupProperty } from './-task-group';
57
import { all, allSettled, hash, race } from './-cancelable-promise-helpers';
68
import { waitForQueue, waitForEvent, waitForProperty } from './-wait-for';
9+
import { resolveScheduler } from './-property-modifiers-mixin';
10+
import { gte } from 'ember-compatibility-helpers';
11+
12+
function _computed(fn) {
13+
if (gte('3.10.0')) {
14+
let cp = function(proto, key) {
15+
if (cp.setup !== undefined) {
16+
cp.setup(proto, key);
17+
}
18+
19+
return computed(fn)(...arguments);
20+
};
21+
22+
Ember._setComputedDecorator(cp);
23+
24+
return cp;
25+
} else {
26+
return computed(fn);
27+
}
28+
}
729

830
/**
931
* A Task is a cancelable, restartable, asynchronous operation that
@@ -50,8 +72,26 @@ import { waitForQueue, waitForEvent, waitForProperty } from './-wait-for';
5072
* @param {function} generatorFunction the generator function backing the task.
5173
* @returns {TaskProperty}
5274
*/
53-
export function task(...args) {
54-
return new TaskProperty(...args);
75+
export function task(taskFn) {
76+
let tp = _computed(function(_propertyName) {
77+
tp.taskFn.displayName = `${_propertyName} (task)`;
78+
return Task.create({
79+
fn: tp.taskFn,
80+
context: this,
81+
_origin: this,
82+
_taskGroupPath: tp._taskGroupPath,
83+
_scheduler: resolveScheduler(tp, this, TaskGroup),
84+
_propertyName,
85+
_debug: tp._debug,
86+
_hasEnabledEvents: tp._hasEnabledEvents,
87+
});
88+
});
89+
90+
tp.taskFn = taskFn;
91+
92+
Object.setPrototypeOf(tp, TaskProperty.prototype);
93+
94+
return tp;
5595
}
5696

5797
/**
@@ -74,9 +114,25 @@ export function task(...args) {
74114
* ```
75115
*
76116
* @returns {TaskGroup}
77-
*/
78-
export function taskGroup(...args) {
79-
return new TaskGroupProperty(...args);
117+
*/
118+
export function taskGroup(taskFn) {
119+
let tp = _computed(function(_propertyName) {
120+
return TaskGroup.create({
121+
fn: tp.taskFn,
122+
context: this,
123+
_origin: this,
124+
_taskGroupPath: tp._taskGroupPath,
125+
_scheduler: resolveScheduler(tp, this, TaskGroup),
126+
_propertyName,
127+
});
128+
});
129+
130+
tp.taskFn = taskFn;
131+
132+
Object.setPrototypeOf(tp, TaskGroupProperty.prototype);
133+
Ember._setComputedDecorator(tp);
134+
135+
return tp;
80136
}
81137

82138
export {
@@ -89,5 +145,5 @@ export {
89145
waitForQueue,
90146
waitForEvent,
91147
waitForProperty,
92-
forever
148+
forever,
93149
};

0 commit comments

Comments
 (0)