Open History to see the last commit
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β
14import { InjectionToken, inject, runInInjectionContext } from '@angular/core'
15import { assertInjector } from '../assert-injector/assert-injector'
16import type {
17 EnvironmentProviders,
18 FactoryProvider,
19 Host,
20 InjectOptions,
21 Injector,
22 Optional,
23 Provider,
24 Self,
25 SkipSelf,
26 Type,
27} from '@angular/core'
28β
29type CreateInjectionTokenDep<TTokenType> =
30 | Type<TTokenType>
31 // NOTE: we don't have an AbstractType
32 | (abstract new (...args: Array<any>) => TTokenType)
33 | InjectionToken<TTokenType>
34β
35type CreateInjectionTokenDeps<
36 TFactory extends (...args: Array<any>) => any,
37 TFactoryDeps extends Parameters<TFactory> = Parameters<TFactory>,
38> = {
39 [Index in keyof TFactoryDeps]:
40 | CreateInjectionTokenDep<TFactoryDeps[Index]>
41 | [
42 ...modifiers: Array<Optional | Self | SkipSelf | Host>,
43 token: CreateInjectionTokenDep<TFactoryDeps[Index]>,
44 ]
45} & { length: TFactoryDeps['length'] }
46β
47type CreateInjectionTokenOptions<
48 TFactory extends (...args: Array<any>) => any,
49 TFactoryDeps extends Parameters<TFactory> = Parameters<TFactory>,
50> =
51 // this means TFunction has no parameters
52 (TFactoryDeps[0] extends undefined
53 ? { deps?: never }
54 : { deps: CreateInjectionTokenDeps<TFactory, TFactoryDeps> }) & {
55 isRoot?: boolean
56 multi?: boolean
57 token?: InjectionToken<ReturnType<TFactory>>
58 extraProviders?: Provider | EnvironmentProviders
59 }
60β
61type CreateProvideFnOptions<
62 TFactory extends (...args: Array<any>) => any,
63 TFactoryDeps extends Parameters<TFactory> = Parameters<TFactory>,
64> = Pick<
65 CreateInjectionTokenOptions<TFactory, TFactoryDeps>,
66 'deps' | 'extraProviders' | 'multi'
67>
68β
69type InjectFn<TFactoryReturn> = {
70 (): TFactoryReturn
71 (
72 injectOptions: InjectOptions & { optional?: false } & {
73 injector?: Injector
74 },
75 ): TFactoryReturn
76 (
77 injectOptions: InjectOptions & { injector?: Injector },
78 ): TFactoryReturn | null
79}
80β
81type ProvideFn<
82 TNoop extends boolean,
83 TFactoryReturn,
84 TReturn = TFactoryReturn extends Array<infer Item> ? Item : TFactoryReturn,
85> = (TNoop extends true
86 ? (value: TReturn | (() => TReturn)) => Provider
87 : () => Provider) &
88 (TReturn extends Function
89 ? (value: TReturn | (() => TReturn), isFunctionValue: boolean) => Provider
90 : (value: TReturn | (() => TReturn)) => Provider)
91β
92type CreateInjectionTokenReturn<
93 TFactoryReturn,
94 TNoop extends boolean = false,
95> = [
96 InjectFn<TFactoryReturn>,
97 ProvideFn<TNoop, TFactoryReturn>,
98 InjectionToken<TFactoryReturn>,
99 () => Provider,
100]
101β
102function createInjectFn<TValue>(token: InjectionToken<TValue>) {
103 return function (
104 this: Function,
105 {
106 injector,
107 ...injectOptions
108 }: InjectOptions & { injector?: Injector } = {},
109 ) {
110 injector = assertInjector(this, injector)
111 return runInInjectionContext(injector, () =>
112 inject(token, injectOptions as InjectOptions),
113 )
114 }
115}
116β
117function createProvideFn<
118 TValue,
119 TFactory extends (...args: Array<any>) => any = (
120 ...args: Array<any>
121 ) => TValue,
122 TFactoryDeps extends Parameters<TFactory> = Parameters<TFactory>,
123>(
124 token: InjectionToken<TValue>,
125 factory: (...args: Array<any>) => TValue,
126 opts: CreateProvideFnOptions<TFactory, TFactoryDeps> = {},
127) {
128 const { deps = [], multi = false, extraProviders = [] } = opts
129 return (value?: TValue | (() => TValue), isFunctionValue = false) => {
130 let provider: Provider
131 if (value !== undefined) {
132 // eslint-disable-next-line no-shadow
133 const factory =
134 typeof value === 'function'
135 ? isFunctionValue
136 ? () => value
137 : value
138 : () => value
139β
140 provider = {
141 provide: token,
142 useFactory: factory,
143 multi,
144 }
145 } else {
146 provider = {
147 provide: token,
148 useFactory: factory,
149 deps: deps as FactoryProvider['deps'],
150 multi,
151 }
152 }
153β
154 return [extraProviders, provider]
155 }
156}
157β
158export function createNoopInjectionToken<
159 TValue,
160 TMulti extends boolean = false,
161 TOptions = Pick<
162 CreateInjectionTokenOptions<() => void, []>,
163 'extraProviders'
164 > &
165 (TMulti extends true ? { multi: true } : Record<string, never>),
166>(description: string, options?: TOptions) {
167 type TReturn = TMulti extends true ? Array<TValue> : TValue
168β
169 const token =
170 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
171 (options as CreateInjectionTokenOptions<() => void, []>)?.token ||
172 new InjectionToken<TReturn>(description)
173 return [
174 createInjectFn(token) as CreateInjectionTokenReturn<TReturn, true>[0],
175 createProvideFn(
176 token,
177 () => null!,
178 (options || {}) as CreateProvideFnOptions<() => void, []>,
179 ) as CreateInjectionTokenReturn<TReturn, true>[1],
180 token,
181 () => {},
182 ] as CreateInjectionTokenReturn<TReturn, true>
183}