Skip to content

Commit d22f7c9

Browse files
authored
Merge pull request #4166 from Tyriar/event_with_emitter
Create new event with emitter object to simplify code
2 parents 1f8e6f0 + 5b785d2 commit d22f7c9

33 files changed

+269
-313
lines changed

addons/xterm-addon-canvas/src/CanvasRenderer.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { IColorSet, ILinkifier2 } from 'browser/Types';
1414
import { ICharacterJoinerService, ICharSizeService, ICoreBrowserService } from 'browser/services/Services';
1515
import { IBufferService, IOptionsService, IDecorationService, ICoreService } from 'common/services/Services';
1616
import { removeTerminalFromCache } from './atlas/CharAtlasCache';
17-
import { EventEmitter, IEvent } from 'common/EventEmitter';
17+
import { initEvent, EventEmitter, IEvent } from 'common/EventEmitter';
1818
import { observeDevicePixelDimensions } from 'browser/renderer/DevicePixelObserver';
1919

2020
let nextRendererId = 1;
@@ -27,8 +27,7 @@ export class CanvasRenderer extends Disposable implements IRenderer {
2727

2828
public dimensions: IRenderDimensions;
2929

30-
private readonly _onRequestRedraw = new EventEmitter<IRequestRedrawEvent>();
31-
public readonly onRequestRedraw = this._onRequestRedraw.event;
30+
public readonly onRequestRedraw = initEvent<IRequestRedrawEvent>();
3231

3332
constructor(
3433
private _colors: IColorSet,
@@ -48,7 +47,7 @@ export class CanvasRenderer extends Disposable implements IRenderer {
4847
new TextRenderLayer(this._screenElement, 0, this._colors, allowTransparency, this._id, this._bufferService, this._optionsService, characterJoinerService, decorationService, this._coreBrowserService),
4948
new SelectionRenderLayer(this._screenElement, 1, this._colors, this._id, this._bufferService, this._coreBrowserService, decorationService, this._optionsService),
5049
new LinkRenderLayer(this._screenElement, 2, this._colors, this._id, linkifier2, this._bufferService, this._optionsService, decorationService, this._coreBrowserService),
51-
new CursorRenderLayer(this._screenElement, 3, this._colors, this._id, this._onRequestRedraw, this._bufferService, this._optionsService, coreService, this._coreBrowserService, decorationService)
50+
new CursorRenderLayer(this._screenElement, 3, this._colors, this._id, this.onRequestRedraw, this._bufferService, this._optionsService, coreService, this._coreBrowserService, decorationService)
5251
];
5352
this.dimensions = {
5453
scaledCharWidth: 0,
@@ -128,7 +127,7 @@ export class CanvasRenderer extends Disposable implements IRenderer {
128127
this._runOperation(l => l.onSelectionChanged(start, end, columnSelectMode));
129128
// Selection foreground requires a full re-render
130129
if (this._colors.selectionForeground) {
131-
this._onRequestRedraw.fire({ start: 0, end: this._bufferService.rows - 1 });
130+
this.onRequestRedraw.fire({ start: 0, end: this._bufferService.rows - 1 });
132131
}
133132
}
134133

@@ -201,6 +200,6 @@ export class CanvasRenderer extends Disposable implements IRenderer {
201200
}
202201

203202
private _requestRedrawViewport(): void {
204-
this._onRequestRedraw.fire({ start: 0, end: this._bufferService.rows - 1 });
203+
this.onRequestRedraw.fire({ start: 0, end: this._bufferService.rows - 1 });
205204
}
206205
}

addons/xterm-addon-search/src/SearchAddon.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { Terminal, IDisposable, ITerminalAddon, IBufferRange, IDecoration } from 'xterm';
7-
import { EventEmitter } from 'common/EventEmitter';
7+
import { initEvent } from 'common/EventEmitter';
88

99
export interface ISearchOptions {
1010
regex?: boolean;
@@ -72,8 +72,7 @@ export class SearchAddon implements ITerminalAddon {
7272

7373
private _resultIndex: number | undefined;
7474

75-
private readonly _onDidChangeResults = new EventEmitter<{ resultIndex: number, resultCount: number } | undefined>();
76-
public readonly onDidChangeResults = this._onDidChangeResults.event;
75+
public readonly onDidChangeResults = initEvent<{ resultIndex: number, resultCount: number } | undefined>();
7776

7877
public activate(terminal: Terminal): void {
7978
this._terminal = terminal;
@@ -89,7 +88,7 @@ export class SearchAddon implements ITerminalAddon {
8988
this._highlightTimeout = setTimeout(() => {
9089
this.findPrevious(this._cachedSearchTerm!, { ...this._lastSearchOptions, incremental: true, noScroll: true });
9190
this._resultIndex = this._searchResults ? this._searchResults.size - 1 : -1;
92-
this._onDidChangeResults.fire({ resultIndex: this._resultIndex, resultCount: this._searchResults?.size ?? -1 });
91+
this.onDidChangeResults.fire({ resultIndex: this._resultIndex, resultCount: this._searchResults?.size ?? -1 });
9392
}, 200);
9493
}
9594
}
@@ -325,9 +324,9 @@ export class SearchAddon implements ITerminalAddon {
325324
private _fireResults(term: string, found: boolean, searchOptions?: ISearchOptions): boolean {
326325
if (searchOptions?.decorations) {
327326
if (this._resultIndex !== undefined && this._searchResults?.size !== undefined) {
328-
this._onDidChangeResults.fire({ resultIndex: this._resultIndex, resultCount: this._searchResults.size });
327+
this.onDidChangeResults.fire({ resultIndex: this._resultIndex, resultCount: this._searchResults.size });
329328
} else {
330-
this._onDidChangeResults.fire(undefined);
329+
this.onDidChangeResults.fire(undefined);
331330
}
332331
}
333332
this._cachedSearchTerm = term;

addons/xterm-addon-webgl/src/WebglAddon.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,20 @@ import { Terminal, ITerminalAddon, IEvent } from 'xterm';
77
import { WebglRenderer } from './WebglRenderer';
88
import { ICharacterJoinerService, ICoreBrowserService, IRenderService } from 'browser/services/Services';
99
import { IColorSet } from 'browser/Types';
10-
import { EventEmitter, forwardEvent } from 'common/EventEmitter';
10+
import { EventEmitter, forwardEvent, initEvent } from 'common/EventEmitter';
1111
import { isSafari } from 'common/Platform';
1212
import { ICoreService, IDecorationService } from 'common/services/Services';
1313

1414
export class WebglAddon implements ITerminalAddon {
1515
private _terminal?: Terminal;
1616
private _renderer?: WebglRenderer;
1717

18-
private readonly _onChangeTextureAtlas = new EventEmitter<HTMLElement>();
19-
public readonly onChangeTextureAtlas = this._onChangeTextureAtlas.event;
20-
private readonly _onContextLoss = new EventEmitter<void>();
21-
public readonly onContextLoss = this._onContextLoss.event;
18+
public readonly onChangeTextureAtlas = initEvent<HTMLElement>();
19+
public readonly onContextLoss = initEvent<void>();
2220

2321
constructor(
2422
private _preserveDrawingBuffer?: boolean
25-
) {}
23+
) { }
2624

2725
public activate(terminal: Terminal): void {
2826
if (!terminal.element) {
@@ -39,8 +37,8 @@ export class WebglAddon implements ITerminalAddon {
3937
const decorationService: IDecorationService = (terminal as any)._core._decorationService;
4038
const colors: IColorSet = (terminal as any)._core._colorManager.colors;
4139
this._renderer = new WebglRenderer(terminal, colors, characterJoinerService, coreBrowserService, coreService, decorationService, this._preserveDrawingBuffer);
42-
forwardEvent(this._renderer.onContextLoss, this._onContextLoss);
43-
forwardEvent(this._renderer.onChangeTextureAtlas, this._onChangeTextureAtlas);
40+
forwardEvent(this._renderer.onContextLoss, this.onContextLoss);
41+
forwardEvent(this._renderer.onChangeTextureAtlas, this.onChangeTextureAtlas);
4442
renderService.setRenderer(this._renderer);
4543
}
4644

addons/xterm-addon-webgl/src/WebglRenderer.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { IRenderLayer } from './renderLayer/Types';
1818
import { IRenderDimensions, IRenderer, IRequestRedrawEvent } from 'browser/renderer/Types';
1919
import { observeDevicePixelDimensions } from 'browser/renderer/DevicePixelObserver';
2020
import { ITerminal, IColorSet } from 'browser/Types';
21-
import { EventEmitter } from 'common/EventEmitter';
21+
import { EventEmitter, initEvent } from 'common/EventEmitter';
2222
import { CellData } from 'common/buffer/CellData';
2323
import { addDisposableDomListener } from 'browser/Lifecycle';
2424
import { ICharacterJoinerService, ICoreBrowserService } from 'browser/services/Services';
@@ -53,12 +53,9 @@ export class WebglRenderer extends Disposable implements IRenderer {
5353
private _isAttached: boolean;
5454
private _contextRestorationTimeout: number | undefined;
5555

56-
private readonly _onChangeTextureAtlas = new EventEmitter<HTMLCanvasElement>();
57-
public readonly onChangeTextureAtlas = this._onChangeTextureAtlas.event;
58-
private readonly _onRequestRedraw = new EventEmitter<IRequestRedrawEvent>();
59-
public readonly onRequestRedraw = this._onRequestRedraw.event;
60-
private readonly _onContextLoss = new EventEmitter<void>();
61-
public readonly onContextLoss = this._onContextLoss.event;
56+
public readonly onChangeTextureAtlas = initEvent<HTMLCanvasElement>();
57+
public readonly onRequestRedraw = initEvent<IRequestRedrawEvent>();
58+
public readonly onContextLoss = initEvent<void>();
6259

6360
constructor(
6461
private _terminal: Terminal,
@@ -75,7 +72,7 @@ export class WebglRenderer extends Disposable implements IRenderer {
7572

7673
this._renderLayers = [
7774
new LinkRenderLayer(this._core.screenElement!, 2, this._colors, this._core, this._coreBrowserService),
78-
new CursorRenderLayer(_terminal, this._core.screenElement!, 3, this._colors, this._onRequestRedraw, this._coreBrowserService, coreService)
75+
new CursorRenderLayer(_terminal, this._core.screenElement!, 3, this._colors, this.onRequestRedraw, this._coreBrowserService, coreService)
7976
];
8077
this.dimensions = {
8178
scaledCharWidth: 0,
@@ -115,7 +112,7 @@ export class WebglRenderer extends Disposable implements IRenderer {
115112
this._contextRestorationTimeout = setTimeout(() => {
116113
this._contextRestorationTimeout = undefined;
117114
console.warn('webgl context not restored; firing onContextLoss');
118-
this._onContextLoss.fire(e);
115+
this.onContextLoss.fire(e);
119116
}, 3000 /* ms */);
120117
}));
121118
this.register(addDisposableDomListener(this._canvas, 'webglcontextrestored', (e) => {
@@ -283,7 +280,7 @@ export class WebglRenderer extends Disposable implements IRenderer {
283280
throw new Error('The webgl renderer only works with the webgl char atlas');
284281
}
285282
if (this._charAtlas !== atlas) {
286-
this._onChangeTextureAtlas.fire(atlas.cacheCanvas);
283+
this.onChangeTextureAtlas.fire(atlas.cacheCanvas);
287284
}
288285
this._charAtlas = atlas;
289286
this._charAtlas.warmUp();
@@ -422,9 +419,9 @@ export class WebglRenderer extends Disposable implements IRenderer {
422419

423420
// Nothing has changed, no updates needed
424421
if (this._model.cells[i] === code &&
425-
this._model.cells[i + RENDER_MODEL_BG_OFFSET] === this._workColors.bg &&
426-
this._model.cells[i + RENDER_MODEL_FG_OFFSET] === this._workColors.fg &&
427-
this._model.cells[i + RENDER_MODEL_EXT_OFFSET] === this._workColors.ext) {
422+
this._model.cells[i + RENDER_MODEL_BG_OFFSET] === this._workColors.bg &&
423+
this._model.cells[i + RENDER_MODEL_FG_OFFSET] === this._workColors.fg &&
424+
this._model.cells[i + RENDER_MODEL_EXT_OFFSET] === this._workColors.ext) {
428425
continue;
429426
}
430427

@@ -676,7 +673,7 @@ export class WebglRenderer extends Disposable implements IRenderer {
676673
}
677674

678675
private _requestRedrawViewport(): void {
679-
this._onRequestRedraw.fire({ start: 0, end: this._terminal.rows - 1 });
676+
this.onRequestRedraw.fire({ start: 0, end: this._terminal.rows - 1 });
680677
}
681678
}
682679

src/browser/Linkifier2.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ILinkifier2, ILinkProvider, IBufferCellPosition, ILink, ILinkifierEvent
77
import { IDisposable } from 'common/Types';
88
import { IMouseService, IRenderService } from './services/Services';
99
import { IBufferService } from 'common/services/Services';
10-
import { EventEmitter, IEvent } from 'common/EventEmitter';
10+
import { EventEmitter, IEvent, initEvent } from 'common/EventEmitter';
1111
import { Disposable, getDisposeArrayDisposable, disposeArray } from 'common/Lifecycle';
1212
import { addDisposableDomListener } from 'browser/Lifecycle';
1313

@@ -26,10 +26,8 @@ export class Linkifier2 extends Disposable implements ILinkifier2 {
2626
private _activeProviderReplies: Map<Number, ILinkWithState[] | undefined> | undefined;
2727
private _activeLine: number = -1;
2828

29-
private readonly _onShowLinkUnderline = this.register(new EventEmitter<ILinkifierEvent>());
30-
public readonly onShowLinkUnderline = this._onShowLinkUnderline.event;
31-
private readonly _onHideLinkUnderline = this.register(new EventEmitter<ILinkifierEvent>());
32-
public readonly onHideLinkUnderline = this._onHideLinkUnderline.event;
29+
public readonly onShowLinkUnderline = this.register(initEvent<ILinkifierEvent>());
30+
public readonly onHideLinkUnderline = this.register(initEvent<ILinkifierEvent>());
3331

3432
constructor(
3533
@IBufferService private readonly _bufferService: IBufferService
@@ -343,7 +341,7 @@ export class Linkifier2 extends Disposable implements ILinkifier2 {
343341
const range = link.range;
344342
const scrollOffset = this._bufferService.buffer.ydisp;
345343
const event = this._createLinkUnderlineEvent(range.start.x - 1, range.start.y - scrollOffset - 1, range.end.x, range.end.y - scrollOffset - 1, undefined);
346-
const emitter = showEvent ? this._onShowLinkUnderline : this._onHideLinkUnderline;
344+
const emitter = showEvent ? this.onShowLinkUnderline : this.onHideLinkUnderline;
347345
emitter.fire(event);
348346
}
349347

src/browser/Terminal.ts

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { ITheme, IMarker, IDisposable, ILinkProvider, IDecorationOptions, IDecor
3737
import { DomRenderer } from 'browser/renderer/dom/DomRenderer';
3838
import { KeyboardResultType, CoreMouseEventType, CoreMouseButton, CoreMouseAction, ITerminalOptions, ScrollSource, IColorEvent, ColorIndex, ColorRequestType } from 'common/Types';
3939
import { evaluateKeyboardEvent } from 'common/input/Keyboard';
40-
import { EventEmitter, IEvent, forwardEvent } from 'common/EventEmitter';
40+
import { EventEmitter, IEvent, forwardEvent, initEvent } from 'common/EventEmitter';
4141
import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine';
4242
import { ColorManager } from 'browser/ColorManager';
4343
import { RenderService } from 'browser/services/RenderService';
@@ -122,27 +122,16 @@ export class Terminal extends CoreTerminal implements ITerminal {
122122
private _colorManager: ColorManager | undefined;
123123
private _theme: ITheme | undefined;
124124

125-
private readonly _onCursorMove = new EventEmitter<void>();
126-
public readonly onCursorMove = this._onCursorMove.event;
127-
private readonly _onKey = new EventEmitter<{ key: string, domEvent: KeyboardEvent }>();
128-
public readonly onKey = this._onKey.event;
129-
private readonly _onRender = new EventEmitter<{ start: number, end: number }>();
130-
public readonly onRender = this._onRender.event;
131-
private readonly _onSelectionChange = new EventEmitter<void>();
132-
public readonly onSelectionChange = this._onSelectionChange.event;
133-
private readonly _onTitleChange = new EventEmitter<string>();
134-
public readonly onTitleChange = this._onTitleChange.event;
135-
private readonly _onBell = new EventEmitter<void>();
136-
public readonly onBell = this._onBell.event;
137-
138-
private readonly _onFocus = new EventEmitter<void>();
139-
public readonly onFocus = this._onFocus.event;
140-
private readonly _onBlur = new EventEmitter<void>();
141-
public readonly onBlur = this._onBlur.event;
142-
private readonly _onA11yCharEmitter = new EventEmitter<string>();
143-
public readonly onA11yChar = this._onA11yCharEmitter.event;
144-
private readonly _onA11yTabEmitter = new EventEmitter<number>();
145-
public readonly onA11yTab = this._onA11yTabEmitter.event;
125+
public readonly onCursorMove = initEvent<void>();
126+
public readonly onKey = initEvent<{ key: string, domEvent: KeyboardEvent }>();
127+
public readonly onRender = initEvent<{ start: number, end: number }>();
128+
public readonly onSelectionChange = initEvent<void>();
129+
public readonly onTitleChange = initEvent<string>();
130+
public readonly onBell = initEvent<void>();
131+
public readonly onFocus = initEvent<void>();
132+
public readonly onBlur = initEvent<void>();
133+
public readonly onA11yChar = initEvent<string>();
134+
public readonly onA11yTab = initEvent<number>();
146135

147136
/**
148137
* Creates a new `Terminal` object.
@@ -169,16 +158,16 @@ export class Terminal extends CoreTerminal implements ITerminal {
169158
this._instantiationService.setService(IDecorationService, this._decorationService);
170159

171160
// Setup InputHandler listeners
172-
this.register(this._inputHandler.onRequestBell(() => this._onBell.fire()));
161+
this.register(this._inputHandler.onRequestBell(() => this.onBell.fire()));
173162
this.register(this._inputHandler.onRequestRefreshRows((start, end) => this.refresh(start, end)));
174163
this.register(this._inputHandler.onRequestSendFocus(() => this._reportFocus()));
175164
this.register(this._inputHandler.onRequestReset(() => this.reset()));
176165
this.register(this._inputHandler.onRequestWindowsOptionsReport(type => this._reportWindowsOptions(type)));
177166
this.register(this._inputHandler.onColor((event) => this._handleColorEvent(event)));
178-
this.register(forwardEvent(this._inputHandler.onCursorMove, this._onCursorMove));
179-
this.register(forwardEvent(this._inputHandler.onTitleChange, this._onTitleChange));
180-
this.register(forwardEvent(this._inputHandler.onA11yChar, this._onA11yCharEmitter));
181-
this.register(forwardEvent(this._inputHandler.onA11yTab, this._onA11yTabEmitter));
167+
this.register(forwardEvent(this._inputHandler.onCursorMove, this.onCursorMove));
168+
this.register(forwardEvent(this._inputHandler.onTitleChange, this.onTitleChange));
169+
this.register(forwardEvent(this._inputHandler.onA11yChar, this.onA11yChar));
170+
this.register(forwardEvent(this._inputHandler.onA11yTab, this.onA11yTab));
182171

183172
// Setup listeners
184173
this.register(this._bufferService.onResize(e => this._afterResize(e.cols, e.rows)));
@@ -326,7 +315,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
326315
this.updateCursorStyle(ev);
327316
this.element!.classList.add('focus');
328317
this._showCursor();
329-
this._onFocus.fire();
318+
this.onFocus.fire();
330319
}
331320

332321
/**
@@ -349,7 +338,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
349338
this.coreService.triggerDataEvent(C0.ESC + '[O');
350339
}
351340
this.element!.classList.remove('focus');
352-
this._onBlur.fire();
341+
this.onBlur.fire();
353342
}
354343

355344
private _syncTextArea(): void {
@@ -512,7 +501,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
512501
const renderer = this._createRenderer();
513502
this._renderService = this.register(this._instantiationService.createInstance(RenderService, renderer, this.rows, this.screenElement));
514503
this._instantiationService.setService(IRenderService, this._renderService);
515-
this.register(this._renderService.onRenderedViewportChange(e => this._onRender.fire(e)));
504+
this.register(this._renderService.onRenderedViewportChange(e => this.onRender.fire(e)));
516505
this.onResize(e => this._renderService!.resize(e.cols, e.rows));
517506

518507
this._compositionView = document.createElement('div');
@@ -552,7 +541,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
552541
));
553542
this._instantiationService.setService(ISelectionService, this._selectionService);
554543
this.register(this._selectionService.onRequestScrollLines(e => this.scrollLines(e.amount, e.suppressScrollEvent)));
555-
this.register(this._selectionService.onSelectionChange(() => this._onSelectionChange.fire()));
544+
this.register(this._selectionService.onSelectionChange(() => this.onSelectionChange.fire()));
556545
this.register(this._selectionService.onRequestRedraw(e => this._renderService!.onSelectionChanged(e.start, e.end, e.columnSelectMode)));
557546
this.register(this._selectionService.onLinuxMouseSelection(text => {
558547
// If there's a new selection, put it into the textarea, focus and select it
@@ -1101,7 +1090,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
11011090
this.textarea!.value = '';
11021091
}
11031092

1104-
this._onKey.fire({ key: result.key, domEvent: event });
1093+
this.onKey.fire({ key: result.key, domEvent: event });
11051094
this._showCursor();
11061095
this.coreService.triggerDataEvent(result.key, true);
11071096

@@ -1184,7 +1173,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
11841173

11851174
key = String.fromCharCode(key);
11861175

1187-
this._onKey.fire({ key, domEvent: ev });
1176+
this.onKey.fire({ key, domEvent: ev });
11881177
this._showCursor();
11891178
this.coreService.triggerDataEvent(key, true);
11901179

0 commit comments

Comments
 (0)