@solidtv/solid 1.6.1 → 1.6.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solidtv/solid",
3
- "version": "1.6.1",
3
+ "version": "1.6.3",
4
4
  "description": "SolidTV",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1062,7 +1062,7 @@ export class ElementNode {
1062
1062
  ): IAnimationController {
1063
1063
  if (!this.rendered) {
1064
1064
  if (isDev) console.log('NOT RENDERED! CANNOT ANIMATE');
1065
- return {} as IAnimationController;
1065
+ return { start: () => {} } as IAnimationController;
1066
1066
  }
1067
1067
  return (this.lng as IRendererNode).animate(
1068
1068
  props,
@@ -1667,18 +1667,20 @@ export class ElementNode {
1667
1667
  flexDirection === 'row' || flexDirection === 'row-reverse';
1668
1668
  flexFitsWidth = isFlexRow && node.flexBoundary !== 'fixed';
1669
1669
 
1670
- if (isDev && flexFitsWidth) {
1671
- const needsWidthForJustify = [
1672
- 'center',
1673
- 'flex-end',
1674
- 'space-between',
1675
- 'space-around',
1676
- 'space-evenly',
1677
- ].includes(node.justifyContent as string);
1678
-
1679
- if (needsWidthForJustify) {
1670
+ // Every justify mode except flexStart positions children out of the
1671
+ // container's free space. Shrinking to fit leaves none, so fall back
1672
+ // to filling the parent unless the developer explicitly asked to
1673
+ // contain — in which case honor it and warn about the contradiction.
1674
+ if (
1675
+ flexFitsWidth &&
1676
+ node.justifyContent !== undefined &&
1677
+ node.justifyContent !== 'flexStart'
1678
+ ) {
1679
+ if (node.flexBoundary === undefined) {
1680
+ flexFitsWidth = false;
1681
+ } else if (isDev) {
1680
1682
  console.warn(
1681
- `justifyContent '${node.justifyContent}' requires an explicit width on the flex container; without one it shrinks to fit its children and has no free space to distribute: `,
1683
+ `justifyContent '${node.justifyContent}' has no free space to distribute on a flexBoundary 'contain' container without an explicit width: `,
1682
1684
  this,
1683
1685
  );
1684
1686
  }
@@ -558,7 +558,37 @@ const handleKeyEvents = (keydown?: KeyboardEvent, keyup?: KeyboardEvent) => {
558
558
  }
559
559
  };
560
560
 
561
- export const useFocusManager = (userKeyMap?: Partial<KeyMap>) => {
561
+ /**
562
+ * What the focus manager reads off a key event. A browser's `KeyboardEvent`
563
+ * has these; a host without a DOM raises objects that carry at least them,
564
+ * and key handlers receive whichever object the host raised.
565
+ */
566
+ export interface KeyEventLike {
567
+ readonly key: string;
568
+ readonly keyCode: number;
569
+ readonly repeat: boolean;
570
+ }
571
+
572
+ /**
573
+ * Where {@link useFocusManager} listens for `keydown` and `keyup`: `document`
574
+ * in a browser, or anything with the same two methods on a host without one,
575
+ * such as a native key bridge.
576
+ */
577
+ export interface KeyEventTarget {
578
+ addEventListener(
579
+ type: 'keydown' | 'keyup',
580
+ listener: (event: KeyEventLike) => void,
581
+ ): void;
582
+ removeEventListener(
583
+ type: 'keydown' | 'keyup',
584
+ listener: (event: KeyEventLike) => void,
585
+ ): void;
586
+ }
587
+
588
+ export const useFocusManager = (
589
+ userKeyMap?: Partial<KeyMap>,
590
+ target: KeyEventTarget = document,
591
+ ) => {
562
592
  if (userKeyMap) {
563
593
  flattenKeyMap(userKeyMap, keyMapEntries);
564
594
  }
@@ -577,17 +607,19 @@ export const useFocusManager = (userKeyMap?: Partial<KeyMap>) => {
577
607
  Config.setActiveElement = (elm) =>
578
608
  ownerContext(() => setActiveElementSignal(elm));
579
609
 
580
- const keyPressHandler = (event: KeyboardEvent) =>
581
- ownerContext(() => handleKeyEvents(event, undefined));
582
- const keyUpHandler = (event: KeyboardEvent) =>
583
- ownerContext(() => handleKeyEvents(undefined, event));
610
+ // Handlers are typed as KeyboardEvent throughout; on a host that raises
611
+ // its own objects they see those, which carry the fields read here.
612
+ const keyPressHandler = (event: KeyEventLike) =>
613
+ ownerContext(() => handleKeyEvents(event as KeyboardEvent, undefined));
614
+ const keyUpHandler = (event: KeyEventLike) =>
615
+ ownerContext(() => handleKeyEvents(undefined, event as KeyboardEvent));
584
616
 
585
- document.addEventListener('keydown', keyPressHandler);
586
- document.addEventListener('keyup', keyUpHandler);
617
+ target.addEventListener('keydown', keyPressHandler);
618
+ target.addEventListener('keyup', keyUpHandler);
587
619
 
588
620
  onCleanup(() => {
589
- document.removeEventListener('keydown', keyPressHandler);
590
- document.removeEventListener('keyup', keyUpHandler);
621
+ target.removeEventListener('keydown', keyPressHandler);
622
+ target.removeEventListener('keyup', keyUpHandler);
591
623
  suppressedKeys.clear();
592
624
  });
593
625
  };
package/src/index.ts CHANGED
@@ -1,7 +1,12 @@
1
1
  export type * from '@solidtv/solid/jsx-runtime';
2
2
  export * from './core/index.js';
3
3
  export type * from './core/index.js';
4
- export type { KeyHandler, KeyMap } from './core/focusManager.js';
4
+ export type {
5
+ KeyEventLike,
6
+ KeyEventTarget,
7
+ KeyHandler,
8
+ KeyMap,
9
+ } from './core/focusManager.js';
5
10
  export { activeElement, setActiveElement } from './core/activeElement.js';
6
11
  export { setActiveElementCore } from './core/focusManager.js';
7
12
  export * from './utils.js';
@@ -3,8 +3,8 @@ import {
3
3
  createResource,
4
4
  createMemo,
5
5
  untrack,
6
- Component,
7
- JSX,
6
+ type Component,
7
+ type JSX,
8
8
  sharedConfig,
9
9
  } from 'solid-js';
10
10
 
@@ -31,14 +31,31 @@ const NATIVE_ESM_CHUNK =
31
31
  * @internal Exported for tests. The primitives barrel re-exports `lazy` by
32
32
  * name, so this stays out of the package's public API.
33
33
  */
34
- export const cacheBustableUrl = (error: unknown): string | undefined =>
35
- NATIVE_ESM_CHUNK.exec(
34
+ export const cacheBustableUrl = (error: unknown): string | undefined => {
35
+ const match = NATIVE_ESM_CHUNK.exec(
36
36
  error instanceof Error ? error.message : String(error),
37
- )?.[1];
37
+ );
38
+ return match ? match[1] : undefined;
39
+ };
40
+
41
+ /**
42
+ * Retry backoff delays in milliseconds (500ms, 2s, 5s). Max of 3 retries.
43
+ *
44
+ * @internal Exported for tests — see `cacheBustableUrl`.
45
+ */
46
+ export const RETRY_DELAYS = [500, 2000, 5000];
38
47
 
39
48
  /** @internal Exported for tests — see `cacheBustableUrl`. */
40
- export const cacheBust = (url: string): string =>
41
- `${url}${url.indexOf('?') === -1 ? '?' : '&'}chunkRetry=1`;
49
+ export const cacheBust = (url: string, retry = 1): string => {
50
+ const cleanUrl = url
51
+ .replace(/([?&])chunkRetry=\d+(&|$)/, '$1')
52
+ .replace(/[?&]$/, '');
53
+ const separator = cleanUrl.indexOf('?') === -1 ? '?' : '&';
54
+ return cleanUrl + separator + 'chunkRetry=' + retry;
55
+ };
56
+
57
+ const wait = (ms: number): Promise<void> =>
58
+ new Promise((resolve) => setTimeout(resolve, ms));
42
59
 
43
60
  /**
44
61
  * A dynamic `import()`, built at runtime instead of written inline.
@@ -83,12 +100,12 @@ export function lazy<T extends Component<any>>(
83
100
  let comp: () => T | undefined;
84
101
  let p: Promise<{ default: T }> | undefined;
85
102
 
86
- // Retry the import exactly once before giving up. On a TV the chunk fetch is
87
- // the fragile part — a brief dropout as the viewer presses OK on a rail
88
- // rejects the import, and because `p` memoises the promise that single
89
- // rejection is replayed for the rest of the session: the route never renders
90
- // again, not even on a later navigation. One retry turns the common transient
91
- // failure into a marginally slower navigation.
103
+ // Retry the import up to 3 times before giving up, with a backoff of 500ms, 2s,
104
+ // and 5s between retries. On a TV the chunk fetch is the fragile part — a brief
105
+ // dropout as the viewer presses OK on a rail rejects the import, and because
106
+ // `p` memoises the promise that single rejection is replayed for the rest of
107
+ // the session: the route never renders again, not even on a later navigation.
108
+ // Retries with backoff turn transient failures into a slightly delayed navigation.
92
109
  //
93
110
  // How it retries depends on which module system failed, because the two need
94
111
  // opposite things.
@@ -101,26 +118,59 @@ export function lazy<T extends Component<any>>(
101
118
  //
102
119
  // Native ESM does the opposite. The module map memoises the *failure*, so a
103
120
  // second `import()` of the same specifier resolves straight to the cached
104
- // rejection without ever touching the network — the retry is a no-op exactly
105
- // where it is needed. It also names one module and only one, the one handed to
106
- // `import()`, so a cache-busted re-import of it is both safe and necessary: a
107
- // distinct URL gets a fresh module-map entry and really re-fetches.
121
+ // rejection without ever touching the network — re-running `fn` is a no-op
122
+ // exactly where it is needed. It also names one module and only one, the one
123
+ // handed to `import()`, so a cache-busted re-import of it is both safe and
124
+ // necessary: each retry gets a distinct URL (`chunkRetry=1`, `chunkRetry=2`,
125
+ // `chunkRetry=3`), creating a fresh module-map entry that really re-fetches.
108
126
  //
109
127
  // The cost there is a duplicate module record for this one chunk. Its own
110
128
  // imports are unaffected — they resolve to their normal, already-cached URLs —
111
129
  // so the duplication does not spread, and it only happens on a retry that
112
130
  // would otherwise have left the route dead.
113
- const load = (): Promise<{ default: T }> =>
114
- fn().catch((error: unknown) => {
115
- const url = cacheBustableUrl(error);
116
- if (url === undefined) return fn();
117
- // Null when this engine cannot compile a dynamic import, or a CSP forbids
118
- // compiling one — see `getNativeImport`. Re-running the loader is then the
119
- // best available retry, exactly as it is for every SystemJS failure.
120
- const importer = getNativeImport();
121
- if (importer === null) return fn();
122
- return importer(cacheBust(url)) as Promise<{ default: T }>;
123
- });
131
+ //
132
+ // Written using explicit Promise chaining instead of `async`/`await` so older
133
+ // engines (such as Chrome 38/53 on webOS) and ES5 downleveling do not require
134
+ // regeneratorRuntime or complex state-machine transpilation helpers.
135
+ const load = (): Promise<{ default: T }> => {
136
+ let url: string | undefined;
137
+
138
+ const attemptLoad = (attempt: number): Promise<{ default: T }> => {
139
+ let p: Promise<{ default: T }>;
140
+ try {
141
+ if (attempt === 0) {
142
+ p = fn();
143
+ } else if (url !== undefined) {
144
+ // Null when this engine cannot compile a dynamic import, or a CSP
145
+ // forbids compiling one — see `getNativeImport`. Re-running the
146
+ // loader is then the best available retry, exactly as it is for
147
+ // every SystemJS failure.
148
+ const importer = getNativeImport();
149
+ p = (
150
+ importer !== null ? importer(cacheBust(url, attempt)) : fn()
151
+ ) as Promise<{ default: T }>;
152
+ } else {
153
+ p = fn();
154
+ }
155
+ } catch (syncError: unknown) {
156
+ p = Promise.reject(syncError);
157
+ }
158
+
159
+ return p.catch((error: unknown) => {
160
+ const nextUrl = cacheBustableUrl(error);
161
+ url = nextUrl !== undefined ? nextUrl : url;
162
+ if (attempt < RETRY_DELAYS.length) {
163
+ const delay = RETRY_DELAYS[attempt];
164
+ if (delay !== undefined) {
165
+ return wait(delay).then(() => attemptLoad(attempt + 1));
166
+ }
167
+ }
168
+ return Promise.reject(error);
169
+ });
170
+ };
171
+
172
+ return attemptLoad(0);
173
+ };
124
174
 
125
175
  const wrap: T & { preload?: () => void } = ((props: any) => {
126
176
  const ctx = sharedConfig.context;
@@ -7,6 +7,8 @@ export {
7
7
  printFocusHistory,
8
8
  getFocusHistory,
9
9
  type FocusHistoryEntry,
10
+ type KeyEventLike,
11
+ type KeyEventTarget,
10
12
  type KeyMap,
11
13
  } from '../core/focusManager.js';
12
14
  export { activeElement, setActiveElement } from '../core/activeElement.js';