From a50987fdacffd370eb5f3d103b68ca4b04210227 Mon Sep 17 00:00:00 2001 From: bekzod Date: Tue, 23 Jan 2018 11:45:58 +0500 Subject: [PATCH] move event manager to separate class --- lib/backburner/evented.ts | 65 +++++++++++++++++++++++++++++++++ lib/index.ts | 76 +++++---------------------------------- 2 files changed, 73 insertions(+), 68 deletions(-) create mode 100644 lib/backburner/evented.ts diff --git a/lib/backburner/evented.ts b/lib/backburner/evented.ts new file mode 100644 index 00000000..7b18dd0c --- /dev/null +++ b/lib/backburner/evented.ts @@ -0,0 +1,65 @@ + +export default class Evented { + + private _eventCallbacks: { + end: Function[]; + begin: Function[]; + } = { + end: [], + begin: [] + }; + + public on(eventName, callback) { + if (typeof callback !== 'function') { + throw new TypeError(`Callback must be a function`); + } + let callbacks = this._eventCallbacks[eventName]; + if (callbacks !== undefined) { + callbacks.push(callback); + } else { + throw new TypeError(`Cannot on() event ${eventName} because it does not exist`); + } + } + + public off(eventName, callback) { + let callbacks = this._eventCallbacks[eventName]; + if (!eventName || callbacks === undefined) { + throw new TypeError(`Cannot off() event ${eventName} because it does not exist`); + } + let callbackFound = false; + if (callback) { + for (let i = 0; i < callbacks.length; i++) { + if (callbacks[i] === callback) { + callbackFound = true; + callbacks.splice(i, 1); + i--; + } + } + } + if (!callbackFound) { + throw new TypeError(`Cannot off() callback that does not exist`); + } + } + + /** + Trigger an event. Supports up to two arguments. Designed around + triggering transition events from one run loop instance to the + next, which requires an argument for the first instance and then + an argument for the next instance. + + @protected + @method _trigger + @param {String} eventName + @param {any} arg1 + @param {any} arg2 + */ + protected _trigger(eventName: string, arg1: T, arg2: U) { + let callbacks = this._eventCallbacks[eventName]; + if (callbacks !== undefined) { + for (let i = 0; i < callbacks.length; i++) { + callbacks[i](arg1, arg2); + } + } + } + +} diff --git a/lib/index.ts b/lib/index.ts index 3c806839..ff6b09a2 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,3 +1,8 @@ +import searchTimer from './backburner/binary-search'; +import DeferredActionQueues from './backburner/deferred-action-queues'; +import Evented from './backburner/evented'; +import iteratorDrain, { Iteratable } from './backburner/iterator-drain'; +import Queue, { QUEUE_STATE } from './backburner/queue'; import { findItem, findTimer, @@ -5,12 +10,6 @@ import { isCoercableNumber } from './backburner/utils'; -import searchTimer from './backburner/binary-search'; -import DeferredActionQueues from './backburner/deferred-action-queues'; -import iteratorDrain, { Iteratable } from './backburner/iterator-drain'; - -import Queue, { QUEUE_STATE } from './backburner/queue'; - type Timer = any; const noop = function() {}; @@ -45,7 +44,7 @@ function parseArgs() { let UUID = 0; -export default class Backburner { +export default class Backburner extends Evented { public static Queue = Queue; public DEBUG = false; @@ -60,13 +59,6 @@ export default class Backburner { private instanceStack: DeferredActionQueues[] = []; private _debouncees: any[] = []; private _throttlers: any[] = []; - private _eventCallbacks: { - end: Function[]; - begin: Function[]; - } = { - end: [], - begin: [] - }; private _timerTimeoutId: number | null = null; private _timers: any[] = []; @@ -83,7 +75,8 @@ export default class Backburner { private _autorun: number | null = null; private _boundAutorunEnd: () => void; - constructor(queueNames: string[], options: any = {} ) { + constructor(queueNames: string[], options: any = {}) { + super(); this.queueNames = queueNames; this.options = options; if (!this.options.defaultQueue) { @@ -172,38 +165,6 @@ export default class Backburner { } } - public on(eventName, callback) { - if (typeof callback !== 'function') { - throw new TypeError(`Callback must be a function`); - } - let callbacks = this._eventCallbacks[eventName]; - if (callbacks !== undefined) { - callbacks.push(callback); - } else { - throw new TypeError(`Cannot on() event ${eventName} because it does not exist`); - } - } - - public off(eventName, callback) { - let callbacks = this._eventCallbacks[eventName]; - if (!eventName || callbacks === undefined) { - throw new TypeError(`Cannot off() event ${eventName} because it does not exist`); - } - let callbackFound = false; - if (callback) { - for (let i = 0; i < callbacks.length; i++) { - if (callbacks[i] === callback) { - callbackFound = true; - callbacks.splice(i, 1); - i--; - } - } - } - if (!callbackFound) { - throw new TypeError(`Cannot off() callback that does not exist`); - } - } - public run(target: Function); public run(target: Function | any | null, method?: Function | string, ...args); public run(target: any | null | undefined, method?: Function, ...args: any[]); @@ -614,27 +575,6 @@ export default class Backburner { return false; } - /** - Trigger an event. Supports up to two arguments. Designed around - triggering transition events from one run loop instance to the - next, which requires an argument for the first instance and then - an argument for the next instance. - - @private - @method _trigger - @param {String} eventName - @param {any} arg1 - @param {any} arg2 - */ - private _trigger(eventName: string, arg1: T, arg2: U) { - let callbacks = this._eventCallbacks[eventName]; - if (callbacks !== undefined) { - for (let i = 0; i < callbacks.length; i++) { - callbacks[i](arg1, arg2); - } - } - } - private _runExpiredTimers() { this._timerTimeoutId = null; if (this._timers.length > 0) {