@tanstack/angular-query-experimental 5.34.1 → 5.35.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.
Files changed (78) hide show
  1. package/README.md +19 -2
  2. package/build/README.md +19 -2
  3. package/build/esm2022/create-base-query.mjs +3 -3
  4. package/build/esm2022/index.mjs +2 -2
  5. package/build/esm2022/infinite-query-options.mjs +9 -1
  6. package/build/esm2022/inject-infinite-query.mjs +11 -3
  7. package/build/esm2022/inject-is-fetching.mjs +11 -1
  8. package/build/esm2022/inject-is-mutating.mjs +10 -1
  9. package/build/esm2022/inject-mutation-state.mjs +8 -1
  10. package/build/esm2022/inject-mutation.mjs +13 -4
  11. package/build/esm2022/inject-queries.mjs +4 -1
  12. package/build/esm2022/inject-query-client.mjs +21 -3
  13. package/build/esm2022/inject-query.mjs +36 -3
  14. package/build/esm2022/providers.mjs +40 -1
  15. package/build/esm2022/query-options.mjs +23 -1
  16. package/build/esm2022/signal-proxy.mjs +2 -4
  17. package/build/esm2022/types.mjs +1 -1
  18. package/build/fesm2022/tanstack-angular-query-experimental.mjs +179 -14
  19. package/build/fesm2022/tanstack-angular-query-experimental.mjs.map +1 -1
  20. package/build/rollup.d.ts +591 -0
  21. package/package.json +10 -6
  22. package/build/create-base-query.d.ts +0 -6
  23. package/build/index.d.ts +0 -14
  24. package/build/infinite-query-options.d.ts +0 -18
  25. package/build/inject-infinite-query.d.ts +0 -7
  26. package/build/inject-is-fetching.d.ts +0 -3
  27. package/build/inject-is-mutating.d.ts +0 -3
  28. package/build/inject-mutation-state.d.ts +0 -11
  29. package/build/inject-mutation.d.ts +0 -4
  30. package/build/inject-queries.d.ts +0 -71
  31. package/build/inject-query-client.d.ts +0 -13
  32. package/build/inject-query.d.ts +0 -7
  33. package/build/providers.d.ts +0 -3
  34. package/build/query-options.d.ts +0 -16
  35. package/build/signal-proxy.d.ts +0 -13
  36. package/build/types.d.ts +0 -44
  37. package/build/util/assert-injector/assert-injector.d.ts +0 -54
  38. package/build/util/create-injection-token/create-injection-token.d.ts +0 -52
  39. package/build/util/index.d.ts +0 -2
  40. package/build/util/lazy-init/lazy-init.d.ts +0 -1
  41. package/build/util/lazy-signal-initializer/lazy-signal-initializer.d.ts +0 -4
  42. package/src/__tests__/inject-infinite-query.test.ts +0 -64
  43. package/src/__tests__/inject-is-fetching.test.ts +0 -35
  44. package/src/__tests__/inject-is-mutating.test.ts +0 -39
  45. package/src/__tests__/inject-mutation-state.test-d.ts +0 -22
  46. package/src/__tests__/inject-mutation-state.test.ts +0 -177
  47. package/src/__tests__/inject-mutation.test-d.ts +0 -71
  48. package/src/__tests__/inject-mutation.test.ts +0 -458
  49. package/src/__tests__/inject-query.test-d.ts +0 -59
  50. package/src/__tests__/inject-query.test.ts +0 -349
  51. package/src/__tests__/query-options.test-d.ts +0 -127
  52. package/src/__tests__/signal-proxy.test.ts +0 -27
  53. package/src/__tests__/test-utils.ts +0 -131
  54. package/src/__tests__/util/lazy-init/lazy-init.test.ts +0 -126
  55. package/src/__tests__/util/lazy-signal-initializer/lazy-signal-initializer.test.ts +0 -130
  56. package/src/create-base-query.ts +0 -116
  57. package/src/index.ts +0 -28
  58. package/src/infinite-query-options.ts +0 -94
  59. package/src/inject-infinite-query.ts +0 -93
  60. package/src/inject-is-fetching.ts +0 -39
  61. package/src/inject-is-mutating.ts +0 -39
  62. package/src/inject-mutation-state.ts +0 -97
  63. package/src/inject-mutation.ts +0 -109
  64. package/src/inject-queries.ts +0 -260
  65. package/src/inject-query-client.ts +0 -7
  66. package/src/inject-query.ts +0 -68
  67. package/src/providers.ts +0 -26
  68. package/src/query-options.ts +0 -50
  69. package/src/signal-proxy.ts +0 -48
  70. package/src/test-setup.ts +0 -12
  71. package/src/types.ts +0 -260
  72. package/src/util/assert-injector/assert-injector.test.ts +0 -74
  73. package/src/util/assert-injector/assert-injector.ts +0 -81
  74. package/src/util/create-injection-token/create-injection-token.test.ts +0 -32
  75. package/src/util/create-injection-token/create-injection-token.ts +0 -185
  76. package/src/util/index.ts +0 -13
  77. package/src/util/lazy-init/lazy-init.ts +0 -34
  78. package/src/util/lazy-signal-initializer/lazy-signal-initializer.ts +0 -28
package/src/types.ts DELETED
@@ -1,260 +0,0 @@
1
- /* istanbul ignore file */
2
-
3
- import type {
4
- DefaultError,
5
- DefinedInfiniteQueryObserverResult,
6
- DefinedQueryObserverResult,
7
- InfiniteQueryObserverOptions,
8
- InfiniteQueryObserverResult,
9
- MutateFunction,
10
- MutationObserverOptions,
11
- MutationObserverResult,
12
- OmitKeyof,
13
- QueryKey,
14
- QueryObserverOptions,
15
- QueryObserverResult,
16
- } from '@tanstack/query-core'
17
- import type { MapToSignals } from './signal-proxy'
18
-
19
- export interface CreateBaseQueryOptions<
20
- TQueryFnData = unknown,
21
- TError = DefaultError,
22
- TData = TQueryFnData,
23
- TQueryData = TQueryFnData,
24
- TQueryKey extends QueryKey = QueryKey,
25
- > extends QueryObserverOptions<
26
- TQueryFnData,
27
- TError,
28
- TData,
29
- TQueryData,
30
- TQueryKey
31
- > {}
32
-
33
- export interface CreateQueryOptions<
34
- TQueryFnData = unknown,
35
- TError = DefaultError,
36
- TData = TQueryFnData,
37
- TQueryKey extends QueryKey = QueryKey,
38
- > extends OmitKeyof<
39
- CreateBaseQueryOptions<
40
- TQueryFnData,
41
- TError,
42
- TData,
43
- TQueryFnData,
44
- TQueryKey
45
- >,
46
- 'suspense'
47
- > {}
48
-
49
- type CreateStatusBasedQueryResult<
50
- TStatus extends QueryObserverResult['status'],
51
- TData = unknown,
52
- TError = DefaultError,
53
- > = Extract<QueryObserverResult<TData, TError>, { status: TStatus }>
54
-
55
- export interface BaseQueryNarrowing<TData = unknown, TError = DefaultError> {
56
- isSuccess: (
57
- this: CreateBaseQueryResult<TData, TError>,
58
- ) => this is CreateBaseQueryResult<
59
- TData,
60
- TError,
61
- CreateStatusBasedQueryResult<'success', TData, TError>
62
- >
63
- isError: (
64
- this: CreateBaseQueryResult<TData, TError>,
65
- ) => this is CreateBaseQueryResult<
66
- TData,
67
- TError,
68
- CreateStatusBasedQueryResult<'error', TData, TError>
69
- >
70
- isPending: (
71
- this: CreateBaseQueryResult<TData, TError>,
72
- ) => this is CreateBaseQueryResult<
73
- TData,
74
- TError,
75
- CreateStatusBasedQueryResult<'pending', TData, TError>
76
- >
77
- }
78
-
79
- export interface CreateInfiniteQueryOptions<
80
- TQueryFnData = unknown,
81
- TError = DefaultError,
82
- TData = TQueryFnData,
83
- TQueryData = TQueryFnData,
84
- TQueryKey extends QueryKey = QueryKey,
85
- TPageParam = unknown,
86
- > extends OmitKeyof<
87
- InfiniteQueryObserverOptions<
88
- TQueryFnData,
89
- TError,
90
- TData,
91
- TQueryData,
92
- TQueryKey,
93
- TPageParam
94
- >,
95
- 'suspense'
96
- > {}
97
-
98
- export type CreateBaseQueryResult<
99
- TData = unknown,
100
- TError = DefaultError,
101
- TState = QueryObserverResult<TData, TError>,
102
- > = BaseQueryNarrowing<TData, TError> &
103
- MapToSignals<OmitKeyof<TState, keyof BaseQueryNarrowing, 'safely'>>
104
-
105
- export type CreateQueryResult<
106
- TData = unknown,
107
- TError = DefaultError,
108
- > = CreateBaseQueryResult<TData, TError>
109
-
110
- export type DefinedCreateQueryResult<
111
- TData = unknown,
112
- TError = DefaultError,
113
- TDefinedQueryObserver = DefinedQueryObserverResult<TData, TError>,
114
- > = MapToSignals<TDefinedQueryObserver>
115
-
116
- export type CreateInfiniteQueryResult<
117
- TData = unknown,
118
- TError = DefaultError,
119
- > = MapToSignals<InfiniteQueryObserverResult<TData, TError>>
120
-
121
- export type DefinedCreateInfiniteQueryResult<
122
- TData = unknown,
123
- TError = DefaultError,
124
- TDefinedInfiniteQueryObserver = DefinedInfiniteQueryObserverResult<
125
- TData,
126
- TError
127
- >,
128
- > = MapToSignals<TDefinedInfiniteQueryObserver>
129
-
130
- export interface CreateMutationOptions<
131
- TData = unknown,
132
- TError = DefaultError,
133
- TVariables = void,
134
- TContext = unknown,
135
- > extends OmitKeyof<
136
- MutationObserverOptions<TData, TError, TVariables, TContext>,
137
- '_defaulted'
138
- > {}
139
-
140
- export type CreateMutateFunction<
141
- TData = unknown,
142
- TError = DefaultError,
143
- TVariables = void,
144
- TContext = unknown,
145
- > = (
146
- ...args: Parameters<MutateFunction<TData, TError, TVariables, TContext>>
147
- ) => void
148
-
149
- export type CreateMutateAsyncFunction<
150
- TData = unknown,
151
- TError = DefaultError,
152
- TVariables = void,
153
- TContext = unknown,
154
- > = MutateFunction<TData, TError, TVariables, TContext>
155
-
156
- export type CreateBaseMutationResult<
157
- TData = unknown,
158
- TError = DefaultError,
159
- TVariables = unknown,
160
- TContext = unknown,
161
- > = Override<
162
- MutationObserverResult<TData, TError, TVariables, TContext>,
163
- { mutate: CreateMutateFunction<TData, TError, TVariables, TContext> }
164
- > & {
165
- mutateAsync: CreateMutateAsyncFunction<TData, TError, TVariables, TContext>
166
- }
167
-
168
- type CreateStatusBasedMutationResult<
169
- TStatus extends CreateBaseMutationResult['status'],
170
- TData = unknown,
171
- TError = DefaultError,
172
- TVariables = unknown,
173
- TContext = unknown,
174
- > = Extract<
175
- CreateBaseMutationResult<TData, TError, TVariables, TContext>,
176
- { status: TStatus }
177
- >
178
-
179
- export interface BaseMutationNarrowing<
180
- TData = unknown,
181
- TError = DefaultError,
182
- TVariables = unknown,
183
- TContext = unknown,
184
- > {
185
- isSuccess: (
186
- this: CreateMutationResult<TData, TError, TVariables, TContext>,
187
- ) => this is CreateMutationResult<
188
- TData,
189
- TError,
190
- TVariables,
191
- TContext,
192
- CreateStatusBasedMutationResult<
193
- 'success',
194
- TData,
195
- TError,
196
- TVariables,
197
- TContext
198
- >
199
- >
200
- isError: (
201
- this: CreateMutationResult<TData, TError, TVariables, TContext>,
202
- ) => this is CreateMutationResult<
203
- TData,
204
- TError,
205
- TVariables,
206
- TContext,
207
- CreateStatusBasedMutationResult<
208
- 'error',
209
- TData,
210
- TError,
211
- TVariables,
212
- TContext
213
- >
214
- >
215
- isPending: (
216
- this: CreateMutationResult<TData, TError, TVariables, TContext>,
217
- ) => this is CreateMutationResult<
218
- TData,
219
- TError,
220
- TVariables,
221
- TContext,
222
- CreateStatusBasedMutationResult<
223
- 'pending',
224
- TData,
225
- TError,
226
- TVariables,
227
- TContext
228
- >
229
- >
230
- isIdle: (
231
- this: CreateMutationResult<TData, TError, TVariables, TContext>,
232
- ) => this is CreateMutationResult<
233
- TData,
234
- TError,
235
- TVariables,
236
- TContext,
237
- CreateStatusBasedMutationResult<'idle', TData, TError, TVariables, TContext>
238
- >
239
- }
240
-
241
- export type CreateMutationResult<
242
- TData = unknown,
243
- TError = DefaultError,
244
- TVariables = unknown,
245
- TContext = unknown,
246
- TState = CreateStatusBasedMutationResult<
247
- CreateBaseMutationResult['status'],
248
- TData,
249
- TError,
250
- TVariables,
251
- TContext
252
- >,
253
- > = BaseMutationNarrowing<TData, TError, TVariables, TContext> &
254
- MapToSignals<OmitKeyof<TState, keyof BaseMutationNarrowing, 'safely'>>
255
-
256
- type Override<TTargetA, TTargetB> = {
257
- [AKey in keyof TTargetA]: AKey extends keyof TTargetB
258
- ? TTargetB[AKey]
259
- : TTargetA[AKey]
260
- }
@@ -1,74 +0,0 @@
1
- /* eslint-disable @cspell/spellchecker */
2
- /**
3
- * The code in this file is adapted from NG Extension Platform at https://ngxtension.netlify.app.
4
- *
5
- * Original Author: Chau Tran
6
- *
7
- * NG Extension Platform is an open-source project licensed under the MIT license.
8
- *
9
- * For more information about the original code, see
10
- * https://github.com/nartc/ngxtension-platform
11
- */
12
- /* eslint-enable */
13
-
14
- import {
15
- InjectionToken,
16
- Injector,
17
- inject,
18
- runInInjectionContext,
19
- } from '@angular/core'
20
- import { TestBed } from '@angular/core/testing'
21
- import { assertInjector } from './assert-injector'
22
-
23
- describe(assertInjector.name, () => {
24
- const token = new InjectionToken('token', {
25
- factory: () => 1,
26
- })
27
-
28
- function injectDummy(injector?: Injector) {
29
- injector = assertInjector(injectDummy, injector)
30
- return runInInjectionContext(injector, () => inject(token))
31
- }
32
-
33
- function injectDummyTwo(injector?: Injector) {
34
- return assertInjector(injectDummyTwo, injector, () => inject(token) + 1)
35
- }
36
-
37
- it('given no custom injector, when run in injection context, then return value', () => {
38
- TestBed.runInInjectionContext(() => {
39
- const value = injectDummy()
40
- const valueTwo = injectDummyTwo()
41
- expect(value).toEqual(1)
42
- expect(valueTwo).toEqual(2)
43
- })
44
- })
45
-
46
- it('given no custom injector, when run outside injection context, then throw', () => {
47
- expect(() => injectDummy()).toThrowError(
48
- /injectDummy\(\) can only be used within an injection context/i,
49
- )
50
- expect(() => injectDummyTwo()).toThrowError(
51
- /injectDummyTwo\(\) can only be used within an injection context/i,
52
- )
53
- })
54
-
55
- it('given a custom injector, when run in that injector context without providing number, then throw', () => {
56
- expect(() => injectDummy(Injector.create({ providers: [] }))).toThrowError(
57
- /No provider for InjectionToken/i,
58
- )
59
- expect(() =>
60
- injectDummyTwo(Injector.create({ providers: [] })),
61
- ).toThrowError(/No provider for InjectionToken/i)
62
- })
63
-
64
- it('given a custom injector, when run in that injector context and providing number, then return value', () => {
65
- const value = injectDummy(
66
- Injector.create({ providers: [{ provide: token, useValue: 2 }] }),
67
- )
68
- const valueTwo = injectDummyTwo(
69
- Injector.create({ providers: [{ provide: token, useValue: 2 }] }),
70
- )
71
- expect(value).toEqual(2)
72
- expect(valueTwo).toEqual(3)
73
- })
74
- })
@@ -1,81 +0,0 @@
1
- /* eslint-disable @cspell/spellchecker */
2
- /**
3
- * The code in this file is adapted from NG Extension Platform at https://ngxtension.netlify.app.
4
- *
5
- * Original Author: Chau Tran
6
- *
7
- * NG Extension Platform is an open-source project licensed under the MIT license.
8
- *
9
- * For more information about the original code, see
10
- * https://github.com/nartc/ngxtension-platform
11
- */
12
- /* eslint-enable */
13
-
14
- import {
15
- Injector,
16
- assertInInjectionContext,
17
- inject,
18
- runInInjectionContext,
19
- } from '@angular/core'
20
-
21
- /**
22
- * `assertInjector` extends `assertInInjectionContext` with an optional `Injector`
23
- * After assertion, `assertInjector` runs the `runner` function with the guaranteed `Injector`
24
- * whether it is the default `Injector` within the current **Injection Context**
25
- * or the custom `Injector` that was passed in.
26
- *
27
- * @template {() => any} Runner - Runner is a function that can return anything
28
- * @param {Function} fn - the Function to pass in `assertInInjectionContext`
29
- * @param {Injector | undefined | null} injector - the optional "custom" Injector
30
- * @param {Runner} runner - the runner fn
31
- * @returns {ReturnType<Runner>} result - returns the result of the Runner
32
- *
33
- * @example
34
- * ```ts
35
- * function injectValue(injector?: Injector) {
36
- * return assertInjector(injectValue, injector, () => 'value');
37
- * }
38
- *
39
- * injectValue(); // string
40
- * ```
41
- */
42
- export function assertInjector<TRunner extends () => any>(
43
- fn: Function,
44
- injector: Injector | undefined | null,
45
- runner: TRunner,
46
- ): ReturnType<TRunner>
47
- /**
48
- * `assertInjector` extends `assertInInjectionContext` with an optional `Injector`
49
- * After assertion, `assertInjector` returns a guaranteed `Injector` whether it is the default `Injector`
50
- * within the current **Injection Context** or the custom `Injector` that was passed in.
51
- *
52
- * @param {Function} fn - the Function to pass in `assertInInjectionContext`
53
- * @param {Injector | undefined | null} injector - the optional "custom" Injector
54
- * @returns Injector
55
- *
56
- * @example
57
- * ```ts
58
- * function injectDestroy(injector?: Injector) {
59
- * injector = assertInjector(injectDestroy, injector);
60
- *
61
- * return runInInjectionContext(injector, () => {
62
- * // code
63
- * })
64
- * }
65
- * ```
66
- */
67
- export function assertInjector(
68
- fn: Function,
69
- injector: Injector | undefined | null,
70
- ): Injector
71
- export function assertInjector(
72
- fn: Function,
73
- injector: Injector | undefined | null,
74
- runner?: () => any,
75
- ) {
76
- !injector && assertInInjectionContext(fn)
77
- const assertedInjector = injector ?? inject(Injector)
78
-
79
- if (!runner) return assertedInjector
80
- return runInInjectionContext(assertedInjector, runner)
81
- }
@@ -1,32 +0,0 @@
1
- /* eslint-disable @cspell/spellchecker */
2
- /**
3
- * The code in this file is adapted from NG Extension Platform at https://ngxtension.netlify.app.
4
- *
5
- * Original Author: Chau Tran
6
- *
7
- * NG Extension Platform is an open-source project licensed under the MIT license.
8
- *
9
- * For more information about the original code, see
10
- * https://github.com/nartc/ngxtension-platform
11
- */
12
- /* eslint-enable */
13
-
14
- import { TestBed } from '@angular/core/testing'
15
- import { createNoopInjectionToken } from './create-injection-token'
16
-
17
- describe(createNoopInjectionToken.name, () => {
18
- describe('given an injection token', () => {
19
- const [injectFn, provideFn] = createNoopInjectionToken<number, true>(
20
- 'noop',
21
- { multi: true },
22
- )
23
- it('then work properly', () => {
24
- TestBed.configureTestingModule({
25
- providers: [provideFn(1), provideFn(() => 2)],
26
- }).runInInjectionContext(() => {
27
- const values = injectFn()
28
- expect(values).toEqual([1, 2])
29
- })
30
- })
31
- })
32
- })
@@ -1,185 +0,0 @@
1
- /* eslint-disable @cspell/spellchecker */
2
- /**
3
- * The code in this file is adapted from NG Extension Platform at https://ngxtension.netlify.app.
4
- *
5
- * Original Author: Chau Tran
6
- *
7
- * NG Extension Platform is an open-source project licensed under the MIT license.
8
- *
9
- * For more information about the original code, see
10
- * https://github.com/nartc/ngxtension-platform
11
- */
12
- /* eslint-enable */
13
-
14
- import {
15
- type EnvironmentProviders,
16
- type FactoryProvider,
17
- type Host,
18
- type InjectOptions,
19
- InjectionToken,
20
- type Injector,
21
- type Optional,
22
- type Provider,
23
- type Self,
24
- type SkipSelf,
25
- type Type,
26
- inject,
27
- runInInjectionContext,
28
- } from '@angular/core'
29
- import { assertInjector } from '../assert-injector/assert-injector'
30
-
31
- type CreateInjectionTokenDep<TTokenType> =
32
- | Type<TTokenType>
33
- // NOTE: we don't have an AbstractType
34
- | (abstract new (...args: Array<any>) => TTokenType)
35
- | InjectionToken<TTokenType>
36
-
37
- type CreateInjectionTokenDeps<
38
- TFactory extends (...args: Array<any>) => any,
39
- TFactoryDeps extends Parameters<TFactory> = Parameters<TFactory>,
40
- > = {
41
- [Index in keyof TFactoryDeps]:
42
- | CreateInjectionTokenDep<TFactoryDeps[Index]>
43
- | [
44
- ...modifiers: Array<Optional | Self | SkipSelf | Host>,
45
- token: CreateInjectionTokenDep<TFactoryDeps[Index]>,
46
- ]
47
- } & { length: TFactoryDeps['length'] }
48
-
49
- type CreateInjectionTokenOptions<
50
- TFactory extends (...args: Array<any>) => any,
51
- TFactoryDeps extends Parameters<TFactory> = Parameters<TFactory>,
52
- > =
53
- // this means TFunction has no parameters
54
- (TFactoryDeps[0] extends undefined
55
- ? { deps?: never }
56
- : { deps: CreateInjectionTokenDeps<TFactory, TFactoryDeps> }) & {
57
- isRoot?: boolean
58
- multi?: boolean
59
- token?: InjectionToken<ReturnType<TFactory>>
60
- extraProviders?: Provider | EnvironmentProviders
61
- }
62
-
63
- type CreateProvideFnOptions<
64
- TFactory extends (...args: Array<any>) => any,
65
- TFactoryDeps extends Parameters<TFactory> = Parameters<TFactory>,
66
- > = Pick<
67
- CreateInjectionTokenOptions<TFactory, TFactoryDeps>,
68
- 'deps' | 'extraProviders' | 'multi'
69
- >
70
-
71
- type InjectFn<TFactoryReturn> = {
72
- (): TFactoryReturn
73
- (
74
- injectOptions: InjectOptions & { optional?: false } & {
75
- injector?: Injector
76
- },
77
- ): TFactoryReturn
78
- (
79
- injectOptions: InjectOptions & { injector?: Injector },
80
- ): TFactoryReturn | null
81
- }
82
-
83
- type ProvideFn<
84
- TNoop extends boolean,
85
- TFactoryReturn,
86
- TReturn = TFactoryReturn extends Array<infer Item> ? Item : TFactoryReturn,
87
- > = (TNoop extends true
88
- ? (value: TReturn | (() => TReturn)) => Provider
89
- : () => Provider) &
90
- (TReturn extends Function
91
- ? (value: TReturn | (() => TReturn), isFunctionValue: boolean) => Provider
92
- : (value: TReturn | (() => TReturn)) => Provider)
93
-
94
- type CreateInjectionTokenReturn<
95
- TFactoryReturn,
96
- TNoop extends boolean = false,
97
- > = [
98
- InjectFn<TFactoryReturn>,
99
- ProvideFn<TNoop, TFactoryReturn>,
100
- InjectionToken<TFactoryReturn>,
101
- () => Provider,
102
- ]
103
-
104
- function createInjectFn<TValue>(token: InjectionToken<TValue>) {
105
- return function (
106
- this: Function,
107
- {
108
- injector,
109
- ...injectOptions
110
- }: InjectOptions & { injector?: Injector } = {},
111
- ) {
112
- injector = assertInjector(this, injector)
113
- return runInInjectionContext(injector, () =>
114
- inject(token, injectOptions as InjectOptions),
115
- )
116
- }
117
- }
118
-
119
- function createProvideFn<
120
- TValue,
121
- TFactory extends (...args: Array<any>) => any = (
122
- ...args: Array<any>
123
- ) => TValue,
124
- TFactoryDeps extends Parameters<TFactory> = Parameters<TFactory>,
125
- >(
126
- token: InjectionToken<TValue>,
127
- factory: (...args: Array<any>) => TValue,
128
- opts: CreateProvideFnOptions<TFactory, TFactoryDeps> = {},
129
- ) {
130
- const { deps = [], multi = false, extraProviders = [] } = opts
131
- return (value?: TValue | (() => TValue), isFunctionValue = false) => {
132
- let provider: Provider
133
- if (value !== undefined) {
134
- // eslint-disable-next-line no-shadow
135
- const factory =
136
- typeof value === 'function'
137
- ? isFunctionValue
138
- ? () => value
139
- : value
140
- : () => value
141
-
142
- provider = {
143
- provide: token,
144
- useFactory: factory,
145
- multi,
146
- }
147
- } else {
148
- provider = {
149
- provide: token,
150
- useFactory: factory,
151
- deps: deps as FactoryProvider['deps'],
152
- multi,
153
- }
154
- }
155
-
156
- return [extraProviders, provider]
157
- }
158
- }
159
-
160
- export function createNoopInjectionToken<
161
- TValue,
162
- TMulti extends boolean = false,
163
- TOptions = Pick<
164
- CreateInjectionTokenOptions<() => void, []>,
165
- 'extraProviders'
166
- > &
167
- (TMulti extends true ? { multi: true } : Record<string, never>),
168
- >(description: string, options?: TOptions) {
169
- type TReturn = TMulti extends true ? Array<TValue> : TValue
170
-
171
- const token =
172
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
173
- (options as CreateInjectionTokenOptions<() => void, []>)?.token ||
174
- new InjectionToken<TReturn>(description)
175
- return [
176
- createInjectFn(token) as CreateInjectionTokenReturn<TReturn, true>[0],
177
- createProvideFn(
178
- token,
179
- () => null!,
180
- (options || {}) as CreateProvideFnOptions<() => void, []>,
181
- ) as CreateInjectionTokenReturn<TReturn, true>[1],
182
- token,
183
- () => {},
184
- ] as CreateInjectionTokenReturn<TReturn, true>
185
- }
package/src/util/index.ts DELETED
@@ -1,13 +0,0 @@
1
- export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
2
- throwError: boolean | T | undefined,
3
- params: Parameters<T>,
4
- ): boolean {
5
- // Allow throwError function to override throwing behavior on a per-error basis
6
- if (typeof throwError === 'function') {
7
- return throwError(...params)
8
- }
9
-
10
- return !!throwError
11
- }
12
-
13
- export function noop() {}
@@ -1,34 +0,0 @@
1
- import { untracked } from '@angular/core'
2
-
3
- export function lazyInit<T extends object>(initializer: () => T): T {
4
- let object: T | null = null
5
-
6
- const initializeObject = () => {
7
- if (!object) {
8
- object = untracked(() => initializer())
9
- }
10
- }
11
-
12
- queueMicrotask(() => initializeObject())
13
-
14
- return new Proxy<T>({} as T, {
15
- get(_, prop, receiver) {
16
- initializeObject()
17
- return Reflect.get(object as T, prop, receiver)
18
- },
19
- has(_, prop) {
20
- initializeObject()
21
- return Reflect.has(object as T, prop)
22
- },
23
- ownKeys() {
24
- initializeObject()
25
- return Reflect.ownKeys(object as T)
26
- },
27
- getOwnPropertyDescriptor() {
28
- return {
29
- enumerable: true,
30
- configurable: true,
31
- }
32
- },
33
- })
34
- }
@@ -1,28 +0,0 @@
1
- import {
2
- Injector,
3
- type Signal,
4
- computed,
5
- inject,
6
- untracked,
7
- } from '@angular/core'
8
-
9
- type SignalInitializerFn<T> = (injector: Injector) => Signal<T>
10
-
11
- export function lazySignalInitializer<T>(
12
- initializerFn: SignalInitializerFn<T>,
13
- ) {
14
- const injector = inject(Injector)
15
-
16
- let source: Signal<T> | null = null
17
-
18
- const unwrapSignal = () => {
19
- if (!source) {
20
- source = untracked(() => initializerFn(injector))
21
- }
22
- return source()
23
- }
24
-
25
- queueMicrotask(() => unwrapSignal())
26
-
27
- return computed(unwrapSignal)
28
- }