@tstdl/base 0.85.14 → 0.85.16

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 (42) hide show
  1. package/data-structures/array-list.d.ts +1 -1
  2. package/data-structures/array-list.js +4 -4
  3. package/data-structures/circular-buffer.js +5 -2
  4. package/data-structures/distinct-collection.d.ts +24 -0
  5. package/data-structures/distinct-collection.js +77 -0
  6. package/data-structures/index.d.ts +2 -1
  7. package/data-structures/index.js +2 -1
  8. package/data-structures/list.js +13 -5
  9. package/data-structures/multi-key-map.d.ts +1 -1
  10. package/data-structures/multi-key-map.js +2 -0
  11. package/data-structures/multi-key-set.d.ts +19 -0
  12. package/data-structures/multi-key-set.js +81 -0
  13. package/data-structures/{set.d.ts → set-collection.d.ts} +2 -2
  14. package/data-structures/{set.js → set-collection.js} +6 -6
  15. package/data-structures/sorted-array-list.d.ts +1 -1
  16. package/data-structures/sorted-array-list.js +1 -4
  17. package/data-structures/weak-ref-map.js +4 -1
  18. package/injector/decorators.d.ts +70 -0
  19. package/injector/decorators.js +112 -0
  20. package/injector/inject.d.ts +80 -0
  21. package/injector/inject.js +94 -0
  22. package/injector/injector.d.ts +111 -0
  23. package/injector/injector.js +454 -0
  24. package/injector/interfaces.d.ts +14 -0
  25. package/injector/interfaces.js +26 -0
  26. package/injector/provider.d.ts +44 -0
  27. package/injector/provider.js +64 -0
  28. package/injector/resolve-chain.d.ts +31 -0
  29. package/injector/resolve-chain.js +113 -0
  30. package/injector/resolve.error.d.ts +5 -0
  31. package/injector/resolve.error.js +36 -0
  32. package/injector/symbols.d.ts +2 -0
  33. package/injector/symbols.js +26 -0
  34. package/injector/token.d.ts +18 -0
  35. package/injector/token.js +41 -0
  36. package/injector/type-info.d.ts +26 -0
  37. package/injector/type-info.js +16 -0
  38. package/injector/types.d.ts +43 -0
  39. package/injector/types.js +16 -0
  40. package/package.json +9 -9
  41. package/pool/pool.js +2 -2
  42. package/utils/type-guards.js +2 -2
@@ -0,0 +1,80 @@
1
+ import { Injector } from './injector.js';
2
+ import type { Resolvable, ResolveArgument } from './interfaces.js';
3
+ import type { InjectionToken } from './token.js';
4
+ import type { ResolveOptions } from './types.js';
5
+ export type InjectOptions = ResolveOptions & {
6
+ /** if defined, resolve the ForwardRefToken using ForwardRef strategy instead resolving the token */
7
+ forwardRef?: boolean;
8
+ };
9
+ export type InjectArgumentOptions = {
10
+ optional?: boolean;
11
+ };
12
+ export type InjectionContext = {
13
+ argument: unknown;
14
+ inject<T, A>(token: InjectionToken<T, A>, argument?: ResolveArgument<T, A>, options?: InjectOptions): T;
15
+ injectAll<T, A>(token: InjectionToken<T, A>, argument?: ResolveArgument<T, A>, options?: InjectOptions): T[];
16
+ injectAsync<T, A>(token: InjectionToken<T, A>, argument?: ResolveArgument<T, A>, options?: InjectOptions): Promise<T>;
17
+ injectAllAsync<T, A>(token: InjectionToken<T, A>, argument?: ResolveArgument<T, A>, options?: InjectOptions): Promise<T[]>;
18
+ };
19
+ /**
20
+ * Resolves a token using the {@link Injector} of the current injection context
21
+ *
22
+ * @param token token to resolve
23
+ * @param argument argument to resolve token with
24
+ * @param options resolve options
25
+ */
26
+ export declare function inject<T = unknown, A = unknown>(token: InjectionToken<T, A>, argument: ResolveArgument<T, A>, options: InjectOptions & {
27
+ optional: true;
28
+ }): T | undefined;
29
+ export declare function inject<T = unknown, A = unknown>(token: InjectionToken<T, A>, argument?: ResolveArgument<T, A>, options?: InjectOptions): T;
30
+ /**
31
+ * Resolves a token using the {@link Injector} of the current injection context
32
+ *
33
+ * @param token token to resolve
34
+ * @param argument argument to resolve token with
35
+ * @param options resolve options
36
+ */
37
+ export declare function injectAsync<T = unknown, A = unknown>(token: InjectionToken<T, A>, argument: ResolveArgument<T, A>, options: InjectOptions & {
38
+ optional: true;
39
+ }): Promise<T | undefined>;
40
+ export declare function injectAsync<T = unknown, A = unknown>(token: InjectionToken<T, A>, argument?: ResolveArgument<T, A>, options?: InjectOptions): Promise<T>;
41
+ /**
42
+ * Resolves a token using the {@link Injector} of the current injection context
43
+ *
44
+ * @param token token to resolve
45
+ * @param argument argument to resolve token with
46
+ * @param options resolve options
47
+ */
48
+ export declare function injectAll<T, A>(token: InjectionToken<T, A>, argument?: ResolveArgument<T, A>, options?: InjectOptions): T[];
49
+ /**
50
+ * Resolves a token using the {@link Injector} of the current injection context
51
+ *
52
+ * @param token token to resolve
53
+ * @param argument argument to resolve token with
54
+ * @param options resolve options
55
+ */
56
+ export declare function injectAllAsync<T, A>(token: InjectionToken<T, A>, argument?: ResolveArgument<T, A>, options?: InjectOptions): Promise<T[]>;
57
+ /**
58
+ * Injects the resolve argument from the current resolution
59
+ * @param _this can be used for type infer by simply passing `this`. Requires class to implement {@link Resolvable}.
60
+ * @returns
61
+ */
62
+ export declare function injectArgument<T, R>(_this?: Resolvable<T>, options?: InjectArgumentOptions & {
63
+ optional: R;
64
+ }): T | (R extends true ? undefined : never);
65
+ export declare function getCurrentInjectionContext(): InjectionContext | null;
66
+ export declare function setCurrentInjectionContext(context: InjectionContext | null): InjectionContext | null;
67
+ /**
68
+ * Runs the given function in the context of the given {@link InjectionContext}.
69
+ *
70
+ * @param context the injection context which will satisfy calls to {@link inject} while `fn` is executing
71
+ * @param fn the closure to be run in the context of `injector`
72
+ * @returns the return value of the function, if any
73
+ */
74
+ export declare function runInInjectionContext<ReturnT>(injectorOrContext: Injector | InjectionContext, fn: () => ReturnT): ReturnT;
75
+ /**
76
+ * Asserts that the current stack frame is within an injection context and has access to {@link inject}.
77
+ *
78
+ * @param debugFn a reference to the function making the assertion (used for the error message).
79
+ */
80
+ export declare function assertInInjectionContext(debugFn: Function): void;
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var inject_exports = {};
20
+ __export(inject_exports, {
21
+ assertInInjectionContext: () => assertInInjectionContext,
22
+ getCurrentInjectionContext: () => getCurrentInjectionContext,
23
+ inject: () => inject,
24
+ injectAll: () => injectAll,
25
+ injectAllAsync: () => injectAllAsync,
26
+ injectArgument: () => injectArgument,
27
+ injectAsync: () => injectAsync,
28
+ runInInjectionContext: () => runInInjectionContext,
29
+ setCurrentInjectionContext: () => setCurrentInjectionContext
30
+ });
31
+ module.exports = __toCommonJS(inject_exports);
32
+ var import_type_guards = require("../utils/type-guards.js");
33
+ var import_injector = require("./injector.js");
34
+ let currentInjectionContext = null;
35
+ function inject(token, argument, options) {
36
+ assertInInjectionContext(inject);
37
+ return currentInjectionContext.inject(token, argument, options);
38
+ }
39
+ async function injectAsync(token, argument, options) {
40
+ assertInInjectionContext(inject);
41
+ return currentInjectionContext.injectAsync(token, argument, options);
42
+ }
43
+ function injectAll(token, argument, options) {
44
+ assertInInjectionContext(injectAll);
45
+ return currentInjectionContext.injectAll(token, argument, options);
46
+ }
47
+ async function injectAllAsync(token, argument, options) {
48
+ assertInInjectionContext(injectAll);
49
+ return currentInjectionContext.injectAllAsync(token, argument, options);
50
+ }
51
+ function injectArgument(_this, options) {
52
+ assertInInjectionContext(injectArgument);
53
+ const argument = currentInjectionContext.argument;
54
+ if (options?.optional != true) {
55
+ (0, import_type_guards.assertDefined)(argument, "No resolve argument available in current injection context.");
56
+ }
57
+ return argument;
58
+ }
59
+ function getCurrentInjectionContext() {
60
+ return currentInjectionContext;
61
+ }
62
+ function setCurrentInjectionContext(context) {
63
+ const previous = currentInjectionContext;
64
+ currentInjectionContext = context;
65
+ return previous;
66
+ }
67
+ function runInInjectionContext(injectorOrContext, fn) {
68
+ const context = injectorOrContext instanceof import_injector.Injector ? {
69
+ argument: void 0,
70
+ inject(token, argument, options) {
71
+ return injectorOrContext.resolve(token, argument, options);
72
+ },
73
+ injectAll(token, argument, options) {
74
+ return injectorOrContext.resolveAll(token, argument, options);
75
+ },
76
+ async injectAsync(token, argument, options) {
77
+ return injectorOrContext.resolveAsync(token, argument, options);
78
+ },
79
+ async injectAllAsync(token, argument, options) {
80
+ return injectorOrContext.resolveAllAsync(token, argument, options);
81
+ }
82
+ } : injectorOrContext;
83
+ const previousContext = setCurrentInjectionContext(context);
84
+ try {
85
+ return fn();
86
+ } finally {
87
+ setCurrentInjectionContext(previousContext);
88
+ }
89
+ }
90
+ function assertInInjectionContext(debugFn) {
91
+ if ((0, import_type_guards.isNull)(currentInjectionContext)) {
92
+ throw new Error(`${debugFn.name}() can only be used within an injection context such as a constructor, a factory function, a field initializer, or a function used with \`runInInjectionContext\``);
93
+ }
94
+ }
@@ -0,0 +1,111 @@
1
+ import { CircularBuffer } from '../data-structures/circular-buffer.js';
2
+ import { MultiKeyMap } from '../data-structures/multi-key-map.js';
3
+ import { DeferredPromise } from '../promise/deferred-promise.js';
4
+ import type { TypedOmit } from '../types.js';
5
+ import { ForwardRef } from '../utils/object/forward-ref.js';
6
+ import type { ResolveArgument } from './interfaces.js';
7
+ import { type Provider } from './provider.js';
8
+ import { ResolveChain } from './resolve-chain.js';
9
+ import { type InjectionToken } from './token.js';
10
+ import type { RegistrationOptions, ResolveOptions } from './types.js';
11
+ type InternalResolveContext = {
12
+ resolves: number;
13
+ /** Currently resolving tokens (for circular dependency detection) */
14
+ resolving: Set<InjectionToken>;
15
+ /** resolutions for resolution scoped dependencies */
16
+ resolutionScopedResolutions: MultiKeyMap<[InjectionToken, unknown], Resolution<any, any>>;
17
+ /** all resolutions in this chain for postprocessing */
18
+ resolutions: Resolution<any, any>[];
19
+ forwardRefQueue: CircularBuffer<(() => void | Promise<void>)>;
20
+ forwardRefs: Set<ForwardRef>;
21
+ $done: DeferredPromise;
22
+ };
23
+ type Resolution<T, A> = {
24
+ registration: Registration<T>;
25
+ value: T;
26
+ argument: ResolveArgument<T, A>;
27
+ chain: ResolveChain;
28
+ };
29
+ export type GlobalRegistration<T = any, A = any> = {
30
+ token: InjectionToken<T, A>;
31
+ provider: Provider<T, A>;
32
+ options: RegistrationOptions<T, A>;
33
+ };
34
+ export type Registration<T = any, A = any> = GlobalRegistration<T, A> & {
35
+ resolutions: Map<any, T>;
36
+ };
37
+ export type GetRegistrationOptions = {
38
+ skipSelf?: boolean;
39
+ onlySelf?: boolean;
40
+ };
41
+ export declare class Injector {
42
+ #private;
43
+ readonly name: string;
44
+ constructor(name: string, parent?: Injector | null);
45
+ /**
46
+ * Register a provider for a token
47
+ * @param token token to register
48
+ * @param provider provider used to resolve the token
49
+ * @param options registration options
50
+ */
51
+ static registerGlobal<T, A = any>(token: InjectionToken<T, A>, provider: Provider<T, A>, options?: RegistrationOptions<T, A>): void;
52
+ /**
53
+ * Register a provider for a token as a singleton. Alias for {@link register} with `singleton` lifecycle
54
+ * @param token token to register
55
+ * @param provider provider used to resolve the token
56
+ * @param options registration options
57
+ */
58
+ static registerGlobalSingleton<T, A = any>(token: InjectionToken<T, A>, provider: Provider<T, A>, options?: TypedOmit<RegistrationOptions<T, A>, 'lifecycle'>): void;
59
+ fork(name: string): Injector;
60
+ /**
61
+ * Register a provider for a token
62
+ * @param token token to register
63
+ * @param provider provider used to resolve the token
64
+ * @param options registration options
65
+ */
66
+ register<T, A = any>(token: InjectionToken<T, A>, provider: Provider<T, A>, options?: RegistrationOptions<T, A>): void;
67
+ /**
68
+ * Register a provider for a token as a singleton. Alias for {@link register} with `singleton` lifecycle
69
+ * @param token token to register
70
+ * @param provider provider used to resolve the token
71
+ * @param options registration options
72
+ */
73
+ registerSingleton<T, A = any>(token: InjectionToken<T, A>, provider: Provider<T, A>, options?: TypedOmit<RegistrationOptions<T, A>, 'lifecycle'>): void;
74
+ /**
75
+ * Check if token has a registered provider
76
+ * @param token token check
77
+ */
78
+ hasRegistration(token: InjectionToken, options?: GetRegistrationOptions): boolean;
79
+ /**
80
+ * Try to get registration
81
+ * @param token token to get registration for
82
+ */
83
+ tryGetRegistration<T, A>(token: InjectionToken<T, A>, options?: GetRegistrationOptions): Registration<T, A> | Registration<T, A>[] | undefined;
84
+ /**
85
+ * Get registration
86
+ * @param token token to get registration for
87
+ */
88
+ getRegistration<T, A>(token: InjectionToken<T, A>, options?: GetRegistrationOptions): Registration<T, A> | Registration<T, A>[];
89
+ resolve<T, A>(token: InjectionToken<T, A>, argument: ResolveArgument<T, A>, options: ResolveOptions & {
90
+ optional: true;
91
+ }): T | undefined;
92
+ resolve<T, A>(token: InjectionToken<T, A>, argument?: ResolveArgument<T, A>, options?: ResolveOptions): T;
93
+ resolveAll<T, A>(token: InjectionToken<T, A>, argument?: ResolveArgument<T, A>, options?: ResolveOptions): T[];
94
+ resolveAsync<T, A>(token: InjectionToken<T, A>, argument: ResolveArgument<T, A>, options: ResolveOptions & {
95
+ optional: true;
96
+ }): Promise<T | undefined>;
97
+ resolveAsync<T, A>(token: InjectionToken<T, A>, argument?: ResolveArgument<T, A>, options?: ResolveOptions): Promise<T>;
98
+ resolveAllAsync<T, A>(token: InjectionToken<T, A>, argument?: ResolveArgument<T, A>, options?: ResolveOptions): Promise<T[]>;
99
+ _resolve<T, A>(token: InjectionToken<T, A>, argument: ResolveArgument<T, A>, options: ResolveOptions, context: InternalResolveContext, chain: ResolveChain): T | undefined;
100
+ _resolveAll<T, A>(token: InjectionToken<T, A>, argument: ResolveArgument<T, A>, options: ResolveOptions, context: InternalResolveContext, chain: ResolveChain): T[];
101
+ _resolveRegistration<T, A>(registration: Registration<T, A>, argument: ResolveArgument<T, A>, options: ResolveOptions, context: InternalResolveContext, chain: ResolveChain): T;
102
+ _resolveProvider<T, A>(registration: Registration<T, A>, resolveArgument: ResolveArgument<T, A>, options: ResolveOptions, context: InternalResolveContext, chain: ResolveChain): T;
103
+ private resolveClassInjection;
104
+ private resolveInjection;
105
+ private resolveInjectionAsync;
106
+ private resolveInjectionAll;
107
+ private resolveInjectionAllAsync;
108
+ private getResolveContext;
109
+ private getInjectionContext;
110
+ }
111
+ export {};