@navios/di-react 2.0.0-alpha.0 → 2.0.0-alpha.1
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 +27 -2
- package/lib/index.cjs +189 -192
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +88 -40
- package/lib/index.d.cts.map +1 -1
- package/lib/index.d.mts +88 -40
- package/lib/index.d.mts.map +1 -1
- package/lib/index.mjs +189 -192
- package/lib/index.mjs.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -5,7 +5,32 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
-
## [2.0.0-alpha.
|
|
8
|
+
## [2.0.0-alpha.1] - Unreleased
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- **`ServiceHook<K>` and `ServiceHookResults<T>`**: the overloads the service hooks share, as one type. `useService` is a `ServiceHook<'service'>`, `useOptionalService` a `ServiceHook<'optional'>` and `useSuspenseService` a `ServiceHook<'suspense'>`. The hooks are declared as constants of that type; what they accept and return is unchanged.
|
|
13
|
+
|
|
14
|
+
### Changed
|
|
15
|
+
|
|
16
|
+
- **Breaking:** `useService` and `useOptionalService` report `isLoading: true` on the first render when the container does not hold the instance yet. That render used to have every flag `false`.
|
|
17
|
+
- **Breaking:** `useService` reports an error the container raises while reading the token on the first render (an alias cycle, args that contain themselves, a value that is not a token) in `error`. It used to throw during render.
|
|
18
|
+
- **`refetch()` of `useOptionalService`** works like the one of `useService`: it re-renders through `isLoading` and returns `undefined` instead of a promise.
|
|
19
|
+
|
|
20
|
+
### Fixed
|
|
21
|
+
|
|
22
|
+
- **Instance destroyed before the hook listened for it**: `useService` and `useOptionalService` returned an instance the container already held on the first render and subscribed to its invalidation in an effect. An invalidation between the two went unnoticed, and the component kept the destroyed instance. The effect now compares what the container holds and resolves again. A resolution subscribes before it asks for the instance, so an invalidation right after it is not missed either; the hook therefore also resolves again from `error` or `isNotFound` when an instance for its token and args is destroyed.
|
|
23
|
+
- **`useSuspenseService` and an instance the container already held**: the same gap. The cache entry now listens from the render that created it.
|
|
24
|
+
- **`useSuspenseService` cache growth**: a resolved cache entry and its `onInvalidate` listener stayed for the lifetime of the container, one per token and args ever rendered. An entry is dropped one timer turn after the last component that shows it unmounts or changes its args. An entry that was rendered and never mounted, or that is still pending when its component unmounts, stays until the container is collected.
|
|
25
|
+
- **Stale instance in `useOptionalService` after an args change**: a resolution for the previous args that finished after the one for the current args replaced its instance.
|
|
26
|
+
- **`useOptionalService` after `refetch()`**: an instance it resolved after a failure or `isNotFound` was not subscribed to, so invalidating it did not resolve again.
|
|
27
|
+
- **`useSuspenseService` retries after a failure**: a rejected resolution stayed cached for the container, token and args, so resetting the error boundary or mounting the component again threw the same error without another attempt. The error still reaches the error boundary; the next render resolves again. A failure is resolved once per attempt also when the component renders slowly: the entry is dropped two timer turns after the last render that threw.
|
|
28
|
+
|
|
29
|
+
### Removed
|
|
30
|
+
|
|
31
|
+
- **Breaking:** the types `UnionToArray` and `Join`. No hook used them, and `UnionToArray` evaluated to `never` for every input.
|
|
32
|
+
|
|
33
|
+
## [2.0.0-alpha.0] - 2026-11-26
|
|
9
34
|
|
|
10
35
|
### Fixed
|
|
11
36
|
|
|
@@ -107,7 +132,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
107
132
|
|
|
108
133
|
- Fixed package.json exports to use correct CommonJS file extensions (`.cjs` and `.d.cts`)
|
|
109
134
|
|
|
110
|
-
## [0.2.0] - 2025-
|
|
135
|
+
## [0.2.0] - 2025-12-18
|
|
111
136
|
|
|
112
137
|
### Added
|
|
113
138
|
|
package/lib/index.cjs
CHANGED
|
@@ -206,29 +206,32 @@ function getArgsKey(args) {
|
|
|
206
206
|
return `#${id}`;
|
|
207
207
|
}
|
|
208
208
|
}
|
|
209
|
-
// src/service/use-service.mts
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
default: return state;
|
|
209
|
+
// src/service/use-resolved-service.mts
|
|
210
|
+
const LOADING = { status: "loading" };
|
|
211
|
+
const NOT_FOUND = { status: "not-found" };
|
|
212
|
+
/** Whether `error` says that `token` itself has no provider. */
|
|
213
|
+
function isMissingProvider(container, token, error) {
|
|
214
|
+
return error instanceof _navios_di.DIError && error.code === _navios_di.DIErrorCode.ProviderNotFound && !container.isRegistered(token);
|
|
215
|
+
}
|
|
216
|
+
/** What the container holds for `token` and `args` now, or `null`. */
|
|
217
|
+
function peekInstance(container, token, args) {
|
|
218
|
+
try {
|
|
219
|
+
return container.peek(token, args);
|
|
220
|
+
} catch {
|
|
221
|
+
return null;
|
|
223
222
|
}
|
|
224
223
|
}
|
|
225
|
-
|
|
224
|
+
/**
|
|
225
|
+
* Resolution behind `useService` and `useOptionalService`. With `optional`,
|
|
226
|
+
* a token without a provider ends in `not-found` instead of `error`.
|
|
227
|
+
*/
|
|
228
|
+
function useResolvedService(token, args, { optional }) {
|
|
226
229
|
const container = useContainer();
|
|
227
230
|
const argsKey = getArgsKey(args);
|
|
228
231
|
const stableArgs = (0, react.useMemo)(() => args, [argsKey]);
|
|
229
232
|
const initialSyncRef = (0, react.useRef)(void 0);
|
|
230
233
|
if (initialSyncRef.current === void 0) {
|
|
231
|
-
const instance = container
|
|
234
|
+
const instance = peekInstance(container, token, args);
|
|
232
235
|
initialSyncRef.current = instance === null ? null : {
|
|
233
236
|
container,
|
|
234
237
|
token,
|
|
@@ -236,50 +239,62 @@ function useService(token, args) {
|
|
|
236
239
|
instance
|
|
237
240
|
};
|
|
238
241
|
}
|
|
239
|
-
const
|
|
242
|
+
const [state, setState] = (0, react.useState)(() => initialSyncRef.current ? {
|
|
240
243
|
status: "success",
|
|
241
244
|
data: initialSyncRef.current.instance
|
|
242
|
-
} :
|
|
243
|
-
const [
|
|
244
|
-
const [refetchCounter, setRefetchCounter] = (0, react.useState)(0);
|
|
245
|
+
} : LOADING);
|
|
246
|
+
const [refetchCount, setRefetchCount] = (0, react.useState)(0);
|
|
245
247
|
(0, react.useEffect)(() => {
|
|
248
|
+
let isCurrent = true;
|
|
246
249
|
let unsubscribe;
|
|
247
|
-
let
|
|
250
|
+
let latest = 0;
|
|
248
251
|
const subscribe = () => {
|
|
249
252
|
if (unsubscribe) return;
|
|
250
253
|
unsubscribe = container.onInvalidate(token, stableArgs, () => {
|
|
251
|
-
if (!
|
|
252
|
-
|
|
254
|
+
if (!isCurrent) return;
|
|
255
|
+
latest++;
|
|
256
|
+
setState(LOADING);
|
|
253
257
|
queueMicrotask(() => {
|
|
254
|
-
if (
|
|
258
|
+
if (isCurrent) resolve();
|
|
255
259
|
});
|
|
256
260
|
});
|
|
257
261
|
};
|
|
258
|
-
const
|
|
262
|
+
const resolve = async () => {
|
|
263
|
+
const run = ++latest;
|
|
264
|
+
try {
|
|
265
|
+
subscribe();
|
|
266
|
+
} catch {}
|
|
259
267
|
try {
|
|
260
268
|
const instance = await container.get(token, stableArgs);
|
|
261
|
-
if (!
|
|
262
|
-
|
|
263
|
-
|
|
269
|
+
if (!isCurrent || run !== latest) return;
|
|
270
|
+
setState({
|
|
271
|
+
status: "success",
|
|
264
272
|
data: instance
|
|
265
273
|
});
|
|
266
274
|
subscribe();
|
|
267
275
|
} catch (error) {
|
|
268
|
-
if (
|
|
269
|
-
|
|
276
|
+
if (!isCurrent || run !== latest) return;
|
|
277
|
+
setState(optional && isMissingProvider(container, token, error) ? NOT_FOUND : {
|
|
278
|
+
status: "error",
|
|
270
279
|
error
|
|
271
280
|
});
|
|
272
281
|
}
|
|
273
282
|
};
|
|
274
283
|
const initial = initialSyncRef.current;
|
|
275
|
-
if (initial
|
|
276
|
-
|
|
284
|
+
if (initial && initial.container === container && initial.token === token && initial.argsKey === argsKey && refetchCount === 0) {
|
|
285
|
+
subscribe();
|
|
286
|
+
if (peekInstance(container, token, stableArgs) !== initial.instance) {
|
|
287
|
+
initialSyncRef.current = null;
|
|
288
|
+
setState(LOADING);
|
|
289
|
+
resolve();
|
|
290
|
+
}
|
|
291
|
+
} else {
|
|
277
292
|
initialSyncRef.current = null;
|
|
278
|
-
|
|
279
|
-
|
|
293
|
+
setState(LOADING);
|
|
294
|
+
resolve();
|
|
280
295
|
}
|
|
281
296
|
return () => {
|
|
282
|
-
|
|
297
|
+
isCurrent = false;
|
|
283
298
|
unsubscribe?.();
|
|
284
299
|
};
|
|
285
300
|
}, [
|
|
@@ -287,11 +302,18 @@ function useService(token, args) {
|
|
|
287
302
|
token,
|
|
288
303
|
stableArgs,
|
|
289
304
|
argsKey,
|
|
290
|
-
|
|
305
|
+
refetchCount,
|
|
306
|
+
optional
|
|
291
307
|
]);
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
308
|
+
return {
|
|
309
|
+
state,
|
|
310
|
+
refetch: (0, react.useCallback)(() => {
|
|
311
|
+
setRefetchCount((count) => count + 1);
|
|
312
|
+
}, [])
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
/** The result object of `useService` for a state. */
|
|
316
|
+
function serviceResult(state, refetch) {
|
|
295
317
|
return {
|
|
296
318
|
data: state.status === "success" ? state.data : void 0,
|
|
297
319
|
error: state.status === "error" ? state.error : void 0,
|
|
@@ -301,6 +323,27 @@ function useService(token, args) {
|
|
|
301
323
|
refetch
|
|
302
324
|
};
|
|
303
325
|
}
|
|
326
|
+
// src/service/use-service.mts
|
|
327
|
+
/**
|
|
328
|
+
* Hook to load a service from the DI container, with its loading and error
|
|
329
|
+
* state. Resolves again when the instance is invalidated.
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```tsx
|
|
333
|
+
* function UserCard() {
|
|
334
|
+
* const { data: users, error } = useService(UserService)
|
|
335
|
+
*
|
|
336
|
+
* if (error) return <p>{error.message}</p>
|
|
337
|
+
* if (!users) return <p>Loading…</p>
|
|
338
|
+
*
|
|
339
|
+
* return <UserList users={users} />
|
|
340
|
+
* }
|
|
341
|
+
* ```
|
|
342
|
+
*/
|
|
343
|
+
const useService = function useService(token, args) {
|
|
344
|
+
const { state, refetch } = useResolvedService(token, args, { optional: false });
|
|
345
|
+
return serviceResult(state, refetch);
|
|
346
|
+
};
|
|
304
347
|
// src/service/use-suspense-service.mts
|
|
305
348
|
const cacheMap = /* @__PURE__ */ new WeakMap();
|
|
306
349
|
function getCache(container, token) {
|
|
@@ -317,6 +360,33 @@ function getCache(container, token) {
|
|
|
317
360
|
return cache;
|
|
318
361
|
}
|
|
319
362
|
/**
|
|
363
|
+
* The cache entry for `key`, created on first use. An instance the container
|
|
364
|
+
* already holds makes it resolved, so the first render does not suspend. The
|
|
365
|
+
* entry listens for the invalidation of that instance from the start: an
|
|
366
|
+
* effect would subscribe after the render, and miss one in between.
|
|
367
|
+
*/
|
|
368
|
+
function getEntry(cache, key, container, token, args) {
|
|
369
|
+
let entry = cache.get(key);
|
|
370
|
+
if (!entry) {
|
|
371
|
+
const syncInstance = container.peek(token, args);
|
|
372
|
+
entry = {
|
|
373
|
+
promise: null,
|
|
374
|
+
result: syncInstance !== null ? syncInstance : void 0,
|
|
375
|
+
error: void 0,
|
|
376
|
+
status: syncInstance !== null ? "resolved" : "pending",
|
|
377
|
+
version: 0,
|
|
378
|
+
subscribers: /* @__PURE__ */ new Set(),
|
|
379
|
+
unsubscribe: void 0,
|
|
380
|
+
eviction: void 0,
|
|
381
|
+
release: void 0,
|
|
382
|
+
renders: 0
|
|
383
|
+
};
|
|
384
|
+
cache.set(key, entry);
|
|
385
|
+
if (syncInstance !== null) setupInvalidationSubscription(entry, container, token, args);
|
|
386
|
+
}
|
|
387
|
+
return entry;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
320
390
|
* Sets up invalidation subscription for a cache entry if not already subscribed.
|
|
321
391
|
* When the service is destroyed, clears the cache and notifies subscribers.
|
|
322
392
|
*/
|
|
@@ -330,94 +400,99 @@ function setupInvalidationSubscription(entry, container, token, args) {
|
|
|
330
400
|
entry.subscribers.forEach((callback) => callback());
|
|
331
401
|
});
|
|
332
402
|
}
|
|
333
|
-
function
|
|
403
|
+
function fetchService(entry, container, token, args) {
|
|
404
|
+
entry.status = "pending";
|
|
405
|
+
entry.version++;
|
|
406
|
+
entry.promise = container.get(token, args).then((instance) => {
|
|
407
|
+
entry.result = instance;
|
|
408
|
+
entry.status = "resolved";
|
|
409
|
+
setupInvalidationSubscription(entry, container, token, args);
|
|
410
|
+
entry.subscribers.forEach((callback) => callback());
|
|
411
|
+
return instance;
|
|
412
|
+
}).catch((error) => {
|
|
413
|
+
entry.error = error;
|
|
414
|
+
entry.status = "rejected";
|
|
415
|
+
throw error;
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Drops a rejected entry two timer turns after the last render that threw its
|
|
420
|
+
* error. Every render until then throws the error again: React renders a
|
|
421
|
+
* failed component a second time before it shows the error boundary, and when
|
|
422
|
+
* the first of those renders used up its time slice, the second one runs in a
|
|
423
|
+
* later task. That task is queued before the second timer is, so it still
|
|
424
|
+
* finds the entry; with one turn it found the cache empty and resolved the
|
|
425
|
+
* service again. The next render after that, such as the one of an error
|
|
426
|
+
* boundary reset, resolves the service again.
|
|
427
|
+
*/
|
|
428
|
+
function evictAfterRender(cache, key, entry) {
|
|
429
|
+
clearTimeout(entry.eviction);
|
|
430
|
+
entry.eviction = setTimeout(() => {
|
|
431
|
+
entry.eviction = setTimeout(() => {
|
|
432
|
+
if (cache.get(key) === entry && entry.status === "rejected") {
|
|
433
|
+
cache.delete(key);
|
|
434
|
+
entry.unsubscribe?.();
|
|
435
|
+
}
|
|
436
|
+
}, 0);
|
|
437
|
+
}, 0);
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Drops a resolved entry, and its invalidation listener, one timer turn after
|
|
441
|
+
* its last subscriber left, unless a component subscribed or rendered with it
|
|
442
|
+
* in the meantime. A component that shows the service later reads the instance
|
|
443
|
+
* from the container again.
|
|
444
|
+
*/
|
|
445
|
+
function releaseWhenUnused(cache, key, entry) {
|
|
446
|
+
clearTimeout(entry.release);
|
|
447
|
+
const renders = entry.renders;
|
|
448
|
+
entry.release = setTimeout(() => {
|
|
449
|
+
if (cache.get(key) === entry && entry.status === "resolved" && entry.subscribers.size === 0 && entry.renders === renders) {
|
|
450
|
+
cache.delete(key);
|
|
451
|
+
entry.unsubscribe?.();
|
|
452
|
+
entry.unsubscribe = void 0;
|
|
453
|
+
}
|
|
454
|
+
}, 0);
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Hook that returns a service from the DI container and suspends while it is
|
|
458
|
+
* resolved. A failed resolution is thrown to the nearest error boundary.
|
|
459
|
+
* Resolves again, suspending, when the instance is invalidated.
|
|
460
|
+
*/
|
|
461
|
+
const useSuspenseService = function useSuspenseService(token, args) {
|
|
334
462
|
const container = useContainer();
|
|
335
463
|
const cache = getCache(container, token);
|
|
336
464
|
const cacheKey = getArgsKey(args);
|
|
337
|
-
const
|
|
338
|
-
|
|
339
|
-
const syncInstance = container.peek(token, args);
|
|
340
|
-
const entry = {
|
|
341
|
-
promise: null,
|
|
342
|
-
result: syncInstance !== null ? syncInstance : void 0,
|
|
343
|
-
error: void 0,
|
|
344
|
-
status: syncInstance !== null ? "resolved" : "pending",
|
|
345
|
-
version: 0,
|
|
346
|
-
subscribers: /* @__PURE__ */ new Set(),
|
|
347
|
-
unsubscribe: void 0
|
|
348
|
-
};
|
|
349
|
-
cache.set(cacheKey, entry);
|
|
350
|
-
}
|
|
351
|
-
const entry = cache.get(cacheKey);
|
|
352
|
-
entryRef.current = entry;
|
|
353
|
-
const fetchService = (0, react.useCallback)(() => {
|
|
354
|
-
const currentEntry = entryRef.current;
|
|
355
|
-
if (!currentEntry) return;
|
|
356
|
-
currentEntry.status = "pending";
|
|
357
|
-
currentEntry.version++;
|
|
358
|
-
currentEntry.promise = container.get(token, args).then((instance) => {
|
|
359
|
-
currentEntry.result = instance;
|
|
360
|
-
currentEntry.status = "resolved";
|
|
361
|
-
setupInvalidationSubscription(currentEntry, container, token, args);
|
|
362
|
-
currentEntry.subscribers.forEach((callback) => callback());
|
|
363
|
-
return instance;
|
|
364
|
-
}).catch((error) => {
|
|
365
|
-
currentEntry.error = error;
|
|
366
|
-
currentEntry.status = "rejected";
|
|
367
|
-
throw error;
|
|
368
|
-
});
|
|
369
|
-
return currentEntry.promise;
|
|
370
|
-
}, [
|
|
371
|
-
container,
|
|
372
|
-
token,
|
|
373
|
-
args
|
|
374
|
-
]);
|
|
465
|
+
const entry = getEntry(cache, cacheKey, container, token, args);
|
|
466
|
+
entry.renders++;
|
|
375
467
|
const subscribe = (0, react.useCallback)((callback) => {
|
|
376
468
|
entry.subscribers.add(callback);
|
|
377
469
|
return () => {
|
|
378
470
|
entry.subscribers.delete(callback);
|
|
471
|
+
if (entry.subscribers.size === 0) releaseWhenUnused(cache, cacheKey, entry);
|
|
379
472
|
};
|
|
380
|
-
}, [
|
|
473
|
+
}, [
|
|
474
|
+
cache,
|
|
475
|
+
cacheKey,
|
|
476
|
+
entry
|
|
477
|
+
]);
|
|
381
478
|
const getSnapshot = (0, react.useCallback)(() => {
|
|
382
479
|
return `${entry.status}:${entry.version}`;
|
|
383
480
|
}, [entry]);
|
|
384
481
|
(0, react.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
|
|
385
|
-
(
|
|
386
|
-
const currentEntry = entryRef.current;
|
|
387
|
-
if (currentEntry && currentEntry.status === "resolved" && !currentEntry.unsubscribe) setupInvalidationSubscription(currentEntry, container, token, args);
|
|
388
|
-
}, [
|
|
389
|
-
container,
|
|
390
|
-
token,
|
|
391
|
-
args,
|
|
392
|
-
entry
|
|
393
|
-
]);
|
|
394
|
-
if (entry.status === "pending" && !entry.promise) fetchService();
|
|
482
|
+
if (entry.status === "pending" && !entry.promise) fetchService(entry, container, token, args);
|
|
395
483
|
if (entry.status === "pending") throw entry.promise;
|
|
396
|
-
if (entry.status === "rejected")
|
|
484
|
+
if (entry.status === "rejected") {
|
|
485
|
+
evictAfterRender(cache, cacheKey, entry);
|
|
486
|
+
throw entry.error;
|
|
487
|
+
}
|
|
397
488
|
return entry.result;
|
|
398
|
-
}
|
|
489
|
+
};
|
|
399
490
|
// src/service/use-optional-service.mts
|
|
400
|
-
function optionalServiceReducer(state, action) {
|
|
401
|
-
switch (action.type) {
|
|
402
|
-
case "loading": return { status: "loading" };
|
|
403
|
-
case "success": return {
|
|
404
|
-
status: "success",
|
|
405
|
-
data: action.data
|
|
406
|
-
};
|
|
407
|
-
case "not-found": return { status: "not-found" };
|
|
408
|
-
case "error": return {
|
|
409
|
-
status: "error",
|
|
410
|
-
error: action.error
|
|
411
|
-
};
|
|
412
|
-
case "reset": return { status: "idle" };
|
|
413
|
-
default: return state;
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
491
|
/**
|
|
417
492
|
* Hook to optionally load a service from the DI container.
|
|
418
493
|
*
|
|
419
|
-
* Unlike useService,
|
|
420
|
-
*
|
|
494
|
+
* Unlike useService, a token without a provider is not an error: the hook
|
|
495
|
+
* returns `isNotFound: true` instead.
|
|
421
496
|
*
|
|
422
497
|
* This is useful for:
|
|
423
498
|
* - Optional dependencies that may or may not be configured
|
|
@@ -438,91 +513,13 @@ function optionalServiceReducer(state, action) {
|
|
|
438
513
|
* }
|
|
439
514
|
* ```
|
|
440
515
|
*/
|
|
441
|
-
function useOptionalService(token, args) {
|
|
442
|
-
const
|
|
443
|
-
const argsKey = getArgsKey(args);
|
|
444
|
-
const stableArgs = (0, react.useMemo)(() => args, [argsKey]);
|
|
445
|
-
const initialSyncRef = (0, react.useRef)(void 0);
|
|
446
|
-
if (initialSyncRef.current === void 0) {
|
|
447
|
-
let instance = null;
|
|
448
|
-
try {
|
|
449
|
-
instance = container.peek(token, args);
|
|
450
|
-
} catch {}
|
|
451
|
-
initialSyncRef.current = instance === null ? null : {
|
|
452
|
-
container,
|
|
453
|
-
token,
|
|
454
|
-
argsKey,
|
|
455
|
-
instance
|
|
456
|
-
};
|
|
457
|
-
}
|
|
458
|
-
const initialState = initialSyncRef.current !== null ? {
|
|
459
|
-
status: "success",
|
|
460
|
-
data: initialSyncRef.current.instance
|
|
461
|
-
} : { status: "idle" };
|
|
462
|
-
const [state, dispatch] = (0, react.useReducer)(optionalServiceReducer, initialState);
|
|
463
|
-
const fetchService = (0, react.useCallback)(async () => {
|
|
464
|
-
dispatch({ type: "loading" });
|
|
465
|
-
try {
|
|
466
|
-
const instance = await container.get(token, stableArgs);
|
|
467
|
-
dispatch({
|
|
468
|
-
type: "success",
|
|
469
|
-
data: instance
|
|
470
|
-
});
|
|
471
|
-
return true;
|
|
472
|
-
} catch (error) {
|
|
473
|
-
if (error instanceof _navios_di.DIError && error.code === _navios_di.DIErrorCode.ProviderNotFound && !container.isRegistered(token)) dispatch({ type: "not-found" });
|
|
474
|
-
else dispatch({
|
|
475
|
-
type: "error",
|
|
476
|
-
error
|
|
477
|
-
});
|
|
478
|
-
return false;
|
|
479
|
-
}
|
|
480
|
-
}, [
|
|
481
|
-
container,
|
|
482
|
-
token,
|
|
483
|
-
stableArgs
|
|
484
|
-
]);
|
|
485
|
-
(0, react.useEffect)(() => {
|
|
486
|
-
let unsubscribe;
|
|
487
|
-
let isMounted = true;
|
|
488
|
-
const subscribe = () => {
|
|
489
|
-
if (unsubscribe || !isMounted) return;
|
|
490
|
-
unsubscribe = container.onInvalidate(token, stableArgs, () => {
|
|
491
|
-
dispatch({ type: "loading" });
|
|
492
|
-
queueMicrotask(() => {
|
|
493
|
-
if (isMounted) fetchService();
|
|
494
|
-
});
|
|
495
|
-
});
|
|
496
|
-
};
|
|
497
|
-
const initial = initialSyncRef.current;
|
|
498
|
-
if (initial != null && initial.container === container && initial.token === token && initial.argsKey === argsKey) subscribe();
|
|
499
|
-
else {
|
|
500
|
-
initialSyncRef.current = null;
|
|
501
|
-
fetchService().then((loaded) => {
|
|
502
|
-
if (loaded) subscribe();
|
|
503
|
-
});
|
|
504
|
-
}
|
|
505
|
-
return () => {
|
|
506
|
-
isMounted = false;
|
|
507
|
-
unsubscribe?.();
|
|
508
|
-
};
|
|
509
|
-
}, [
|
|
510
|
-
fetchService,
|
|
511
|
-
container,
|
|
512
|
-
token,
|
|
513
|
-
stableArgs,
|
|
514
|
-
argsKey
|
|
515
|
-
]);
|
|
516
|
+
const useOptionalService = function useOptionalService(token, args) {
|
|
517
|
+
const { state, refetch } = useResolvedService(token, args, { optional: true });
|
|
516
518
|
return {
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
isLoading: state.status === "loading",
|
|
520
|
-
isSuccess: state.status === "success",
|
|
521
|
-
isNotFound: state.status === "not-found",
|
|
522
|
-
isError: state.status === "error",
|
|
523
|
-
refetch: fetchService
|
|
519
|
+
...serviceResult(state, refetch),
|
|
520
|
+
isNotFound: state.status === "not-found"
|
|
524
521
|
};
|
|
525
|
-
}
|
|
522
|
+
};
|
|
526
523
|
// src/service/use-invalidate-instance.mts
|
|
527
524
|
/**
|
|
528
525
|
* Hook that returns a function to invalidate a service instance directly.
|