Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 78 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ function parseArgs() {

let UUID = 0;

let beginCount = 0;
let endCount = 0;
let beginEventCount = 0;
let endEventCount = 0;
let runCount = 0;
let joinCount = 0;
let deferCount = 0;
let scheduleCount = 0;
let scheduleIterableCount = 0;
let deferOnceCount = 0;
let scheduleOnceCount = 0;
let setTimeoutCount = 0;
let laterCount = 0;
let throttleCount = 0;
let debounceCount = 0;
let cancelTimersCount = 0;
let cancelCount = 0;
let autorunsCreatedCount = 0;
let autorunsCompletedCount = 0;
let deferredActionQueuesCreatedCount = 0;
let nestedDeferredActionQueuesCreated = 0;

export default class Backburner {
public static Queue = Queue;

Expand All @@ -54,6 +76,38 @@ export default class Backburner {

public options: any;

public get counters() {
return {
begin: beginCount,
end: endCount,
events: {
begin: beginEventCount,
end: endEventCount,
},
autoruns: {
created: autorunsCreatedCount,
completed: autorunsCompletedCount,
},
run: runCount,
join: joinCount,
defer: deferCount,
schedule: scheduleCount,
scheduleIterable: scheduleIterableCount,
deferOnce: deferOnceCount,
scheduleOnce: scheduleOnceCount,
setTimeout: setTimeoutCount,
later: laterCount,
throttle: throttleCount,
debounce: debounceCount,
cancelTimers: cancelTimersCount,
cancel: cancelCount,
loops: {
total: deferredActionQueuesCreatedCount,
nested: nestedDeferredActionQueuesCreated,
},
};
}

private _onBegin: (currentInstance: DeferredActionQueues, previousInstance: DeferredActionQueues | null) => void;
private _onEnd: (currentInstance: DeferredActionQueues, nextInstance: DeferredActionQueues | null) => void;
private queueNames: string[];
Expand Down Expand Up @@ -107,6 +161,7 @@ export default class Backburner {
this._boundRunExpiredTimers = this._runExpiredTimers.bind(this);

this._boundAutorunEnd = () => {
autorunsCompletedCount++;
this._autorun = null;
this.end();
};
Expand All @@ -117,6 +172,7 @@ export default class Backburner {
@return instantiated class DeferredActionQueues
*/
public begin(): DeferredActionQueues {
beginCount++;
let options = this.options;
let previousInstance = this.currentInstance;
let current;
Expand All @@ -126,9 +182,12 @@ export default class Backburner {
this._cancelAutorun();
} else {
if (previousInstance !== null) {
nestedDeferredActionQueuesCreated++;
this.instanceStack.push(previousInstance);
}
deferredActionQueuesCreatedCount++;
current = this.currentInstance = new DeferredActionQueues(this.queueNames, options);
beginEventCount++;
this._trigger('begin', current, previousInstance);
}

Expand All @@ -138,6 +197,7 @@ export default class Backburner {
}

public end() {
endCount++;
let currentInstance = this.currentInstance;
let nextInstance: DeferredActionQueues | null = null;

Expand All @@ -156,6 +216,7 @@ export default class Backburner {
finallyAlreadyCalled = true;

if (result === QUEUE_STATE.Pause) {
autorunsCreatedCount++;
const next = this._platform.next;
this._autorun = next(this._boundAutorunEnd);
} else {
Expand All @@ -165,6 +226,7 @@ export default class Backburner {
nextInstance = this.instanceStack.pop() as DeferredActionQueues;
this.currentInstance = nextInstance;
}
endEventCount++;
this._trigger('end', currentInstance, nextInstance);
this._onEnd(currentInstance, nextInstance);
}
Expand Down Expand Up @@ -208,6 +270,7 @@ export default class Backburner {
public run(target: Function | any | null, method?: Function | string, ...args);
public run(target: any | null | undefined, method?: Function, ...args: any[]);
public run() {
runCount++;
let [target, method, args] = parseArgs(...arguments);
return this._run(target, method, args);
}
Expand All @@ -230,6 +293,7 @@ export default class Backburner {
public join(target: Function | any | null, method?: Function | string, ...args);
public join(target: any | null | undefined, method?: Function, ...args: any[]);
public join() {
joinCount++;
let [target, method, args] = parseArgs(...arguments);
return this._join(target, method, args);
}
Expand All @@ -238,6 +302,7 @@ export default class Backburner {
* @deprecated please use schedule instead.
*/
public defer(queueName, targetOrMethod, ..._args) {
deferCount++;
return this.schedule(queueName, targetOrMethod, ..._args);
}

Expand All @@ -248,6 +313,7 @@ export default class Backburner {
public schedule<T, U extends keyof T>(queueName: string, target: T, method: U, ...args);
public schedule(queueName: string, target: any, method: any | Function, ...args);
public schedule(queueName, ..._args) {
scheduleCount++;
let [target, method, args] = parseArgs(..._args);
let stack = this.DEBUG ? new Error() : undefined;
return this._ensureInstance().schedule(queueName, target, method, args, false, stack);
Expand All @@ -262,6 +328,7 @@ export default class Backburner {
@return method result
*/
public scheduleIterable(queueName: string, iterable: () => Iteratable) {
scheduleIterableCount++;
let stack = this.DEBUG ? new Error() : undefined;
return this._ensureInstance().schedule(queueName, null, iteratorDrain, [iterable], false, stack);
}
Expand All @@ -270,6 +337,7 @@ export default class Backburner {
* @deprecated please use scheduleOnce instead.
*/
public deferOnce(queueName, targetOrMethod, ...args) {
deferOnceCount++;
return this.scheduleOnce(queueName, targetOrMethod, ...args);
}

Expand All @@ -280,6 +348,7 @@ export default class Backburner {
public scheduleOnce<T, U extends keyof T>(queueName: string, target: T, method: U, ...args);
public scheduleOnce(queueName: string, target: any | null, method: any | Function, ...args);
public scheduleOnce(queueName, ..._args) {
scheduleOnceCount++;
let [target, method, args] = parseArgs(..._args);
let stack = this.DEBUG ? new Error() : undefined;
return this._ensureInstance().schedule(queueName, target, method, args, true, stack);
Expand All @@ -290,10 +359,12 @@ export default class Backburner {
*/
public setTimeout(...args);
public setTimeout() {
setTimeoutCount++;
return this.later(...arguments);
}

public later(...args) {
laterCount++;
let length = args.length;

let wait = 0;
Expand Down Expand Up @@ -347,6 +418,7 @@ export default class Backburner {
public throttle<A, B>(method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait?: number | string, immediate?: boolean): Timer;
public throttle<A, B, C>(method: (arg1: A, arg2: B, arg3: C) => void, arg1: A, arg2: B, arg3: C, wait?: number | string, immediate?: boolean): Timer;
public throttle(targetOrThisArgOrMethod: object | Function, ...args): Timer {
throttleCount++;
let target;
let method;
let immediate;
Expand Down Expand Up @@ -423,6 +495,7 @@ export default class Backburner {
public debounce<A, B>(method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait: number | string, immediate?: boolean): Timer;
public debounce<A, B, C>(method: (arg1: A, arg2: B, arg3: C) => void, arg1: A, arg2: B, arg3: C, wait: number | string, immediate?: boolean): Timer;
public debounce(targetOrThisArgOrMethod: object | Function, ...args): Timer {
debounceCount++;
let target;
let method;
let immediate;
Expand Down Expand Up @@ -485,6 +558,7 @@ export default class Backburner {
}

public cancelTimers() {
cancelTimersCount++;
for (let i = 3; i < this._throttlers.length; i += 4) {
this._platform.clearTimeout(this._throttlers[i]);
}
Expand All @@ -509,9 +583,11 @@ export default class Backburner {
}

public cancel(timer?) {
cancelCount++;

if (timer === undefined || timer === null) { return false; }

let timerType = typeof timer;

if (timerType === 'number') { // we're cancelling a throttle or debounce
return this._cancelItem(timer, this._throttlers) || this._cancelItem(timer, this._debouncees);
} else if (timerType === 'string') { // we're cancelling a setTimeout
Expand Down Expand Up @@ -688,6 +764,7 @@ export default class Backburner {
private _ensureInstance(): DeferredActionQueues {
let currentInstance = this.currentInstance;
if (currentInstance === null) {
autorunsCreatedCount++;
currentInstance = this.begin();
const next = this._platform.next;
this._autorun = next(this._boundAutorunEnd);
Expand Down