Skip to content

Commit

Permalink
Merge pull request xtermjs#5106 from Tyriar/tyriar/lifecycle
Browse files Browse the repository at this point in the history
Move to vs/base disposables
  • Loading branch information
Tyriar authored Jul 15, 2024
2 parents b192ba2 + 56ec862 commit cb3bd46
Show file tree
Hide file tree
Showing 50 changed files with 366 additions and 646 deletions.
7 changes: 3 additions & 4 deletions addons/addon-image/src/ImageRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import { toRGBA8888 } from 'sixel/lib/Colors';
import { IDisposable } from '@xterm/xterm';
import { ICellSize, ITerminalExt, IImageSpec, IRenderDimensions, IRenderService } from './Types';
import { Disposable, MutableDisposable, toDisposable } from 'common/Lifecycle';

import { Disposable, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle';

const PLACEHOLDER_LENGTH = 4096;
const PLACEHOLDER_HEIGHT = 24;
Expand All @@ -23,7 +22,7 @@ export class ImageRenderer extends Disposable implements IDisposable {
private _ctx: CanvasRenderingContext2D | null | undefined;
private _placeholder: HTMLCanvasElement | undefined;
private _placeholderBitmap: ImageBitmap | undefined;
private _optionsRefresh = this.register(new MutableDisposable());
private _optionsRefresh = this._register(new MutableDisposable());
private _oldOpen: ((parent: HTMLElement) => void) | undefined;
private _renderService: IRenderService | undefined;
private _oldSetRenderer: ((renderer: any) => void) | undefined;
Expand Down Expand Up @@ -85,7 +84,7 @@ export class ImageRenderer extends Disposable implements IDisposable {
this._renderService?.refreshRows(0, this._terminal.rows);
}
});
this.register(toDisposable(() => {
this._register(toDisposable(() => {
this.removeLayerFromDom();
if (this._terminal._core && this._oldOpen) {
this._terminal._core.open = this._oldOpen;
Expand Down
4 changes: 3 additions & 1 deletion addons/addon-image/src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"paths": {
"browser/*": [ "../../../src/browser/*" ],
"common/*": [ "../../../src/common/*" ],
"vs/*": [ "../../../src/vs/*" ],
"@xterm/addon-image": [ "../typings/addon-image.d.ts" ]
}
},
Expand All @@ -24,6 +25,7 @@
],
"references": [
{ "path": "../../../src/browser" },
{ "path": "../../../src/common" }
{ "path": "../../../src/common" },
{ "path": "../../../src/vs" }
]
}
3 changes: 2 additions & 1 deletion addons/addon-image/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const addon = {
extensions: [ '.js' ],
alias: {
common: path.resolve('../../out/common'),
browser: path.resolve('../../out/browser')
browser: path.resolve('../../out/browser'),
vs: path.resolve('../../out/vs')
}
},
output: {
Expand Down
22 changes: 11 additions & 11 deletions addons/addon-search/src/SearchAddon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import type { Terminal, IDisposable, ITerminalAddon, IDecoration } from '@xterm/xterm';
import type { SearchAddon as ISearchApi } from '@xterm/addon-search';
import { Disposable, toDisposable, disposeArray, MutableDisposable, getDisposeArrayDisposable } from 'common/Lifecycle';
import { Emitter } from 'vs/base/common/event';
import { combinedDisposable, Disposable, dispose, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle';

export interface ISearchOptions {
regex?: boolean;
Expand Down Expand Up @@ -67,7 +67,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
private _cachedSearchTerm: string | undefined;
private _highlightedLines: Set<number> = new Set();
private _highlightDecorations: IHighlight[] = [];
private _selectedDecoration: MutableDisposable<IHighlight> = this.register(new MutableDisposable());
private _selectedDecoration: MutableDisposable<IHighlight> = this._register(new MutableDisposable());
private _highlightLimit: number;
private _lastSearchOptions: ISearchOptions | undefined;
private _highlightTimeout: number | undefined;
Expand All @@ -80,7 +80,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
private _linesCacheTimeoutId = 0;
private _linesCacheDisposables = new MutableDisposable();

private readonly _onDidChangeResults = this.register(new Emitter<{ resultIndex: number, resultCount: number }>());
private readonly _onDidChangeResults = this._register(new Emitter<{ resultIndex: number, resultCount: number }>());
public readonly onDidChangeResults = this._onDidChangeResults.event;

constructor(options?: Partial<ISearchAddonOptions>) {
Expand All @@ -91,9 +91,9 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA

public activate(terminal: Terminal): void {
this._terminal = terminal;
this.register(this._terminal.onWriteParsed(() => this._updateMatches()));
this.register(this._terminal.onResize(() => this._updateMatches()));
this.register(toDisposable(() => this.clearDecorations()));
this._register(this._terminal.onWriteParsed(() => this._updateMatches()));
this._register(this._terminal.onResize(() => this._updateMatches()));
this._register(toDisposable(() => this.clearDecorations()));
}

private _updateMatches(): void {
Expand All @@ -111,7 +111,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA

public clearDecorations(retainCachedSearchTerm?: boolean): void {
this._selectedDecoration.clear();
disposeArray(this._highlightDecorations);
dispose(this._highlightDecorations);
this._highlightDecorations = [];
this._highlightedLines.clear();
if (!retainCachedSearchTerm) {
Expand Down Expand Up @@ -426,11 +426,11 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
const terminal = this._terminal!;
if (!this._linesCache) {
this._linesCache = new Array(terminal.buffer.active.length);
this._linesCacheDisposables.value = getDisposeArrayDisposable([
this._linesCacheDisposables.value = combinedDisposable(
terminal.onLineFeed(() => this._destroyLinesCache()),
terminal.onCursorMove(() => this._destroyLinesCache()),
terminal.onResize(() => this._destroyLinesCache())
]);
);
}

window.clearTimeout(this._linesCacheTimeoutId);
Expand Down Expand Up @@ -678,7 +678,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
const disposables: IDisposable[] = [];
disposables.push(marker);
disposables.push(decoration.onRender((e) => this._applyStyles(e, options.activeMatchBorder, true)));
disposables.push(decoration.onDispose(() => disposeArray(disposables)));
disposables.push(decoration.onDispose(() => dispose(disposables)));
this._selectedDecoration.value = { decoration, match: result, dispose() { decoration.dispose(); } };
}
}
Expand Down Expand Up @@ -740,7 +740,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
const disposables: IDisposable[] = [];
disposables.push(marker);
disposables.push(findResultDecoration.onRender((e) => this._applyStyles(e, options.matchBorder, false)));
disposables.push(findResultDecoration.onDispose(() => disposeArray(disposables)));
disposables.push(findResultDecoration.onDispose(() => dispose(disposables)));
}
return findResultDecoration;
}
Expand Down
12 changes: 6 additions & 6 deletions addons/addon-webgl/src/GlyphRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { allowRescaling, throwIfFalsy } from 'browser/renderer/shared/RendererUt
import { TextureAtlas } from 'browser/renderer/shared/TextureAtlas';
import { IRasterizedGlyph, IRenderDimensions, ITextureAtlas } from 'browser/renderer/shared/Types';
import { NULL_CELL_CODE } from 'common/buffer/Constants';
import { Disposable, toDisposable } from 'common/Lifecycle';
import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
import { Terminal } from '@xterm/xterm';
import { IRenderModel, IWebGL2RenderingContext, IWebGLVertexArrayObject } from './Types';
import { createProgram, GLTexture, PROJECTION_MATRIX } from './WebglUtils';
Expand Down Expand Up @@ -127,7 +127,7 @@ export class GlyphRenderer extends Disposable {
}

this._program = throwIfFalsy(createProgram(gl, vertexShaderSource, createFragmentShaderSource(TextureAtlas.maxAtlasPages)));
this.register(toDisposable(() => gl.deleteProgram(this._program)));
this._register(toDisposable(() => gl.deleteProgram(this._program)));

// Uniform locations
this._projectionLocation = throwIfFalsy(gl.getUniformLocation(this._program, 'u_projection'));
Expand All @@ -141,7 +141,7 @@ export class GlyphRenderer extends Disposable {
// Setup a_unitquad, this defines the 4 vertices of a rectangle
const unitQuadVertices = new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]);
const unitQuadVerticesBuffer = gl.createBuffer();
this.register(toDisposable(() => gl.deleteBuffer(unitQuadVerticesBuffer)));
this._register(toDisposable(() => gl.deleteBuffer(unitQuadVerticesBuffer)));
gl.bindBuffer(gl.ARRAY_BUFFER, unitQuadVerticesBuffer);
gl.bufferData(gl.ARRAY_BUFFER, unitQuadVertices, gl.STATIC_DRAW);
gl.enableVertexAttribArray(VertexAttribLocations.UNIT_QUAD);
Expand All @@ -152,13 +152,13 @@ export class GlyphRenderer extends Disposable {
// triangle strip
const unitQuadElementIndices = new Uint8Array([0, 1, 2, 3]);
const elementIndicesBuffer = gl.createBuffer();
this.register(toDisposable(() => gl.deleteBuffer(elementIndicesBuffer)));
this._register(toDisposable(() => gl.deleteBuffer(elementIndicesBuffer)));
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementIndicesBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, unitQuadElementIndices, gl.STATIC_DRAW);

// Setup attributes
this._attributesBuffer = throwIfFalsy(gl.createBuffer());
this.register(toDisposable(() => gl.deleteBuffer(this._attributesBuffer)));
this._register(toDisposable(() => gl.deleteBuffer(this._attributesBuffer)));
gl.bindBuffer(gl.ARRAY_BUFFER, this._attributesBuffer);
gl.enableVertexAttribArray(VertexAttribLocations.OFFSET);
gl.vertexAttribPointer(VertexAttribLocations.OFFSET, 2, gl.FLOAT, false, BYTES_PER_CELL, 0);
Expand Down Expand Up @@ -193,7 +193,7 @@ export class GlyphRenderer extends Disposable {
this._atlasTextures = [];
for (let i = 0; i < TextureAtlas.maxAtlasPages; i++) {
const glTexture = new GLTexture(throwIfFalsy(gl.createTexture()));
this.register(toDisposable(() => gl.deleteTexture(glTexture.texture)));
this._register(toDisposable(() => gl.deleteTexture(glTexture.texture)));
gl.activeTexture(gl.TEXTURE0 + i);
gl.bindTexture(gl.TEXTURE_2D, glTexture.texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
Expand Down
12 changes: 6 additions & 6 deletions addons/addon-webgl/src/RectangleRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IRenderDimensions } from 'browser/renderer/shared/Types';
import { IThemeService } from 'browser/services/Services';
import { ReadonlyColorSet } from 'browser/Types';
import { Attributes, FgFlags } from 'common/buffer/Constants';
import { Disposable, toDisposable } from 'common/Lifecycle';
import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
import { IColor } from 'common/Types';
import { Terminal } from '@xterm/xterm';
import { RENDER_MODEL_BG_OFFSET, RENDER_MODEL_FG_OFFSET, RENDER_MODEL_INDICIES_PER_CELL } from './RenderModel';
Expand Down Expand Up @@ -96,7 +96,7 @@ export class RectangleRenderer extends Disposable {
const gl = this._gl;

this._program = throwIfFalsy(createProgram(gl, vertexShaderSource, fragmentShaderSource));
this.register(toDisposable(() => gl.deleteProgram(this._program)));
this._register(toDisposable(() => gl.deleteProgram(this._program)));

// Uniform locations
this._projectionLocation = throwIfFalsy(gl.getUniformLocation(this._program, 'u_projection'));
Expand All @@ -108,7 +108,7 @@ export class RectangleRenderer extends Disposable {
// Setup a_unitquad, this defines the 4 vertices of a rectangle
const unitQuadVertices = new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]);
const unitQuadVerticesBuffer = gl.createBuffer();
this.register(toDisposable(() => gl.deleteBuffer(unitQuadVerticesBuffer)));
this._register(toDisposable(() => gl.deleteBuffer(unitQuadVerticesBuffer)));
gl.bindBuffer(gl.ARRAY_BUFFER, unitQuadVerticesBuffer);
gl.bufferData(gl.ARRAY_BUFFER, unitQuadVertices, gl.STATIC_DRAW);
gl.enableVertexAttribArray(VertexAttribLocations.UNIT_QUAD);
Expand All @@ -119,13 +119,13 @@ export class RectangleRenderer extends Disposable {
// triangle strip
const unitQuadElementIndices = new Uint8Array([0, 1, 2, 3]);
const elementIndicesBuffer = gl.createBuffer();
this.register(toDisposable(() => gl.deleteBuffer(elementIndicesBuffer)));
this._register(toDisposable(() => gl.deleteBuffer(elementIndicesBuffer)));
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementIndicesBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, unitQuadElementIndices, gl.STATIC_DRAW);

// Setup attributes
this._attributesBuffer = throwIfFalsy(gl.createBuffer());
this.register(toDisposable(() => gl.deleteBuffer(this._attributesBuffer)));
this._register(toDisposable(() => gl.deleteBuffer(this._attributesBuffer)));
gl.bindBuffer(gl.ARRAY_BUFFER, this._attributesBuffer);
gl.enableVertexAttribArray(VertexAttribLocations.POSITION);
gl.vertexAttribPointer(VertexAttribLocations.POSITION, 2, gl.FLOAT, false, BYTES_PER_RECTANGLE, 0);
Expand All @@ -138,7 +138,7 @@ export class RectangleRenderer extends Disposable {
gl.vertexAttribDivisor(VertexAttribLocations.COLOR, 1);

this._updateCachedColors(_themeService.colors);
this.register(this._themeService.onChangeColors(e => {
this._register(this._themeService.onChangeColors(e => {
this._updateCachedColors(e);
this._updateViewportRectangle();
}));
Expand Down
24 changes: 12 additions & 12 deletions addons/addon-webgl/src/WebglAddon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { ITerminalAddon, Terminal } from '@xterm/xterm';
import type { WebglAddon as IWebglApi } from '@xterm/addon-webgl';
import { ICharacterJoinerService, ICharSizeService, ICoreBrowserService, IRenderService, IThemeService } from 'browser/services/Services';
import { ITerminal } from 'browser/Types';
import { Disposable, toDisposable } from 'common/Lifecycle';
import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
import { getSafariVersion, isSafari } from 'common/Platform';
import { ICoreService, IDecorationService, ILogService, IOptionsService } from 'common/services/Services';
import { IWebGL2RenderingContext } from './Types';
Expand All @@ -19,13 +19,13 @@ export class WebglAddon extends Disposable implements ITerminalAddon , IWebglApi
private _terminal?: Terminal;
private _renderer?: WebglRenderer;

private readonly _onChangeTextureAtlas = this.register(new Emitter<HTMLCanvasElement>());
private readonly _onChangeTextureAtlas = this._register(new Emitter<HTMLCanvasElement>());
public readonly onChangeTextureAtlas = this._onChangeTextureAtlas.event;
private readonly _onAddTextureAtlasCanvas = this.register(new Emitter<HTMLCanvasElement>());
private readonly _onAddTextureAtlasCanvas = this._register(new Emitter<HTMLCanvasElement>());
public readonly onAddTextureAtlasCanvas = this._onAddTextureAtlasCanvas.event;
private readonly _onRemoveTextureAtlasCanvas = this.register(new Emitter<HTMLCanvasElement>());
private readonly _onRemoveTextureAtlasCanvas = this._register(new Emitter<HTMLCanvasElement>());
public readonly onRemoveTextureAtlasCanvas = this._onRemoveTextureAtlasCanvas.event;
private readonly _onContextLoss = this.register(new Emitter<void>());
private readonly _onContextLoss = this._register(new Emitter<void>());
public readonly onContextLoss = this._onContextLoss.event;

constructor(
Expand All @@ -49,7 +49,7 @@ export class WebglAddon extends Disposable implements ITerminalAddon , IWebglApi
public activate(terminal: Terminal): void {
const core = (terminal as any)._core as ITerminal;
if (!terminal.element) {
this.register(core.onWillOpen(() => this.activate(terminal)));
this._register(core.onWillOpen(() => this.activate(terminal)));
return;
}

Expand All @@ -70,7 +70,7 @@ export class WebglAddon extends Disposable implements ITerminalAddon , IWebglApi
// bundled separately to the core module
setTraceLogger(logService);

this._renderer = this.register(new WebglRenderer(
this._renderer = this._register(new WebglRenderer(
terminal,
characterJoinerService,
charSizeService,
Expand All @@ -81,13 +81,13 @@ export class WebglAddon extends Disposable implements ITerminalAddon , IWebglApi
themeService,
this._preserveDrawingBuffer
));
this.register(Event.forward(this._renderer.onContextLoss, this._onContextLoss));
this.register(Event.forward(this._renderer.onChangeTextureAtlas, this._onChangeTextureAtlas));
this.register(Event.forward(this._renderer.onAddTextureAtlasCanvas, this._onAddTextureAtlasCanvas));
this.register(Event.forward(this._renderer.onRemoveTextureAtlasCanvas, this._onRemoveTextureAtlasCanvas));
this._register(Event.forward(this._renderer.onContextLoss, this._onContextLoss));
this._register(Event.forward(this._renderer.onChangeTextureAtlas, this._onChangeTextureAtlas));
this._register(Event.forward(this._renderer.onAddTextureAtlasCanvas, this._onAddTextureAtlasCanvas));
this._register(Event.forward(this._renderer.onRemoveTextureAtlasCanvas, this._onRemoveTextureAtlasCanvas));
renderService.setRenderer(this._renderer);

this.register(toDisposable(() => {
this._register(toDisposable(() => {
const renderService: IRenderService = (this._terminal as any)._core._renderService;
renderService.setRenderer((this._terminal as any)._core._createRenderer());
renderService.handleResize(terminal.cols, terminal.rows);
Expand Down
Loading

0 comments on commit cb3bd46

Please sign in to comment.