Skip to content

Commit b3f37a4

Browse files
committed
move event manager to separate file
1 parent 49eb179 commit b3f37a4

File tree

2 files changed

+73
-68
lines changed

2 files changed

+73
-68
lines changed

lib/backburner/evented.ts

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
export default class Evented {
3+
4+
private _eventCallbacks: {
5+
end: Function[];
6+
begin: Function[];
7+
} = {
8+
end: [],
9+
begin: []
10+
};
11+
12+
public on(eventName, callback) {
13+
if (typeof callback !== 'function') {
14+
throw new TypeError(`Callback must be a function`);
15+
}
16+
let callbacks = this._eventCallbacks[eventName];
17+
if (callbacks !== undefined) {
18+
callbacks.push(callback);
19+
} else {
20+
throw new TypeError(`Cannot on() event ${eventName} because it does not exist`);
21+
}
22+
}
23+
24+
public off(eventName, callback) {
25+
let callbacks = this._eventCallbacks[eventName];
26+
if (!eventName || callbacks === undefined) {
27+
throw new TypeError(`Cannot off() event ${eventName} because it does not exist`);
28+
}
29+
let callbackFound = false;
30+
if (callback) {
31+
for (let i = 0; i < callbacks.length; i++) {
32+
if (callbacks[i] === callback) {
33+
callbackFound = true;
34+
callbacks.splice(i, 1);
35+
i--;
36+
}
37+
}
38+
}
39+
if (!callbackFound) {
40+
throw new TypeError(`Cannot off() callback that does not exist`);
41+
}
42+
}
43+
44+
/**
45+
Trigger an event. Supports up to two arguments. Designed around
46+
triggering transition events from one run loop instance to the
47+
next, which requires an argument for the first instance and then
48+
an argument for the next instance.
49+
50+
@protected
51+
@method _trigger
52+
@param {String} eventName
53+
@param {any} arg1
54+
@param {any} arg2
55+
*/
56+
protected _trigger<T, U>(eventName: string, arg1: T, arg2: U) {
57+
let callbacks = this._eventCallbacks[eventName];
58+
if (callbacks !== undefined) {
59+
for (let i = 0; i < callbacks.length; i++) {
60+
callbacks[i](arg1, arg2);
61+
}
62+
}
63+
}
64+
65+
}

lib/index.ts

+8-68
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1+
import searchTimer from './backburner/binary-search';
2+
import DeferredActionQueues from './backburner/deferred-action-queues';
3+
import Evented from './backburner/evented';
4+
import iteratorDrain, { Iteratable } from './backburner/iterator-drain';
5+
import Queue, { QUEUE_STATE } from './backburner/queue';
16
import {
27
findItem,
38
findTimer,
49
getOnError,
510
isCoercableNumber
611
} from './backburner/utils';
712

8-
import searchTimer from './backburner/binary-search';
9-
import DeferredActionQueues from './backburner/deferred-action-queues';
10-
import iteratorDrain, { Iteratable } from './backburner/iterator-drain';
11-
12-
import Queue, { QUEUE_STATE } from './backburner/queue';
13-
1413
type Timer = any;
1514

1615
const noop = function() {};
@@ -45,7 +44,7 @@ function parseArgs() {
4544

4645
let UUID = 0;
4746

48-
export default class Backburner {
47+
export default class Backburner extends Evented {
4948
public static Queue = Queue;
5049

5150
public DEBUG = false;
@@ -60,13 +59,6 @@ export default class Backburner {
6059
private instanceStack: DeferredActionQueues[] = [];
6160
private _debouncees: any[] = [];
6261
private _throttlers: any[] = [];
63-
private _eventCallbacks: {
64-
end: Function[];
65-
begin: Function[];
66-
} = {
67-
end: [],
68-
begin: []
69-
};
7062

7163
private _timerTimeoutId: number | null = null;
7264
private _timers: any[] = [];
@@ -83,7 +75,8 @@ export default class Backburner {
8375
private _autorun: number | null = null;
8476
private _boundAutorunEnd: () => void;
8577

86-
constructor(queueNames: string[], options: any = {} ) {
78+
constructor(queueNames: string[], options: any = {}) {
79+
super();
8780
this.queueNames = queueNames;
8881
this.options = options;
8982
if (!this.options.defaultQueue) {
@@ -172,38 +165,6 @@ export default class Backburner {
172165
}
173166
}
174167

175-
public on(eventName, callback) {
176-
if (typeof callback !== 'function') {
177-
throw new TypeError(`Callback must be a function`);
178-
}
179-
let callbacks = this._eventCallbacks[eventName];
180-
if (callbacks !== undefined) {
181-
callbacks.push(callback);
182-
} else {
183-
throw new TypeError(`Cannot on() event ${eventName} because it does not exist`);
184-
}
185-
}
186-
187-
public off(eventName, callback) {
188-
let callbacks = this._eventCallbacks[eventName];
189-
if (!eventName || callbacks === undefined) {
190-
throw new TypeError(`Cannot off() event ${eventName} because it does not exist`);
191-
}
192-
let callbackFound = false;
193-
if (callback) {
194-
for (let i = 0; i < callbacks.length; i++) {
195-
if (callbacks[i] === callback) {
196-
callbackFound = true;
197-
callbacks.splice(i, 1);
198-
i--;
199-
}
200-
}
201-
}
202-
if (!callbackFound) {
203-
throw new TypeError(`Cannot off() callback that does not exist`);
204-
}
205-
}
206-
207168
public run(target: Function);
208169
public run(target: Function | any | null, method?: Function | string, ...args);
209170
public run(target: any | null | undefined, method?: Function, ...args: any[]);
@@ -614,27 +575,6 @@ export default class Backburner {
614575
return false;
615576
}
616577

617-
/**
618-
Trigger an event. Supports up to two arguments. Designed around
619-
triggering transition events from one run loop instance to the
620-
next, which requires an argument for the first instance and then
621-
an argument for the next instance.
622-
623-
@private
624-
@method _trigger
625-
@param {String} eventName
626-
@param {any} arg1
627-
@param {any} arg2
628-
*/
629-
private _trigger<T, U>(eventName: string, arg1: T, arg2: U) {
630-
let callbacks = this._eventCallbacks[eventName];
631-
if (callbacks !== undefined) {
632-
for (let i = 0; i < callbacks.length; i++) {
633-
callbacks[i](arg1, arg2);
634-
}
635-
}
636-
}
637-
638578
private _runExpiredTimers() {
639579
this._timerTimeoutId = null;
640580
if (this._timers.length > 0) {

0 commit comments

Comments
 (0)