Permalink
Browse files

scheduleIterable / deferIterable

paired with @lynchbomb
  • Loading branch information...
stefanpenner committed Mar 12, 2017
1 parent 36d4993 commit d47afff05c17c33532f8629d53bea38266972aff
Showing with 99 additions and 0 deletions.
  1. +9 −0 lib/backburner/iterator-drain.ts
  2. +16 −0 lib/index.ts
  3. +73 −0 tests/defer-iterable-test.ts
  4. +1 −0 tests/index.ts
@@ -0,0 +1,9 @@
export default function(fn: () => { next: () => { done: Boolean, value?: any}}) {
let iterator = fn();
let result = iterator.next();

while (result.done === false) {
result.value();
result = iterator.next();
}
}
@@ -13,6 +13,7 @@ import {

import searchTimer from './backburner/binary-search';
import DeferredActionQueues from './backburner/deferred-action-queues';
import iteratorDrain from './backburner/iterator-drain';

import Queue from './backburner/queue';

@@ -24,6 +25,7 @@ export default class Backburner {
public currentInstance: DeferredActionQueues | null | undefined;

public schedule: Function;
public scheduleIterable: Function;
public scheduleOnce: Function;
public later: Function;

@@ -318,6 +320,19 @@ export default class Backburner {
return this._ensureInstance().schedule(queueName, target, method, args, false, stack);
}

/*
Defer the passed iterable of functions to run inside the specified queue.
@method deferIterable
@param {String} queueName
@param {Iterable} an iterable of functions to execute
@return method result
*/
public deferIterable(queueName: string, iterable: any) {
let stack = this.DEBUG ? new Error() : undefined;
return this._ensureInstance().schedule(queueName, null, iteratorDrain, [iterable], false, stack);
}

public deferOnce(queueName: string, ...args);
public deferOnce(queueName: string /* , target, method, args */) {
let length = arguments.length;
@@ -701,4 +716,5 @@ export default class Backburner {

Backburner.prototype.schedule = Backburner.prototype.defer;
Backburner.prototype.scheduleOnce = Backburner.prototype.deferOnce;
Backburner.prototype.scheduleIterable = Backburner.prototype.deferIterable;
Backburner.prototype.later = Backburner.prototype.setTimeout;
@@ -0,0 +1,73 @@
import Backburner from 'backburner';

QUnit.module('tests/defer-iterable');

class Iterator {
private collection: Function[];
private iteration = 0;
constructor(collection) {
this.collection = collection;
}

public next() {
let iteration = this.iteration++;
let collection = this.collection;
let done = collection.length <= iteration;

let value = done ? undefined : collection[iteration];

return {
done,
value
};
}
}

QUnit.test('deferIterable', function(assert) {
let bb = new Backburner(['zomg']);
let order = 0;

let tasks = {
one: { count: 0, order: -1 },
two: { count: 0, order: -1 },
three: { count: 0, order: -1 }
};

function task1() {
tasks.one.count++;
tasks.one.order = order++;
}

function task2() {
tasks.two.count++;
tasks.two.order = order++;
}

function task3() {
tasks.three.count++;
tasks.three.order = order++;
}

let iterator = () => new Iterator([task1, task2, task3]);

bb.run(() => {
bb.deferIterable('zomg', iterator);

assert.deepEqual(tasks, {
one: { count: 0, order: -1 },
two: { count: 0, order: -1 },
three: { count: 0, order: -1 }
});
});

assert.deepEqual(tasks, {
one: { count: 1, order: 0 },
two: { count: 1, order: 1 },
three: { count: 1, order: 2 }
});
});

QUnit.test('deferIterable aliased to scheduleIterable', function(assert) {
let bb = new Backburner(['zomg']);
assert.equal(bb.deferIterable, bb.scheduleIterable);
});
@@ -4,6 +4,7 @@ import './cancel-test';
import './configurable-timeout-test';
import './debounce-test';
import './debug-test';
import './defer-iterable-test';
import './defer-once-test';
import './defer-test';
import './events-test';

0 comments on commit d47afff

Please sign in to comment.