@navios/di-react 0.7.0 → 0.9.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 +51 -0
- package/README.md +14 -67
- package/lib/index.d.mts +8 -62
- package/lib/index.d.mts.map +1 -1
- package/lib/index.d.ts +3 -57
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +41 -51
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +42 -51
- package/lib/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/hooks/__tests__/use-container.spec.mts +3 -2
- package/src/hooks/__tests__/use-invalidate.spec.mts +10 -146
- package/src/hooks/__tests__/use-service.spec.mts +2 -2
- package/src/hooks/index.mts +1 -1
- package/src/hooks/use-container.mts +6 -3
- package/src/hooks/use-invalidate.mts +1 -82
- package/src/hooks/use-optional-service.mts +17 -25
- package/src/hooks/use-service.mts +25 -26
- package/src/hooks/use-suspense-service.mts +10 -17
- package/src/providers/__tests__/scope-provider.spec.mts +5 -2
- package/src/providers/scope-provider.mts +1 -8
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,57 @@ 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
|
+
## [0.9.0] - 2025-12-23
|
|
9
|
+
|
|
10
|
+
### Breaking Changes
|
|
11
|
+
|
|
12
|
+
- **BREAKING**: Removed `useInvalidate` hook
|
|
13
|
+
- This hook relied on the `ServiceLocator` API which was removed in `@navios/di` 0.9.0
|
|
14
|
+
- **Migration**: Use `useInvalidateInstance` instead, which invalidates by instance reference
|
|
15
|
+
- Alternative: Use `container.invalidate(instance)` directly via `useContainer()`
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
// Before (0.8.x)
|
|
19
|
+
const invalidateUser = useInvalidate(UserService)
|
|
20
|
+
invalidateUser()
|
|
21
|
+
|
|
22
|
+
// After (0.9.0) - Option 1: useInvalidateInstance
|
|
23
|
+
const { data: user } = useService(UserService)
|
|
24
|
+
const invalidateInstance = useInvalidateInstance()
|
|
25
|
+
invalidateInstance(user)
|
|
26
|
+
|
|
27
|
+
// After (0.9.0) - Option 2: Direct container access
|
|
28
|
+
const container = useContainer()
|
|
29
|
+
const { data: user } = useService(UserService)
|
|
30
|
+
await container.invalidate(user)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Changed
|
|
34
|
+
|
|
35
|
+
- **Simplified Instance Name Generation**: Refactored all hooks to use the new `container.calculateInstanceName()` method
|
|
36
|
+
- Removed direct access to internal DI components (`getTokenResolver()`, `getNameResolver()`)
|
|
37
|
+
- Cleaner, more maintainable code with less coupling to DI internals
|
|
38
|
+
- **Improved Event Subscription Setup**: Fixed race conditions in invalidation subscription
|
|
39
|
+
- `useOptionalService`: Removed `setTimeout` hack, now properly awaits service fetch before subscribing
|
|
40
|
+
- `useService`: Simplified subscription logic with proper null checks
|
|
41
|
+
- `useSuspenseService`: Streamlined instance name calculation
|
|
42
|
+
|
|
43
|
+
### Fixed
|
|
44
|
+
|
|
45
|
+
- **Instance Name Generation**: Fixed edge cases where instance names could be incorrectly calculated
|
|
46
|
+
- Now properly handles cases where `calculateInstanceName` returns null
|
|
47
|
+
- Prevents potential subscription errors with undefined instance names
|
|
48
|
+
|
|
49
|
+
### Dependencies
|
|
50
|
+
|
|
51
|
+
- Updated to support `@navios/di` ^0.9.0 with new unified storage architecture
|
|
52
|
+
|
|
53
|
+
## [0.8.0] - 2025-12-21
|
|
54
|
+
|
|
55
|
+
### Dependencies
|
|
56
|
+
|
|
57
|
+
- Updated to support `@navios/di` ^0.8.0
|
|
58
|
+
|
|
8
59
|
## [0.7.0] - 2025-12-21
|
|
9
60
|
|
|
10
61
|
### Updated
|
package/README.md
CHANGED
|
@@ -282,50 +282,6 @@ function Analytics() {
|
|
|
282
282
|
}
|
|
283
283
|
```
|
|
284
284
|
|
|
285
|
-
### useInvalidate
|
|
286
|
-
|
|
287
|
-
Get a function to invalidate a service by its token. When called, this will destroy the current service instance and trigger re-fetch in all components using `useService`/`useSuspenseService` for that token.
|
|
288
|
-
|
|
289
|
-
```tsx
|
|
290
|
-
import { useService, useInvalidate } from '@navios/di-react'
|
|
291
|
-
|
|
292
|
-
function UserProfile() {
|
|
293
|
-
const { data: user } = useService(UserService)
|
|
294
|
-
const invalidateUser = useInvalidate(UserService)
|
|
295
|
-
|
|
296
|
-
const handleRefresh = () => {
|
|
297
|
-
invalidateUser() // All components using UserService will re-fetch
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
return (
|
|
301
|
-
<div>
|
|
302
|
-
<span>{user?.name}</span>
|
|
303
|
-
<button onClick={handleRefresh}>Refresh</button>
|
|
304
|
-
</div>
|
|
305
|
-
)
|
|
306
|
-
}
|
|
307
|
-
```
|
|
308
|
-
|
|
309
|
-
#### With Arguments
|
|
310
|
-
|
|
311
|
-
```tsx
|
|
312
|
-
import { useMemo } from 'react'
|
|
313
|
-
import { useService, useInvalidate } from '@navios/di-react'
|
|
314
|
-
|
|
315
|
-
function UserProfile({ userId }: { userId: string }) {
|
|
316
|
-
const args = useMemo(() => ({ userId }), [userId])
|
|
317
|
-
const { data: user } = useService(UserToken, args)
|
|
318
|
-
const invalidateUser = useInvalidate(UserToken, args)
|
|
319
|
-
|
|
320
|
-
return (
|
|
321
|
-
<div>
|
|
322
|
-
<span>{user?.name}</span>
|
|
323
|
-
<button onClick={() => invalidateUser()}>Refresh</button>
|
|
324
|
-
</div>
|
|
325
|
-
)
|
|
326
|
-
}
|
|
327
|
-
```
|
|
328
|
-
|
|
329
285
|
### useInvalidateInstance
|
|
330
286
|
|
|
331
287
|
Invalidate a service instance directly without knowing its token.
|
|
@@ -441,7 +397,7 @@ function ChildComponent() {
|
|
|
441
397
|
|
|
442
398
|
## Service Invalidation
|
|
443
399
|
|
|
444
|
-
Both `useService` and `useSuspenseService` automatically subscribe to service invalidation events via the DI container's event bus. When a service is invalidated (e.g., via `container.invalidate(service)` or `
|
|
400
|
+
Both `useService` and `useSuspenseService` automatically subscribe to service invalidation events via the DI container's event bus. When a service is invalidated (e.g., via `container.invalidate(service)` or `useInvalidateInstance`), the hooks will automatically:
|
|
445
401
|
|
|
446
402
|
1. Clear the cached instance
|
|
447
403
|
2. Re-fetch the service
|
|
@@ -456,11 +412,13 @@ This enables reactive updates when services change, making it easy to implement
|
|
|
456
412
|
```tsx
|
|
457
413
|
function UserList() {
|
|
458
414
|
const { data: users } = useService(UserService)
|
|
459
|
-
const
|
|
415
|
+
const invalidateInstance = useInvalidateInstance()
|
|
460
416
|
|
|
461
417
|
const handleCreateUser = async () => {
|
|
462
418
|
await createUser(newUser)
|
|
463
|
-
|
|
419
|
+
if (users) {
|
|
420
|
+
invalidateInstance(users) // Automatically refreshes all components using UserService
|
|
421
|
+
}
|
|
464
422
|
}
|
|
465
423
|
|
|
466
424
|
return (
|
|
@@ -640,18 +598,6 @@ interface UseOptionalServiceResult<T> {
|
|
|
640
598
|
|
|
641
599
|
Loads a service that may not be registered. Returns `isNotFound: true` when the service doesn't exist instead of throwing an error.
|
|
642
600
|
|
|
643
|
-
### useInvalidate
|
|
644
|
-
|
|
645
|
-
```ts
|
|
646
|
-
function useInvalidate<T>(token: ClassType): () => Promise<void>
|
|
647
|
-
function useInvalidate<T, S>(
|
|
648
|
-
token: InjectionToken<T, S>,
|
|
649
|
-
args: S extends undefined ? never : unknown,
|
|
650
|
-
): () => Promise<void>
|
|
651
|
-
```
|
|
652
|
-
|
|
653
|
-
Returns a function to invalidate a service by its token. When called, destroys the current service instance and triggers re-fetch in all components using `useService`/`useSuspenseService` for that token.
|
|
654
|
-
|
|
655
601
|
### useInvalidateInstance
|
|
656
602
|
|
|
657
603
|
```ts
|
|
@@ -739,17 +685,18 @@ Make sure you've wrapped your component with both `Suspense` and an error bounda
|
|
|
739
685
|
|
|
740
686
|
### Services not invalidating
|
|
741
687
|
|
|
742
|
-
|
|
688
|
+
When using `useInvalidateInstance`, make sure you have a reference to the service instance:
|
|
743
689
|
|
|
744
690
|
```tsx
|
|
745
|
-
// ✅ Good -
|
|
746
|
-
const
|
|
747
|
-
const
|
|
748
|
-
const invalidate = useInvalidate(UserToken, args)
|
|
691
|
+
// ✅ Good - invalidate the actual instance
|
|
692
|
+
const { data: user } = useService(UserService)
|
|
693
|
+
const invalidateInstance = useInvalidateInstance()
|
|
749
694
|
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
695
|
+
const handleRefresh = () => {
|
|
696
|
+
if (user) {
|
|
697
|
+
invalidateInstance(user)
|
|
698
|
+
}
|
|
699
|
+
}
|
|
753
700
|
```
|
|
754
701
|
|
|
755
702
|
## License
|
package/lib/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as react1 from "react";
|
|
2
2
|
import { ReactNode } from "react";
|
|
3
|
-
import { BoundInjectionToken, ClassType, Container, Factorable, FactoryInjectionToken,
|
|
3
|
+
import { BoundInjectionToken, ClassType, Container, Factorable, FactoryInjectionToken, InjectionToken, InjectionTokenSchemaType, ScopedContainer } from "@navios/di";
|
|
4
4
|
import { ZodType, z } from "zod/v4";
|
|
5
5
|
|
|
6
6
|
//#region src/providers/context.d.mts
|
|
@@ -8,12 +8,12 @@ import { ZodType, z } from "zod/v4";
|
|
|
8
8
|
* Context for the root Container.
|
|
9
9
|
* This is set by ContainerProvider and provides the base container.
|
|
10
10
|
*/
|
|
11
|
-
declare const ContainerContext:
|
|
11
|
+
declare const ContainerContext: react1.Context<Container | null>;
|
|
12
12
|
/**
|
|
13
13
|
* Context for the current ScopedContainer (if inside a ScopeProvider).
|
|
14
14
|
* This allows nested components to access request-scoped services.
|
|
15
15
|
*/
|
|
16
|
-
declare const ScopedContainerContext:
|
|
16
|
+
declare const ScopedContainerContext: react1.Context<ScopedContainer | null>;
|
|
17
17
|
//#endregion
|
|
18
18
|
//#region src/providers/container-provider.d.mts
|
|
19
19
|
interface ContainerProviderProps {
|
|
@@ -23,7 +23,7 @@ interface ContainerProviderProps {
|
|
|
23
23
|
declare function ContainerProvider({
|
|
24
24
|
container,
|
|
25
25
|
children
|
|
26
|
-
}: ContainerProviderProps):
|
|
26
|
+
}: ContainerProviderProps): react1.ReactElement<unknown, string | react1.JSXElementConstructor<any>>;
|
|
27
27
|
//#endregion
|
|
28
28
|
//#region src/providers/scope-provider.d.mts
|
|
29
29
|
interface ScopeProviderProps {
|
|
@@ -37,11 +37,6 @@ interface ScopeProviderProps {
|
|
|
37
37
|
* Can be used to pass data like user info, request headers, etc.
|
|
38
38
|
*/
|
|
39
39
|
metadata?: Record<string, unknown>;
|
|
40
|
-
/**
|
|
41
|
-
* Priority for service resolution. Higher priority scopes take precedence.
|
|
42
|
-
* @default 100
|
|
43
|
-
*/
|
|
44
|
-
priority?: number;
|
|
45
40
|
children: ReactNode;
|
|
46
41
|
}
|
|
47
42
|
/**
|
|
@@ -69,9 +64,8 @@ interface ScopeProviderProps {
|
|
|
69
64
|
declare function ScopeProvider({
|
|
70
65
|
scopeId,
|
|
71
66
|
metadata,
|
|
72
|
-
priority,
|
|
73
67
|
children
|
|
74
|
-
}: ScopeProviderProps):
|
|
68
|
+
}: ScopeProviderProps): react1.ReactElement<unknown, string | react1.JSXElementConstructor<any>> | null;
|
|
75
69
|
//#endregion
|
|
76
70
|
//#region src/hooks/use-container.d.mts
|
|
77
71
|
/**
|
|
@@ -84,7 +78,7 @@ declare function ScopeProvider({
|
|
|
84
78
|
*
|
|
85
79
|
* @returns The current container (ScopedContainer or Container)
|
|
86
80
|
*/
|
|
87
|
-
declare function useContainer():
|
|
81
|
+
declare function useContainer(): Container | ScopedContainer;
|
|
88
82
|
/**
|
|
89
83
|
* Hook to get the root Container, regardless of whether we're inside a ScopeProvider.
|
|
90
84
|
*
|
|
@@ -163,54 +157,6 @@ declare function useOptionalService<T>(token: BoundInjectionToken<T, any>): UseO
|
|
|
163
157
|
declare function useOptionalService<T>(token: FactoryInjectionToken<T, any>): UseOptionalServiceResult<T>;
|
|
164
158
|
//#endregion
|
|
165
159
|
//#region src/hooks/use-invalidate.d.mts
|
|
166
|
-
type InvalidatableToken = ClassType | InjectionToken<any, any> | BoundInjectionToken<any, any> | FactoryInjectionToken<any, any>;
|
|
167
|
-
/**
|
|
168
|
-
* Hook that returns a function to invalidate a service by its token.
|
|
169
|
-
*
|
|
170
|
-
* When called, this will:
|
|
171
|
-
* 1. Destroy the current service instance
|
|
172
|
-
* 2. Trigger re-fetch in all components using useService/useSuspenseService for that token
|
|
173
|
-
*
|
|
174
|
-
* @example
|
|
175
|
-
* ```tsx
|
|
176
|
-
* function UserProfile() {
|
|
177
|
-
* const { data: user } = useService(UserService)
|
|
178
|
-
* const invalidateUser = useInvalidate(UserService)
|
|
179
|
-
*
|
|
180
|
-
* const handleRefresh = () => {
|
|
181
|
-
* invalidateUser() // All components using UserService will re-fetch
|
|
182
|
-
* }
|
|
183
|
-
*
|
|
184
|
-
* return (
|
|
185
|
-
* <div>
|
|
186
|
-
* <span>{user?.name}</span>
|
|
187
|
-
* <button onClick={handleRefresh}>Refresh</button>
|
|
188
|
-
* </div>
|
|
189
|
-
* )
|
|
190
|
-
* }
|
|
191
|
-
* ```
|
|
192
|
-
*/
|
|
193
|
-
declare function useInvalidate<T extends InvalidatableToken>(token: T): () => Promise<void>;
|
|
194
|
-
/**
|
|
195
|
-
* Hook that returns a function to invalidate a service by its token with args.
|
|
196
|
-
*
|
|
197
|
-
* @example
|
|
198
|
-
* ```tsx
|
|
199
|
-
* function UserProfile({ userId }: { userId: string }) {
|
|
200
|
-
* const args = useMemo(() => ({ userId }), [userId])
|
|
201
|
-
* const { data: user } = useService(UserToken, args)
|
|
202
|
-
* const invalidateUser = useInvalidate(UserToken, args)
|
|
203
|
-
*
|
|
204
|
-
* return (
|
|
205
|
-
* <div>
|
|
206
|
-
* <span>{user?.name}</span>
|
|
207
|
-
* <button onClick={() => invalidateUser()}>Refresh</button>
|
|
208
|
-
* </div>
|
|
209
|
-
* )
|
|
210
|
-
* }
|
|
211
|
-
* ```
|
|
212
|
-
*/
|
|
213
|
-
declare function useInvalidate<T, S extends InjectionTokenSchemaType>(token: InjectionToken<T, S>, args: S extends undefined ? never : unknown): () => Promise<void>;
|
|
214
160
|
/**
|
|
215
161
|
* Hook that returns a function to invalidate a service instance directly.
|
|
216
162
|
*
|
|
@@ -293,5 +239,5 @@ declare function useScopedContainerOrThrow(): ScopedContainer;
|
|
|
293
239
|
*/
|
|
294
240
|
declare function useScopeMetadata<T = unknown>(key: string): T | undefined;
|
|
295
241
|
//#endregion
|
|
296
|
-
export { ContainerContext, ContainerProvider, type ContainerProviderProps, Join, ScopeProvider, type ScopeProviderProps, ScopedContainerContext, UnionToArray, type UseOptionalServiceResult, type UseServiceResult, useContainer,
|
|
242
|
+
export { ContainerContext, ContainerProvider, type ContainerProviderProps, Join, ScopeProvider, type ScopeProviderProps, ScopedContainerContext, UnionToArray, type UseOptionalServiceResult, type UseServiceResult, useContainer, useInvalidateInstance, useOptionalService, useRootContainer, useScope, useScopeMetadata, useScopeOrThrow, useScopedContainer, useScopedContainerOrThrow, useService, useSuspenseService };
|
|
297
243
|
//# sourceMappingURL=index.d.mts.map
|
package/lib/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/providers/context.mts","../src/providers/container-provider.mts","../src/providers/scope-provider.mts","../src/hooks/use-container.mts","../src/types.mts","../src/hooks/use-service.mts","../src/hooks/use-suspense-service.mts","../src/hooks/use-optional-service.mts","../src/hooks/use-invalidate.mts","../src/hooks/use-scope.mts"],"sourcesContent":[],"mappings":";;;;;;;;;;cAQa,kBAAgB,MAAA,CAAA,QAAA;AAA7B;AAMA;;;cAAa,wBAAsB,MAAA,CAAA,QAAA;;;UCPlB,sBAAA;aACJ;YACD;;ADDC,iBCIG,iBAAA,CDJa;EAAA,SAAA;EAAA;AAAA,CAAA,ECO1B,sBDP0B,CAAA,ECOJ,MAAA,CAAA,YDPI,CAAA,OAAA,EAAA,MAAA,GCOJ,MAAA,CAAA,qBDPI,CAAA,GAAA,CAAA,CAAA;;;UEAZ,kBAAA;;;;;EFAJ,OAAA,CAAA,EAAA,MAAA;EAMA;;;;ECPI,QAAA,CAAA,ECWJ,MDXI,CAAA,MAAA,EAAsB,OAAA,CAAA;EAKvB
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/providers/context.mts","../src/providers/container-provider.mts","../src/providers/scope-provider.mts","../src/hooks/use-container.mts","../src/types.mts","../src/hooks/use-service.mts","../src/hooks/use-suspense-service.mts","../src/hooks/use-optional-service.mts","../src/hooks/use-invalidate.mts","../src/hooks/use-scope.mts"],"sourcesContent":[],"mappings":";;;;;;;;;;cAQa,kBAAgB,MAAA,CAAA,QAAA;AAA7B;AAMA;;;cAAa,wBAAsB,MAAA,CAAA,QAAA;;;UCPlB,sBAAA;aACJ;YACD;;ADDC,iBCIG,iBAAA,CDJa;EAAA,SAAA;EAAA;AAAA,CAAA,ECO1B,sBDP0B,CAAA,ECOJ,MAAA,CAAA,YDPI,CAAA,OAAA,EAAA,MAAA,GCOJ,MAAA,CAAA,qBDPI,CAAA,GAAA,CAAA,CAAA;;;UEAZ,kBAAA;;;;;EFAJ,OAAA,CAAA,EAAA,MAAA;EAMA;;;;ECPI,QAAA,CAAA,ECWJ,MDXI,CAAA,MAAA,EAAsB,OAAA,CAAA;EAKvB,QAAA,ECOJ,SDPI;;;;;;;;;;ACJhB;AAoCA;;;;;;;;;;;ACzBA;AAwBA;iBDCgB,aAAA;;;;GAIb,qBAAkB,MAAA,CAAA,+BAAA,MAAA,CAAA;;;;;;;;AFxCrB;AAMA;;;;ACPiB,iBEYD,YAAA,CAAA,CFZuB,EEYP,SFXnB,GEW+B,eFVvB;AAGrB;;;;;;;;iBE+BgB,gBAAA,CAAA,GAAoB;;;KCzCxB,kBAAkB,i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"react";
|
|
2
2
|
import { ReactNode } from "react";
|
|
3
|
-
import { BoundInjectionToken, ClassType, Container, Factorable, FactoryInjectionToken,
|
|
3
|
+
import { BoundInjectionToken, ClassType, Container, Factorable, FactoryInjectionToken, InjectionToken, InjectionTokenSchemaType, ScopedContainer } from "@navios/di";
|
|
4
4
|
import { ZodType, z } from "zod/v4";
|
|
5
5
|
|
|
6
6
|
//#region src/providers/context.d.mts
|
|
@@ -37,11 +37,6 @@ interface ScopeProviderProps {
|
|
|
37
37
|
* Can be used to pass data like user info, request headers, etc.
|
|
38
38
|
*/
|
|
39
39
|
metadata?: Record<string, unknown>;
|
|
40
|
-
/**
|
|
41
|
-
* Priority for service resolution. Higher priority scopes take precedence.
|
|
42
|
-
* @default 100
|
|
43
|
-
*/
|
|
44
|
-
priority?: number;
|
|
45
40
|
children: ReactNode;
|
|
46
41
|
}
|
|
47
42
|
/**
|
|
@@ -69,7 +64,6 @@ interface ScopeProviderProps {
|
|
|
69
64
|
declare function ScopeProvider({
|
|
70
65
|
scopeId,
|
|
71
66
|
metadata,
|
|
72
|
-
priority,
|
|
73
67
|
children
|
|
74
68
|
}: ScopeProviderProps): react3.ReactElement<unknown, string | react3.JSXElementConstructor<any>> | null;
|
|
75
69
|
//#endregion
|
|
@@ -84,7 +78,7 @@ declare function ScopeProvider({
|
|
|
84
78
|
*
|
|
85
79
|
* @returns The current container (ScopedContainer or Container)
|
|
86
80
|
*/
|
|
87
|
-
declare function useContainer():
|
|
81
|
+
declare function useContainer(): Container | ScopedContainer;
|
|
88
82
|
/**
|
|
89
83
|
* Hook to get the root Container, regardless of whether we're inside a ScopeProvider.
|
|
90
84
|
*
|
|
@@ -163,54 +157,6 @@ declare function useOptionalService<T>(token: BoundInjectionToken<T, any>): UseO
|
|
|
163
157
|
declare function useOptionalService<T>(token: FactoryInjectionToken<T, any>): UseOptionalServiceResult<T>;
|
|
164
158
|
//#endregion
|
|
165
159
|
//#region src/hooks/use-invalidate.d.mts
|
|
166
|
-
type InvalidatableToken = ClassType | InjectionToken<any, any> | BoundInjectionToken<any, any> | FactoryInjectionToken<any, any>;
|
|
167
|
-
/**
|
|
168
|
-
* Hook that returns a function to invalidate a service by its token.
|
|
169
|
-
*
|
|
170
|
-
* When called, this will:
|
|
171
|
-
* 1. Destroy the current service instance
|
|
172
|
-
* 2. Trigger re-fetch in all components using useService/useSuspenseService for that token
|
|
173
|
-
*
|
|
174
|
-
* @example
|
|
175
|
-
* ```tsx
|
|
176
|
-
* function UserProfile() {
|
|
177
|
-
* const { data: user } = useService(UserService)
|
|
178
|
-
* const invalidateUser = useInvalidate(UserService)
|
|
179
|
-
*
|
|
180
|
-
* const handleRefresh = () => {
|
|
181
|
-
* invalidateUser() // All components using UserService will re-fetch
|
|
182
|
-
* }
|
|
183
|
-
*
|
|
184
|
-
* return (
|
|
185
|
-
* <div>
|
|
186
|
-
* <span>{user?.name}</span>
|
|
187
|
-
* <button onClick={handleRefresh}>Refresh</button>
|
|
188
|
-
* </div>
|
|
189
|
-
* )
|
|
190
|
-
* }
|
|
191
|
-
* ```
|
|
192
|
-
*/
|
|
193
|
-
declare function useInvalidate<T extends InvalidatableToken>(token: T): () => Promise<void>;
|
|
194
|
-
/**
|
|
195
|
-
* Hook that returns a function to invalidate a service by its token with args.
|
|
196
|
-
*
|
|
197
|
-
* @example
|
|
198
|
-
* ```tsx
|
|
199
|
-
* function UserProfile({ userId }: { userId: string }) {
|
|
200
|
-
* const args = useMemo(() => ({ userId }), [userId])
|
|
201
|
-
* const { data: user } = useService(UserToken, args)
|
|
202
|
-
* const invalidateUser = useInvalidate(UserToken, args)
|
|
203
|
-
*
|
|
204
|
-
* return (
|
|
205
|
-
* <div>
|
|
206
|
-
* <span>{user?.name}</span>
|
|
207
|
-
* <button onClick={() => invalidateUser()}>Refresh</button>
|
|
208
|
-
* </div>
|
|
209
|
-
* )
|
|
210
|
-
* }
|
|
211
|
-
* ```
|
|
212
|
-
*/
|
|
213
|
-
declare function useInvalidate<T, S extends InjectionTokenSchemaType>(token: InjectionToken<T, S>, args: S extends undefined ? never : unknown): () => Promise<void>;
|
|
214
160
|
/**
|
|
215
161
|
* Hook that returns a function to invalidate a service instance directly.
|
|
216
162
|
*
|
|
@@ -293,5 +239,5 @@ declare function useScopedContainerOrThrow(): ScopedContainer;
|
|
|
293
239
|
*/
|
|
294
240
|
declare function useScopeMetadata<T = unknown>(key: string): T | undefined;
|
|
295
241
|
//#endregion
|
|
296
|
-
export { ContainerContext, ContainerProvider, type ContainerProviderProps, Join, ScopeProvider, type ScopeProviderProps, ScopedContainerContext, UnionToArray, type UseOptionalServiceResult, type UseServiceResult, useContainer,
|
|
242
|
+
export { ContainerContext, ContainerProvider, type ContainerProviderProps, Join, ScopeProvider, type ScopeProviderProps, ScopedContainerContext, UnionToArray, type UseOptionalServiceResult, type UseServiceResult, useContainer, useInvalidateInstance, useOptionalService, useRootContainer, useScope, useScopeMetadata, useScopeOrThrow, useScopedContainer, useScopedContainerOrThrow, useService, useSuspenseService };
|
|
297
243
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/providers/context.mts","../src/providers/container-provider.mts","../src/providers/scope-provider.mts","../src/hooks/use-container.mts","../src/types.mts","../src/hooks/use-service.mts","../src/hooks/use-suspense-service.mts","../src/hooks/use-optional-service.mts","../src/hooks/use-invalidate.mts","../src/hooks/use-scope.mts"],"sourcesContent":[],"mappings":";;;;;;;;;;cAQa,kBAAgB,MAAA,CAAA,QAAA;AAA7B;AAMA;;;cAAa,wBAAsB,MAAA,CAAA,QAAA;;;UCPlB,sBAAA;aACJ;YACD;;ADDC,iBCIG,iBAAA,CDJa;EAAA,SAAA;EAAA;AAAA,CAAA,ECO1B,sBDP0B,CAAA,ECOJ,MAAA,CAAA,YDPI,CAAA,OAAA,EAAA,MAAA,GCOJ,MAAA,CAAA,qBDPI,CAAA,GAAA,CAAA,CAAA;;;UEAZ,kBAAA;;;;;EFAJ,OAAA,CAAA,EAAA,MAAA;EAMA;;;;ECPI,QAAA,CAAA,ECWJ,MDXI,CAAA,MAAA,EAAsB,OAAA,CAAA;EAKvB
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/providers/context.mts","../src/providers/container-provider.mts","../src/providers/scope-provider.mts","../src/hooks/use-container.mts","../src/types.mts","../src/hooks/use-service.mts","../src/hooks/use-suspense-service.mts","../src/hooks/use-optional-service.mts","../src/hooks/use-invalidate.mts","../src/hooks/use-scope.mts"],"sourcesContent":[],"mappings":";;;;;;;;;;cAQa,kBAAgB,MAAA,CAAA,QAAA;AAA7B;AAMA;;;cAAa,wBAAsB,MAAA,CAAA,QAAA;;;UCPlB,sBAAA;aACJ;YACD;;ADDC,iBCIG,iBAAA,CDJa;EAAA,SAAA;EAAA;AAAA,CAAA,ECO1B,sBDP0B,CAAA,ECOJ,MAAA,CAAA,YDPI,CAAA,OAAA,EAAA,MAAA,GCOJ,MAAA,CAAA,qBDPI,CAAA,GAAA,CAAA,CAAA;;;UEAZ,kBAAA;;;;;EFAJ,OAAA,CAAA,EAAA,MAAA;EAMA;;;;ECPI,QAAA,CAAA,ECWJ,MDXI,CAAA,MAAA,EAAsB,OAAA,CAAA;EAKvB,QAAA,ECOJ,SDPI;;;;;;;;;;ACJhB;AAoCA;;;;;;;;;;;ACzBA;AAwBA;iBDCgB,aAAA;;;;GAIb,qBAAkB,MAAA,CAAA,+BAAA,MAAA,CAAA;;;;;;;;AFxCrB;AAMA;;;;ACPiB,iBEYD,YAAA,CAAA,CFZuB,EEYP,SFXnB,GEW+B,eFVvB;AAGrB;;;;;;;;iBE+BgB,gBAAA,CAAA,GAAoB;;;KCzCxB,kBAAkB,iBAAiB;KAC1C,uCAAuC,gBACxC,iBAAiB,QAAQ,GAAG,QAAQ,GAAG,MACvC;KAEQ,kDAAkD,oBAE1D,8FAEO,kBAEL,2HAKO,QAAQ,YAAY,KAAK,MAAM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