@xterm/xterm 5.6.0-beta.50 → 5.6.0-beta.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/lib/xterm.js +1 -1
  2. package/lib/xterm.js.map +1 -1
  3. package/lib/xterm.mjs +16 -16
  4. package/lib/xterm.mjs.map +4 -4
  5. package/package.json +1 -1
  6. package/src/browser/AccessibilityManager.ts +15 -15
  7. package/src/browser/CoreBrowserTerminal.ts +68 -68
  8. package/src/browser/Linkifier.ts +14 -12
  9. package/src/browser/Viewport.ts +15 -15
  10. package/src/browser/decorations/BufferDecorationRenderer.ts +8 -8
  11. package/src/browser/decorations/OverviewRulerRenderer.ts +11 -11
  12. package/src/browser/public/Terminal.ts +4 -4
  13. package/src/browser/renderer/dom/DomRenderer.ts +7 -7
  14. package/src/browser/renderer/shared/DevicePixelObserver.ts +1 -2
  15. package/src/browser/services/CharSizeService.ts +5 -5
  16. package/src/browser/services/CoreBrowserService.ts +15 -19
  17. package/src/browser/services/LinkProviderService.ts +2 -2
  18. package/src/browser/services/RenderService.ts +19 -19
  19. package/src/browser/services/SelectionService.ts +7 -7
  20. package/src/browser/services/ThemeService.ts +4 -4
  21. package/src/common/CircularList.ts +4 -4
  22. package/src/common/CoreTerminal.ts +27 -27
  23. package/src/common/InputHandler.ts +16 -16
  24. package/src/common/buffer/BufferSet.ts +4 -4
  25. package/src/common/buffer/Marker.ts +2 -2
  26. package/src/common/input/WriteBuffer.ts +2 -2
  27. package/src/common/parser/EscapeSequenceParser.ts +4 -4
  28. package/src/common/public/BufferNamespaceApi.ts +2 -2
  29. package/src/common/services/BufferService.ts +4 -4
  30. package/src/common/services/CoreMouseService.ts +2 -2
  31. package/src/common/services/CoreService.ts +5 -5
  32. package/src/common/services/DecorationService.ts +7 -8
  33. package/src/common/services/LogService.ts +2 -2
  34. package/src/common/services/OptionsService.ts +3 -3
  35. package/src/browser/Lifecycle.ts +0 -33
  36. package/src/common/Lifecycle.ts +0 -108
@@ -8,7 +8,7 @@ import { IInputHandler, IAttributeData, IDisposable, IWindowOptions, IColorEvent
8
8
  import { C0, C1 } from 'common/data/EscapeSequences';
9
9
  import { CHARSETS, DEFAULT_CHARSET } from 'common/data/Charsets';
10
10
  import { EscapeSequenceParser } from 'common/parser/EscapeSequenceParser';
11
- import { Disposable } from 'common/Lifecycle';
11
+ import { Disposable } from 'vs/base/common/lifecycle';
12
12
  import { StringToUtf32, stringFromCodePoint, Utf8ToUtf32 } from 'common/input/TextDecoder';
13
13
  import { BufferLine, DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine';
14
14
  import { IParsingState, IEscapeSequenceParser, IParams, IFunctionIdentifier } from 'common/parser/Types';
@@ -131,32 +131,32 @@ export class InputHandler extends Disposable implements IInputHandler {
131
131
 
132
132
  private _activeBuffer: IBuffer;
133
133
 
134
- private readonly _onRequestBell = this.register(new Emitter<void>());
134
+ private readonly _onRequestBell = this._register(new Emitter<void>());
135
135
  public readonly onRequestBell = this._onRequestBell.event;
136
- private readonly _onRequestRefreshRows = this.register(new Emitter<{ start: number, end: number } | undefined>());
136
+ private readonly _onRequestRefreshRows = this._register(new Emitter<{ start: number, end: number } | undefined>());
137
137
  public readonly onRequestRefreshRows = this._onRequestRefreshRows.event;
138
- private readonly _onRequestReset = this.register(new Emitter<void>());
138
+ private readonly _onRequestReset = this._register(new Emitter<void>());
139
139
  public readonly onRequestReset = this._onRequestReset.event;
140
- private readonly _onRequestSendFocus = this.register(new Emitter<void>());
140
+ private readonly _onRequestSendFocus = this._register(new Emitter<void>());
141
141
  public readonly onRequestSendFocus = this._onRequestSendFocus.event;
142
- private readonly _onRequestSyncScrollBar = this.register(new Emitter<void>());
142
+ private readonly _onRequestSyncScrollBar = this._register(new Emitter<void>());
143
143
  public readonly onRequestSyncScrollBar = this._onRequestSyncScrollBar.event;
144
- private readonly _onRequestWindowsOptionsReport = this.register(new Emitter<WindowsOptionsReportType>());
144
+ private readonly _onRequestWindowsOptionsReport = this._register(new Emitter<WindowsOptionsReportType>());
145
145
  public readonly onRequestWindowsOptionsReport = this._onRequestWindowsOptionsReport.event;
146
146
 
147
- private readonly _onA11yChar = this.register(new Emitter<string>());
147
+ private readonly _onA11yChar = this._register(new Emitter<string>());
148
148
  public readonly onA11yChar = this._onA11yChar.event;
149
- private readonly _onA11yTab = this.register(new Emitter<number>());
149
+ private readonly _onA11yTab = this._register(new Emitter<number>());
150
150
  public readonly onA11yTab = this._onA11yTab.event;
151
- private readonly _onCursorMove = this.register(new Emitter<void>());
151
+ private readonly _onCursorMove = this._register(new Emitter<void>());
152
152
  public readonly onCursorMove = this._onCursorMove.event;
153
- private readonly _onLineFeed = this.register(new Emitter<void>());
153
+ private readonly _onLineFeed = this._register(new Emitter<void>());
154
154
  public readonly onLineFeed = this._onLineFeed.event;
155
- private readonly _onScroll = this.register(new Emitter<number>());
155
+ private readonly _onScroll = this._register(new Emitter<number>());
156
156
  public readonly onScroll = this._onScroll.event;
157
- private readonly _onTitleChange = this.register(new Emitter<string>());
157
+ private readonly _onTitleChange = this._register(new Emitter<string>());
158
158
  public readonly onTitleChange = this._onTitleChange.event;
159
- private readonly _onColor = this.register(new Emitter<IColorEvent>());
159
+ private readonly _onColor = this._register(new Emitter<IColorEvent>());
160
160
  public readonly onColor = this._onColor.event;
161
161
 
162
162
  private _parseStack: IParseStack = {
@@ -179,12 +179,12 @@ export class InputHandler extends Disposable implements IInputHandler {
179
179
  private readonly _parser: IEscapeSequenceParser = new EscapeSequenceParser()
180
180
  ) {
181
181
  super();
182
- this.register(this._parser);
182
+ this._register(this._parser);
183
183
  this._dirtyRowTracker = new DirtyRowTracker(this._bufferService);
184
184
 
185
185
  // Track properties used in performance critical code manually to avoid using slow getters
186
186
  this._activeBuffer = this._bufferService.buffer;
187
- this.register(this._bufferService.buffers.onBufferActivate(e => this._activeBuffer = e.activeBuffer));
187
+ this._register(this._bufferService.buffers.onBufferActivate(e => this._activeBuffer = e.activeBuffer));
188
188
 
189
189
  /**
190
190
  * custom fallback handlers
@@ -3,7 +3,7 @@
3
3
  * @license MIT
4
4
  */
5
5
 
6
- import { Disposable } from 'common/Lifecycle';
6
+ import { Disposable } from 'vs/base/common/lifecycle';
7
7
  import { IAttributeData } from 'common/Types';
8
8
  import { Buffer } from 'common/buffer/Buffer';
9
9
  import { IBuffer, IBufferSet } from 'common/buffer/Types';
@@ -19,7 +19,7 @@ export class BufferSet extends Disposable implements IBufferSet {
19
19
  private _alt!: Buffer;
20
20
  private _activeBuffer!: Buffer;
21
21
 
22
- private readonly _onBufferActivate = this.register(new Emitter<{ activeBuffer: IBuffer, inactiveBuffer: IBuffer }>());
22
+ private readonly _onBufferActivate = this._register(new Emitter<{ activeBuffer: IBuffer, inactiveBuffer: IBuffer }>());
23
23
  public readonly onBufferActivate = this._onBufferActivate.event;
24
24
 
25
25
  /**
@@ -31,8 +31,8 @@ export class BufferSet extends Disposable implements IBufferSet {
31
31
  ) {
32
32
  super();
33
33
  this.reset();
34
- this.register(this._optionsService.onSpecificOptionChange('scrollback', () => this.resize(this._bufferService.cols, this._bufferService.rows)));
35
- this.register(this._optionsService.onSpecificOptionChange('tabStopWidth', () => this.setupTabStops()));
34
+ this._register(this._optionsService.onSpecificOptionChange('scrollback', () => this.resize(this._bufferService.cols, this._bufferService.rows)));
35
+ this._register(this._optionsService.onSpecificOptionChange('tabStopWidth', () => this.setupTabStops()));
36
36
  }
37
37
 
38
38
  public reset(): void {
@@ -3,9 +3,9 @@
3
3
  * @license MIT
4
4
  */
5
5
 
6
- import { disposeArray } from 'common/Lifecycle';
7
6
  import { IDisposable, IMarker } from 'common/Types';
8
7
  import { Emitter } from 'vs/base/common/event';
8
+ import { dispose } from 'vs/base/common/lifecycle';
9
9
 
10
10
  export class Marker implements IMarker {
11
11
  private static _nextId = 1;
@@ -32,7 +32,7 @@ export class Marker implements IMarker {
32
32
  this.line = -1;
33
33
  // Emit before super.dispose such that dispose listeners get a change to react
34
34
  this._onDispose.fire();
35
- disposeArray(this._disposables);
35
+ dispose(this._disposables);
36
36
  this._disposables.length = 0;
37
37
  }
38
38
 
@@ -4,7 +4,7 @@
4
4
  * @license MIT
5
5
  */
6
6
 
7
- import { Disposable } from 'common/Lifecycle';
7
+ import { Disposable } from 'vs/base/common/lifecycle';
8
8
  import { Emitter } from 'vs/base/common/event';
9
9
 
10
10
  declare const setTimeout: (handler: () => void, timeout?: number) => void;
@@ -43,7 +43,7 @@ export class WriteBuffer extends Disposable {
43
43
  private _syncCalls = 0;
44
44
  private _didUserInput = false;
45
45
 
46
- private readonly _onWriteParsed = this.register(new Emitter<void>());
46
+ private readonly _onWriteParsed = this._register(new Emitter<void>());
47
47
  public readonly onWriteParsed = this._onWriteParsed.event;
48
48
 
49
49
  constructor(private _action: (data: string | Uint8Array, promiseResult?: boolean) => void | Promise<boolean>) {
@@ -5,7 +5,7 @@
5
5
 
6
6
  import { IParsingState, IDcsHandler, IEscapeSequenceParser, IParams, IOscHandler, IHandlerCollection, CsiHandlerType, OscFallbackHandlerType, IOscParser, EscHandlerType, IDcsParser, DcsFallbackHandlerType, IFunctionIdentifier, ExecuteFallbackHandlerType, CsiFallbackHandlerType, EscFallbackHandlerType, PrintHandlerType, PrintFallbackHandlerType, ExecuteHandlerType, IParserStackState, ParserStackType, ResumableHandlersType } from 'common/parser/Types';
7
7
  import { ParserState, ParserAction } from 'common/parser/Constants';
8
- import { Disposable, toDisposable } from 'common/Lifecycle';
8
+ import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
9
9
  import { IDisposable } from 'common/Types';
10
10
  import { Params } from 'common/parser/Params';
11
11
  import { OscParser } from 'common/parser/OscParser';
@@ -283,13 +283,13 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
283
283
  this._executeHandlers = Object.create(null);
284
284
  this._csiHandlers = Object.create(null);
285
285
  this._escHandlers = Object.create(null);
286
- this.register(toDisposable(() => {
286
+ this._register(toDisposable(() => {
287
287
  this._csiHandlers = Object.create(null);
288
288
  this._executeHandlers = Object.create(null);
289
289
  this._escHandlers = Object.create(null);
290
290
  }));
291
- this._oscParser = this.register(new OscParser());
292
- this._dcsParser = this.register(new DcsParser());
291
+ this._oscParser = this._register(new OscParser());
292
+ this._dcsParser = this._register(new DcsParser());
293
293
  this._errorHandler = this._errorHandlerFb;
294
294
 
295
295
  // swallow 7bit ST (ESC+\)
@@ -6,14 +6,14 @@
6
6
  import { IBuffer as IBufferApi, IBufferNamespace as IBufferNamespaceApi } from '@xterm/xterm';
7
7
  import { BufferApiView } from 'common/public/BufferApiView';
8
8
  import { ICoreTerminal } from 'common/Types';
9
- import { Disposable } from 'common/Lifecycle';
9
+ import { Disposable } from 'vs/base/common/lifecycle';
10
10
  import { Emitter } from 'vs/base/common/event';
11
11
 
12
12
  export class BufferNamespaceApi extends Disposable implements IBufferNamespaceApi {
13
13
  private _normal: BufferApiView;
14
14
  private _alternate: BufferApiView;
15
15
 
16
- private readonly _onBufferChange = this.register(new Emitter<IBufferApi>());
16
+ private readonly _onBufferChange = this._register(new Emitter<IBufferApi>());
17
17
  public readonly onBufferChange = this._onBufferChange.event;
18
18
 
19
19
  constructor(private _core: ICoreTerminal) {
@@ -3,7 +3,7 @@
3
3
  * @license MIT
4
4
  */
5
5
 
6
- import { Disposable } from 'common/Lifecycle';
6
+ import { Disposable } from 'vs/base/common/lifecycle';
7
7
  import { IAttributeData, IBufferLine } from 'common/Types';
8
8
  import { BufferSet } from 'common/buffer/BufferSet';
9
9
  import { IBuffer, IBufferSet } from 'common/buffer/Types';
@@ -22,9 +22,9 @@ export class BufferService extends Disposable implements IBufferService {
22
22
  /** Whether the user is scrolling (locks the scroll position) */
23
23
  public isUserScrolling: boolean = false;
24
24
 
25
- private readonly _onResize = this.register(new Emitter<{ cols: number, rows: number }>());
25
+ private readonly _onResize = this._register(new Emitter<{ cols: number, rows: number }>());
26
26
  public readonly onResize = this._onResize.event;
27
- private readonly _onScroll = this.register(new Emitter<number>());
27
+ private readonly _onScroll = this._register(new Emitter<number>());
28
28
  public readonly onScroll = this._onScroll.event;
29
29
 
30
30
  public get buffer(): IBuffer { return this.buffers.active; }
@@ -36,7 +36,7 @@ export class BufferService extends Disposable implements IBufferService {
36
36
  super();
37
37
  this.cols = Math.max(optionsService.rawOptions.cols || 0, MINIMUM_COLS);
38
38
  this.rows = Math.max(optionsService.rawOptions.rows || 0, MINIMUM_ROWS);
39
- this.buffers = this.register(new BufferSet(optionsService, this));
39
+ this.buffers = this._register(new BufferSet(optionsService, this));
40
40
  }
41
41
 
42
42
  public resize(cols: number, rows: number): void {
@@ -4,7 +4,7 @@
4
4
  */
5
5
  import { IBufferService, ICoreService, ICoreMouseService } from 'common/services/Services';
6
6
  import { ICoreMouseProtocol, ICoreMouseEvent, CoreMouseEncoding, CoreMouseEventType, CoreMouseButton, CoreMouseAction } from 'common/Types';
7
- import { Disposable } from 'common/Lifecycle';
7
+ import { Disposable } from 'vs/base/common/lifecycle';
8
8
  import { Emitter } from 'vs/base/common/event';
9
9
 
10
10
  /**
@@ -175,7 +175,7 @@ export class CoreMouseService extends Disposable implements ICoreMouseService {
175
175
  private _activeEncoding: string = '';
176
176
  private _lastEvent: ICoreMouseEvent | null = null;
177
177
 
178
- private readonly _onProtocolChange = this.register(new Emitter<CoreMouseEventType>());
178
+ private readonly _onProtocolChange = this._register(new Emitter<CoreMouseEventType>());
179
179
  public readonly onProtocolChange = this._onProtocolChange.event;
180
180
 
181
181
  constructor(
@@ -4,7 +4,7 @@
4
4
  */
5
5
 
6
6
  import { clone } from 'common/Clone';
7
- import { Disposable } from 'common/Lifecycle';
7
+ import { Disposable } from 'vs/base/common/lifecycle';
8
8
  import { IDecPrivateModes, IModes } from 'common/Types';
9
9
  import { IBufferService, ICoreService, ILogService, IOptionsService } from 'common/services/Services';
10
10
  import { Emitter } from 'vs/base/common/event';
@@ -31,13 +31,13 @@ export class CoreService extends Disposable implements ICoreService {
31
31
  public modes: IModes;
32
32
  public decPrivateModes: IDecPrivateModes;
33
33
 
34
- private readonly _onData = this.register(new Emitter<string>());
34
+ private readonly _onData = this._register(new Emitter<string>());
35
35
  public readonly onData = this._onData.event;
36
- private readonly _onUserInput = this.register(new Emitter<void>());
36
+ private readonly _onUserInput = this._register(new Emitter<void>());
37
37
  public readonly onUserInput = this._onUserInput.event;
38
- private readonly _onBinary = this.register(new Emitter<string>());
38
+ private readonly _onBinary = this._register(new Emitter<string>());
39
39
  public readonly onBinary = this._onBinary.event;
40
- private readonly _onRequestScrollToBottom = this.register(new Emitter<void>());
40
+ private readonly _onRequestScrollToBottom = this._register(new Emitter<void>());
41
41
  public readonly onRequestScrollToBottom = this._onRequestScrollToBottom.event;
42
42
 
43
43
  constructor(
@@ -4,7 +4,7 @@
4
4
  */
5
5
 
6
6
  import { css } from 'common/Color';
7
- import { Disposable, toDisposable } from 'common/Lifecycle';
7
+ import { Disposable, DisposableStore, toDisposable } from 'vs/base/common/lifecycle';
8
8
  import { IDecorationService, IInternalDecoration } from 'common/services/Services';
9
9
  import { SortedList } from 'common/SortedList';
10
10
  import { IColor } from 'common/Types';
@@ -25,9 +25,9 @@ export class DecorationService extends Disposable implements IDecorationService
25
25
  */
26
26
  private readonly _decorations: SortedList<IInternalDecoration> = new SortedList(e => e?.marker.line);
27
27
 
28
- private readonly _onDecorationRegistered = this.register(new Emitter<IInternalDecoration>());
28
+ private readonly _onDecorationRegistered = this._register(new Emitter<IInternalDecoration>());
29
29
  public readonly onDecorationRegistered = this._onDecorationRegistered.event;
30
- private readonly _onDecorationRemoved = this.register(new Emitter<IInternalDecoration>());
30
+ private readonly _onDecorationRemoved = this._register(new Emitter<IInternalDecoration>());
31
31
  public readonly onDecorationRemoved = this._onDecorationRemoved.event;
32
32
 
33
33
  public get decorations(): IterableIterator<IInternalDecoration> { return this._decorations.values(); }
@@ -35,7 +35,7 @@ export class DecorationService extends Disposable implements IDecorationService
35
35
  constructor() {
36
36
  super();
37
37
 
38
- this.register(toDisposable(() => this.reset()));
38
+ this._register(toDisposable(() => this.reset()));
39
39
  }
40
40
 
41
41
  public registerDecoration(options: IDecorationOptions): IDecoration | undefined {
@@ -90,14 +90,13 @@ export class DecorationService extends Disposable implements IDecorationService
90
90
  }
91
91
  }
92
92
 
93
- class Decoration extends Disposable implements IInternalDecoration {
93
+ class Decoration extends DisposableStore implements IInternalDecoration {
94
94
  public readonly marker: IMarker;
95
95
  public element: HTMLElement | undefined;
96
- public get isDisposed(): boolean { return this._isDisposed; }
97
96
 
98
- public readonly onRenderEmitter = this.register(new Emitter<HTMLElement>());
97
+ public readonly onRenderEmitter = this.add(new Emitter<HTMLElement>());
99
98
  public readonly onRender = this.onRenderEmitter.event;
100
- private readonly _onDispose = this.register(new Emitter<void>());
99
+ private readonly _onDispose = this.add(new Emitter<void>());
101
100
  public readonly onDispose = this._onDispose.event;
102
101
 
103
102
  private _cachedBg: IColor | undefined | null = null;
@@ -3,7 +3,7 @@
3
3
  * @license MIT
4
4
  */
5
5
 
6
- import { Disposable } from 'common/Lifecycle';
6
+ import { Disposable } from 'vs/base/common/lifecycle';
7
7
  import { ILogService, IOptionsService, LogLevelEnum } from 'common/services/Services';
8
8
 
9
9
  type LogType = (message?: any, ...optionalParams: any[]) => void;
@@ -42,7 +42,7 @@ export class LogService extends Disposable implements ILogService {
42
42
  ) {
43
43
  super();
44
44
  this._updateLogLevel();
45
- this.register(this._optionsService.onSpecificOptionChange('logLevel', () => this._updateLogLevel()));
45
+ this._register(this._optionsService.onSpecificOptionChange('logLevel', () => this._updateLogLevel()));
46
46
 
47
47
  // For trace logging, assume the latest created log service is valid
48
48
  traceLogger = this;
@@ -3,7 +3,7 @@
3
3
  * @license MIT
4
4
  */
5
5
 
6
- import { Disposable, toDisposable } from 'common/Lifecycle';
6
+ import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
7
7
  import { isMac } from 'common/Platform';
8
8
  import { CursorStyle, IDisposable } from 'common/Types';
9
9
  import { FontWeight, IOptionsService, ITerminalOptions } from 'common/services/Services';
@@ -65,7 +65,7 @@ export class OptionsService extends Disposable implements IOptionsService {
65
65
  public readonly rawOptions: Required<ITerminalOptions>;
66
66
  public options: Required<ITerminalOptions>;
67
67
 
68
- private readonly _onOptionChange = this.register(new Emitter<keyof ITerminalOptions>());
68
+ private readonly _onOptionChange = this._register(new Emitter<keyof ITerminalOptions>());
69
69
  public readonly onOptionChange = this._onOptionChange.event;
70
70
 
71
71
  constructor(options: Partial<ITerminalOptions>) {
@@ -90,7 +90,7 @@ export class OptionsService extends Disposable implements IOptionsService {
90
90
 
91
91
  // Clear out options that could link outside xterm.js as they could easily cause an embedder
92
92
  // memory leak
93
- this.register(toDisposable(() => {
93
+ this._register(toDisposable(() => {
94
94
  this.rawOptions.linkHandler = null;
95
95
  this.rawOptions.documentOverride = null;
96
96
  }));
@@ -1,33 +0,0 @@
1
- /**
2
- * Copyright (c) 2018 The xterm.js authors. All rights reserved.
3
- * @license MIT
4
- */
5
-
6
- import { IDisposable } from 'common/Types';
7
-
8
- /**
9
- * Adds a disposable listener to a node in the DOM, returning the disposable.
10
- * @param node The node to add a listener to.
11
- * @param type The event type.
12
- * @param handler The handler for the listener.
13
- * @param options The boolean or options object to pass on to the event
14
- * listener.
15
- */
16
- export function addDisposableDomListener(
17
- node: Element | Window | Document,
18
- type: string,
19
- handler: (e: any) => void,
20
- options?: boolean | AddEventListenerOptions
21
- ): IDisposable {
22
- node.addEventListener(type, handler, options);
23
- let disposed = false;
24
- return {
25
- dispose: () => {
26
- if (disposed) {
27
- return;
28
- }
29
- disposed = true;
30
- node.removeEventListener(type, handler, options);
31
- }
32
- };
33
- }
@@ -1,108 +0,0 @@
1
- /**
2
- * Copyright (c) 2018 The xterm.js authors. All rights reserved.
3
- * @license MIT
4
- */
5
-
6
- import { IDisposable } from 'common/Types';
7
-
8
- /**
9
- * A base class that can be extended to provide convenience methods for managing the lifecycle of an
10
- * object and its components.
11
- */
12
- export abstract class Disposable implements IDisposable {
13
- protected _disposables: IDisposable[] = [];
14
- protected _isDisposed: boolean = false;
15
-
16
- /**
17
- * Disposes the object, triggering the `dispose` method on all registered IDisposables.
18
- */
19
- public dispose(): void {
20
- this._isDisposed = true;
21
- for (const d of this._disposables) {
22
- d.dispose();
23
- }
24
- this._disposables.length = 0;
25
- }
26
-
27
- /**
28
- * Registers a disposable object.
29
- * @param d The disposable to register.
30
- * @returns The disposable.
31
- */
32
- public register<T extends IDisposable>(d: T): T {
33
- this._disposables.push(d);
34
- return d;
35
- }
36
-
37
- /**
38
- * Unregisters a disposable object if it has been registered, if not do
39
- * nothing.
40
- * @param d The disposable to unregister.
41
- */
42
- public unregister<T extends IDisposable>(d: T): void {
43
- const index = this._disposables.indexOf(d);
44
- if (index !== -1) {
45
- this._disposables.splice(index, 1);
46
- }
47
- }
48
- }
49
-
50
- export class MutableDisposable<T extends IDisposable> implements IDisposable {
51
- private _value?: T;
52
- private _isDisposed = false;
53
-
54
- /**
55
- * Gets the value if it exists.
56
- */
57
- public get value(): T | undefined {
58
- return this._isDisposed ? undefined : this._value;
59
- }
60
-
61
- /**
62
- * Sets the value, disposing of the old value if it exists.
63
- */
64
- public set value(value: T | undefined) {
65
- if (this._isDisposed || value === this._value) {
66
- return;
67
- }
68
- this._value?.dispose();
69
- this._value = value;
70
- }
71
-
72
- /**
73
- * Resets the stored value and disposes of the previously stored value.
74
- */
75
- public clear(): void {
76
- this.value = undefined;
77
- }
78
-
79
- public dispose(): void {
80
- this._isDisposed = true;
81
- this._value?.dispose();
82
- this._value = undefined;
83
- }
84
- }
85
-
86
- /**
87
- * Wrap a function in a disposable.
88
- */
89
- export function toDisposable(f: () => void): IDisposable {
90
- return { dispose: f };
91
- }
92
-
93
- /**
94
- * Dispose of all disposables in an array and set its length to 0.
95
- */
96
- export function disposeArray(disposables: IDisposable[]): void {
97
- for (const d of disposables) {
98
- d.dispose();
99
- }
100
- disposables.length = 0;
101
- }
102
-
103
- /**
104
- * Creates a disposable that will dispose of an array of disposables when disposed.
105
- */
106
- export function getDisposeArrayDisposable(array: IDisposable[]): IDisposable {
107
- return { dispose: () => disposeArray(array) };
108
- }