@adecore/terminal 0.23.0 → 0.24.0

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.
@@ -89,24 +89,25 @@ export function TerminalView({ ref, onData, onResize, onOpenLink, onLinkHover, f
89
89
  if (initial.scaledByAncestor) {
90
90
  followAncestorScale(term);
91
91
  }
92
- fitToHost(term, fit);
93
92
  /*
94
93
  * The grid this terminal fits and tells the program, and the grid the program has. They part while
95
94
  * another client sizes the same program; this one then draws that grid, clipped or with room to
96
- * spare, until a fit here changes the grid and claims it back.
95
+ * spare, until a fit here changes the grid and claims it back. Null until a fit had room.
97
96
  */
98
- let claimed = { cols: term.cols, rows: term.rows };
97
+ let claimed = fitToHost(term, fit) ? { cols: term.cols, rows: term.rows } : null;
99
98
  let shared = claimed;
100
99
  const drawShared = () => {
101
- if (term.cols !== shared.cols || term.rows !== shared.rows) {
100
+ if (shared !== null && (term.cols !== shared.cols || term.rows !== shared.rows)) {
102
101
  term.resize(shared.cols, shared.rows);
103
102
  }
104
103
  };
105
104
  // A resize, a font change and a renderer swap all land here: WebGL and the DOM measure a glyph
106
105
  // differently, so a swap can change how many cells fit.
107
106
  const refit = () => {
108
- fitToHost(term, fit);
109
- if (term.cols !== claimed.cols || term.rows !== claimed.rows) {
107
+ if (!fitToHost(term, fit)) {
108
+ return;
109
+ }
110
+ if (claimed === null || term.cols !== claimed.cols || term.rows !== claimed.rows) {
110
111
  claimed = { cols: term.cols, rows: term.rows };
111
112
  shared = claimed;
112
113
  latest.current.onResize?.(claimed.cols, claimed.rows);
@@ -1,4 +1,5 @@
1
1
  export declare const DEFAULT_WEBGL_CONTEXTS = 10;
2
+ export declare const OUTPUT_SWITCH_INTERVAL_MS = 1000;
2
3
  export interface SlotChange {
3
4
  granted: string[];
4
5
  revoked: string[];
@@ -13,7 +14,9 @@ export declare class WebglSlots {
13
14
  private cap;
14
15
  private focusedId;
15
16
  private stamp;
16
- constructor(cap?: number);
17
+ private outputSwitchedAt;
18
+ private readonly now;
19
+ constructor(cap?: number, now?: () => number);
17
20
  holders(): string[];
18
21
  holds(id: string): boolean;
19
22
  waiting(): string[];
@@ -26,5 +29,6 @@ export declare class WebglSlots {
26
29
  setCap(cap: number): SlotChange;
27
30
  private ranked;
28
31
  private rankOf;
32
+ private target;
29
33
  private reconcile;
30
34
  }
@@ -2,6 +2,10 @@
2
2
  contexts per page and silently drops the oldest, so the budget stays well under that and leaves
3
3
  room for whatever else on the page draws with WebGL. */
4
4
  export const DEFAULT_WEBGL_CONTEXTS = 10;
5
+ /* Output moves a context at most once in this window, and only away from a holder that was quiet
6
+ for as long. Without it, more writing terminals than slots trade contexts on every chunk, and
7
+ each switch rebuilds an atlas and can resize the pty, which makes the program write again. */
8
+ export const OUTPUT_SWITCH_INTERVAL_MS = 1000;
5
9
  const nothing = () => ({ granted: [], revoked: [] });
6
10
  /**
7
11
  * Hands a fixed number of WebGL contexts to the terminals that deserve them most, the focused one
@@ -13,8 +17,12 @@ export class WebglSlots {
13
17
  cap;
14
18
  focusedId = null;
15
19
  stamp = 0;
16
- constructor(cap = DEFAULT_WEBGL_CONTEXTS) {
20
+ outputSwitchedAt = Number.NEGATIVE_INFINITY;
21
+ now;
22
+ /* `now` is a clock in milliseconds. */
23
+ constructor(cap = DEFAULT_WEBGL_CONTEXTS, now = () => performance.now()) {
17
24
  this.cap = Math.max(1, cap);
25
+ this.now = now;
18
26
  }
19
27
  /* The terminals holding a context, highest ranked first. */
20
28
  holders() {
@@ -32,9 +40,10 @@ export class WebglSlots {
32
40
  const member = this.members.get(id);
33
41
  if (member) {
34
42
  member.seq = ++this.stamp;
43
+ member.activeAt = this.now();
35
44
  }
36
45
  else {
37
- this.members.set(id, { seq: ++this.stamp, holding: false, blocked: false });
46
+ this.members.set(id, { seq: ++this.stamp, activeAt: this.now(), holding: false, blocked: false });
38
47
  }
39
48
  return this.reconcile();
40
49
  }
@@ -55,6 +64,7 @@ export class WebglSlots {
55
64
  return nothing();
56
65
  }
57
66
  member.seq = ++this.stamp;
67
+ member.activeAt = this.now();
58
68
  // Focusing is how a terminal that lost its context asks for a new one.
59
69
  member.blocked = false;
60
70
  return this.reconcile();
@@ -72,9 +82,24 @@ export class WebglSlots {
72
82
  if (!member) {
73
83
  return nothing();
74
84
  }
85
+ const now = this.now();
75
86
  member.seq = ++this.stamp;
87
+ member.activeAt = now;
76
88
  // A holder that climbs stays a holder; only a waiter can change who holds what.
77
- return member.holding ? nothing() : this.reconcile();
89
+ if (member.holding || now - this.outputSwitchedAt < OUTPUT_SWITCH_INTERVAL_MS) {
90
+ return nothing();
91
+ }
92
+ const target = this.target();
93
+ const evictsBusyHolder = [...this.members].some(([other, entry]) => entry.holding && !target.has(other) && now - entry.activeAt < OUTPUT_SWITCH_INTERVAL_MS);
94
+ // The waiter keeps its new rank; its next write tries again, and so does any request, focus or release.
95
+ if (evictsBusyHolder) {
96
+ return nothing();
97
+ }
98
+ const change = this.reconcile(target);
99
+ if (change.granted.length > 0 || change.revoked.length > 0) {
100
+ this.outputSwitchedAt = now;
101
+ }
102
+ return change;
78
103
  }
79
104
  /* The browser reported a context loss, free the slot and wait for a focus before asking again. */
80
105
  lost(id) {
@@ -100,9 +125,11 @@ export class WebglSlots {
100
125
  }
101
126
  return this.members.get(id)?.seq ?? -1;
102
127
  }
103
- reconcile() {
128
+ target() {
104
129
  const eligible = this.ranked().filter((id) => this.members.get(id)?.blocked !== true);
105
- const target = new Set(eligible.slice(0, this.cap));
130
+ return new Set(eligible.slice(0, this.cap));
131
+ }
132
+ reconcile(target = this.target()) {
106
133
  const granted = [];
107
134
  const revoked = [];
108
135
  for (const [id, member] of this.members) {
@@ -1,6 +1,6 @@
1
1
  import type { FitAddon } from '@xterm/addon-fit';
2
2
  import type { Terminal } from '@xterm/xterm';
3
- export declare const fitToHost: (term: Terminal, fit: FitAddon) => void;
3
+ export declare const fitToHost: (term: Terminal, fit: FitAddon) => boolean;
4
4
  interface Pointer {
5
5
  clientX: number;
6
6
  clientY: number;
@@ -6,18 +6,23 @@
6
6
  * FitAddon measures the host's border box, so a vertical padding on the host would count as room for a
7
7
  * row that is cut off. The host has none; what a whole row does not fill is split above and below, as an
8
8
  * offset rather than a padding, since FitAddon subtracts the terminal element's own padding too.
9
+ * False, without a fit, for a host the layout has given no room yet: FitAddon would make two columns
10
+ * of it, a grid nobody sees that a shell still redraws its prompt for.
9
11
  */
10
12
  export const fitToHost = (term, fit) => {
11
- fit.fit();
12
13
  const host = term.element?.parentElement;
14
+ if (!host || host.clientWidth === 0 || host.clientHeight === 0) {
15
+ return false;
16
+ }
17
+ fit.fit();
13
18
  // The same private dimensions FitAddon itself divides by.
14
19
  const cellHeight = term._core._renderService
15
20
  .dimensions.css.cell.height;
16
- if (!host || cellHeight === 0) {
17
- return;
21
+ if (cellHeight !== 0) {
22
+ const slack = host.clientHeight - term.rows * cellHeight;
23
+ host.style.setProperty('--term-offset', `${Math.max(0, Math.floor(slack / 2))}px`);
18
24
  }
19
- const slack = host.clientHeight - term.rows * cellHeight;
20
- host.style.setProperty('--term-offset', `${Math.max(0, Math.floor(slack / 2))}px`);
25
+ return true;
21
26
  };
22
27
  /** Where a pointer would stand on an element whose ancestors scale it from its layout size to `rect`. */
23
28
  export const unscaledPointer = (pointer, rect, layout) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adecore/terminal",
3
- "version": "0.23.0",
3
+ "version": "0.24.0",
4
4
  "description": "A themed terminal pane on xterm.js that fits its container and is fed by the app.",
5
5
  "license": "FSL-1.1-MIT",
6
6
  "author": "Bas Milius",
@@ -43,7 +43,7 @@ export interface TerminalViewProps {
43
43
  /* What the person types or pastes. */
44
44
  onData?(data: string): void;
45
45
  /* The grid changed after the container or the font did, which is what the program should be told.
46
- Not called for the grid at mount: `size()` has that one. */
46
+ Not called for the grid at mount, which `size()` has, unless the host had no room then: its first fit comes here. */
47
47
  onResize?(cols: number, rows: number): void;
48
48
  /* Where a link in the output goes. Without it, a new window opens on the link. */
49
49
  onOpenLink?(uri: string, event: MouseEvent): void;
@@ -175,25 +175,25 @@ export function TerminalView({
175
175
  if (initial.scaledByAncestor) {
176
176
  followAncestorScale(term);
177
177
  }
178
- fitToHost(term, fit);
179
-
180
178
  /*
181
179
  * The grid this terminal fits and tells the program, and the grid the program has. They part while
182
180
  * another client sizes the same program; this one then draws that grid, clipped or with room to
183
- * spare, until a fit here changes the grid and claims it back.
181
+ * spare, until a fit here changes the grid and claims it back. Null until a fit had room.
184
182
  */
185
- let claimed: TerminalSize = { cols: term.cols, rows: term.rows };
183
+ let claimed: TerminalSize | null = fitToHost(term, fit) ? { cols: term.cols, rows: term.rows } : null;
186
184
  let shared = claimed;
187
185
  const drawShared = (): void => {
188
- if (term.cols !== shared.cols || term.rows !== shared.rows) {
186
+ if (shared !== null && (term.cols !== shared.cols || term.rows !== shared.rows)) {
189
187
  term.resize(shared.cols, shared.rows);
190
188
  }
191
189
  };
192
190
  // A resize, a font change and a renderer swap all land here: WebGL and the DOM measure a glyph
193
191
  // differently, so a swap can change how many cells fit.
194
192
  const refit = (): void => {
195
- fitToHost(term, fit);
196
- if (term.cols !== claimed.cols || term.rows !== claimed.rows) {
193
+ if (!fitToHost(term, fit)) {
194
+ return;
195
+ }
196
+ if (claimed === null || term.cols !== claimed.cols || term.rows !== claimed.rows) {
197
197
  claimed = { cols: term.cols, rows: term.rows };
198
198
  shared = claimed;
199
199
  latest.current.onResize?.(claimed.cols, claimed.rows);
@@ -3,6 +3,11 @@
3
3
  room for whatever else on the page draws with WebGL. */
4
4
  export const DEFAULT_WEBGL_CONTEXTS = 10;
5
5
 
6
+ /* Output moves a context at most once in this window, and only away from a holder that was quiet
7
+ for as long. Without it, more writing terminals than slots trade contexts on every chunk, and
8
+ each switch rebuilds an atlas and can resize the pty, which makes the program write again. */
9
+ export const OUTPUT_SWITCH_INTERVAL_MS = 1000;
10
+
6
11
  export interface SlotChange {
7
12
  granted: string[];
8
13
  revoked: string[];
@@ -11,6 +16,8 @@ export interface SlotChange {
11
16
  interface Member {
12
17
  /* Stamp of the last focus, write or request; the ranking is this number, high to low. */
13
18
  seq: number;
19
+ /* When the terminal last asked, was focused or was written to, on the clock of the slots. */
20
+ activeAt: number;
14
21
  holding: boolean;
15
22
  /* The browser took this terminal's context away, so it stays out of the running until a focus. */
16
23
  blocked: boolean;
@@ -28,9 +35,13 @@ export class WebglSlots {
28
35
  private cap: number;
29
36
  private focusedId: string | null = null;
30
37
  private stamp = 0;
38
+ private outputSwitchedAt = Number.NEGATIVE_INFINITY;
39
+ private readonly now: () => number;
31
40
 
32
- constructor(cap: number = DEFAULT_WEBGL_CONTEXTS) {
41
+ /* `now` is a clock in milliseconds. */
42
+ constructor(cap: number = DEFAULT_WEBGL_CONTEXTS, now: () => number = () => performance.now()) {
33
43
  this.cap = Math.max(1, cap);
44
+ this.now = now;
34
45
  }
35
46
 
36
47
  /* The terminals holding a context, highest ranked first. */
@@ -52,8 +63,9 @@ export class WebglSlots {
52
63
  const member = this.members.get(id);
53
64
  if (member) {
54
65
  member.seq = ++this.stamp;
66
+ member.activeAt = this.now();
55
67
  } else {
56
- this.members.set(id, { seq: ++this.stamp, holding: false, blocked: false });
68
+ this.members.set(id, { seq: ++this.stamp, activeAt: this.now(), holding: false, blocked: false });
57
69
  }
58
70
  return this.reconcile();
59
71
  }
@@ -76,6 +88,7 @@ export class WebglSlots {
76
88
  return nothing();
77
89
  }
78
90
  member.seq = ++this.stamp;
91
+ member.activeAt = this.now();
79
92
  // Focusing is how a terminal that lost its context asks for a new one.
80
93
  member.blocked = false;
81
94
  return this.reconcile();
@@ -95,9 +108,26 @@ export class WebglSlots {
95
108
  if (!member) {
96
109
  return nothing();
97
110
  }
111
+ const now = this.now();
98
112
  member.seq = ++this.stamp;
113
+ member.activeAt = now;
99
114
  // A holder that climbs stays a holder; only a waiter can change who holds what.
100
- return member.holding ? nothing() : this.reconcile();
115
+ if (member.holding || now - this.outputSwitchedAt < OUTPUT_SWITCH_INTERVAL_MS) {
116
+ return nothing();
117
+ }
118
+ const target = this.target();
119
+ const evictsBusyHolder = [...this.members].some(
120
+ ([other, entry]) => entry.holding && !target.has(other) && now - entry.activeAt < OUTPUT_SWITCH_INTERVAL_MS
121
+ );
122
+ // The waiter keeps its new rank; its next write tries again, and so does any request, focus or release.
123
+ if (evictsBusyHolder) {
124
+ return nothing();
125
+ }
126
+ const change = this.reconcile(target);
127
+ if (change.granted.length > 0 || change.revoked.length > 0) {
128
+ this.outputSwitchedAt = now;
129
+ }
130
+ return change;
101
131
  }
102
132
 
103
133
  /* The browser reported a context loss, free the slot and wait for a focus before asking again. */
@@ -128,9 +158,12 @@ export class WebglSlots {
128
158
  return this.members.get(id)?.seq ?? -1;
129
159
  }
130
160
 
131
- private reconcile(): SlotChange {
161
+ private target(): Set<string> {
132
162
  const eligible = this.ranked().filter((id) => this.members.get(id)?.blocked !== true);
133
- const target = new Set(eligible.slice(0, this.cap));
163
+ return new Set(eligible.slice(0, this.cap));
164
+ }
165
+
166
+ private reconcile(target: Set<string> = this.target()): SlotChange {
134
167
  const granted: string[] = [];
135
168
  const revoked: string[] = [];
136
169
  for (const [id, member] of this.members) {
@@ -10,18 +10,23 @@ import type { Terminal } from '@xterm/xterm';
10
10
  * FitAddon measures the host's border box, so a vertical padding on the host would count as room for a
11
11
  * row that is cut off. The host has none; what a whole row does not fill is split above and below, as an
12
12
  * offset rather than a padding, since FitAddon subtracts the terminal element's own padding too.
13
+ * False, without a fit, for a host the layout has given no room yet: FitAddon would make two columns
14
+ * of it, a grid nobody sees that a shell still redraws its prompt for.
13
15
  */
14
- export const fitToHost = (term: Terminal, fit: FitAddon): void => {
15
- fit.fit();
16
+ export const fitToHost = (term: Terminal, fit: FitAddon): boolean => {
16
17
  const host = term.element?.parentElement;
18
+ if (!host || host.clientWidth === 0 || host.clientHeight === 0) {
19
+ return false;
20
+ }
21
+ fit.fit();
17
22
  // The same private dimensions FitAddon itself divides by.
18
23
  const cellHeight: number = (term as unknown as { _core: { _renderService: { dimensions: { css: { cell: { height: number } } } } } })._core._renderService
19
24
  .dimensions.css.cell.height;
20
- if (!host || cellHeight === 0) {
21
- return;
25
+ if (cellHeight !== 0) {
26
+ const slack = host.clientHeight - term.rows * cellHeight;
27
+ host.style.setProperty('--term-offset', `${Math.max(0, Math.floor(slack / 2))}px`);
22
28
  }
23
- const slack = host.clientHeight - term.rows * cellHeight;
24
- host.style.setProperty('--term-offset', `${Math.max(0, Math.floor(slack / 2))}px`);
29
+ return true;
25
30
  };
26
31
 
27
32
  interface Pointer {