@mmstack/di 21.0.4 → 21.0.5

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.
@@ -20,12 +20,22 @@ class ScopeRegistry {
20
20
  injector = inject(Injector);
21
21
  // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
22
22
  registry = new Map();
23
- getOrCreate(factory) {
23
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
24
+ resolving = new Set();
25
+ getOrCreate(factory, scopeName, factoryName) {
24
26
  if (this.registry.has(factory))
25
27
  return this.registry.get(factory);
26
- const val = runInInjectionContext(this.injector, factory);
27
- this.registry.set(factory, val);
28
- return val;
28
+ if (this.resolving.has(factory))
29
+ throw new Error(`[mmstack/di]: Circular dependency detected in scope "${scopeName ?? 'unknown'}"${factoryName ? ` while resolving "${factoryName}"` : ''}`);
30
+ this.resolving.add(factory);
31
+ try {
32
+ const val = runInInjectionContext(this.injector, factory);
33
+ this.registry.set(factory, val);
34
+ return val;
35
+ }
36
+ finally {
37
+ this.resolving.delete(factory);
38
+ }
29
39
  }
30
40
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.0", ngImport: i0, type: ScopeRegistry, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
31
41
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.0", ngImport: i0, type: ScopeRegistry });
@@ -76,12 +86,12 @@ function createScope(name) {
76
86
  provide: token,
77
87
  useClass: ScopeRegistry,
78
88
  });
79
- const registerFn = (factory) => {
89
+ const registerFn = (factory, factoryName) => {
80
90
  return () => {
81
91
  const registry = inject(token, { optional: true });
82
92
  if (!registry)
83
93
  throw new Error(`[mmstack/di]: Scope ${name ?? 'unknown'} not found. Please make sure you provide it`);
84
- return registry.getOrCreate(factory);
94
+ return registry.getOrCreate(factory, name, factoryName);
85
95
  };
86
96
  };
87
97
  return [registerFn, provideFn];
@@ -147,7 +157,7 @@ function injectable(tokenOrFn, optOrString) {
147
157
  }
148
158
 
149
159
  /**
150
- * Creates a tree-shakable root singleton without a class.
160
+ * Creates a root-level singleton hooked into the global injector.
151
161
  * @example const injectUser = rootInjectable(() => ({ name: signal('John') }));
152
162
  */
153
163
  function rootInjectable(factory, // Keeping the injector just in case
@@ -1 +1 @@
1
- {"version":3,"file":"mmstack-di.mjs","sources":["../../../../packages/di/src/lib/create-run-in-injection-context.ts","../../../../packages/di/src/lib/create-scope.ts","../../../../packages/di/src/lib/inject-lazy.ts","../../../../packages/di/src/lib/injectable.ts","../../../../packages/di/src/lib/root-injectable.ts","../../../../packages/di/src/mmstack-di.ts"],"sourcesContent":["import { inject, Injector, runInInjectionContext } from '@angular/core';\n\n/**\n * Captures an injection context and returns a runner function.\n *\n * This runner function allows you to execute callbacks inside the captured context at a later time.\n * It's really just a slight DX improvement over calling runInInjectionContext over and over\n *\n * @param passedInjector An optional injector. If not provided, the current context's injector is pulled natively via `inject(Injector)`.\n * @returns A runner function that executes any provided callback within the captured context.\n */\n\nexport function createRunInInjectionContext(injector?: Injector) {\n if (!injector) injector = inject(Injector);\n\n return <T>(fn: () => T) => runInInjectionContext(injector, fn);\n}\n","import {\n inject,\n Injectable,\n InjectionToken,\n Injector,\n runInInjectionContext,\n type Provider,\n} from '@angular/core';\n\n@Injectable()\nexport class ScopeRegistry {\n private readonly injector = inject(Injector);\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n private readonly registry = new Map<Function, any>();\n\n getOrCreate<T>(factory: () => T): T {\n if (this.registry.has(factory)) return this.registry.get(factory);\n\n const val = runInInjectionContext(this.injector, factory);\n this.registry.set(factory, val);\n return val;\n }\n}\n\n/**\n * Creates a specialized dependency injection scope.\n *\n * This utility allows you to create a localized dependency injection scope where you can\n * register and provide shared state, services, or primitives that are bound to a specific\n * component tree instead of the global root injector.\n *\n * @param name Optional name for the scope, primarily used for debugging and error messages.\n * @returns A tuple containing `[registerFn, provideFn]`:\n * - `registerFn`: A function to register a factory within the scope. Returns an injection function to retrieve the value.\n * - `provideFn`: An Angular provider function that must be added to the `providers` array where the scope begins.\n *\n * @example\n * ```ts\n * const [registerInUserScope, provideUserScope] = createScope('UserScope');\n *\n * // Define a state/service bound to this scope\n * const injectUserState = registerInUserScope(() => signal({ name: 'John Doe' }));\n * const injectLogger = registerInUserScope(() => {\n * const globalLogger = inject(GlobalLogger);\n * const user = injectUserState();\n * return {\n * log: (msg: string) => globalLogger.log(`[USER MODULE (${user().name})]: ${msg}`),\n * }\n * })\n * @Component({\n * providers: [provideUserScope()] // provides a new instance of every dependency registered to the scope\n * })\n * class ParentComponent {}\n *\n * @Component({})\n * class ChildComponent {\n * readonly userState = injectUserState();\n * readonly logger = injectLogger();\n * }\n * ```\n */\nexport function createScope(name?: string) {\n const token = new InjectionToken<ScopeRegistry>(name ?? '@mmstack/di/scope');\n\n const provideFn = (): Provider => ({\n provide: token,\n useClass: ScopeRegistry,\n });\n\n const registerFn = <T>(factory: () => T) => {\n return () => {\n const registry = inject(token, { optional: true });\n if (!registry)\n throw new Error(\n `[mmstack/di]: Scope ${name ?? 'unknown'} not found. Please make sure you provide it`,\n );\n\n return registry.getOrCreate(factory);\n };\n };\n\n return [registerFn, provideFn] as const;\n}\n","import {\n inject,\n Injector,\n runInInjectionContext,\n type HostAttributeToken,\n type InjectOptions,\n type ProviderToken,\n} from '@angular/core';\n\nconst UNINITIALIZED_SYMBOL = Symbol('@mmstack/di/inject-lazy/uninitialized');\n\ntype OptionalInjectOptions = Omit<InjectOptions, 'optional'> & {\n optional: true;\n};\n\ntype NonOptionalInjectOptions = Omit<InjectOptions, 'optional'> & {\n optional?: false;\n};\n\n/**\n * Defers the resolution and instantiation of a token until the returned getter function is called.\n * The resolved value is cached for subsequent calls.\n *\n * @param token The dependency token to inject.\n * @returns A getter function that returns the lazily resolved dependency.\n */\nexport function injectLazy<T>(token: ProviderToken<T>): () => T;\n\n/**\n * Defers the resolution and instantiation of a token until the returned getter function is called.\n * The resolved value is cached for subsequent calls.\n *\n * @param token The dependency token to inject.\n * @param options Injection options specifying optional resolution.\n * @returns A getter function that returns the lazily resolved dependency or null.\n */\nexport function injectLazy<T>(\n token: ProviderToken<T>,\n options: OptionalInjectOptions,\n): () => T | null;\n\n/**\n * Defers the resolution and instantiation of a token until the returned getter function is called.\n * The resolved value is cached for subsequent calls.\n *\n * @param token The dependency token to inject.\n * @param options Injection options specifying non-optional resolution.\n * @returns A getter function that returns the lazily resolved dependency.\n */\nexport function injectLazy<T>(\n token: ProviderToken<T>,\n options: NonOptionalInjectOptions,\n): () => T;\n\n/**\n * Defers the resolution and instantiation of a host attribute token until the returned getter function is called.\n * The resolved value is cached for subsequent calls.\n *\n * @param token The host attribute token to inject.\n * @returns A getter function that returns the lazily resolved attribute string.\n */\nexport function injectLazy(token: HostAttributeToken): () => string;\n\n/**\n * Defers the resolution and instantiation of a host attribute token until the returned getter function is called.\n * The resolved value is cached for subsequent calls.\n *\n * @param token The host attribute token to inject.\n * @param options Injection options specifying non-optional resolution.\n * @returns A getter function that returns the lazily resolved attribute string.\n */\nexport function injectLazy(\n token: HostAttributeToken,\n options?: {\n optional?: false;\n },\n): () => string;\n\n/**\n * Defers the resolution and instantiation of a host attribute token until the returned getter function is called.\n * The resolved value is cached for subsequent calls.\n *\n * @param token The host attribute token to inject.\n * @param options Injection options specifying optional resolution.\n * @returns A getter function that returns the lazily resolved attribute string or null.\n */\nexport function injectLazy(\n token: HostAttributeToken,\n options: {\n optional: true;\n },\n): () => string | null;\n\nexport function injectLazy<T>(\n token: ProviderToken<T> | HostAttributeToken,\n options?: Partial<OptionalInjectOptions | NonOptionalInjectOptions>,\n): () => T | string | null {\n const injector = inject(Injector);\n let instance: T | string | null | typeof UNINITIALIZED_SYMBOL =\n UNINITIALIZED_SYMBOL;\n\n return () => {\n if (instance === UNINITIALIZED_SYMBOL)\n instance = runInInjectionContext(injector, () => {\n return options\n ? inject(token as any, options as any)\n : inject(token as any);\n });\n\n return instance as T | string | null;\n };\n}\n","import {\n inject,\n InjectionToken,\n type AbstractType,\n type InjectOptions,\n type Provider,\n type Type,\n} from '@angular/core';\n\ntype ServiceType<T> =\n T extends Type<infer U>\n ? U\n : T extends AbstractType<infer U>\n ? U\n : T extends InjectionToken<infer U>\n ? U\n : never;\n\ntype MapDeps<T extends readonly any[]> = {\n [K in keyof T]: ServiceType<T[K]>;\n};\n\ntype ProviderFn<T> = {\n (value: T): Provider;\n <const TDeps extends any[]>(\n fn: (...deps: MapDeps<TDeps>) => T,\n deps: TDeps,\n ): Provider;\n};\n\ntype InjectFns<T> = [\n (opt?: Omit<InjectOptions, 'optional'>) => T,\n ProviderFn<T>,\n];\n\ntype FallbackInjectableOptions<T> = {\n /** Default value returned when the injectable is not provided */\n fallback: T;\n};\n\ntype LazyFallbackInjectableOptions<T> = {\n /** Function that returns a default value when the injectable is not provided. Useful for expensive defaults. */\n lazyFallback: () => T;\n};\n\ntype ErrorMessageInjectableOptions = {\n /** Error message thrown when the injectable is not provided */\n errorMessage: string;\n};\n\ntype InjectableOptions<T> =\n | FallbackInjectableOptions<T>\n | LazyFallbackInjectableOptions<T>\n | ErrorMessageInjectableOptions;\n\n/**\n * Creates a typed InjectionToken with inject and provide helper functions.\n *\n * @param token - Unique token identifier\n * @param opt - Optional configuration for fallback value or error message\n * @returns A tuple of [injectFn, provideFn] for type-safe dependency injection\n */\nexport function injectable<T>(token: string): InjectFns<T | null>;\n\n/**\n * Creates a typed InjectionToken with inject and provide helper functions.\n * Returns a fallback value when the injectable is not provided.\n *\n * @param token - Unique token identifier\n * @param opt - Configuration with fallback value\n * @returns A tuple of [injectFn, provideFn] for type-safe dependency injection\n */\nexport function injectable<T>(\n token: string,\n opt: FallbackInjectableOptions<T>,\n): InjectFns<T>;\n\n/**\n *\n * Creates a typed InjectionToken with inject and provide helper functions.\n * Returns a lazily evaluated fallback value when the injectable is not provided.\n *\n * @param token\n * @param opt\n */\nexport function injectable<T>(\n token: string,\n opt: LazyFallbackInjectableOptions<T>,\n): InjectFns<T>;\n\n/**\n * Creates a typed InjectionToken with inject and provide helper functions.\n * Throws an error with a custom message when the injectable is not provided.\n *\n * @param token - Unique token identifier\n * @param opt - Configuration with error message\n * @returns A tuple of [injectFn, provideFn] for type-safe dependency injection\n */\nexport function injectable<T>(\n token: string,\n opt: ErrorMessageInjectableOptions,\n): InjectFns<T>;\n\n/**\n * Creates an injectable with a baked-in factory.\n * @example const [injectUser, provideUser] = injectable(() => inject(HttpClient).get('/user'), 'UserToken');\n */\nexport function injectable<T>(fn: () => T, name?: string): InjectFns<T>;\n\nexport function injectable<T>(\n tokenOrFn: string | (() => T),\n optOrString?: InjectableOptions<T> | string,\n): InjectFns<T> {\n const token =\n typeof tokenOrFn === 'string'\n ? tokenOrFn\n : typeof optOrString === 'string'\n ? optOrString\n : '@mmstack/di/injectable';\n\n const opt =\n typeof tokenOrFn === 'function'\n ? { lazyFallback: tokenOrFn }\n : typeof optOrString === 'string'\n ? undefined\n : optOrString;\n\n const injectionToken = new InjectionToken<T>(token);\n\n const options = opt as\n | Partial<\n FallbackInjectableOptions<T> &\n LazyFallbackInjectableOptions<T> &\n ErrorMessageInjectableOptions\n >\n | undefined;\n\n let fallback: T | undefined | null = options?.fallback;\n\n const initFallback =\n options?.lazyFallback ?? (() => options?.fallback ?? null);\n\n const fallbackFn = () => {\n if (fallback === undefined) fallback = initFallback();\n return fallback;\n };\n\n const injectFn = (iOpt?: Omit<InjectOptions, 'optional'>) => {\n const injected =\n inject(injectionToken, {\n ...iOpt,\n optional: true,\n }) ?? fallbackFn();\n\n if (injected === null && options?.errorMessage)\n throw new Error(options.errorMessage);\n\n return injected as T;\n };\n\n const provideFn = (\n fnOrValue: T | ((...deps: any[]) => T),\n deps?: any[],\n ): Provider => {\n if (deps !== undefined)\n return {\n provide: injectionToken,\n useFactory: fnOrValue as (...args: any[]) => T,\n deps,\n };\n\n return {\n provide: injectionToken,\n useValue: fnOrValue,\n };\n };\n\n return [injectFn, provideFn];\n}\n","import { inject, InjectionToken, Injector } from '@angular/core';\n\n/**\n * Creates a tree-shakable root singleton without a class.\n * @example const injectUser = rootInjectable(() => ({ name: signal('John') }));\n */\nexport function rootInjectable<T>(\n factory: (injector: Injector) => T, // Keeping the injector just in case\n name?: string,\n): () => T {\n const token = new InjectionToken<T>(name ?? '@mmstack/di/root-injectable', {\n providedIn: 'root',\n factory: () => factory(inject(Injector)),\n });\n\n return () => inject(token);\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;AAEA;;;;;;;;AAQG;AAEG,SAAU,2BAA2B,CAAC,QAAmB,EAAA;AAC7D,IAAA,IAAI,CAAC,QAAQ;AAAE,QAAA,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;IAE1C,OAAO,CAAI,EAAW,KAAK,qBAAqB,CAAC,QAAQ,EAAE,EAAE,CAAC;AAChE;;MCNa,aAAa,CAAA;AACP,IAAA,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;;AAE3B,IAAA,QAAQ,GAAG,IAAI,GAAG,EAAiB;AAEpD,IAAA,WAAW,CAAI,OAAgB,EAAA;AAC7B,QAAA,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC;YAAE,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC;QAEjE,MAAM,GAAG,GAAG,qBAAqB,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC;QACzD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC;AAC/B,QAAA,OAAO,GAAG;IACZ;uGAXW,aAAa,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA;2GAAb,aAAa,EAAA,CAAA;;2FAAb,aAAa,EAAA,UAAA,EAAA,CAAA;kBADzB;;AAeD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAo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AAC;AAED,IAAA,OAAO,CAAC,QAAQ,EAAE,SAAS,CAAC;AAC9B;;AChLA;;;AAGG;AACG,SAAU,cAAc,CAC5B,OAAkC;AAClC,IAAa,EAAA;IAEb,MAAM,KAAK,GAAG,IAAI,cAAc,CAAI,IAAI,IAAI,6BAA6B,EAAE;AACzE,QAAA,UAAU,EAAE,MAAM;QAClB,OAAO,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AACzC,KAAA,CAAC;AAEF,IAAA,OAAO,MAAM,MAAM,CAAC,KAAK,CAAC;AAC5B;;AChBA;;AAEG;;;;"}
1
+ {"version":3,"file":"mmstack-di.mjs","sources":["../../../../packages/di/src/lib/create-run-in-injection-context.ts","../../../../packages/di/src/lib/create-scope.ts","../../../../packages/di/src/lib/inject-lazy.ts","../../../../packages/di/src/lib/injectable.ts","../../../../packages/di/src/lib/root-injectable.ts","../../../../packages/di/src/mmstack-di.ts"],"sourcesContent":["import { inject, Injector, runInInjectionContext } from '@angular/core';\n\n/**\n * Captures an injection context and returns a runner function.\n *\n * This runner function allows you to execute callbacks inside the captured context at a later time.\n * It's really just a slight DX improvement over calling runInInjectionContext over and over\n *\n * @param passedInjector An optional injector. If not provided, the current context's injector is pulled natively via `inject(Injector)`.\n * @returns A runner function that executes any provided callback within the captured context.\n */\n\nexport function createRunInInjectionContext(injector?: Injector) {\n if (!injector) injector = inject(Injector);\n\n return <T>(fn: () => T) => runInInjectionContext(injector, fn);\n}\n","import {\n inject,\n Injectable,\n InjectionToken,\n Injector,\n runInInjectionContext,\n type Provider,\n} from '@angular/core';\n\n@Injectable()\nclass ScopeRegistry {\n private readonly injector = inject(Injector);\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n private readonly registry = new Map<Function, any>();\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n private readonly resolving = new Set<Function>();\n\n getOrCreate<T>(factory: () => T, scopeName?: string, factoryName?: string): T {\n if (this.registry.has(factory)) return this.registry.get(factory);\n if (this.resolving.has(factory))\n throw new Error(\n `[mmstack/di]: Circular dependency detected in scope \"${scopeName ?? 'unknown'}\"${factoryName ? ` while resolving \"${factoryName}\"` : ''}`,\n );\n\n this.resolving.add(factory);\n try {\n const val = runInInjectionContext(this.injector, factory);\n this.registry.set(factory, val);\n return val;\n } finally {\n this.resolving.delete(factory);\n }\n }\n}\n\n/**\n * Creates a specialized dependency injection scope.\n *\n * This utility allows you to create a localized dependency injection scope where you can\n * register and provide shared state, services, or primitives that are bound to a specific\n * component tree instead of the global root injector.\n *\n * @param name Optional name for the scope, primarily used for debugging and error messages.\n * @returns A tuple containing `[registerFn, provideFn]`:\n * - `registerFn`: A function to register a factory within the scope. Returns an injection function to retrieve the value.\n * - `provideFn`: An Angular provider function that must be added to the `providers` array where the scope begins.\n *\n * @example\n * ```ts\n * const [registerInUserScope, provideUserScope] = createScope('UserScope');\n *\n * // Define a state/service bound to this scope\n * const injectUserState = registerInUserScope(() => signal({ name: 'John Doe' }));\n * const injectLogger = registerInUserScope(() => {\n * const globalLogger = inject(GlobalLogger);\n * const user = injectUserState();\n * return {\n * log: (msg: string) => globalLogger.log(`[USER MODULE (${user().name})]: ${msg}`),\n * }\n * })\n * @Component({\n * providers: [provideUserScope()] // provides a new instance of every dependency registered to the scope\n * })\n * class ParentComponent {}\n *\n * @Component({})\n * class ChildComponent {\n * readonly userState = injectUserState();\n * readonly logger = injectLogger();\n * }\n * ```\n */\nexport function createScope(name?: string) {\n const token = new InjectionToken<ScopeRegistry>(name ?? '@mmstack/di/scope');\n\n const provideFn = (): Provider => ({\n provide: token,\n useClass: ScopeRegistry,\n });\n\n const registerFn = <T>(factory: () => T, factoryName?: string) => {\n return () => {\n const registry = inject(token, { optional: true });\n if (!registry)\n throw new Error(\n `[mmstack/di]: Scope ${name ?? 'unknown'} not found. Please make sure you provide it`,\n );\n\n return registry.getOrCreate(factory, name, factoryName);\n };\n };\n\n return [registerFn, provideFn] as const;\n}\n","import {\n inject,\n Injector,\n runInInjectionContext,\n type HostAttributeToken,\n type InjectOptions,\n type ProviderToken,\n} from '@angular/core';\n\nconst UNINITIALIZED_SYMBOL = Symbol('@mmstack/di/inject-lazy/uninitialized');\n\ntype OptionalInjectOptions = Omit<InjectOptions, 'optional'> & {\n optional: true;\n};\n\ntype NonOptionalInjectOptions = Omit<InjectOptions, 'optional'> & {\n optional?: false;\n};\n\n/**\n * Defers the resolution and instantiation of a token until the returned getter function is called.\n * The resolved value is cached for subsequent calls.\n *\n * @param token The dependency token to inject.\n * @returns A getter function that returns the lazily resolved dependency.\n */\nexport function injectLazy<T>(token: ProviderToken<T>): () => T;\n\n/**\n * Defers the resolution and instantiation of a token until the returned getter function is called.\n * The resolved value is cached for subsequent calls.\n *\n * @param token The dependency token to inject.\n * @param options Injection options specifying optional resolution.\n * @returns A getter function that returns the lazily resolved dependency or null.\n */\nexport function injectLazy<T>(\n token: ProviderToken<T>,\n options: OptionalInjectOptions,\n): () => T | null;\n\n/**\n * Defers the resolution and instantiation of a token until the returned getter function is called.\n * The resolved value is cached for subsequent calls.\n *\n * @param token The dependency token to inject.\n * @param options Injection options specifying non-optional resolution.\n * @returns A getter function that returns the lazily resolved dependency.\n */\nexport function injectLazy<T>(\n token: ProviderToken<T>,\n options: NonOptionalInjectOptions,\n): () => T;\n\n/**\n * Defers the resolution and instantiation of a host attribute token until the returned getter function is called.\n * The resolved value is cached for subsequent calls.\n *\n * @param token The host attribute token to inject.\n * @returns A getter function that returns the lazily resolved attribute string.\n */\nexport function injectLazy(token: HostAttributeToken): () => string;\n\n/**\n * Defers the resolution and instantiation of a host attribute token until the returned getter function is called.\n * The resolved value is cached for subsequent calls.\n *\n * @param token The host attribute token to inject.\n * @param options Injection options specifying non-optional resolution.\n * @returns A getter function that returns the lazily resolved attribute string.\n */\nexport function injectLazy(\n token: HostAttributeToken,\n options?: {\n optional?: false;\n },\n): () => string;\n\n/**\n * Defers the resolution and instantiation of a host attribute token until the returned getter function is called.\n * The resolved value is cached for subsequent calls.\n *\n * @param token The host attribute token to inject.\n * @param options Injection options specifying optional resolution.\n * @returns A getter function that returns the lazily resolved attribute string or null.\n */\nexport function injectLazy(\n token: HostAttributeToken,\n options: {\n optional: true;\n },\n): () => string | null;\n\nexport function injectLazy<T>(\n token: ProviderToken<T> | HostAttributeToken,\n options?: Partial<OptionalInjectOptions | NonOptionalInjectOptions>,\n): () => T | string | null {\n const injector = inject(Injector);\n let instance: T | string | null | typeof UNINITIALIZED_SYMBOL =\n UNINITIALIZED_SYMBOL;\n\n return () => {\n if (instance === UNINITIALIZED_SYMBOL)\n instance = runInInjectionContext(injector, () => {\n return options\n ? inject(token as any, options as any)\n : inject(token as any);\n });\n\n return instance as T | string | null;\n };\n}\n","import {\n inject,\n InjectionToken,\n type AbstractType,\n type InjectOptions,\n type Provider,\n type Type,\n} from '@angular/core';\n\ntype ServiceType<T> =\n T extends Type<infer U>\n ? U\n : T extends AbstractType<infer U>\n ? U\n : T extends InjectionToken<infer U>\n ? U\n : never;\n\ntype MapDeps<T extends readonly any[]> = {\n [K in keyof T]: ServiceType<T[K]>;\n};\n\ntype ProviderFn<T> = {\n (value: T): Provider;\n <const TDeps extends any[]>(\n fn: (...deps: MapDeps<TDeps>) => T,\n deps: TDeps,\n ): Provider;\n};\n\ntype InjectFns<T> = [\n (opt?: Omit<InjectOptions, 'optional'>) => T,\n ProviderFn<T>,\n];\n\ntype FallbackInjectableOptions<T> = {\n /** Default value returned when the injectable is not provided */\n fallback: T;\n};\n\ntype LazyFallbackInjectableOptions<T> = {\n /** Function that returns a default value when the injectable is not provided. Useful for expensive defaults. */\n lazyFallback: () => T;\n};\n\ntype ErrorMessageInjectableOptions = {\n /** Error message thrown when the injectable is not provided */\n errorMessage: string;\n};\n\ntype InjectableOptions<T> =\n | FallbackInjectableOptions<T>\n | LazyFallbackInjectableOptions<T>\n | ErrorMessageInjectableOptions;\n\n/**\n * Creates a typed InjectionToken with inject and provide helper functions.\n *\n * @param token - Unique token identifier\n * @param opt - Optional configuration for fallback value or error message\n * @returns A tuple of [injectFn, provideFn] for type-safe dependency injection\n */\nexport function injectable<T>(token: string): InjectFns<T | null>;\n\n/**\n * Creates a typed InjectionToken with inject and provide helper functions.\n * Returns a fallback value when the injectable is not provided.\n *\n * @param token - Unique token identifier\n * @param opt - Configuration with fallback value\n * @returns A tuple of [injectFn, provideFn] for type-safe dependency injection\n */\nexport function injectable<T>(\n token: string,\n opt: FallbackInjectableOptions<T>,\n): InjectFns<T>;\n\n/**\n *\n * Creates a typed InjectionToken with inject and provide helper functions.\n * Returns a lazily evaluated fallback value when the injectable is not provided.\n *\n * @param token\n * @param opt\n */\nexport function injectable<T>(\n token: string,\n opt: LazyFallbackInjectableOptions<T>,\n): InjectFns<T>;\n\n/**\n * Creates a typed InjectionToken with inject and provide helper functions.\n * Throws an error with a custom message when the injectable is not provided.\n *\n * @param token - Unique token identifier\n * @param opt - Configuration with error message\n * @returns A tuple of [injectFn, provideFn] for type-safe dependency injection\n */\nexport function injectable<T>(\n token: string,\n opt: ErrorMessageInjectableOptions,\n): InjectFns<T>;\n\n/**\n * Creates an injectable with a baked-in factory.\n * @example const [injectUser, provideUser] = injectable(() => inject(HttpClient).get('/user'), 'UserToken');\n */\nexport function injectable<T>(fn: () => T, name?: string): InjectFns<T>;\n\nexport function injectable<T>(\n tokenOrFn: string | (() => T),\n optOrString?: InjectableOptions<T> | string,\n): InjectFns<T> {\n const token =\n typeof tokenOrFn === 'string'\n ? tokenOrFn\n : typeof optOrString === 'string'\n ? optOrString\n : '@mmstack/di/injectable';\n\n const opt =\n typeof tokenOrFn === 'function'\n ? { lazyFallback: tokenOrFn }\n : typeof optOrString === 'string'\n ? undefined\n : optOrString;\n\n const injectionToken = new InjectionToken<T>(token);\n\n const options = opt as\n | Partial<\n FallbackInjectableOptions<T> &\n LazyFallbackInjectableOptions<T> &\n ErrorMessageInjectableOptions\n >\n | undefined;\n\n let fallback: T | undefined | null = options?.fallback;\n\n const initFallback =\n options?.lazyFallback ?? (() => options?.fallback ?? null);\n\n const fallbackFn = () => {\n if (fallback === undefined) fallback = initFallback();\n return fallback;\n };\n\n const injectFn = (iOpt?: Omit<InjectOptions, 'optional'>) => {\n const injected =\n inject(injectionToken, {\n ...iOpt,\n optional: true,\n }) ?? fallbackFn();\n\n if (injected === null && options?.errorMessage)\n throw new Error(options.errorMessage);\n\n return injected as T;\n };\n\n const provideFn = (\n fnOrValue: T | ((...deps: any[]) => T),\n deps?: any[],\n ): Provider => {\n if (deps !== undefined)\n return {\n provide: injectionToken,\n useFactory: fnOrValue as (...args: any[]) => T,\n deps,\n };\n\n return {\n provide: injectionToken,\n useValue: fnOrValue,\n };\n };\n\n return [injectFn, provideFn];\n}\n","import { inject, InjectionToken, Injector } from '@angular/core';\n\n/**\n * Creates a root-level singleton hooked into the global injector.\n * @example const injectUser = rootInjectable(() => ({ name: signal('John') }));\n */\nexport function rootInjectable<T>(\n factory: (injector: Injector) => T, // Keeping the injector just in case\n name?: string,\n): () => T {\n const token = new InjectionToken<T>(name ?? '@mmstack/di/root-injectable', {\n providedIn: 'root',\n factory: () => factory(inject(Injector)),\n });\n\n return () => inject(token);\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;AAEA;;;;;;;;AAQG;AAEG,SAAU,2BAA2B,CAAC,QAAmB,EAAA;AAC7D,IAAA,IAAI,CAAC,QAAQ;AAAE,QAAA,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;IAE1C,OAAO,CAAI,EAAW,KAAK,qBAAqB,CAAC,QAAQ,EAAE,EAAE,CAAC;AAChE;;ACPA,MACM,aAAa,CAAA;AACA,IAAA,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;;AAE3B,IAAA,QAAQ,GAAG,IAAI,GAAG,EAAiB;;AAEnC,IAAA,SAAS,GAAG,IAAI,GAAG,EAAY;AAEhD,IAAA,WAAW,CAAI,OAAgB,EAAE,SAAkB,EAAE,WAAoB,EAAA;AACvE,Q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,KAAA,CAAC;AAEF,IAAA,OAAO,MAAM,MAAM,CAAC,KAAK,CAAC;AAC5B;;AChBA;;AAEG;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mmstack/di",
3
- "version": "21.0.4",
3
+ "version": "21.0.5",
4
4
  "keywords": [
5
5
  "angular",
6
6
  "dependency injection",
@@ -48,7 +48,7 @@ declare function createRunInInjectionContext(injector?: Injector): <T>(fn: () =>
48
48
  * }
49
49
  * ```
50
50
  */
51
- declare function createScope(name?: string): readonly [<T>(factory: () => T) => () => T, () => Provider];
51
+ declare function createScope(name?: string): readonly [<T>(factory: () => T, factoryName?: string) => () => T, () => Provider];
52
52
 
53
53
  type OptionalInjectOptions = Omit<InjectOptions, 'optional'> & {
54
54
  optional: true;
@@ -179,7 +179,7 @@ declare function injectable<T>(token: string, opt: ErrorMessageInjectableOptions
179
179
  declare function injectable<T>(fn: () => T, name?: string): InjectFns<T>;
180
180
 
181
181
  /**
182
- * Creates a tree-shakable root singleton without a class.
182
+ * Creates a root-level singleton hooked into the global injector.
183
183
  * @example const injectUser = rootInjectable(() => ({ name: signal('John') }));
184
184
  */
185
185
  declare function rootInjectable<T>(factory: (injector: Injector) => T, // Keeping the injector just in case