Removing tokens in favor of simpler mechanism
MODIFY
addon/services/scheduler.js
addon/services/scheduler.js
import Ember from 'ember';
import { DEBUG } from '@glimmer/env';
-import Token from '../token';
import TaskQueue from '../task-queue';
const {
@@ -26,19 +25,20 @@ const Scheduler = Service.extend({
scheduleWork(queueName, callback) {
const queue = this.queues[queueName];
- const token = new Token();
if (queue.isActive) {
- queue.enqueue({token, callback});
+ queue.enqueue(callback);
} else {
callback();
}
- return token;
+ return callback;
},
- cancelWork(token) {
- token.cancel();
+ cancelWork(queueName, token) {
+ const queue = this.queues[queueName];
+
+ queue.cancel(token);
},
flushQueue(queueName) {

MODIFY
addon/task-queue.js
addon/task-queue.js
@@ -9,6 +9,14 @@ export default class TaskQueue {
this.tasks.push(task);
}
+ cancel(token) {
+ let index = this.tasks.indexOf(token);
+
+ if (index > -1) {
+ this.tasks.splice(index, 1);
+ }
+ }
+
size() {
return this.tasks.length;
}
@@ -17,7 +25,7 @@ export default class TaskQueue {
this.isActive = false;
for (let i = 0; i < this.tasks.length; i++) {
- this.tasks[i].callback();
+ this.tasks[i]();
}
this.tasks = [];

MODIFY
tests/dummy/app/components/defer-render.js
tests/dummy/app/components/defer-render.js
@@ -18,7 +18,7 @@ export default Ember.Component.extend({
willDestroyElement() {
this._super(...arguments);
if (this._token) {
- this.get('scheduler').cancelWork(this._token);
+ this.get('scheduler').cancelWork('afterFirstRoutePaint', this._token);
}
}
});

MODIFY
tests/dummy/app/components/right-rail-ad.js
tests/dummy/app/components/right-rail-ad.js
@@ -18,7 +18,7 @@ export default Ember.Component.extend({
willDestroyElement() {
this._super(...arguments);
if (this._token) {
- this.get('scheduler').cancelWork(this._token);
+ this.get('scheduler').cancelWork('afterContentPaint', this._token);
}
}
});

MODIFY
tests/unit/services/scheduler-test.js
tests/unit/services/scheduler-test.js
@@ -29,7 +29,7 @@ test('it should have an active queue after scheduling work', function(assert) {
assert.ok(this.scheduler.hasActiveQueue(), 'has active queues');
- this.scheduler.cancelWork(workToken);
+ this.scheduler.cancelWork(AFTER_CONTENT_PAINT, workToken);
});
test('it should not have an active queue after flushing that queue', function(assert) {
@@ -47,7 +47,7 @@ test('it should not have an active queue after flushing that queue', function(as
assert.notOk(this.scheduler.hasActiveQueue(), 'has no active queues');
- this.scheduler.cancelWork(workToken);
+ this.scheduler.cancelWork(AFTER_CONTENT_PAINT, workToken);
});
test('it should have no active queues after the router\'s willTransition event', function(assert) {
@@ -65,19 +65,17 @@ test('it should have no active queues after the router\'s willTransition event',
assert.notOk(this.scheduler.hasActiveQueue(), 'has no active queues');
- this.scheduler.cancelWork(workToken);
+ this.scheduler.cancelWork(AFTER_CONTENT_PAINT, workToken);
});
test('it should cancel work when given a token', function(assert) {
- assert.expect(2);
+ assert.expect(1);
let myWork = () => true;
const workToken = this.scheduler.scheduleWork(AFTER_CONTENT_PAINT, myWork);
- assert.notOk(workToken.cancelled, 'token is not cancelled');
-
- this.scheduler.cancelWork(workToken);
+ this.scheduler.cancelWork(AFTER_CONTENT_PAINT, workToken);
- assert.ok(workToken.cancelled, 'token is cancelled');
+ assert.equal(this.scheduler.queues[AFTER_CONTENT_PAINT].tasks.indexOf(workToken), -1, 'token is cancelled');
});

MODIFY
tests/unit/task-queue-test.js
tests/unit/task-queue-test.js
import { module, test } from 'qunit';
import TaskQueue from 'ember-app-scheduler/task-queue';
-import Token from 'ember-app-scheduler/token';
module(`Unit | ember-app-scheduler | Queue`, {
beforeEach() {
this.taskQueue = new TaskQueue();
this.createTask = () => {
- return { token: new Token(), callback: () => {} };
+ return () => {};
}
}
});

Removing tokens in favor of simpler mechanism