Skip to content
Merged
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
17 changes: 6 additions & 11 deletions src/common/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,18 @@ export interface IEventEmitter<T, U = void> {
}

export class EventEmitter<T, U = void> implements IEventEmitter<T, U> {
private _listeners: IListener<T, U>[] = [];
private _listeners: Set<IListener<T, U>> = new Set();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tyriar Just a remark, I think this could theoretically introduce a bug: if you add the same listener function more than once. The Set will filter out duplicate references, so registering the same listener callback function twice will now only fire the listener once. I don't think this will be a problem in practice because listener callbacks are most likely passed as anonymous callback functions anyway, but yeah. Just saying :D

private _event?: IEvent<T, U>;
private _disposed: boolean = false;

public get event(): IEvent<T, U> {
if (!this._event) {
this._event = (listener: (arg1: T, arg2: U) => any) => {
this._listeners.push(listener);
this._listeners.add(listener);
const disposable = {
dispose: () => {
if (!this._disposed) {
for (let i = 0; i < this._listeners.length; i++) {
if (this._listeners[i] === listener) {
this._listeners.splice(i, 1);
return;
}
}
this._listeners.delete(listener);
}
}
};
Expand All @@ -48,8 +43,8 @@ export class EventEmitter<T, U = void> implements IEventEmitter<T, U> {

public fire(arg1: T, arg2: U): void {
const queue: IListener<T, U>[] = [];
for (let i = 0; i < this._listeners.length; i++) {
queue.push(this._listeners[i]);
for (const l of this._listeners.values()) {
queue.push(l);
}
for (let i = 0; i < queue.length; i++) {
queue[i].call(undefined, arg1, arg2);
Expand All @@ -63,7 +58,7 @@ export class EventEmitter<T, U = void> implements IEventEmitter<T, U> {

public clearListeners(): void {
if (this._listeners) {
this._listeners.length = 0;
this._listeners.clear();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/buffer/Buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,8 @@ export class Buffer implements IBuffer {
this._isClearing = true;
for (let i = 0; i < this.markers.length; i++) {
this.markers[i].dispose();
this.markers.splice(i--, 1);
}
this.markers.length = 0;
this._isClearing = false;
}

Expand Down