Skip to content

Commit 672b03e

Browse files
committed
fix(fields): recoverable lazy widgets hand over the loaded widget through load(); the form-field census reads it
1 parent b572a58 commit 672b03e

3 files changed

Lines changed: 35 additions & 17 deletions

File tree

‎ui/examples/schema-catalog/test/form-field-channel-census-12130.test.tsx‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,16 @@ function recorded<T extends object>(target: T, via: 'entry' | 'props'): T {
121121

122122
/* ── every field widget, re-registered with recorded props ───────────────── */
123123

124-
interface LazyInternals {
125-
_init: (payload: unknown) => unknown;
126-
_payload: unknown;
127-
}
128124
type WidgetFn = (props: Record<string, unknown>) => React.ReactNode;
129125

130-
/** The component a `React.lazy` resolves to. Module loading, not rendering. */
126+
/**
127+
* The component a lazily loaded field widget resolves to. Module loading, not
128+
* rendering: `getLazyFieldWidget` hands back the fields package's recoverable
129+
* lazy component, whose `load()` resolves to the widget itself
130+
* (objectstack#36608).
131+
*/
131132
async function resolveLazy(lazy: unknown): Promise<unknown> {
132-
const internals = lazy as LazyInternals;
133-
for (;;) {
134-
try {
135-
return internals._init(internals._payload);
136-
} catch (pending) {
137-
if (pending && typeof (pending as PromiseLike<unknown>).then === 'function') await pending;
138-
else throw pending;
139-
}
140-
}
133+
return (lazy as { load(): Promise<unknown> }).load();
141134
}
142135

143136
const MEMO = Symbol.for('react.memo');

‎ui/packages/fields/src/__tests__/lazyWidgetRecovery-36608.test.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ describe('recoverableLazy', () => {
6868

6969
render(tree(Widget));
7070
expect(screen.getByTestId('widget-loaded')).toBeTruthy();
71+
// `load()` hands over the widget itself, through the same request.
72+
expect(await Widget.load()).toBe(Loaded);
7173
expect(loader).toHaveBeenCalledTimes(1);
7274
});
7375

‎ui/packages/fields/src/recoverableLazy.ts‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ import React from 'react';
1313
/** What `React.lazy` takes: a dynamic import of a module whose `default` is the component. */
1414
export type LazyWidgetLoader<C extends React.ComponentType<any>> = () => Promise<{ default: C }>;
1515

16+
/**
17+
* The component {@link recoverableLazy} returns. `load()` resolves to the
18+
* loaded widget itself, through the same shared request a mount makes (and,
19+
* like a mount, asks again after a failure). It is the seam a caller that
20+
* needs the widget's own function, not an element of it, reads instead of
21+
* `React.lazy`'s private fields; hosts render the component and never need it.
22+
*/
23+
export type RecoverableLazyComponent<C extends React.ComponentType<any>> = React.ComponentType<
24+
React.ComponentProps<C>
25+
> & { load(): Promise<C> };
26+
1627
/** One request for the chunk, shared by every mount that asks while it stands. */
1728
interface LoadAttempt {
1829
readonly Lazy: React.ComponentType<any>;
@@ -49,14 +60,24 @@ interface LoadAttempt {
4960
*/
5061
export function recoverableLazy<C extends React.ComponentType<any>>(
5162
loader: LazyWidgetLoader<C>,
52-
): React.ComponentType<React.ComponentProps<C>> {
63+
): RecoverableLazyComponent<C> {
5364
let current: LoadAttempt | undefined;
65+
// The import in flight, shared by every attempt and by `load()`; forgotten
66+
// on failure, so whoever asks next imports again.
67+
let inflight: Promise<{ default: C }> | undefined;
68+
const importOnce = (): Promise<{ default: C }> => {
69+
inflight ??= loader().catch((error: unknown) => {
70+
inflight = undefined;
71+
throw error;
72+
});
73+
return inflight;
74+
};
5475

5576
const currentAttempt = (): LoadAttempt => {
5677
if (!current || current.retired) {
5778
const attempt: { Lazy?: React.ComponentType<any>; retired: boolean } = { retired: false };
5879
attempt.Lazy = React.lazy<React.ComponentType<any>>(() =>
59-
loader().then(
80+
importOnce().then(
6081
(loaded) => loaded,
6182
(error: unknown) => ({ default: failedLoad(attempt as LoadAttempt, error) }),
6283
),
@@ -72,7 +93,9 @@ export function recoverableLazy<C extends React.ComponentType<any>>(
7293
const [attempt] = React.useState(currentAttempt);
7394
return React.createElement(attempt.Lazy, props);
7495
}
75-
return RecoverableLazy;
96+
return Object.assign(RecoverableLazy, {
97+
load: (): Promise<C> => importOnce().then((loaded) => loaded.default),
98+
});
7699
}
77100

78101
/**

0 commit comments

Comments
 (0)