diff --git a/lib/index.ts b/lib/index.ts index 3c806839..d5e51c00 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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; @@ -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[]; @@ -107,6 +161,7 @@ export default class Backburner { this._boundRunExpiredTimers = this._runExpiredTimers.bind(this); this._boundAutorunEnd = () => { + autorunsCompletedCount++; this._autorun = null; this.end(); }; @@ -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; @@ -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); } @@ -138,6 +197,7 @@ export default class Backburner { } public end() { + endCount++; let currentInstance = this.currentInstance; let nextInstance: DeferredActionQueues | null = null; @@ -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 { @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -248,6 +313,7 @@ export default class Backburner { public schedule(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); @@ -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); } @@ -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); } @@ -280,6 +348,7 @@ export default class Backburner { public scheduleOnce(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); @@ -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; @@ -347,6 +418,7 @@ export default class Backburner { public throttle(method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait?: number | string, immediate?: boolean): Timer; public throttle(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; @@ -423,6 +495,7 @@ export default class Backburner { public debounce(method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait: number | string, immediate?: boolean): Timer; public debounce(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; @@ -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]); } @@ -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 @@ -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);