@solidtv/solid 1.7.4 → 1.8.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.
Files changed (68) hide show
  1. package/MIGRATION-1.8.md +270 -0
  2. package/dist/src/core/dom-renderer/dashedBorderStyle.d.ts +20 -0
  3. package/dist/src/core/dom-renderer/dashedBorderStyle.js +104 -0
  4. package/dist/src/core/dom-renderer/dashedBorderStyle.js.map +1 -0
  5. package/dist/src/core/dom-renderer/domRenderer.d.ts +14 -0
  6. package/dist/src/core/dom-renderer/domRenderer.js +194 -12
  7. package/dist/src/core/dom-renderer/domRenderer.js.map +1 -1
  8. package/dist/src/core/dom-renderer/domRendererTypes.d.ts +12 -2
  9. package/dist/src/core/elementNode.d.ts +176 -9
  10. package/dist/src/core/elementNode.js +1906 -411
  11. package/dist/src/core/elementNode.js.map +1 -1
  12. package/dist/src/core/flexLayout.d.ts +6 -0
  13. package/dist/src/core/flexLayout.js +535 -218
  14. package/dist/src/core/flexLayout.js.map +1 -1
  15. package/dist/src/core/focusManager.js +70 -23
  16. package/dist/src/core/focusManager.js.map +1 -1
  17. package/dist/src/core/fontLoaded.d.ts +10 -0
  18. package/dist/src/core/fontLoaded.js +26 -0
  19. package/dist/src/core/fontLoaded.js.map +1 -0
  20. package/dist/src/core/intrinsicTypes.d.ts +2 -1
  21. package/dist/src/core/lightningInit.js +8 -1
  22. package/dist/src/core/lightningInit.js.map +1 -1
  23. package/dist/src/core/shaders.d.ts +5 -2
  24. package/dist/src/core/shaders.js +6 -0
  25. package/dist/src/core/shaders.js.map +1 -1
  26. package/dist/src/core/states.d.ts +2 -0
  27. package/dist/src/core/states.js +26 -7
  28. package/dist/src/core/states.js.map +1 -1
  29. package/dist/src/core/stylePlan.d.ts +39 -0
  30. package/dist/src/core/stylePlan.js +159 -0
  31. package/dist/src/core/stylePlan.js.map +1 -0
  32. package/dist/src/core/utils.js +2 -2
  33. package/dist/src/primitives/Grid.jsx +22 -3
  34. package/dist/src/primitives/Grid.jsx.map +1 -1
  35. package/dist/src/primitives/Virtual.jsx +16 -6
  36. package/dist/src/primitives/Virtual.jsx.map +1 -1
  37. package/dist/src/primitives/utils/handleNavigation.js +29 -6
  38. package/dist/src/primitives/utils/handleNavigation.js.map +1 -1
  39. package/dist/src/primitives/utils/withScrolling.js +2 -1
  40. package/dist/src/primitives/utils/withScrolling.js.map +1 -1
  41. package/dist/src/solidOpts.js +22 -8
  42. package/dist/src/solidOpts.js.map +1 -1
  43. package/dist/tsconfig.tsbuildinfo +1 -1
  44. package/docs/AI_CODING_GUIDELINES.md +7 -6
  45. package/docs/AI_UPGRADE_1.8.md +290 -0
  46. package/package.json +5 -3
  47. package/src/core/dom-renderer/dashedBorderStyle.ts +128 -0
  48. package/src/core/dom-renderer/domRenderer.ts +201 -10
  49. package/src/core/dom-renderer/domRendererTypes.ts +12 -0
  50. package/src/core/elementNode.ts +2124 -459
  51. package/src/core/flexLayout.ts +583 -231
  52. package/src/core/focusManager.ts +86 -27
  53. package/src/core/fontLoaded.ts +29 -0
  54. package/src/core/intrinsicTypes.ts +2 -0
  55. package/src/core/lightningInit.ts +8 -1
  56. package/src/core/shaders.ts +12 -0
  57. package/src/core/states.ts +29 -9
  58. package/src/core/stylePlan.ts +195 -0
  59. package/src/core/utils.ts +2 -2
  60. package/src/primitives/Grid.tsx +35 -9
  61. package/src/primitives/Virtual.tsx +20 -6
  62. package/src/primitives/utils/handleNavigation.ts +40 -6
  63. package/src/primitives/utils/withScrolling.ts +3 -2
  64. package/src/solidOpts.ts +23 -8
  65. package/dist/src/core/flex.d.ts +0 -2
  66. package/dist/src/core/flex.js +0 -285
  67. package/dist/src/core/flex.js.map +0 -1
  68. package/src/core/flex.ts +0 -332
@@ -1,4 +1,10 @@
1
- import { createSignal, getOwner, onCleanup, runWithOwner } from 'solid-js';
1
+ import {
2
+ batch,
3
+ createSignal,
4
+ getOwner,
5
+ onCleanup,
6
+ runWithOwner,
7
+ } from 'solid-js';
2
8
  import { Config, isDev } from './config.js';
3
9
  import { IRendererNode } from './dom-renderer/domRendererTypes.js';
4
10
  export type * from './focusKeyTypes.js';
@@ -10,8 +16,6 @@ import {
10
16
  setActiveElement as setActiveElementSignal,
11
17
  } from './activeElement.js';
12
18
 
13
- let _signalWrapper: (cb: () => void) => void = (cb) => cb();
14
-
15
19
  type KeyMapEntries = Record<KeyNameOrKeyCode, string>;
16
20
 
17
21
  const keyMapEntries: KeyMapEntries = {
@@ -230,8 +234,29 @@ export const printFocusHistory = (count: number): void => {
230
234
  * through the focus-path logic.
231
235
  */
232
236
  export const setActiveElementCore = (elm: ElementNode) => {
233
- const prev = activeElement();
234
- if (elm === prev) return;
237
+ // No local: terser inlines this single-use function into its caller, and a
238
+ // block-scoped local there would make it an IIFE (a closure per change).
239
+ _focusPrev = activeElement();
240
+ if (elm === _focusPrev) {
241
+ _focusPrev = undefined;
242
+ return;
243
+ }
244
+ _focusTarget = elm;
245
+ // The whole focus phase runs in one batch (5.1): the callbacks keep their
246
+ // order, and the effects their signal writes trigger, like those of
247
+ // focusPath and the active element, run once, after the last of them.
248
+ batch(applyFocus);
249
+ };
250
+
251
+ // The focus change setActiveElementCore hands to applyFocus, so the batch
252
+ // takes no closure. applyFocus reads both on entry.
253
+ let _focusTarget: ElementNode | undefined;
254
+ let _focusPrev: ElementNode | undefined;
255
+
256
+ const applyFocus = (): void => {
257
+ const elm = _focusTarget!;
258
+ const prev = _focusPrev;
259
+ _focusTarget = _focusPrev = undefined;
235
260
  updateFocusPath(elm, prev);
236
261
  recordFocusHistory(elm, prev);
237
262
  // Reset key attribution so programmatic focus changes show '—' for key fields
@@ -244,21 +269,45 @@ export const setActiveElementCore = (elm: ElementNode) => {
244
269
 
245
270
  export const [focusPath, setFocusPath] = createSignal<ElementNode[]>([]);
246
271
 
272
+ // Each focus change takes a new generation and stamps it on every node of the
273
+ // new path; a node of the previous path without that stamp lost focus. O(depth),
274
+ // no membership scan.
275
+ let focusGen = 0;
276
+ // The new path is built here, leaf first, then published as a copy: the
277
+ // signal's value must be a fresh array per change, since readers keep it.
278
+ const pathBuffer: ElementNode[] = [];
279
+ // True while updateFocusPath runs. A focus callback that calls
280
+ // setActiveElementCore re-enters it: the inner change builds its own array,
281
+ // and the outer one, whose stamps it overwrote, falls back to a scan.
282
+ let updatingPath = false;
283
+
247
284
  const updateFocusPath = (
248
285
  currentFocusedElm: ElementNode,
249
286
  prevFocusedElm: ElementNode | undefined,
250
287
  ) => {
288
+ const nested = updatingPath;
289
+ updatingPath = true;
290
+ try {
291
+ buildFocusPath(currentFocusedElm, prevFocusedElm, nested ? [] : pathBuffer);
292
+ } finally {
293
+ updatingPath = nested;
294
+ }
295
+ };
296
+
297
+ const buildFocusPath = (
298
+ currentFocusedElm: ElementNode,
299
+ prevFocusedElm: ElementNode | undefined,
300
+ fp: ElementNode[],
301
+ ) => {
302
+ const focusKey = Config.focusStateKey;
303
+ const gen = ++focusGen;
304
+ let length = 0;
251
305
  let current: ElementNode | undefined = currentFocusedElm;
252
- // fp escapes through the focusPath signal, so it must be a fresh array; the
253
- // membership test below runs on paths of a handful of elements every single
254
- // keypress, where a linear scan beats allocating and hashing a Set.
255
- const fp: ElementNode[] = [];
256
306
  while (current) {
257
- if (
258
- !current.states.has(Config.focusStateKey) ||
259
- current === currentFocusedElm
260
- ) {
261
- current.states.add(Config.focusStateKey);
307
+ current._focusGen = gen;
308
+ const states = current.states;
309
+ if (!states.has(focusKey) || current === currentFocusedElm) {
310
+ states.add(focusKey);
262
311
  current.onFocus?.call(
263
312
  current,
264
313
  currentFocusedElm,
@@ -273,15 +322,17 @@ const updateFocusPath = (
273
322
  current,
274
323
  );
275
324
  }
276
- fp.push(current);
325
+ fp[length++] = current;
277
326
  current = current.parent;
278
327
  }
328
+ fp.length = length;
279
329
 
280
330
  const prevFp = focusPath();
281
331
  for (let i = 0; i < prevFp.length; i++) {
282
332
  const elm = prevFp[i]!;
283
- if (fp.indexOf(elm) === -1) {
284
- elm.states.remove(Config.focusStateKey);
333
+ // focusGen moves on only when a callback changed focus re-entrantly.
334
+ if (focusGen === gen ? elm._focusGen !== gen : fp.indexOf(elm) === -1) {
335
+ elm.states.remove(focusKey);
285
336
  elm.onBlur?.call(elm, currentFocusedElm, prevFocusedElm!, elm);
286
337
  elm.onFocusChanged?.call(
287
338
  elm,
@@ -293,11 +344,12 @@ const updateFocusPath = (
293
344
  }
294
345
  }
295
346
 
347
+ const newFp = fp.slice();
296
348
  if (Config.focusDebug) {
297
- addFocusDebug(prevFp, fp);
349
+ addFocusDebug(prevFp, newFp);
298
350
  }
299
351
 
300
- _signalWrapper(() => setFocusPath(fp));
352
+ setFocusPath(newFp);
301
353
  };
302
354
 
303
355
  let lastGlobalKeyPressTime = 0;
@@ -670,6 +722,14 @@ export interface KeyEventTarget {
670
722
  ): void;
671
723
  }
672
724
 
725
+ // The element Config.setActiveElement (as useFocusManager sets it) hands to
726
+ // publishActiveElement, so a focus change allocates no closure.
727
+ let _activeToPublish: ElementNode | undefined;
728
+
729
+ const publishActiveElement = (): void => {
730
+ setActiveElementSignal(_activeToPublish);
731
+ };
732
+
673
733
  // The key event being dispatched, handed to dispatchKeyDown/dispatchKeyUp so
674
734
  // that a key event allocates no closure. Each takes it on entry, so a handler
675
735
  // that raises another key event cannot disturb the one in progress.
@@ -718,18 +778,17 @@ export const useFocusManager = (
718
778
  // can run inside it — needed for programmatic .setFocus(), post-mutation
719
779
  // focus, and any effect subscribers that rely on onCleanup.
720
780
  const owner = getOwner();
721
- const ownerContext = (cb: () => void) => {
722
- runWithOwner(owner, cb);
723
- };
724
- _signalWrapper = ownerContext;
725
781
  // Drive the active-element signal inside this owner so its effect subscribers
726
782
  // have a parent for cleanup. Consumers replacing the focus manager can wire
727
783
  // Config.setActiveElement themselves instead of calling useFocusManager.
728
- Config.setActiveElement = (elm) =>
729
- ownerContext(() => setActiveElementSignal(elm));
784
+ Config.setActiveElement = (elm) => {
785
+ _activeToPublish = elm;
786
+ runWithOwner(owner, publishActiveElement);
787
+ _activeToPublish = undefined;
788
+ };
730
789
 
731
- // One listener per event type, run inside the owner like ownerContext,
732
- // with no closure per event.
790
+ // One listener per event type, run inside the owner like the active-element
791
+ // signal above, with no closure per event.
733
792
  const keyPressHandler = (event: KeyEventLike) => {
734
793
  _dispatchEvent = event;
735
794
  runWithOwner(owner, dispatchKeyDown);
@@ -0,0 +1,29 @@
1
+ // A font loaded through loadFonts() (lightningInit.ts): Solid's text
2
+ // measurement (elementNode.ts) measures again the texts that waited for
3
+ // their font, including those no renderer walk visits (under a hidden or
4
+ // out-of-bounds ancestor), which hear no `loaded`. Internal: this module is
5
+ // not re-exported, so neither name is part of the package's API.
6
+
7
+ let listener: (() => void) | undefined;
8
+
9
+ /** elementNode.ts: what to run when a font has loaded. */
10
+ export function onFontLoaded(fn: () => void): void {
11
+ listener = fn;
12
+ }
13
+
14
+ /** lightningInit.ts: a font passed to loadFonts() has loaded. */
15
+ export function fontLoaded(): void {
16
+ if (listener !== undefined) {
17
+ listener();
18
+ }
19
+ }
20
+
21
+ /**
22
+ * lightningInit.ts: a font passed to loadFonts() failed for good. The texts
23
+ * that waited are measured again (one whose font is still missing waits
24
+ * again; a destroyed one is dropped), and the failure goes on to the caller.
25
+ */
26
+ export function fontFailed(error: unknown): never {
27
+ fontLoaded();
28
+ throw error;
29
+ }
@@ -4,6 +4,7 @@ import type { TextNode } from './nodeTypes.js';
4
4
  import { NodeStates } from './states.js';
5
5
  import {
6
6
  ShaderBorderProps,
7
+ ShaderDashedBorderProps,
7
8
  ShaderHolePunchProps,
8
9
  ShaderLinearGradientProps,
9
10
  ShaderRadialGradientProps,
@@ -47,6 +48,7 @@ export interface Effects {
47
48
  linearGradient?: Partial<ShaderLinearGradientProps>;
48
49
  radialGradient?: Partial<ShaderRadialGradientProps>;
49
50
  holePunch?: Partial<ShaderHolePunchProps>;
51
+ dashedBorder?: AddColorString<Partial<ShaderDashedBorderProps>>;
50
52
  shadow?: Partial<ShaderShadowProps>;
51
53
  rounded?: Partial<ShaderRoundedProps>;
52
54
  borderRadius?: Partial<BorderRadius>;
@@ -3,6 +3,7 @@ import { prefetchFont } from '@solidtv/renderer';
3
3
  import { Config, DOM_RENDERING } from './config.js';
4
4
  import { DOMRendererMain, loadFontToDom } from './dom-renderer/domRenderer.js';
5
5
  import { DomRendererMainSettings } from './dom-renderer/domRendererTypes.js';
6
+ import { fontFailed, fontLoaded } from './fontLoaded.js';
6
7
  import { FontLoadOptions } from './intrinsicTypes.js';
7
8
 
8
9
  export type SdfFontType = 'ssdf' | 'msdf';
@@ -129,7 +130,13 @@ function attachFonts(fonts: FontLoadOptions[]) {
129
130
  'type' in font &&
130
131
  (font.type === 'msdf' || font.type === 'ssdf')
131
132
  ) {
132
- return renderer.stage.loadFont('sdf', font as lng.FontLoadOptions);
133
+ // Loaded: Solid measures the texts that waited for a font, those
134
+ // under a hidden or out-of-bounds ancestor too (no walk visits
135
+ // them, so they hear no `loaded`). Failed: the same, so destroyed
136
+ // ones leave the list; the rejection goes on.
137
+ return renderer.stage
138
+ .loadFont('sdf', font as lng.FontLoadOptions)
139
+ .then(fontLoaded, fontFailed);
133
140
  }
134
141
  // DOM — Web
135
142
  if ('fontUrl' in font && enableDomRenderer) {
@@ -2,6 +2,7 @@ import * as lngr from '@solidtv/renderer';
2
2
  import * as lngr_shaders from '@solidtv/renderer/webgl/shaders';
3
3
 
4
4
  import type {
5
+ DashedBorderProps as ShaderDashedBorderProps,
5
6
  HolePunchProps as ShaderHolePunchProps,
6
7
  LinearGradientProps as ShaderLinearGradientProps,
7
8
  RadialGradientProps as ShaderRadialGradientProps,
@@ -10,6 +11,7 @@ import type {
10
11
  } from '@solidtv/renderer';
11
12
  import { type WebGlShaderType as WebGlShader } from '@solidtv/renderer';
12
13
  export {
14
+ ShaderDashedBorderProps,
13
15
  ShaderHolePunchProps,
14
16
  ShaderLinearGradientProps,
15
17
  ShaderRadialGradientProps,
@@ -57,6 +59,7 @@ export type ShaderRoundedWithShadow = WebGlShader<ShaderRoundedWithShadowProps>;
57
59
  export type ShaderRoundedWithBorderAndShadow =
58
60
  WebGlShader<ShaderRoundedWithBorderAndShadowProps>;
59
61
  export type ShaderHolePunch = WebGlShader<ShaderHolePunchProps>;
62
+ export type ShaderDashedBorder = WebGlShader<ShaderDashedBorderProps>;
60
63
  export type ShaderRadialGradient = WebGlShader<ShaderRadialGradientProps>;
61
64
  export type ShaderLinearGradient = WebGlShader<ShaderLinearGradientProps>;
62
65
 
@@ -68,6 +71,8 @@ export const defaultShaderRoundedWithShadow: ShaderRoundedWithShadow =
68
71
  export const defaultShaderRoundedWithBorderAndShadow =
69
72
  lngr_shaders.RoundedWithBorderAndShadow as ShaderRoundedWithBorderAndShadow;
70
73
  export const defaultShaderHolePunch: ShaderHolePunch = lngr_shaders.HolePunch;
74
+ export const defaultShaderDashedBorder: ShaderDashedBorder =
75
+ lngr_shaders.DashedBorder;
71
76
  export const defaultShaderRadialGradient: ShaderRadialGradient =
72
77
  lngr_shaders.RadialGradient;
73
78
  export const defaultShaderLinearGradient: ShaderLinearGradient =
@@ -160,6 +165,12 @@ export function registerDefaultShaderHolePunch(shManager: CoreShaderManager) {
160
165
  if (SHADERS_ENABLED && !isDomRendererActive())
161
166
  shManager.registerShaderType('holePunch', defaultShaderHolePunch);
162
167
  }
168
+ export function registerDefaultShaderDashedBorder(
169
+ shManager: CoreShaderManager,
170
+ ) {
171
+ if (SHADERS_ENABLED && !isDomRendererActive())
172
+ shManager.registerShaderType('dashedBorder', defaultShaderDashedBorder);
173
+ }
163
174
  export function registerDefaultShaderRadialGradient(
164
175
  shManager: CoreShaderManager,
165
176
  ) {
@@ -181,6 +192,7 @@ export function registerDefaultShaders(shManager: CoreShaderManager) {
181
192
  registerDefaultShaderRoundedWithShadow(shManager);
182
193
  registerDefaultShaderRoundedWithBorderAndShadow(shManager);
183
194
  registerDefaultShaderHolePunch(shManager);
195
+ registerDefaultShaderDashedBorder(shManager);
184
196
  registerDefaultShaderRadialGradient(shManager);
185
197
  registerDefaultShaderLinearGradient(shManager);
186
198
  }
@@ -6,7 +6,15 @@ export type NodeStates =
6
6
  | Record<DollarString, boolean | undefined>;
7
7
 
8
8
  export default class States extends Array<DollarString> {
9
- private onChange: () => void;
9
+ // Assigned in the constructor, not a class field (no field emit).
10
+ declare private onChange: () => void;
11
+
12
+ // Array methods (slice, splice, filter, …) build a plain Array, not a
13
+ // States: building a States ran this constructor with a length as its
14
+ // callback, once per remove().
15
+ static override get [Symbol.species]() {
16
+ return Array;
17
+ }
10
18
 
11
19
  constructor(callback: () => void, initialState: NodeStates = {}) {
12
20
  if (isArray(initialState)) {
@@ -20,11 +28,10 @@ export default class States extends Array<DollarString> {
20
28
  } else if (isString(initialState)) {
21
29
  super(initialState); // Assert as DollarString
22
30
  } else {
23
- super(
24
- ...Object.entries(initialState)
25
- .filter(([_key, value]) => value)
26
- .map(([key]) => key as DollarString), // Assert as DollarString
27
- );
31
+ super();
32
+ for (const key in initialState) {
33
+ if (initialState[key as DollarString]) this.push(key as DollarString);
34
+ }
28
35
  }
29
36
 
30
37
  this.onChange = callback;
@@ -97,7 +104,7 @@ export default class States extends Array<DollarString> {
97
104
  } else {
98
105
  const stateIndexToRemove = this.indexOf(state as DollarString);
99
106
  if (stateIndexToRemove >= 0) {
100
- this.splice(stateIndexToRemove, 1);
107
+ this.removeAt(stateIndexToRemove);
101
108
  }
102
109
  }
103
110
  }
@@ -106,10 +113,23 @@ export default class States extends Array<DollarString> {
106
113
  }
107
114
 
108
115
  remove(state: DollarString) {
109
- const stateIndexToRemove = this.indexOf(state);
116
+ let stateIndexToRemove = this.indexOf(state);
117
+ // remove('focus') removes '$focus', as has('focus') matches it (B2).
118
+ if (stateIndexToRemove < 0 && state.charCodeAt(0) !== 36) {
119
+ stateIndexToRemove = this.indexOf(('$' + state) as DollarString);
120
+ }
110
121
  if (stateIndexToRemove >= 0) {
111
- this.splice(stateIndexToRemove, 1);
122
+ this.removeAt(stateIndexToRemove);
112
123
  this.onChange();
113
124
  }
114
125
  }
126
+
127
+ // Shift in place: splice would allocate the array of removed entries.
128
+ private removeAt(index: number) {
129
+ const last = this.length - 1;
130
+ for (let i = index; i < last; i++) {
131
+ this[i] = this[i + 1]!;
132
+ }
133
+ this.length = last;
134
+ }
115
135
  }
@@ -0,0 +1,195 @@
1
+ // Compiled style plans (design 3.3). A `$state` block and a border or shadow
2
+ // object are read once per object, into flat arrays, and cached by object
3
+ // identity: static module-level styles compile once per app, an inline
4
+ // literal once per node. A key that is a getter keeps being read from its
5
+ // object on each apply, so getters are read at the same moments as before.
6
+ //
7
+ // No imports from the rest of core: elementNode.ts imports this file.
8
+
9
+ /** One `$state` block, compiled. */
10
+ export interface BlockPlan {
11
+ /** The block's own enumerable keys, in its key order. */
12
+ readonly keys: string[];
13
+ /** The value of each key, by position (undefined for a getter). */
14
+ readonly values: unknown[];
15
+ /** By position: true for a getter, whose value is read from `block`. */
16
+ readonly getters: boolean[];
17
+ /** The position of each key (an object without a prototype). */
18
+ readonly index: Record<string, number | undefined>;
19
+ /** The block itself. */
20
+ readonly block: Record<string, unknown>;
21
+ }
22
+
23
+ /** A style's `$state` blocks, compiled: one BlockPlan per `$` key. */
24
+ export type StylePlan = Record<string, BlockPlan>;
25
+
26
+ /** A border or shadow object as shader props, in the order they are written. */
27
+ export interface ShaderParse {
28
+ /** The prefix itself (`border` for every side), then one per object key. */
29
+ readonly keys: string[];
30
+ readonly values: unknown[];
31
+ /** False when the object has a getter: parsed again on each write. */
32
+ readonly cached: boolean;
33
+ }
34
+
35
+ const blockPlans = new WeakMap<object, BlockPlan>();
36
+ const stylePlans = new WeakMap<object, StylePlan>();
37
+
38
+ /** The shader-prop prefixes of the style keys that take a border or shadow object. */
39
+ const SHADER_PREFIXES: Record<string, string | undefined> = {
40
+ border: 'border',
41
+ borderTop: 'border',
42
+ borderRight: 'border',
43
+ borderBottom: 'border',
44
+ borderLeft: 'border',
45
+ shadow: 'shadow',
46
+ };
47
+
48
+ /** A border side's width key. */
49
+ const BORDER_SIDES: Record<string, string | undefined> = {
50
+ borderTop: 'top',
51
+ borderRight: 'right',
52
+ borderBottom: 'bottom',
53
+ borderLeft: 'left',
54
+ };
55
+
56
+ /** One parse cache per style key: the same object parses differently under each. */
57
+ const shaderParses: Record<string, WeakMap<object, ShaderParse>> = {
58
+ border: new WeakMap(),
59
+ borderTop: new WeakMap(),
60
+ borderRight: new WeakMap(),
61
+ borderBottom: new WeakMap(),
62
+ borderLeft: new WeakMap(),
63
+ shadow: new WeakMap(),
64
+ };
65
+
66
+ function isGetter(obj: object, key: string): boolean {
67
+ const desc = Object.getOwnPropertyDescriptor(obj, key);
68
+ return desc !== undefined && desc.get !== undefined;
69
+ }
70
+
71
+ function isObject(value: unknown): value is Record<string, unknown> {
72
+ return value !== null && typeof value === 'object';
73
+ }
74
+
75
+ /** The plan of a `$state` block, compiled on first use. */
76
+ export function compileBlock(block: Record<string, unknown>): BlockPlan {
77
+ let plan = blockPlans.get(block);
78
+ if (plan !== undefined) {
79
+ return plan;
80
+ }
81
+ // Object.keys: the own enumerable keys, the ones Object.assign and spread
82
+ // read when states were applied before 1.7.
83
+ const keys = Object.keys(block);
84
+ const values: unknown[] = [];
85
+ const getters: boolean[] = [];
86
+ const index = Object.create(null) as Record<string, number | undefined>;
87
+ let cacheable = true;
88
+ for (let i = 0; i < keys.length; i++) {
89
+ const key = keys[i]!;
90
+ // A getter is not read here: each apply reads it, as before 1.7.
91
+ const getter = isGetter(block, key);
92
+ const value = getter ? undefined : block[key];
93
+ values.push(value);
94
+ getters.push(getter);
95
+ if (getter) {
96
+ cacheable = false;
97
+ }
98
+ index[key] = i;
99
+ if (SHADER_PREFIXES[key] !== undefined && isObject(value)) {
100
+ // Pre-parse into shader keys, so the first apply finds it cached.
101
+ shaderParse(key, value);
102
+ }
103
+ }
104
+ plan = { keys, values, getters, index, block };
105
+ // A block with a getter (every key of a Solid store) can gain or lose keys
106
+ // with the same identity: it is compiled on every call, as compileStyle
107
+ // does for a getter `$state` key.
108
+ if (cacheable) {
109
+ blockPlans.set(block, plan);
110
+ }
111
+ return plan;
112
+ }
113
+
114
+ /**
115
+ * The plans of a style's `$state` blocks, cached by the style object. A
116
+ * style whose `$state` key is a getter can give another block each read: it
117
+ * is compiled on every call.
118
+ */
119
+ export function compileStyle(style: object): StylePlan {
120
+ let plan = stylePlans.get(style);
121
+ if (plan !== undefined) {
122
+ return plan;
123
+ }
124
+ plan = Object.create(null) as StylePlan;
125
+ let cacheable = true;
126
+ const source = style as Record<string, unknown>;
127
+ for (const key in source) {
128
+ if (key.charCodeAt(0) !== 36 /* $ */) {
129
+ continue;
130
+ }
131
+ const block = source[key];
132
+ if (isObject(block)) {
133
+ plan[key] = compileBlock(block);
134
+ if (isGetter(style, key)) {
135
+ cacheable = false;
136
+ }
137
+ }
138
+ }
139
+ if (cacheable) {
140
+ stylePlans.set(style, plan);
141
+ }
142
+ return plan;
143
+ }
144
+
145
+ /**
146
+ * The shader props a `border`, `borderTop`/`Right`/`Bottom`/`Left` or
147
+ * `shadow` object stands for: `<prefix>` itself (the object), then
148
+ * `<prefix>-<key>` per key, with `width` written `w` (a side's width is the
149
+ * side: `border-top`). The same keys, order and values as the writes before
150
+ * 1.7, parsed once per object.
151
+ */
152
+ export function shaderParse(
153
+ key: string,
154
+ obj: Record<string, unknown>,
155
+ ): ShaderParse {
156
+ const cache = shaderParses[key];
157
+ let parse = cache !== undefined ? cache.get(obj) : undefined;
158
+ if (parse !== undefined) {
159
+ return parse;
160
+ }
161
+ const side = BORDER_SIDES[key];
162
+ const prefix = side !== undefined ? 'border' : key;
163
+ const keys: string[] = [prefix];
164
+ const values: unknown[] = [obj];
165
+ // The position of each shader key, to merge `width` and `w`.
166
+ const index = Object.create(null) as Record<string, number | undefined>;
167
+ index[prefix] = 0;
168
+ let cached = true;
169
+ const own = Object.keys(obj);
170
+ for (let i = 0; i < own.length; i++) {
171
+ const name = own[i]!;
172
+ if (isGetter(obj, name)) {
173
+ cached = false;
174
+ }
175
+ let sub = name === 'width' ? 'w' : name;
176
+ if (side !== undefined && sub === 'w') {
177
+ sub = side;
178
+ }
179
+ const shaderKey = prefix + '-' + sub;
180
+ const at = index[shaderKey];
181
+ if (at !== undefined) {
182
+ // `width` and `w` both given: the later one wins, as it did.
183
+ values[at] = obj[name];
184
+ } else {
185
+ index[shaderKey] = keys.length;
186
+ keys.push(shaderKey);
187
+ values.push(obj[name]);
188
+ }
189
+ }
190
+ parse = { keys, values, cached };
191
+ if (cached && cache !== undefined) {
192
+ cache.set(obj, parse);
193
+ }
194
+ return parse;
195
+ }
package/src/core/utils.ts CHANGED
@@ -126,8 +126,8 @@ function convertEffectsToShader(styleEffects) {
126
126
  const effects = [];
127
127
  let index = 0;
128
128
 
129
- for (const [type, props] of Object.entries(styleEffects)) {
130
- effects.push({ type, props });
129
+ for (const type in styleEffects) {
130
+ effects.push({ type, props: styleEffects[type] });
131
131
  index++;
132
132
  }
133
133
  return createShader('DynamicShader', { effects });
@@ -1,6 +1,6 @@
1
- import { createSignal, createMemo, createEffect, JSX, untrack, Index } from "solid-js";
1
+ import { createSignal, createMemo, createEffect, createRenderEffect, JSX, untrack, Index } from "solid-js";
2
2
  import { type NodeProps, type INode, ElementNode, NewOmit, hasFocus } from "@solidtv/solid";
3
- import { chainFunctions, chainRefs } from "./utils/chainFunctions.js";
3
+ import { chainRefs } from "./utils/chainFunctions.js";
4
4
 
5
5
  export interface GridItemProps<T> {
6
6
  item: T
@@ -144,21 +144,39 @@ export function Grid<T>(props: GridProps<T>): JSX.Element {
144
144
  return -topRow * totalHeight() + y;
145
145
  });
146
146
 
147
+ // Created once. The spread below re-runs on every vertical move (for `y`),
148
+ // and handlers built inline in it were new functions each time. Each runs
149
+ // the app's handler of the same name first (read when called, so a reactive
150
+ // one still applies) and stops if it returns true.
151
+ type HandlerName = "onUp" | "onDown" | "onLeft" | "onRight" | "onFocus";
152
+ function chainProp(name: HandlerName, own: () => boolean) {
153
+ return function (this: ElementNode, a: unknown, b: unknown, c: unknown, d?: unknown) {
154
+ const user = props[name] as ((...args: unknown[]) => unknown) | undefined;
155
+ if (typeof user === "function" && user.call(this, a, b, c, d) === true) return true;
156
+ return own();
157
+ };
158
+ }
159
+ const onUp = chainProp("onUp", () => moveFocus(-columns()));
160
+ const onDown = chainProp("onDown", () => moveFocus(columns()));
161
+ const onLeft = chainProp("onLeft", () => handleHorizontalFocus(-1));
162
+ const onRight = chainProp("onRight", () => handleHorizontalFocus(1));
163
+ const onFocus = chainProp("onFocus", () => handleHorizontalFocus(0));
164
+
147
165
  let gridRef!: ElementNode;
148
- return (
166
+ const view = (
149
167
  <view
150
168
  {...props}
151
169
  ref={chainRefs(el => gridRef = el, props.ref)}
152
170
  transition={/* @once */ { y: true }}
153
171
  height={totalHeight() * rows()}
154
172
  scrollToIndex={/* @once */ scrollToIndex}
155
- onUp={chainFunctions(props.onUp, () => moveFocus(-columns()))}
156
- onDown={chainFunctions(props.onDown, () => moveFocus(columns()))}
157
- onLeft={chainFunctions(props.onLeft, () => handleHorizontalFocus(-1))}
158
- onRight={chainFunctions(props.onRight, () => handleHorizontalFocus(1))}
159
- onFocus={chainFunctions(props.onFocus, () => handleHorizontalFocus(0))}
173
+ onUp={/* @once */ onUp}
174
+ onDown={/* @once */ onDown}
175
+ onLeft={/* @once */ onLeft}
176
+ onRight={/* @once */ onRight}
177
+ onFocus={/* @once */ onFocus}
160
178
  strictBounds={false}
161
- y={scrollY()}
179
+ y={/* @once */ untrack(scrollY)}
162
180
  >
163
181
  <Index each={props.items}>
164
182
  {(item, index) => (
@@ -174,4 +192,12 @@ export function Grid<T>(props: GridProps<T>): JSX.Element {
174
192
  </Index>
175
193
  </view>
176
194
  );
195
+
196
+ // `y` has an effect of its own: in the spread above, every vertical move
197
+ // (a focusedIndex change) re-ran every prop of the spread.
198
+ createRenderEffect(() => {
199
+ gridRef.y = scrollY();
200
+ });
201
+
202
+ return view;
177
203
  };