optimize Event.fire
This commit is contained in:
+10
-15
@@ -27,11 +27,13 @@ export class Emitter<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const entry = { fn: listener, thisArgs };
|
const entry = { fn: listener, thisArgs };
|
||||||
|
this._listeners = this._listeners.slice();
|
||||||
this._listeners.push(entry);
|
this._listeners.push(entry);
|
||||||
|
|
||||||
const result = toDisposable(() => {
|
const result = toDisposable(() => {
|
||||||
const idx = this._listeners.indexOf(entry);
|
const idx = this._listeners.indexOf(entry);
|
||||||
if (idx !== -1) {
|
if (idx !== -1) {
|
||||||
|
this._listeners = this._listeners.slice();
|
||||||
this._listeners.splice(idx, 1);
|
this._listeners.splice(idx, 1);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -50,23 +52,16 @@ export class Emitter<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public fire(event: T): void {
|
public fire(event: T): void {
|
||||||
if (this._disposed) {
|
if (this._disposed || !this._listeners.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch (this._listeners.length) {
|
if (this._listeners.length === 1) {
|
||||||
case 0: return;
|
this._listeners[0].fn.call(this._listeners[0].thisArgs, event);
|
||||||
case 1: {
|
return;
|
||||||
const { fn, thisArgs } = this._listeners[0];
|
}
|
||||||
fn.call(thisArgs, event);
|
const listeners = this._listeners;
|
||||||
return;
|
for (let i = 0, len = listeners.length; i < len; ++i) {
|
||||||
}
|
listeners[i].fn.call(listeners[i].thisArgs, event);
|
||||||
default: {
|
|
||||||
// Snapshot listeners to allow modifications during iteration (2+ listeners)
|
|
||||||
const listeners = this._listeners.slice();
|
|
||||||
for (const { fn, thisArgs } of listeners) {
|
|
||||||
fn.call(thisArgs, event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user