forked from microsoft/node-pty
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of microsoft#282
- Loading branch information
Showing
7 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Copyright (c) 2019, Microsoft Corporation (MIT License). | ||
*/ | ||
|
||
import * as assert from 'assert'; | ||
import { EventEmitter2 } from './eventEmitter2'; | ||
|
||
describe('EventEmitter2', () => { | ||
it('should fire listeners multiple times', () => { | ||
const order: string[] = []; | ||
const emitter = new EventEmitter2<number>(); | ||
emitter.event(data => order.push(data + 'a')); | ||
emitter.event(data => order.push(data + 'b')); | ||
emitter.fire(1); | ||
emitter.fire(2); | ||
assert.deepEqual(order, [ '1a', '1b', '2a', '2b' ]); | ||
}); | ||
|
||
it('should not fire listeners once disposed', () => { | ||
const order: string[] = []; | ||
const emitter = new EventEmitter2<number>(); | ||
emitter.event(data => order.push(data + 'a')); | ||
const disposeB = emitter.event(data => order.push(data + 'b')); | ||
emitter.event(data => order.push(data + 'c')); | ||
emitter.fire(1); | ||
disposeB.dispose(); | ||
emitter.fire(2); | ||
assert.deepEqual(order, [ '1a', '1b', '1c', '2a', '2c' ]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright (c) 2019, Microsoft Corporation (MIT License). | ||
*/ | ||
|
||
import { IDisposable } from './types'; | ||
|
||
interface IListener<T> { | ||
(e: T): void; | ||
} | ||
|
||
export interface IEvent<T> { | ||
(listener: (e: T) => any): IDisposable; | ||
} | ||
|
||
export class EventEmitter2<T> { | ||
private _listeners: IListener<T>[] = []; | ||
private _event?: IEvent<T>; | ||
|
||
public get event(): IEvent<T> { | ||
if (!this._event) { | ||
this._event = (listener: (e: T) => any) => { | ||
this._listeners.push(listener); | ||
const disposable = { | ||
dispose: () => { | ||
for (let i = 0; i < this._listeners.length; i++) { | ||
if (this._listeners[i] === listener) { | ||
this._listeners.splice(i, 1); | ||
return; | ||
} | ||
} | ||
} | ||
}; | ||
return disposable; | ||
}; | ||
} | ||
return this._event; | ||
} | ||
|
||
public fire(data: T): void { | ||
const queue: IListener<T>[] = []; | ||
for (let i = 0; i < this._listeners.length; i++) { | ||
queue.push(this._listeners[i]); | ||
} | ||
for (let i = 0; i < queue.length; i++) { | ||
queue[i].call(undefined, data); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters