mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Cleanup <EventEmitter> (#3338)
This commit is contained in:
parent
2ea589a599
commit
14b70a6c53
102
src/common/__tests__/event-emitter.test.ts
Normal file
102
src/common/__tests__/event-emitter.test.ts
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2021 OpenLens Authors
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
* this software and associated documentation files (the "Software"), to deal in
|
||||||
|
* the Software without restriction, including without limitation the rights to
|
||||||
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||||
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
* subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { EventEmitter } from "../event-emitter";
|
||||||
|
|
||||||
|
describe("EventEmitter", () => {
|
||||||
|
it("should stop early if a listener returns false", () => {
|
||||||
|
let called = false;
|
||||||
|
const e = new EventEmitter<[]>();
|
||||||
|
|
||||||
|
e.addListener(() => false, {});
|
||||||
|
e.addListener(() => { called = true; }, {});
|
||||||
|
e.emit();
|
||||||
|
|
||||||
|
expect(called).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shouldn't stop early if a listener returns 0", () => {
|
||||||
|
let called = false;
|
||||||
|
const e = new EventEmitter<[]>();
|
||||||
|
|
||||||
|
e.addListener(() => 0 as any, {});
|
||||||
|
e.addListener(() => { called = true; }, {});
|
||||||
|
e.emit();
|
||||||
|
|
||||||
|
expect(called).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("prepended listeners should be called before others", () => {
|
||||||
|
const callOrder: number[] = [];
|
||||||
|
const e = new EventEmitter<[]>();
|
||||||
|
|
||||||
|
e.addListener(() => { callOrder.push(1); }, {});
|
||||||
|
e.addListener(() => { callOrder.push(2); }, {});
|
||||||
|
e.addListener(() => { callOrder.push(3); }, { prepend: true });
|
||||||
|
e.emit();
|
||||||
|
|
||||||
|
expect(callOrder).toStrictEqual([3, 1, 2]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("once listeners should be called only once", () => {
|
||||||
|
const callOrder: number[] = [];
|
||||||
|
const e = new EventEmitter<[]>();
|
||||||
|
|
||||||
|
e.addListener(() => { callOrder.push(1); }, {});
|
||||||
|
e.addListener(() => { callOrder.push(2); }, {});
|
||||||
|
e.addListener(() => { callOrder.push(3); }, { once: true });
|
||||||
|
e.emit();
|
||||||
|
e.emit();
|
||||||
|
|
||||||
|
expect(callOrder).toStrictEqual([1, 2, 3, 1, 2]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removeListener should stop the listener from being called", () => {
|
||||||
|
const callOrder: number[] = [];
|
||||||
|
const e = new EventEmitter<[]>();
|
||||||
|
const r = () => { callOrder.push(3); };
|
||||||
|
|
||||||
|
e.addListener(() => { callOrder.push(1); }, {});
|
||||||
|
e.addListener(() => { callOrder.push(2); }, {});
|
||||||
|
e.addListener(r);
|
||||||
|
|
||||||
|
e.emit();
|
||||||
|
e.removeListener(r);
|
||||||
|
e.emit();
|
||||||
|
|
||||||
|
expect(callOrder).toStrictEqual([1, 2, 3, 1, 2]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removeAllListeners should stop the all listeners from being called", () => {
|
||||||
|
const callOrder: number[] = [];
|
||||||
|
const e = new EventEmitter<[]>();
|
||||||
|
|
||||||
|
e.addListener(() => { callOrder.push(1); });
|
||||||
|
e.addListener(() => { callOrder.push(2); });
|
||||||
|
e.addListener(() => { callOrder.push(3); });
|
||||||
|
|
||||||
|
e.emit();
|
||||||
|
e.removeAllListeners();
|
||||||
|
e.emit();
|
||||||
|
|
||||||
|
expect(callOrder).toStrictEqual([1, 2, 3]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -29,35 +29,31 @@ interface Options {
|
|||||||
type Callback<D extends [...any[]]> = (...data: D) => void | boolean;
|
type Callback<D extends [...any[]]> = (...data: D) => void | boolean;
|
||||||
|
|
||||||
export class EventEmitter<D extends [...any[]]> {
|
export class EventEmitter<D extends [...any[]]> {
|
||||||
protected listeners = new Map<Callback<D>, Options>();
|
protected listeners: [Callback<D>, Options][] = [];
|
||||||
|
|
||||||
addListener(callback: Callback<D>, options: Options = {}) {
|
addListener(callback: Callback<D>, options: Options = {}) {
|
||||||
if (options.prepend) {
|
const fn = options.prepend ? "unshift" : "push";
|
||||||
const listeners = [...this.listeners];
|
|
||||||
|
|
||||||
listeners.unshift([callback, options]);
|
this.listeners[fn]([callback, options]);
|
||||||
this.listeners = new Map(listeners);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this.listeners.set(callback, options);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
removeListener(callback: Callback<D>) {
|
removeListener(callback: Callback<D>) {
|
||||||
this.listeners.delete(callback);
|
this.listeners = this.listeners.filter(([cb]) => cb !== callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
removeAllListeners() {
|
removeAllListeners() {
|
||||||
this.listeners.clear();
|
this.listeners.length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
emit(...data: D) {
|
emit(...data: D) {
|
||||||
[...this.listeners].every(([callback, options]) => {
|
for (const [callback, { once }] of this.listeners) {
|
||||||
if (options.once) {
|
if (once) {
|
||||||
this.removeListener(callback);
|
this.removeListener(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
return callback(...data) !== false;
|
if (callback(...data) === false) {
|
||||||
});
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user