Create new event with emitter object to simplify code

This commit is contained in:
Daniel Imms
2022-10-01 08:45:40 -07:00
parent 969db2bcf9
commit ed15a8e170
33 changed files with 247 additions and 307 deletions
+6 -8
View File
@@ -6,7 +6,7 @@
import { IBufferService, IOptionsService } from 'common/services/Services';
import { BufferSet } from 'common/buffer/BufferSet';
import { IBufferSet, IBuffer } from 'common/buffer/Types';
import { EventEmitter, IEventEmitter, IEvent } from 'common/EventEmitter';
import { EventEmitter, IEventEmitter, IEvent, initEvent } from 'common/EventEmitter';
import { Disposable } from 'common/Lifecycle';
import { IAttributeData, IBufferLine, ScrollSource } from 'common/Types';
@@ -22,10 +22,8 @@ export class BufferService extends Disposable implements IBufferService {
/** Whether the user is scrolling (locks the scroll position) */
public isUserScrolling: boolean = false;
private readonly _onResize = new EventEmitter<{ cols: number, rows: number }>();
public readonly onResize = this._onResize.event;
private readonly _onScroll = new EventEmitter<number>();
public readonly onScroll = this._onScroll.event;
public readonly onResize = initEvent<{ cols: number, rows: number }>();
public readonly onScroll = initEvent<number>();
public get buffer(): IBuffer { return this.buffers.active; }
@@ -49,7 +47,7 @@ export class BufferService extends Disposable implements IBufferService {
this.rows = rows;
this.buffers.resize(cols, rows);
this.buffers.setupTabStops(this.cols);
this._onResize.fire({ cols, rows });
this.onResize.fire({ cols, rows });
}
public reset(): void {
@@ -118,7 +116,7 @@ export class BufferService extends Disposable implements IBufferService {
buffer.ydisp = buffer.ybase;
}
this._onScroll.fire(buffer.ydisp);
this.onScroll.fire(buffer.ydisp);
}
/**
@@ -148,7 +146,7 @@ export class BufferService extends Disposable implements IBufferService {
}
if (!suppressScrollEvent) {
this._onScroll.fire(buffer.ydisp);
this.onScroll.fire(buffer.ydisp);
}
}
+3 -4
View File
@@ -3,7 +3,7 @@
* @license MIT
*/
import { IBufferService, ICoreService, ICoreMouseService } from 'common/services/Services';
import { EventEmitter, IEvent } from 'common/EventEmitter';
import { EventEmitter, IEvent, initEvent } from 'common/EventEmitter';
import { ICoreMouseProtocol, ICoreMouseEvent, CoreMouseEncoding, CoreMouseEventType, CoreMouseButton, CoreMouseAction } from 'common/Types';
/**
@@ -172,8 +172,7 @@ export class CoreMouseService implements ICoreMouseService {
private _activeEncoding: string = '';
private _lastEvent: ICoreMouseEvent | null = null;
private readonly _onProtocolChange = new EventEmitter<CoreMouseEventType>();
public readonly onProtocolChange = this._onProtocolChange.event;
public readonly onProtocolChange = initEvent<CoreMouseEventType>();
constructor(
@IBufferService private readonly _bufferService: IBufferService,
@@ -207,7 +206,7 @@ export class CoreMouseService implements ICoreMouseService {
throw new Error(`unknown protocol "${name}"`);
}
this._activeProtocol = name;
this._onProtocolChange.fire(this._protocols[name].events);
this.onProtocolChange.fire(this._protocols[name].events);
}
public get activeEncoding(): string {
+7 -10
View File
@@ -4,7 +4,7 @@
*/
import { ICoreService, ILogService, IOptionsService, IBufferService } from 'common/services/Services';
import { EventEmitter, IEvent } from 'common/EventEmitter';
import { EventEmitter, IEvent, initEvent } from 'common/EventEmitter';
import { IDecPrivateModes, IModes } from 'common/Types';
import { clone } from 'common/Clone';
import { Disposable } from 'common/Lifecycle';
@@ -34,12 +34,9 @@ export class CoreService extends Disposable implements ICoreService {
// Circular dependency, this must be unset or memory will leak after Terminal.dispose
private _scrollToBottom: (() => void) | undefined;
private readonly _onData = this.register(new EventEmitter<string>());
public readonly onData = this._onData.event;
private readonly _onUserInput = this.register(new EventEmitter<void>());
public readonly onUserInput = this._onUserInput.event;
private readonly _onBinary = this.register(new EventEmitter<string>());
public readonly onBinary = this._onBinary.event;
public readonly onData = this.register(initEvent<string>());
public readonly onUserInput = this.register(initEvent<void>());
public readonly onBinary = this.register(initEvent<string>());
constructor(
// TODO: Move this into a service
@@ -74,12 +71,12 @@ export class CoreService extends Disposable implements ICoreService {
// Fire onUserInput so listeners can react as well (eg. clear selection)
if (wasUserInput) {
this._onUserInput.fire();
this.onUserInput.fire();
}
// Fire onData API
this._logService.debug(`sending data "${data}"`, () => data.split('').map(e => e.charCodeAt(0)));
this._onData.fire(data);
this.onData.fire(data);
}
public triggerBinaryEvent(data: string): void {
@@ -87,6 +84,6 @@ export class CoreService extends Disposable implements ICoreService {
return;
}
this._logService.debug(`sending binary "${data}"`, () => data.split('').map(e => e.charCodeAt(0)));
this._onBinary.fire(data);
this.onBinary.fire(data);
}
}
+9 -13
View File
@@ -4,7 +4,7 @@
*/
import { css } from 'common/Color';
import { EventEmitter } from 'common/EventEmitter';
import { EventEmitter, initEvent } from 'common/EventEmitter';
import { Disposable } from 'common/Lifecycle';
import { IDecorationService, IInternalDecoration } from 'common/services/Services';
import { SortedList } from 'common/SortedList';
@@ -25,10 +25,8 @@ export class DecorationService extends Disposable implements IDecorationService
*/
private readonly _decorations: SortedList<IInternalDecoration> = new SortedList(e => e?.marker.line);
private readonly _onDecorationRegistered = this.register(new EventEmitter<IInternalDecoration>());
public readonly onDecorationRegistered = this._onDecorationRegistered.event;
private readonly _onDecorationRemoved = this.register(new EventEmitter<IInternalDecoration>());
public readonly onDecorationRemoved = this._onDecorationRemoved.event;
public readonly onDecorationRegistered = this.register(initEvent<IInternalDecoration>());
public readonly onDecorationRemoved = this.register(initEvent<IInternalDecoration>());
public get decorations(): IterableIterator<IInternalDecoration> { return this._decorations.values(); }
@@ -42,13 +40,13 @@ export class DecorationService extends Disposable implements IDecorationService
decoration.onDispose(() => {
if (decoration) {
if (this._decorations.delete(decoration)) {
this._onDecorationRemoved.fire(decoration);
this.onDecorationRemoved.fire(decoration);
}
markerDispose.dispose();
}
});
this._decorations.insert(decoration);
this._onDecorationRegistered.fire(decoration);
this.onDecorationRegistered.fire(decoration);
}
return decoration;
}
@@ -84,7 +82,7 @@ export class DecorationService extends Disposable implements IDecorationService
public dispose(): void {
for (const d of this._decorations.values()) {
this._onDecorationRemoved.fire(d);
this.onDecorationRemoved.fire(d);
}
this.reset();
}
@@ -95,10 +93,8 @@ class Decoration extends Disposable implements IInternalDecoration {
public element: HTMLElement | undefined;
public isDisposed: boolean = false;
public readonly onRenderEmitter = this.register(new EventEmitter<HTMLElement>());
public readonly onRender = this.onRenderEmitter.event;
private readonly _onDispose = this.register(new EventEmitter<void>());
public readonly onDispose = this._onDispose.event;
public readonly onRender = this.register(initEvent<HTMLElement>());
public readonly onDispose = this.register(initEvent<void>());
private _cachedBg: IColor | undefined | null = null;
public get backgroundColorRGB(): IColor | undefined {
@@ -139,7 +135,7 @@ class Decoration extends Disposable implements IInternalDecoration {
return;
}
this._isDisposed = true;
this._onDispose.fire();
this.onDispose.fire();
super.dispose();
}
}
+3 -4
View File
@@ -4,9 +4,9 @@
*/
import { IOptionsService, ITerminalOptions, FontWeight } from 'common/services/Services';
import { EventEmitter, IEvent } from 'common/EventEmitter';
import { isMac } from 'common/Platform';
import { CursorStyle } from 'common/Types';
import { initEvent } from 'common/EventEmitter';
export const DEFAULT_OPTIONS: Readonly<Required<ITerminalOptions>> = {
cols: 80,
@@ -57,8 +57,7 @@ export class OptionsService implements IOptionsService {
public readonly rawOptions: Required<ITerminalOptions>;
public options: Required<ITerminalOptions>;
private readonly _onOptionChange = new EventEmitter<string>();
public readonly onOptionChange = this._onOptionChange.event;
public readonly onOptionChange = initEvent<string>();
constructor(options: Partial<ITerminalOptions>) {
// set the default value of each option
@@ -97,7 +96,7 @@ export class OptionsService implements IOptionsService {
// Don't fire an option change event if they didn't change
if (this.rawOptions[propName] !== value) {
this.rawOptions[propName] = value;
this._onOptionChange.fire(propName);
this.onOptionChange.fire(propName);
}
};
+2 -2
View File
@@ -3,7 +3,7 @@
* @license MIT
*/
import { IEvent, IEventEmitter } from 'common/EventEmitter';
import { IEvent, IEventEmitter, IEventWithEmitter } from 'common/EventEmitter';
import { IBuffer, IBufferSet } from 'common/buffer/Types';
import { IDecPrivateModes, ICoreMouseEvent, CoreMouseEncoding, ICoreMouseProtocol, CoreMouseEventType, ICharset, IWindowOptions, IModes, IAttributeData, ScrollSource, IDisposable, IColor, CursorStyle, IOscLinkData } from 'common/Types';
import { createDecorator } from 'common/services/ServiceRegistry';
@@ -318,5 +318,5 @@ export interface IInternalDecoration extends IDecoration {
readonly options: IDecorationOptions;
readonly backgroundColorRGB: IColor | undefined;
readonly foregroundColorRGB: IColor | undefined;
readonly onRenderEmitter: IEventEmitter<HTMLElement>;
readonly onRender: IEventWithEmitter<HTMLElement>;
}
+3 -4
View File
@@ -3,7 +3,7 @@
* @license MIT
*/
import { IUnicodeService, IUnicodeVersionProvider } from 'common/services/Services';
import { EventEmitter, IEvent } from 'common/EventEmitter';
import { EventEmitter, IEvent, initEvent } from 'common/EventEmitter';
import { UnicodeV6 } from 'common/input/UnicodeV6';
@@ -14,8 +14,7 @@ export class UnicodeService implements IUnicodeService {
private _active: string = '';
private _activeProvider: IUnicodeVersionProvider;
private readonly _onChange = new EventEmitter<string>();
public readonly onChange = this._onChange.event;
public readonly onChange = initEvent<string>();
constructor() {
const defaultProvider = new UnicodeV6();
@@ -38,7 +37,7 @@ export class UnicodeService implements IUnicodeService {
}
this._active = version;
this._activeProvider = this._providers[version];
this._onChange.fire(version);
this.onChange.fire(version);
}
public register(provider: IUnicodeVersionProvider): void {