@xterm/xterm 5.6.0-beta.7 → 5.6.0-beta.70

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 (118) hide show
  1. package/README.md +6 -3
  2. package/css/xterm.css +71 -4
  3. package/lib/xterm.js +1 -1
  4. package/lib/xterm.js.map +1 -1
  5. package/lib/xterm.mjs +53 -0
  6. package/lib/xterm.mjs.map +7 -0
  7. package/package.json +43 -33
  8. package/src/browser/AccessibilityManager.ts +53 -25
  9. package/src/browser/{Terminal.ts → CoreBrowserTerminal.ts} +135 -146
  10. package/src/browser/Linkifier.ts +26 -14
  11. package/src/browser/LocalizableStrings.ts +15 -4
  12. package/src/browser/{Types.d.ts → Types.ts} +67 -15
  13. package/src/browser/Viewport.ts +143 -370
  14. package/src/browser/decorations/BufferDecorationRenderer.ts +14 -9
  15. package/src/browser/decorations/OverviewRulerRenderer.ts +40 -44
  16. package/src/browser/public/Terminal.ts +25 -19
  17. package/src/browser/renderer/dom/DomRenderer.ts +14 -16
  18. package/src/browser/renderer/shared/CharAtlasUtils.ts +4 -0
  19. package/src/browser/renderer/shared/CustomGlyphs.ts +6 -0
  20. package/src/browser/renderer/shared/DevicePixelObserver.ts +1 -2
  21. package/src/browser/renderer/shared/TextureAtlas.ts +3 -3
  22. package/src/browser/renderer/shared/{Types.d.ts → Types.ts} +4 -4
  23. package/src/browser/services/CharSizeService.ts +6 -6
  24. package/src/browser/services/CoreBrowserService.ts +15 -15
  25. package/src/browser/services/LinkProviderService.ts +2 -2
  26. package/src/browser/services/RenderService.ts +20 -20
  27. package/src/browser/services/SelectionService.ts +8 -8
  28. package/src/browser/services/Services.ts +13 -13
  29. package/src/browser/services/ThemeService.ts +17 -56
  30. package/src/browser/shared/Constants.ts +8 -0
  31. package/src/common/CircularList.ts +5 -5
  32. package/src/common/CoreTerminal.ts +35 -41
  33. package/src/common/InputHandler.ts +34 -28
  34. package/src/common/{Types.d.ts → Types.ts} +11 -17
  35. package/src/common/buffer/Buffer.ts +5 -1
  36. package/src/common/buffer/BufferSet.ts +5 -5
  37. package/src/common/buffer/Marker.ts +4 -4
  38. package/src/common/buffer/{Types.d.ts → Types.ts} +2 -2
  39. package/src/common/input/WriteBuffer.ts +3 -3
  40. package/src/common/parser/EscapeSequenceParser.ts +4 -4
  41. package/src/common/public/BufferNamespaceApi.ts +3 -3
  42. package/src/common/services/BufferService.ts +7 -7
  43. package/src/common/services/CoreMouseService.ts +5 -3
  44. package/src/common/services/CoreService.ts +6 -6
  45. package/src/common/services/DecorationService.ts +8 -9
  46. package/src/common/services/LogService.ts +2 -2
  47. package/src/common/services/OptionsService.ts +5 -5
  48. package/src/common/services/Services.ts +24 -17
  49. package/src/common/services/UnicodeService.ts +2 -2
  50. package/src/vs/base/browser/browser.ts +141 -0
  51. package/src/vs/base/browser/canIUse.ts +49 -0
  52. package/src/vs/base/browser/dom.ts +2369 -0
  53. package/src/vs/base/browser/fastDomNode.ts +316 -0
  54. package/src/vs/base/browser/globalPointerMoveMonitor.ts +112 -0
  55. package/src/vs/base/browser/iframe.ts +135 -0
  56. package/src/vs/base/browser/keyboardEvent.ts +213 -0
  57. package/src/vs/base/browser/mouseEvent.ts +229 -0
  58. package/src/vs/base/browser/touch.ts +372 -0
  59. package/src/vs/base/browser/ui/scrollbar/abstractScrollbar.ts +303 -0
  60. package/src/vs/base/browser/ui/scrollbar/horizontalScrollbar.ts +114 -0
  61. package/src/vs/base/browser/ui/scrollbar/scrollableElement.ts +720 -0
  62. package/src/vs/base/browser/ui/scrollbar/scrollableElementOptions.ts +165 -0
  63. package/src/vs/base/browser/ui/scrollbar/scrollbarArrow.ts +114 -0
  64. package/src/vs/base/browser/ui/scrollbar/scrollbarState.ts +243 -0
  65. package/src/vs/base/browser/ui/scrollbar/scrollbarVisibilityController.ts +118 -0
  66. package/src/vs/base/browser/ui/scrollbar/verticalScrollbar.ts +116 -0
  67. package/src/vs/base/browser/ui/widget.ts +57 -0
  68. package/src/vs/base/browser/window.ts +14 -0
  69. package/src/vs/base/common/arrays.ts +887 -0
  70. package/src/vs/base/common/arraysFind.ts +202 -0
  71. package/src/vs/base/common/assert.ts +71 -0
  72. package/src/vs/base/common/async.ts +1992 -0
  73. package/src/vs/base/common/cancellation.ts +148 -0
  74. package/src/vs/base/common/charCode.ts +450 -0
  75. package/src/vs/base/common/collections.ts +140 -0
  76. package/src/vs/base/common/decorators.ts +130 -0
  77. package/src/vs/base/common/equals.ts +146 -0
  78. package/src/vs/base/common/errors.ts +303 -0
  79. package/src/vs/base/common/event.ts +1778 -0
  80. package/src/vs/base/common/functional.ts +32 -0
  81. package/src/vs/base/common/hash.ts +316 -0
  82. package/src/vs/base/common/iterator.ts +159 -0
  83. package/src/vs/base/common/keyCodes.ts +526 -0
  84. package/src/vs/base/common/keybindings.ts +284 -0
  85. package/src/vs/base/common/lazy.ts +47 -0
  86. package/src/vs/base/common/lifecycle.ts +801 -0
  87. package/src/vs/base/common/linkedList.ts +142 -0
  88. package/src/vs/base/common/map.ts +202 -0
  89. package/src/vs/base/common/numbers.ts +98 -0
  90. package/src/vs/base/common/observable.ts +76 -0
  91. package/src/vs/base/common/observableInternal/api.ts +31 -0
  92. package/src/vs/base/common/observableInternal/autorun.ts +281 -0
  93. package/src/vs/base/common/observableInternal/base.ts +489 -0
  94. package/src/vs/base/common/observableInternal/debugName.ts +145 -0
  95. package/src/vs/base/common/observableInternal/derived.ts +428 -0
  96. package/src/vs/base/common/observableInternal/lazyObservableValue.ts +146 -0
  97. package/src/vs/base/common/observableInternal/logging.ts +328 -0
  98. package/src/vs/base/common/observableInternal/promise.ts +209 -0
  99. package/src/vs/base/common/observableInternal/utils.ts +610 -0
  100. package/src/vs/base/common/platform.ts +281 -0
  101. package/src/vs/base/common/scrollable.ts +522 -0
  102. package/src/vs/base/common/sequence.ts +34 -0
  103. package/src/vs/base/common/stopwatch.ts +43 -0
  104. package/src/vs/base/common/strings.ts +557 -0
  105. package/src/vs/base/common/symbols.ts +9 -0
  106. package/src/vs/base/common/uint.ts +59 -0
  107. package/src/vs/patches/nls.ts +90 -0
  108. package/src/vs/typings/base-common.d.ts +20 -0
  109. package/src/vs/typings/require.d.ts +42 -0
  110. package/src/vs/typings/thenable.d.ts +12 -0
  111. package/src/vs/typings/vscode-globals-nls.d.ts +36 -0
  112. package/src/vs/typings/vscode-globals-product.d.ts +33 -0
  113. package/typings/xterm.d.ts +59 -15
  114. package/src/browser/Lifecycle.ts +0 -33
  115. package/src/common/EventEmitter.ts +0 -78
  116. package/src/common/Lifecycle.ts +0 -108
  117. /package/src/browser/selection/{Types.d.ts → Types.ts} +0 -0
  118. /package/src/common/parser/{Types.d.ts → Types.ts} +0 -0
@@ -0,0 +1,20 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+
6
+ // Declare types that we probe for to implement util and/or polyfill functions
7
+
8
+ declare global {
9
+
10
+ interface IdleDeadline {
11
+ readonly didTimeout: boolean;
12
+ timeRemaining(): number;
13
+ }
14
+
15
+ function requestIdleCallback(callback: (args: IdleDeadline) => void, options?: { timeout: number }): number;
16
+ function cancelIdleCallback(handle: number): void;
17
+
18
+ }
19
+
20
+ export { }
@@ -0,0 +1,42 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+
6
+ declare class LoaderEvent {
7
+ readonly type: number;
8
+ readonly timestamp: number;
9
+ readonly detail: string;
10
+ }
11
+
12
+ declare const define: {
13
+ (moduleName: string, dependencies: string[], callback: (...args: any[]) => any): any;
14
+ (moduleName: string, dependencies: string[], definition: any): any;
15
+ (moduleName: string, callback: (...args: any[]) => any): any;
16
+ (moduleName: string, definition: any): any;
17
+ (dependencies: string[], callback: (...args: any[]) => any): any;
18
+ (dependencies: string[], definition: any): any;
19
+ };
20
+
21
+ interface NodeRequire {
22
+ /**
23
+ * @deprecated use `FileAccess.asFileUri()` for node.js contexts or `FileAccess.asBrowserUri` for browser contexts.
24
+ */
25
+ toUrl(path: string): string;
26
+
27
+ /**
28
+ * @deprecated MUST not be used anymore
29
+ *
30
+ * With the move from AMD to ESM we cannot use this anymore. There will be NO MORE node require like this.
31
+ */
32
+ __$__nodeRequire<T>(moduleName: string): T;
33
+
34
+ (dependencies: string[], callback: (...args: any[]) => any, errorback?: (err: any) => void): any;
35
+ config(data: any): any;
36
+ onError: Function;
37
+ getStats?(): ReadonlyArray<LoaderEvent>;
38
+ hasDependencyCycle?(): boolean;
39
+ define(amdModuleId: string, dependencies: string[], callback: (...args: any[]) => any): any;
40
+ }
41
+
42
+ declare var require: NodeRequire;
@@ -0,0 +1,12 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+
6
+ /**
7
+ * Thenable is a common denominator between ES6 promises, Q, jquery.Deferred, WinJS.Promise,
8
+ * and others. This API makes no assumption about what promise library is being used which
9
+ * enables reusing existing code without migrating to a specific promise implementation. Still,
10
+ * we recommend the use of native promises which are available in VS Code.
11
+ */
12
+ interface Thenable<T> extends PromiseLike<T> { }
@@ -0,0 +1,36 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+
6
+ // AMD2ESM mirgation relevant
7
+
8
+ /**
9
+ * NLS Globals: these need to be defined in all contexts that make
10
+ * use of our `nls.localize` and `nls.localize2` functions. This includes:
11
+ * - Electron main process
12
+ * - Electron window (renderer) process
13
+ * - Utility Process
14
+ * - Node.js
15
+ * - Browser
16
+ * - Web worker
17
+ *
18
+ * That is because during build time we strip out all english strings from
19
+ * the resulting JS code and replace it with a <number> that is then looked
20
+ * up from the `_VSCODE_NLS_MESSAGES` array.
21
+ */
22
+ declare global {
23
+ /**
24
+ * All NLS messages produced by `localize` and `localize2` calls
25
+ * under `src/vs` translated to the language as indicated by
26
+ * `_VSCODE_NLS_LANGUAGE`.
27
+ */
28
+ var _VSCODE_NLS_MESSAGES: string[];
29
+ /**
30
+ * The actual language of the NLS messages (e.g. 'en', de' or 'pt-br').
31
+ */
32
+ var _VSCODE_NLS_LANGUAGE: string | undefined;
33
+ }
34
+
35
+ // fake export to make global work
36
+ export { }
@@ -0,0 +1,33 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+
6
+ // AMD2ESM mirgation relevant
7
+
8
+ declare global {
9
+
10
+ /**
11
+ * Holds the file root for resources.
12
+ */
13
+ var _VSCODE_FILE_ROOT: string;
14
+
15
+ /**
16
+ * CSS loader that's available during development time.
17
+ * DO NOT call directly, instead just import css modules, like `import 'some.css'`
18
+ */
19
+ var _VSCODE_CSS_LOAD: (module: string) => void;
20
+
21
+ /**
22
+ * @deprecated You MUST use `IProductService` whenever possible.
23
+ */
24
+ var _VSCODE_PRODUCT_JSON: Record<string, any>;
25
+ /**
26
+ * @deprecated You MUST use `IProductService` whenever possible.
27
+ */
28
+ var _VSCODE_PACKAGE_JSON: Record<string, any>;
29
+
30
+ }
31
+
32
+ // fake export to make global work
33
+ export { }
@@ -47,11 +47,13 @@ declare module '@xterm/xterm' {
47
47
 
48
48
  /**
49
49
  * When enabled the cursor will be set to the beginning of the next line
50
- * with every new line. This is equivalent to sending '\r\n' for each '\n'.
51
- * Normally the termios settings of the underlying PTY deals with the
52
- * translation of '\n' to '\r\n' and this setting should not be used. If you
50
+ * with every new line. This is equivalent to sending `\r\n` for each `\n`.
51
+ * Normally the settings of the underlying PTY (`termios`) deal with the
52
+ * translation of `\n` to `\r\n` and this setting should not be used. If you
53
53
  * deal with data from a non-PTY related source, this settings might be
54
54
  * useful.
55
+ *
56
+ * @see https://pubs.opengroup.org/onlinepubs/007904975/basedefs/termios.h.html
55
57
  */
56
58
  convertEol?: boolean;
57
59
 
@@ -107,11 +109,13 @@ declare module '@xterm/xterm' {
107
109
 
108
110
  /**
109
111
  * The modifier key hold to multiply scroll speed.
112
+ * @deprecated This option is no longer available and will always use alt.
113
+ * Setting this will be ignored.
110
114
  */
111
115
  fastScrollModifier?: 'none' | 'alt' | 'ctrl' | 'shift';
112
116
 
113
117
  /**
114
- * The scroll speed multiplier used for fast scrolling.
118
+ * The scroll speed multiplier used for fast scrolling when `Alt` is held.
115
119
  */
116
120
  fastScrollSensitivity?: number;
117
121
 
@@ -323,10 +327,10 @@ declare module '@xterm/xterm' {
323
327
  windowOptions?: IWindowOptions;
324
328
 
325
329
  /**
326
- * The width, in pixels, of the canvas for the overview ruler. The overview
327
- * ruler will be hidden when not set.
330
+ * Controls the visibility and style of the overview ruler which visualizes
331
+ * decorations underneath the scroll bar.
328
332
  */
329
- overviewRulerWidth?: number;
333
+ overviewRuler?: IOverviewRulerOptions;
330
334
  }
331
335
 
332
336
  /**
@@ -366,6 +370,27 @@ declare module '@xterm/xterm' {
366
370
  * be transparent)
367
371
  */
368
372
  selectionInactiveBackground?: string;
373
+ /**
374
+ * The scrollbar slider background color. Defaults to
375
+ * {@link ITerminalOptions.foreground foreground} with 20% opacity.
376
+ */
377
+ scrollbarSliderBackground?: string;
378
+ /**
379
+ * The scrollbar slider background color when hovered. Defaults to
380
+ * {@link ITerminalOptions.foreground foreground} with 40% opacity.
381
+ */
382
+ scrollbarSliderHoverBackground?: string;
383
+ /**
384
+ * The scrollbar slider background color when clicked. Defaults to
385
+ * {@link ITerminalOptions.foreground foreground} with 50% opacity.
386
+ */
387
+ scrollbarSliderActiveBackground?: string;
388
+ /**
389
+ * The border color of the overview ruler. This visually separates the
390
+ * terminal from the scroll bar when {@link IOverviewRulerOptions.width} is
391
+ * set. When this is not set it defaults to black (`#000000`).
392
+ */
393
+ overviewRulerBorder?: string;
369
394
  /** ANSI black (eg. `\x1b[30m`) */
370
395
  black?: string;
371
396
  /** ANSI red (eg. `\x1b[31m`) */
@@ -586,16 +611,13 @@ declare module '@xterm/xterm' {
586
611
  * What layer to render the decoration at when {@link backgroundColor} or
587
612
  * {@link foregroundColor} are used. `'bottom'` will render under the
588
613
  * selection, `'top`' will render above the selection\*.
589
- *
590
- * *\* The selection will render on top regardless of layer on the canvas
591
- * renderer due to how it renders selection separately.*
592
614
  */
593
615
  readonly layer?: 'bottom' | 'top';
594
616
 
595
617
  /**
596
618
  * When defined, renders the decoration in the overview ruler to the right
597
- * of the terminal. {@link ITerminalOptions.overviewRulerWidth} must be set
598
- * in order to see the overview ruler.
619
+ * of the terminal. {@link IOverviewRulerOptions.width} must be set in order
620
+ * to see the overview ruler.
599
621
  * @param color The color of the decoration.
600
622
  * @param position The position of the decoration.
601
623
  */
@@ -618,6 +640,28 @@ declare module '@xterm/xterm' {
618
640
  tooMuchOutput: string;
619
641
  }
620
642
 
643
+ export interface IOverviewRulerOptions {
644
+ /**
645
+ * When defined, renders decorations in the overview ruler to the right of
646
+ * the terminal. This must be set in order to see the overview ruler.
647
+ * @param color The color of the decoration.
648
+ * @param position The position of the decoration.
649
+ */
650
+ width?: number;
651
+
652
+ /**
653
+ * Whether to show the top border of the overview ruler, which uses the
654
+ * {@link ITheme.overviewRulerBorder} color.
655
+ */
656
+ showTopBorder?: boolean;
657
+
658
+ /**
659
+ * Whether to show the bottom border of the overview ruler, which uses the
660
+ * {@link ITheme.overviewRulerBorder} color.
661
+ */
662
+ showBottomBorder?: boolean;
663
+ }
664
+
621
665
  /**
622
666
  * Enable various window manipulation and report features
623
667
  * (`CSI Ps ; Ps ; Ps t`).
@@ -1090,7 +1134,7 @@ declare module '@xterm/xterm' {
1090
1134
  * render together, since they aren't drawn as optimally as individual
1091
1135
  * characters.
1092
1136
  *
1093
- * NOTE: character joiners are only used by the canvas renderer.
1137
+ * NOTE: character joiners are only used by the webgl renderer.
1094
1138
  *
1095
1139
  * @param handler The function that determines character joins. It is called
1096
1140
  * with a string of text that is eligible for joining and returns an array
@@ -1102,7 +1146,7 @@ declare module '@xterm/xterm' {
1102
1146
 
1103
1147
  /**
1104
1148
  * (EXPERIMENTAL) Deregisters the character joiner if one was registered.
1105
- * NOTE: character joiners are only used by the canvas renderer.
1149
+ * NOTE: character joiners are only used by the webgl renderer.
1106
1150
  * @param joinerId The character joiner's ID (returned after register)
1107
1151
  */
1108
1152
  deregisterCharacterJoiner(joinerId: number): void;
@@ -1241,7 +1285,7 @@ declare module '@xterm/xterm' {
1241
1285
  refresh(start: number, end: number): void;
1242
1286
 
1243
1287
  /**
1244
- * Clears the texture atlas of the canvas renderer if it's active. Doing
1288
+ * Clears the texture atlas of the webgl renderer if it's active. Doing
1245
1289
  * this will force a redraw of all glyphs which can workaround issues
1246
1290
  * causing the texture to become corrupt, for example Chromium/Nvidia has an
1247
1291
  * issue where the texture gets messed up when resuming the OS from sleep.
@@ -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,78 +0,0 @@
1
- /**
2
- * Copyright (c) 2019 The xterm.js authors. All rights reserved.
3
- * @license MIT
4
- */
5
-
6
- import { IDisposable } from 'common/Types';
7
-
8
- interface IListener<T, U = void> {
9
- (arg1: T, arg2: U): void;
10
- }
11
-
12
- export interface IEvent<T, U = void> {
13
- (listener: (arg1: T, arg2: U) => any): IDisposable;
14
- }
15
-
16
- export interface IEventEmitter<T, U = void> {
17
- event: IEvent<T, U>;
18
- fire(arg1: T, arg2: U): void;
19
- dispose(): void;
20
- }
21
-
22
- export class EventEmitter<T, U = void> implements IEventEmitter<T, U> {
23
- private _listeners: IListener<T, U>[] = [];
24
- private _event?: IEvent<T, U>;
25
- private _disposed: boolean = false;
26
-
27
- public get event(): IEvent<T, U> {
28
- if (!this._event) {
29
- this._event = (listener: (arg1: T, arg2: U) => any) => {
30
- this._listeners.push(listener);
31
- const disposable = {
32
- dispose: () => {
33
- if (!this._disposed) {
34
- for (let i = 0; i < this._listeners.length; i++) {
35
- if (this._listeners[i] === listener) {
36
- this._listeners.splice(i, 1);
37
- return;
38
- }
39
- }
40
- }
41
- }
42
- };
43
- return disposable;
44
- };
45
- }
46
- return this._event;
47
- }
48
-
49
- public fire(arg1: T, arg2: U): void {
50
- const queue: IListener<T, U>[] = [];
51
- for (let i = 0; i < this._listeners.length; i++) {
52
- queue.push(this._listeners[i]);
53
- }
54
- for (let i = 0; i < queue.length; i++) {
55
- queue[i].call(undefined, arg1, arg2);
56
- }
57
- }
58
-
59
- public dispose(): void {
60
- this.clearListeners();
61
- this._disposed = true;
62
- }
63
-
64
- public clearListeners(): void {
65
- if (this._listeners) {
66
- this._listeners.length = 0;
67
- }
68
- }
69
- }
70
-
71
- export function forwardEvent<T>(from: IEvent<T>, to: IEventEmitter<T>): IDisposable {
72
- return from(e => to.fire(e));
73
- }
74
-
75
- export function runAndSubscribe<T>(event: IEvent<T>, handler: (e: T | undefined) => any): IDisposable {
76
- handler(undefined);
77
- return event(e => handler(e));
78
- }
@@ -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
- }
File without changes
File without changes