Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move event manager to separate class #322

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
65 changes: 65 additions & 0 deletions lib/backburner/evented.ts
Original file line number Diff line number Diff line change
@@ -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<T, U>(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);
}
}
}

}
76 changes: 8 additions & 68 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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,
getOnError,
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() {};
Expand Down Expand Up @@ -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;
Expand All @@ -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[] = [];
Expand All @@ -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) {
Expand Down Expand Up @@ -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[]);
Expand Down Expand Up @@ -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<T, U>(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) {
Expand Down