Skip to content

Commit

Permalink
Remove old event emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyriar committed Jun 9, 2019
1 parent c5c3708 commit 447fc4f
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 496 deletions.
6 changes: 3 additions & 3 deletions src/AccessibilityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ export class AccessibilityManager extends Disposable {
this.register(this._terminal.onRender(e => this._refreshRows(e.start, e.end)));
this.register(this._terminal.onScroll(() => this._refreshRows()));
// Line feed is an issue as the prompt won't be read out after a command is run
this.register(this._terminal.addDisposableListener('a11y.char', (char) => this._onChar(char)));
this.register(this._terminal.onA11yChar(char => this._onChar(char)));
this.register(this._terminal.onLineFeed(() => this._onChar('\n')));
this.register(this._terminal.addDisposableListener('a11y.tab', spaceCount => this._onTab(spaceCount)));
this.register(this._terminal.onA11yTab(spaceCount => this._onTab(spaceCount)));
this.register(this._terminal.onKey(e => this._onKey(e.key)));
this.register(this._terminal.addDisposableListener('blur', () => this._clearLiveRegion()));
this.register(this._terminal.onBlur(() => this._clearLiveRegion()));

this._screenDprMonitor = new ScreenDprMonitor();
this.register(this._screenDprMonitor);
Expand Down
1 change: 0 additions & 1 deletion src/Clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export function pasteHandler(ev: ClipboardEvent, term: ITerminal): void {
text = bracketTextForPaste(text, term.bracketedPasteMode);
term.handler(text);
term.textarea.value = '';
term.emit('paste', text);
term.cancel(ev);
};

Expand Down
6 changes: 3 additions & 3 deletions src/InputHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ export class InputHandler extends Disposable implements IInputHandler {

buffer = this._terminal.buffer;
if (buffer.x !== cursorStartX || buffer.y !== cursorStartY) {
this._terminal.emit('cursormove');
this._onCursorMove.fire();
}
}

Expand Down Expand Up @@ -377,7 +377,7 @@ export class InputHandler extends Disposable implements IInputHandler {
}

if (screenReaderMode) {
this._terminal.emit('a11y.char', stringFromCodePoint(code));
this._terminal.onA11yCharEmitter.fire(stringFromCodePoint(code));
}

// insert combining char at last cursor position
Expand Down Expand Up @@ -542,7 +542,7 @@ export class InputHandler extends Disposable implements IInputHandler {
const originalX = this._terminal.buffer.x;
this._terminal.buffer.x = this._terminal.buffer.nextStop();
if (this._terminal.options.screenReaderMode) {
this._terminal.emit('a11y.tab', this._terminal.buffer.x - originalX);
this._terminal.onA11yTabEmitter.fire(this._terminal.buffer.x - originalX);
}
}

Expand Down
176 changes: 12 additions & 164 deletions src/Terminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ describe('Terminal', () => {
term.handler('fake');
});
it('should fire the onCursorMove event', (done) => {
term.on('cursormove', () => done());
term.onCursorMove(() => done());
term.write('foo');
});
it('should fire the onLineFeed event', (done) => {
term.on('linefeed', () => done());
term.onLineFeed(() => done());
term.write('\n');
});
it('should fire a scroll event when scrollback is created', (done) => {
term.on('scroll', () => done());
term.onScroll(() => done());
term.write('\n'.repeat(INIT_ROWS));
});
it('should fire a scroll event when scrollback is cleared', (done) => {
term.write('\n'.repeat(INIT_ROWS));
term.on('scroll', () => done());
term.onScroll(() => done());
term.clear();
});
it('should fire a key event after a keypress DOM event', (done) => {
Expand Down Expand Up @@ -126,158 +126,6 @@ describe('Terminal', () => {
});
});

describe('on', () => {
beforeEach(() => {
term.on('key', () => { });
term.on('keypress', () => { });
term.on('keydown', () => { });
});

describe('data', () => {
it('should emit a data event', (done) => {
term.on('data', () => {
done();
});

term.handler('fake');
});
});

describe('cursormove', () => {
it('should emit a cursormove event', (done) => {
term.on('cursormove', () => {
done();
});
term.write('foo');
});
});

describe('linefeed', () => {
it('should emit a linefeed event', (done) => {
term.on('linefeed', () => {
done();
});
term.write('\n');
});
});

describe('scroll', () => {
it('should emit a scroll event when scrollback is created', (done) => {
term.on('scroll', () => {
done();
});
term.write('\n'.repeat(INIT_ROWS));
});
it('should emit a scroll event when scrollback is cleared', (done) => {
term.write('\n'.repeat(INIT_ROWS));
term.on('scroll', () => {
done();
});
term.clear();
});
});

describe(`keypress (including 'key' event)`, () => {
it('should receive a string and event object', (done) => {
let steps = 0;

const finish = () => {
if ((++steps) === 2) {
done();
}
};

const evKeyPress = <KeyboardEvent>{
preventDefault: () => { },
stopPropagation: () => { },
type: 'keypress',
keyCode: 13
};

term.on('keypress', (key, event) => {
assert.equal(typeof key, 'string');
expect(event).to.be.an.instanceof(Object);
finish();
});

term.on('key', (key, event) => {
assert.equal(typeof key, 'string');
expect(event).to.be.an.instanceof(Object);
finish();
});

term.keyPress(evKeyPress);
});
});

describe(`keydown (including 'key' event)`, () => {
it(`should receive an event object for 'keydown' and a string and event object for 'key'`, (done) => {
let steps = 0;

const finish = () => {
if ((++steps) === 2) {
done();
}
};

const evKeyDown = <KeyboardEvent>{
preventDefault: () => { },
stopPropagation: () => { },
type: 'keydown',
keyCode: 13
};

term.on('keydown', (event) => {
expect(event).to.be.an.instanceof(Object);
finish();
});

term.on('key', (key, event) => {
assert.equal(typeof key, 'string');
expect(event).to.be.an.instanceof(Object);
finish();
});

term.keyDown(evKeyDown);
});
});

describe('resize', () => {
it('should receive an object: {cols: number, rows: number}', (done) => {
term.on('resize', (data) => {
expect(data).to.have.keys(['cols', 'rows']);
assert.equal(typeof data.cols, 'number');
assert.equal(typeof data.rows, 'number');
done();
});

term.resize(1, 1);
});
});

describe('scroll', () => {
it('should receive a number', (done) => {
term.on('scroll', (ydisp) => {
assert.equal(typeof ydisp, 'number');
done();
});

term.scroll();
});
});

describe('title', () => {
it('should receive a string', (done) => {
term.on('title', (title) => {
assert.equal(typeof title, 'string');
done();
});

term.handleTitle('title');
});
});
});

describe('attachCustomKeyEventHandler', () => {
const evKeyDown = <KeyboardEvent>{
preventDefault: () => { },
Expand Down Expand Up @@ -741,10 +589,10 @@ describe('Terminal', () => {
it('should emit key with alt + key on keyPress', (done) => {
const keys = ['@', '@', '\\', '\\', '|', '|'];

term.on('keypress', (key) => {
if (key) {
const index = keys.indexOf(key);
assert(index !== -1, 'Emitted wrong key: ' + key);
term.onKey(e => {
if (e.key) {
const index = keys.indexOf(e.key);
assert(index !== -1, 'Emitted wrong key: ' + e.key);
keys.splice(index, 1);
}
if (keys.length === 0) done();
Expand Down Expand Up @@ -807,10 +655,10 @@ describe('Terminal', () => {
it('should emit key with alt + ctrl + key on keyPress', (done) => {
const keys = ['@', '@', '\\', '\\', '|', '|'];

term.on('keypress', (key) => {
if (key) {
const index = keys.indexOf(key);
assert(index !== -1, 'Emitted wrong key: ' + key);
term.onKey(e => {
if (e.key) {
const index = keys.indexOf(e.key);
assert(index !== -1, 'Emitted wrong key: ' + e.key);
keys.splice(index, 1);
}
if (keys.length === 0) done();
Expand Down
42 changes: 15 additions & 27 deletions src/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { IRenderer, CharacterJoinerHandler } from 'browser/renderer/Types';
import { BufferSet } from 'common/buffer/BufferSet';
import { Buffer } from 'common/buffer/Buffer';
import { CompositionHelper } from './CompositionHelper';
import { EventEmitter } from 'common/EventEmitter';
import { Viewport } from './Viewport';
import { rightClickHandler, moveTextAreaUnderMouseCursor, pasteHandler, copyHandler } from './Clipboard';
import { C0 } from 'common/data/EscapeSequences';
Expand Down Expand Up @@ -56,6 +55,7 @@ import { OptionsService } from 'common/services/OptionsService';
import { ICharSizeService } from 'browser/services/Services';
import { CharSizeService } from 'browser/services/CharSizeService';
import { BufferService, MINIMUM_COLS, MINIMUM_ROWS } from 'common/services/BufferService';
import { Disposable } from 'common/Lifecycle';

// Let it work inside Node.js for automated testing purposes.
const document = (typeof window !== 'undefined') ? window.document : null;
Expand All @@ -76,7 +76,7 @@ const WRITE_BUFFER_PAUSE_THRESHOLD = 5;
const WRITE_TIMEOUT_MS = 12;
const WRITE_BUFFER_LENGTH_THRESHOLD = 50;

export class Terminal extends EventEmitter implements ITerminal, IDisposable, IInputHandlingTerminal {
export class Terminal extends Disposable implements ITerminal, IDisposable, IInputHandlingTerminal {
public textarea: HTMLTextAreaElement;
public element: HTMLElement;
public screenElement: HTMLElement;
Expand Down Expand Up @@ -209,6 +209,15 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
private _onTitleChange = new EventEmitter2<string>();
public get onTitleChange(): IEvent<string> { return this._onTitleChange.event; }

private _onFocus = new EventEmitter2<void>();
public get onFocus(): IEvent<void> { return this._onFocus.event; }
private _onBlur = new EventEmitter2<void>();
public get onBlur(): IEvent<void> { return this._onBlur.event; }
public onA11yCharEmitter = new EventEmitter2<string>();
public get onA11yChar(): IEvent<string> { return this.onA11yCharEmitter.event; }
public onA11yTabEmitter = new EventEmitter2<number>();
public get onA11yTab(): IEvent<number> { return this.onA11yTabEmitter.event; }

/**
* Creates a new `Terminal` object.
*
Expand All @@ -234,18 +243,6 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II

// this.options = clone(options);
this._setup();

// TODO: Remove these in v4
// Fire old style events from new emitters
this.onCursorMove(() => this.emit('cursormove'));
this.onData(e => this.emit('data', e));
this.onKey(e => this.emit('key', e.key, e.domEvent));
this.onLineFeed(() => this.emit('linefeed'));
this.onRender(e => this.emit('refresh', e));
this.onResize(e => this.emit('resize', e));
this.onSelectionChange(() => this.emit('selection'));
this.onScroll(e => this.emit('scroll', e));
this.onTitleChange(e => this.emit('title', e));
}

public dispose(): void {
Expand Down Expand Up @@ -444,7 +441,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
this.updateCursorStyle(ev);
this.element.classList.add('focus');
this.showCursor();
this.emit('focus');
this._onFocus.fire();
}

/**
Expand All @@ -467,7 +464,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
this.handler(C0.ESC + '[O');
}
this.element.classList.remove('focus');
this.emit('blur');
this._onBlur.fire();
}

/**
Expand Down Expand Up @@ -639,8 +636,8 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II

this.register(this.onCursorMove(() => this._renderService.onCursorMove()));
this.register(this.onResize(() => this._renderService.onResize(this.cols, this.rows)));
this.register(this.addDisposableListener('blur', () => this._renderService.onBlur()));
this.register(this.addDisposableListener('focus', () => this._renderService.onFocus()));
this.register(this.onBlur(() => this._renderService.onBlur()));
this.register(this.onFocus(() => this._renderService.onFocus()));
this.register(this._renderService.onDimensionsChange(() => this.viewport.syncScrollArea()));

this.selectionManager = new SelectionManager(this, this._charSizeService, this._bufferService);
Expand Down Expand Up @@ -803,12 +800,6 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
// vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
// locator: CSI P e ; P b ; P r ; P c ; P p & w
function sendEvent(button: number, pos: {x: number, y: number}): void {
// self.emit('mouse', {
// x: pos.x - 32,
// y: pos.x - 32,
// button: button
// });

if (self._vt300Mouse) {
// NOTE: Unstable.
// http://www.vt100.net/docs/vt3xx-gp/chapter15.html
Expand Down Expand Up @@ -1597,7 +1588,6 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
return true;
}

this.emit('keydown', event);
this._onKey.fire({ key: result.key, domEvent: event });
this.showCursor();
this.handler(result.key);
Expand Down Expand Up @@ -1676,7 +1666,6 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II

key = String.fromCharCode(key);

this.emit('keypress', key, ev);
this._onKey.fire({ key, domEvent: ev });
this.showCursor();
this.handler(key);
Expand All @@ -1689,7 +1678,6 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
* Note: We could do sweet things with webaudio here
*/
public bell(): void {
this.emit('bell');
if (this._soundBell()) {
this.soundManager.playBellSound();
}
Expand Down
Loading

0 comments on commit 447fc4f

Please sign in to comment.