@typewirets/react 0.1.0 → 0.2.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.
- package/CHANGELOG.md +28 -0
- package/README.md +87 -1
- package/dist/cjs/index.d.ts +235 -2
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +330 -34
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/package.json +1 -0
- package/dist/esm/index.d.ts +235 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +329 -34
- package/dist/esm/index.js.map +1 -1
- package/dist/types/index.d.ts +235 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +329 -34
- package/dist/types/index.js.map +1 -1
- package/package.json +11 -10
- package/src/index.tsx +0 -111
package/dist/cjs/index.js
CHANGED
|
@@ -43,94 +43,390 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
43
43
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
44
44
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
45
45
|
};
|
|
46
|
-
var _ReactResolutionContext_resolutionContext,
|
|
46
|
+
var _ReactResolutionContext_instances, _ReactResolutionContext_resolutionContext, _ReactResolutionContext_resolutionErrors, _ReactResolutionContext_generation, _ReactResolutionContext_allClearedAt, _ReactResolutionContext_clearedAt, _ReactResolutionContext_clearedSince;
|
|
47
47
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
48
|
exports.ContainerContext = exports.ReactResolutionContext = void 0;
|
|
49
49
|
exports.useContainer = useContainer;
|
|
50
50
|
exports.useTypeWire = useTypeWire;
|
|
51
|
+
exports.usePreload = usePreload;
|
|
51
52
|
exports.ResolutionContextProvider = ResolutionContextProvider;
|
|
52
53
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
54
|
+
/**
|
|
55
|
+
* TypeWire React Integration
|
|
56
|
+
*
|
|
57
|
+
* Provides React bindings for TypeWire dependency injection, enabling
|
|
58
|
+
* context-based dependency resolution within React component trees.
|
|
59
|
+
*
|
|
60
|
+
* Key exports:
|
|
61
|
+
* - {@link ReactResolutionContext} — Wraps a `ResolutionContext` with error caching, so a failed resolution doesn't loop under Suspense
|
|
62
|
+
* - {@link ContainerContext} — React Context holding the active resolution context
|
|
63
|
+
* - {@link useContainer} — Hook to access the current resolution context
|
|
64
|
+
* - {@link useTypeWire} — Hook to resolve a TypeWire with React Suspense support
|
|
65
|
+
* - {@link usePreload} — Hook that suspends a subtree once until a set of wires is created
|
|
66
|
+
* - {@link ResolutionContextProvider} — Component that provides a resolution context to the tree
|
|
67
|
+
*
|
|
68
|
+
* @packageDocumentation
|
|
69
|
+
*/
|
|
53
70
|
const React = __importStar(require("react"));
|
|
54
71
|
const core_1 = require("@typewirets/core");
|
|
72
|
+
/**
|
|
73
|
+
* A React-aware wrapper around {@link ResolutionContext} that caches resolution
|
|
74
|
+
* errors.
|
|
75
|
+
*
|
|
76
|
+
* When a component suspends on a resolution that then fails, React renders it
|
|
77
|
+
* again, and that render would start a new creation, suspend, fail and repeat
|
|
78
|
+
* forever. To stop that loop, the error from a failed `get` is cached, and
|
|
79
|
+
* later `get` and `getSync` calls for the same `TypeSymbol` throw it without
|
|
80
|
+
* calling the underlying context.
|
|
81
|
+
*
|
|
82
|
+
* A cached error lasts until {@link ReactResolutionContext.clearErrors} is
|
|
83
|
+
* called, even across `unbind` and a re-apply: this class only sees a
|
|
84
|
+
* `ResolutionContext`, so it cannot tell that a binding changed. To retry
|
|
85
|
+
* after replacing a binding, call `clearErrors` and then reset the error
|
|
86
|
+
* boundary that caught the error.
|
|
87
|
+
*
|
|
88
|
+
* Calls that run while a resolution is in flight are not deduplicated here.
|
|
89
|
+
* The underlying context must share one in-flight creation per singleton
|
|
90
|
+
* itself, or readers may get different instances. `TypeWireContainer` does,
|
|
91
|
+
* and never hands a creation started before `unbind` to new callers. So does
|
|
92
|
+
* the `@typewirets/inversify` adapter, because Inversify 7 shares concurrent
|
|
93
|
+
* singleton `getAsync` calls. A custom `ResolutionContext` must do the same.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* const container = new TypeWireContainer();
|
|
98
|
+
* const context = new ReactResolutionContext(container);
|
|
99
|
+
* const value = await context.get(MySymbol);
|
|
100
|
+
*
|
|
101
|
+
* // After replacing the binding for MySymbol:
|
|
102
|
+
* context.clearErrors(MySymbol);
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
55
105
|
class ReactResolutionContext {
|
|
56
106
|
constructor(resolutionContext) {
|
|
107
|
+
_ReactResolutionContext_instances.add(this);
|
|
108
|
+
/** The underlying resolution context that performs actual dependency resolution. */
|
|
57
109
|
_ReactResolutionContext_resolutionContext.set(this, void 0);
|
|
58
|
-
|
|
110
|
+
/** Cached resolution errors, keyed by symbol, to fail fast on repeated access. */
|
|
59
111
|
_ReactResolutionContext_resolutionErrors.set(this, new Map());
|
|
112
|
+
/** Counts `clearErrors` calls, so a `get` can tell if one ran while it waited. */
|
|
113
|
+
_ReactResolutionContext_generation.set(this, 0);
|
|
114
|
+
/** The generation of the last `clearErrors()` call that cleared every symbol. */
|
|
115
|
+
_ReactResolutionContext_allClearedAt.set(this, 0);
|
|
116
|
+
/** The generation of the last `clearErrors(typeSymbol)` call, per symbol. */
|
|
117
|
+
_ReactResolutionContext_clearedAt.set(this, new Map());
|
|
60
118
|
__classPrivateFieldSet(this, _ReactResolutionContext_resolutionContext, resolutionContext, "f");
|
|
61
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Synchronously retrieves a resolved instance for the given type symbol.
|
|
122
|
+
*
|
|
123
|
+
* @template T The type of the resolved instance
|
|
124
|
+
* @param typeSymbol - The typed symbol identifying the dependency to resolve
|
|
125
|
+
* @returns The resolved instance
|
|
126
|
+
* @throws Re-throws the cached error if a previous async resolution for this symbol failed
|
|
127
|
+
* @throws Throws if the instance has not been resolved yet (see {@link ResolutionContext.getSync})
|
|
128
|
+
*/
|
|
62
129
|
getSync(typeSymbol) {
|
|
63
130
|
if (__classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").has(typeSymbol.symbol)) {
|
|
64
131
|
throw __classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").get(typeSymbol.symbol);
|
|
65
132
|
}
|
|
66
133
|
return __classPrivateFieldGet(this, _ReactResolutionContext_resolutionContext, "f").getSync(typeSymbol);
|
|
67
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Asynchronously resolves an instance for the given type symbol.
|
|
137
|
+
*
|
|
138
|
+
* If a previous resolution for this symbol failed, the cached error is
|
|
139
|
+
* thrown immediately. Otherwise the call goes to the underlying context,
|
|
140
|
+
* which joins a creation already in flight for a singleton.
|
|
141
|
+
*
|
|
142
|
+
* @template T The type of the resolved instance
|
|
143
|
+
* @param typeSymbol - The typed symbol identifying the dependency to resolve
|
|
144
|
+
* @returns A promise that resolves to the instance
|
|
145
|
+
* @throws Re-throws the cached error if a previous resolution for this symbol failed
|
|
146
|
+
*/
|
|
68
147
|
async get(typeSymbol) {
|
|
69
|
-
|
|
70
|
-
|
|
148
|
+
const symbol = typeSymbol.symbol;
|
|
149
|
+
if (__classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").has(symbol)) {
|
|
150
|
+
throw __classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").get(symbol);
|
|
71
151
|
}
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
return
|
|
152
|
+
const startedAt = __classPrivateFieldGet(this, _ReactResolutionContext_generation, "f");
|
|
153
|
+
try {
|
|
154
|
+
return await __classPrivateFieldGet(this, _ReactResolutionContext_resolutionContext, "f").get(typeSymbol);
|
|
75
155
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
156
|
+
catch (err) {
|
|
157
|
+
// A failure that settles after `clearErrors` belongs to the attempt the
|
|
158
|
+
// caller just discarded: hand it to this caller, but don't cache it.
|
|
159
|
+
if (!__classPrivateFieldGet(this, _ReactResolutionContext_instances, "m", _ReactResolutionContext_clearedSince).call(this, symbol, startedAt)) {
|
|
160
|
+
__classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").set(symbol, err);
|
|
161
|
+
}
|
|
81
162
|
throw err;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Forgets cached resolution errors, so the next `get` or `getSync` asks the
|
|
167
|
+
* underlying context again.
|
|
168
|
+
*
|
|
169
|
+
* Call it after replacing a failed binding (`unbind`, then apply the wire
|
|
170
|
+
* again), then reset the error boundary that shows the error. A failure that
|
|
171
|
+
* was still in flight when you call this is not cached when it settles.
|
|
172
|
+
*
|
|
173
|
+
* @param typeSymbol - The symbol whose error to forget. Omit to forget all.
|
|
174
|
+
*/
|
|
175
|
+
clearErrors(typeSymbol) {
|
|
176
|
+
__classPrivateFieldSet(this, _ReactResolutionContext_generation, __classPrivateFieldGet(this, _ReactResolutionContext_generation, "f") + 1, "f");
|
|
177
|
+
if (typeSymbol) {
|
|
178
|
+
__classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").delete(typeSymbol.symbol);
|
|
179
|
+
__classPrivateFieldGet(this, _ReactResolutionContext_clearedAt, "f").set(typeSymbol.symbol, __classPrivateFieldGet(this, _ReactResolutionContext_generation, "f"));
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
__classPrivateFieldGet(this, _ReactResolutionContext_resolutionErrors, "f").clear();
|
|
183
|
+
__classPrivateFieldGet(this, _ReactResolutionContext_clearedAt, "f").clear();
|
|
184
|
+
__classPrivateFieldSet(this, _ReactResolutionContext_allClearedAt, __classPrivateFieldGet(this, _ReactResolutionContext_generation, "f"), "f");
|
|
86
185
|
}
|
|
87
186
|
}
|
|
88
187
|
exports.ReactResolutionContext = ReactResolutionContext;
|
|
89
|
-
_ReactResolutionContext_resolutionContext = new WeakMap(),
|
|
188
|
+
_ReactResolutionContext_resolutionContext = new WeakMap(), _ReactResolutionContext_resolutionErrors = new WeakMap(), _ReactResolutionContext_generation = new WeakMap(), _ReactResolutionContext_allClearedAt = new WeakMap(), _ReactResolutionContext_clearedAt = new WeakMap(), _ReactResolutionContext_instances = new WeakSet(), _ReactResolutionContext_clearedSince = function _ReactResolutionContext_clearedSince(symbol, generation) {
|
|
189
|
+
const clearedAt = Math.max(__classPrivateFieldGet(this, _ReactResolutionContext_allClearedAt, "f"), __classPrivateFieldGet(this, _ReactResolutionContext_clearedAt, "f").get(symbol) ?? 0);
|
|
190
|
+
return clearedAt > generation;
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* The wrapper {@link ResolutionContextProvider} uses for each plain context, so
|
|
194
|
+
* every render and every mount of a provider for that context share one error
|
|
195
|
+
* cache and one context value.
|
|
196
|
+
*/
|
|
197
|
+
const reactResolutionContexts = new WeakMap();
|
|
198
|
+
function reactResolutionContextFor(resolutionContext) {
|
|
199
|
+
if (resolutionContext instanceof ReactResolutionContext) {
|
|
200
|
+
return resolutionContext;
|
|
201
|
+
}
|
|
202
|
+
let wrapper = reactResolutionContexts.get(resolutionContext);
|
|
203
|
+
if (!wrapper) {
|
|
204
|
+
wrapper = new ReactResolutionContext(resolutionContext);
|
|
205
|
+
reactResolutionContexts.set(resolutionContext, wrapper);
|
|
206
|
+
}
|
|
207
|
+
return wrapper;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* React Context that holds the active {@link ResolutionContext} for the component tree.
|
|
211
|
+
*
|
|
212
|
+
* The default value is an empty {@link TypeWireContainer} wrapped in a
|
|
213
|
+
* {@link ReactResolutionContext}. In practice, you should provide your own
|
|
214
|
+
* container via {@link ResolutionContextProvider}.
|
|
215
|
+
*/
|
|
90
216
|
exports.ContainerContext = React.createContext(new ReactResolutionContext(new core_1.TypeWireContainer()));
|
|
217
|
+
/**
|
|
218
|
+
* Hook to access the current {@link ResolutionContext} from the nearest
|
|
219
|
+
* {@link ResolutionContextProvider}.
|
|
220
|
+
*
|
|
221
|
+
* @returns The current resolution context
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```tsx
|
|
225
|
+
* function MyComponent() {
|
|
226
|
+
* const container = useContainer();
|
|
227
|
+
* // Use container for manual resolution if needed
|
|
228
|
+
* }
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
91
231
|
function useContainer() {
|
|
92
232
|
const container = React.useContext(exports.ContainerContext);
|
|
93
233
|
return container;
|
|
94
234
|
}
|
|
95
|
-
|
|
235
|
+
/** Error reason thrown by {@link useTypeWire} with `{ suspend: false }`. */
|
|
236
|
+
const ReasonNotPreloaded = "NotPreloaded";
|
|
237
|
+
const ScopeSingleton = "singleton";
|
|
238
|
+
/** Marks a state that has no value yet, so falsy values still count as values. */
|
|
239
|
+
const NotReady = Symbol("NotReady");
|
|
240
|
+
/**
|
|
241
|
+
* Reads a wire synchronously. Returns `NotReady` when the value isn't
|
|
242
|
+
* created yet and can be waited for; rethrows any other error.
|
|
243
|
+
*/
|
|
244
|
+
function readSync(typeWire, container) {
|
|
245
|
+
try {
|
|
246
|
+
return typeWire.getInstanceSync(container);
|
|
247
|
+
}
|
|
248
|
+
catch (err) {
|
|
249
|
+
if (err instanceof core_1.TypeWireError && err.retriable) {
|
|
250
|
+
return NotReady;
|
|
251
|
+
}
|
|
252
|
+
throw err;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
function notPreloaded(typeWire, cause) {
|
|
256
|
+
const name = typeWire.type.symbol.description ?? "(unnamed wire)";
|
|
257
|
+
const err = new core_1.TypeWireError(ReasonNotPreloaded, `${name} is not ready. It was rendered with { suspend: false } before it was created: preload it (container.preload or usePreload) before this component renders.`);
|
|
258
|
+
err.retriable = false;
|
|
259
|
+
Object.defineProperty(err, "cause", {
|
|
260
|
+
value: cause,
|
|
261
|
+
enumerable: false,
|
|
262
|
+
writable: true,
|
|
263
|
+
configurable: true,
|
|
264
|
+
});
|
|
265
|
+
return err;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Hook that resolves a {@link TypeWire} within the current container context,
|
|
269
|
+
* with React Suspense integration.
|
|
270
|
+
*
|
|
271
|
+
* Resolution behavior:
|
|
272
|
+
* 1. **Sync available** — If the wire can be resolved synchronously (e.g., a singleton
|
|
273
|
+
* that was already created or preloaded), the value is returned immediately,
|
|
274
|
+
* including falsy values such as `0`, `""`, `false` and `null`.
|
|
275
|
+
* 2. **Async pending** — If sync resolution fails with a retriable error, the hook
|
|
276
|
+
* starts an async resolution and throws the promise to trigger React Suspense.
|
|
277
|
+
* With `{ suspend: false }` it throws a `TypeWireError` with reason
|
|
278
|
+
* `NotPreloaded` instead.
|
|
279
|
+
* 3. **Error** — If sync resolution fails with a non-retriable error, it is re-thrown.
|
|
280
|
+
*
|
|
281
|
+
* To render without a fallback, preload the wires first, with
|
|
282
|
+
* `container.preload` before rendering or {@link usePreload} higher in the
|
|
283
|
+
* tree. A wire nobody preloaded suspends, so this hook must be used inside a
|
|
284
|
+
* `<Suspense>` boundary unless you pass `{ suspend: false }`.
|
|
285
|
+
*
|
|
286
|
+
* @template T The type of the resolved instance
|
|
287
|
+
* @param typeWire - The TypeWire to resolve
|
|
288
|
+
* @param opts - Optional settings; see {@link UseTypeWireOpts}
|
|
289
|
+
* @returns The resolved instance
|
|
290
|
+
* @throws The resolution promise when the value is not yet available (for Suspense)
|
|
291
|
+
* @throws {@link TypeWireError} with reason `NotPreloaded` when the value is not yet available and `suspend` is `false`
|
|
292
|
+
* @throws {@link TypeWireError} if synchronous resolution fails with a non-retriable error
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```tsx
|
|
296
|
+
* function UserProfile() {
|
|
297
|
+
* const userService = useTypeWire(UserServiceWire);
|
|
298
|
+
* return <div>{userService.getName()}</div>;
|
|
299
|
+
* }
|
|
300
|
+
*
|
|
301
|
+
* // Preloaded before rendering: no fallback.
|
|
302
|
+
* await container.preload({ wires: [UserServiceWire] });
|
|
303
|
+
*
|
|
304
|
+
* // Or wrap in Suspense for wires that may not be ready yet.
|
|
305
|
+
* <Suspense fallback={<Loading />}>
|
|
306
|
+
* <UserProfile />
|
|
307
|
+
* </Suspense>
|
|
308
|
+
* ```
|
|
309
|
+
*/
|
|
310
|
+
function useTypeWire(typeWire, opts) {
|
|
96
311
|
const container = useContainer();
|
|
97
|
-
const
|
|
312
|
+
const suspend = opts?.suspend ?? true;
|
|
313
|
+
const [state, setState] = React.useState(() => ({ value: readSync(typeWire, container) }));
|
|
314
|
+
if (state.value === NotReady && !suspend) {
|
|
315
|
+
let cause;
|
|
98
316
|
try {
|
|
99
|
-
|
|
317
|
+
typeWire.getInstanceSync(container);
|
|
100
318
|
}
|
|
101
319
|
catch (err) {
|
|
102
|
-
|
|
103
|
-
return null;
|
|
104
|
-
}
|
|
105
|
-
throw err;
|
|
320
|
+
cause = err;
|
|
106
321
|
}
|
|
107
|
-
|
|
322
|
+
throw notPreloaded(typeWire, cause);
|
|
323
|
+
}
|
|
108
324
|
const promise = typeWire.getInstance(container);
|
|
109
325
|
React.useEffect(() => {
|
|
110
|
-
let mounted =
|
|
326
|
+
let mounted = true;
|
|
111
327
|
promise
|
|
112
328
|
.then((value) => {
|
|
113
329
|
if (mounted) {
|
|
114
|
-
|
|
115
|
-
|
|
330
|
+
// Keep the same state object for the same value, so React skips the
|
|
331
|
+
// re-render; a new object here would re-render, start a new
|
|
332
|
+
// `getInstance` and run this effect again, forever.
|
|
333
|
+
setState((prev) => (Object.is(prev.value, value) ? prev : { value }));
|
|
116
334
|
}
|
|
117
335
|
})
|
|
118
336
|
.catch((err) => {
|
|
119
|
-
|
|
337
|
+
if (mounted) {
|
|
338
|
+
console.error(err);
|
|
339
|
+
}
|
|
120
340
|
});
|
|
121
341
|
return () => {
|
|
122
342
|
mounted = false;
|
|
123
343
|
};
|
|
124
344
|
}, [promise]);
|
|
125
|
-
if (value) {
|
|
126
|
-
return value;
|
|
345
|
+
if (state.value !== NotReady) {
|
|
346
|
+
return state.value;
|
|
127
347
|
}
|
|
128
348
|
throw promise;
|
|
129
349
|
}
|
|
350
|
+
/**
|
|
351
|
+
* Hook that suspends the calling component until every singleton in
|
|
352
|
+
* `opts.wires` is created, so components below it read them with
|
|
353
|
+
* {@link useTypeWire} without suspending.
|
|
354
|
+
*
|
|
355
|
+
* Use it where a part of the app starts (a route, a panel, a feature), under
|
|
356
|
+
* one `<Suspense>` boundary: the fallback shows once for the whole set, and
|
|
357
|
+
* the creations run in parallel. A creation that fails reaches the nearest
|
|
358
|
+
* error boundary.
|
|
359
|
+
*
|
|
360
|
+
* It works with any resolution context, including the Inversify adapter.
|
|
361
|
+
* Outside React, use `container.preload` instead.
|
|
362
|
+
*
|
|
363
|
+
* @param opts - The wires to preload; see {@link UsePreloadOpts}
|
|
364
|
+
* @throws A promise while any of the wires is still being created (for Suspense)
|
|
365
|
+
* @throws The creation's error, once one has failed
|
|
366
|
+
*
|
|
367
|
+
* @example
|
|
368
|
+
* ```tsx
|
|
369
|
+
* function ReportsRoute() {
|
|
370
|
+
* usePreload({ wires: [reportsServiceWire, chartsWire] });
|
|
371
|
+
* return <ReportsPage />;
|
|
372
|
+
* }
|
|
373
|
+
*
|
|
374
|
+
* <Suspense fallback={<Loading />}>
|
|
375
|
+
* <ReportsRoute />
|
|
376
|
+
* </Suspense>
|
|
377
|
+
* ```
|
|
378
|
+
*/
|
|
379
|
+
function usePreload(opts) {
|
|
380
|
+
const container = useContainer();
|
|
381
|
+
const pending = [];
|
|
382
|
+
for (const wire of opts.wires) {
|
|
383
|
+
if ((wire.scope ?? ScopeSingleton) !== ScopeSingleton) {
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
if (readSync(wire, container) === NotReady) {
|
|
387
|
+
pending.push(wire.getInstance(container));
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
if (pending.length > 0) {
|
|
391
|
+
throw Promise.all(pending);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* React component that provides a {@link ResolutionContext} to the component tree.
|
|
396
|
+
*
|
|
397
|
+
* If the given `resolutionContext` is not already a {@link ReactResolutionContext},
|
|
398
|
+
* it is wrapped in one, so a failed resolution reaches an error boundary
|
|
399
|
+
* instead of retrying under Suspense. Each plain context gets one wrapper,
|
|
400
|
+
* reused on every render and every mount, so re-rendering the provider keeps
|
|
401
|
+
* cached errors and gives consumers the same context value.
|
|
402
|
+
*
|
|
403
|
+
* A cached error lasts until `clearErrors` is called, even after `unbind` and
|
|
404
|
+
* a re-apply. To be able to retry, create the {@link ReactResolutionContext}
|
|
405
|
+
* yourself, pass it here, and call its `clearErrors` after replacing the
|
|
406
|
+
* binding.
|
|
407
|
+
*
|
|
408
|
+
* The context must share one in-flight creation per singleton (as
|
|
409
|
+
* `TypeWireContainer` does); see {@link ReactResolutionContext}.
|
|
410
|
+
*
|
|
411
|
+
* @param props.resolutionContext - The resolution context to provide to descendants
|
|
412
|
+
* @param props.children - Child components that will have access to the context
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* ```tsx
|
|
416
|
+
* async function main() {
|
|
417
|
+
* const container = new TypeWireContainer();
|
|
418
|
+
* await AppWireGroup.apply(container);
|
|
419
|
+
*
|
|
420
|
+
* createRoot(document.getElementById('root')!).render(
|
|
421
|
+
* <ResolutionContextProvider resolutionContext={container}>
|
|
422
|
+
* <App />
|
|
423
|
+
* </ResolutionContextProvider>
|
|
424
|
+
* );
|
|
425
|
+
* }
|
|
426
|
+
* ```
|
|
427
|
+
*/
|
|
130
428
|
function ResolutionContextProvider(props) {
|
|
131
|
-
const resolutionContext = props.resolutionContext
|
|
132
|
-
? props.resolutionContext
|
|
133
|
-
: new ReactResolutionContext(props.resolutionContext);
|
|
429
|
+
const resolutionContext = reactResolutionContextFor(props.resolutionContext);
|
|
134
430
|
return ((0, jsx_runtime_1.jsx)(exports.ContainerContext, { value: resolutionContext, children: props.children }));
|
|
135
431
|
}
|
|
136
432
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8MA,oCAGC;AAsGD,kCAgDC;AA2CD,gCAgBC;AAoCD,8DAUC;;AAhdD;;;;;;;;;;;;;;;GAeG;AAEH,6CAA+B;AAC/B,2CAQ0B;AAE1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAa,sBAAsB;IAYjC,YAAY,iBAAoC;;QAXhD,oFAAoF;QAC3E,4DAAsC;QAC/C,kFAAkF;QACzE,mDAA0C,IAAI,GAAG,EAAE,EAAC;QAC7D,kFAAkF;QAClF,6CAAc,CAAC,EAAC;QAChB,iFAAiF;QACjF,+CAAgB,CAAC,EAAC;QAClB,6EAA6E;QACpE,4CAAkC,IAAI,GAAG,EAAE,EAAC;QAGnD,uBAAA,IAAI,6CAAsB,iBAAiB,MAAA,CAAC;IAC9C,CAAC;IAED;;;;;;;;OAQG;IACH,OAAO,CAAI,UAAyB;QAClC,IAAI,uBAAA,IAAI,gDAAkB,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;YAClD,MAAM,uBAAA,IAAI,gDAAkB,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QACtD,CAAC;QACD,OAAO,uBAAA,IAAI,iDAAmB,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IACrD,CAA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