-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #283 from Microsoft/282_events
New events API
- Loading branch information
Showing
9 changed files
with
143 additions
and
20 deletions.
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 |
---|---|---|
@@ -1,34 +1,27 @@ | ||
var os = require('os'); | ||
var pty = require('../..'); | ||
|
||
var shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash'; | ||
var isWindows = os.platform() === 'win32'; | ||
var shell = isWindows ? 'powershell.exe' : 'bash'; | ||
|
||
var ptyProcess = pty.spawn(shell, [], { | ||
name: 'xterm-256color', | ||
cols: 80, | ||
rows: 26, | ||
cwd: os.platform() === 'win32' ? process.env.USERPROFILE : process.env.HOME, | ||
env: Object.assign({ TEST: "abc" }, process.env), | ||
cwd: isWindows ? process.env.USERPROFILE : process.env.HOME, | ||
env: Object.assign({ TEST: "Environment vars work" }, process.env), | ||
experimentalUseConpty: true | ||
}); | ||
|
||
ptyProcess.on('data', function(data) { | ||
// console.log(data); | ||
process.stdout.write(data); | ||
}); | ||
ptyProcess.onData(data => process.stdout.write(data)); | ||
|
||
ptyProcess.write('dir\r'); | ||
// ptyProcess.write('ls\r'); | ||
ptyProcess.write(isWindows ? 'dir\r' : 'ls\r'); | ||
|
||
setTimeout(() => { | ||
ptyProcess.resize(30, 19); | ||
ptyProcess.write(shell === 'powershell.exe' ? '$Env:TEST\r' : 'echo %TEST%\r'); | ||
ptyProcess.write(isWindows ? '$Env:TEST\r' : 'echo $TEST\r'); | ||
}, 2000); | ||
|
||
process.on('exit', () => { | ||
ptyProcess.kill(); | ||
}); | ||
process.on('exit', () => ptyProcess.kill()); | ||
|
||
setTimeout(() => { | ||
process.exit(); | ||
}, 4000); | ||
setTimeout(() => process.exit(), 4000); |
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
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