injeca 0.1.8 → 0.3.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/README.md +359 -32
- package/dist/decision-odiPb2Au.mjs +194 -0
- package/dist/decision-odiPb2Au.mjs.map +1 -0
- package/dist/global-B9Bk2EG2.d.mts +153 -0
- package/dist/global-C0Nk0pU8.mjs +3790 -0
- package/dist/global-C0Nk0pU8.mjs.map +1 -0
- package/dist/global.d.mts +2 -2
- package/dist/global.mjs +1 -3
- package/dist/index-DXguNx2M.d.mts +643 -0
- package/dist/index.d.mts +174 -2
- package/dist/index.mjs +210 -2
- package/dist/index.mjs.map +1 -0
- package/dist/testing.d.mts +31 -0
- package/dist/testing.mjs +45 -0
- package/dist/testing.mjs.map +1 -0
- package/package.json +12 -25
- package/dist/builtin-BPDNf8Z-.mjs +0 -25
- package/dist/builtin-BPDNf8Z-.mjs.map +0 -1
- package/dist/global-BLY6_SI_.d.mts +0 -130
- package/dist/src-UXkTj4Np.mjs +0 -294
- package/dist/src-UXkTj4Np.mjs.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decision-odiPb2Au.mjs","names":[],"sources":["../src/diagnostics/errors.ts","../src/primitives/decision.ts"],"sourcesContent":["import type { ServiceExplanation } from '../types/service'\n\nexport type InjecaErrorCode\n = | 'build-after-dispose'\n | 'duplicate-service'\n | 'hook-outside-build'\n | 'hook-timeout'\n | 'invalid-argument'\n | 'invalid-decision'\n | 'reentrant-dispose'\n | 'replace-while-stopping'\n | 'stale-registration'\n | 'start-timeout'\n | 'transaction-incomplete'\n | 'wait-timeout'\n\nexport interface InjecaErrorOptions {\n cause?: unknown\n}\n\n/**\n * The base class of all injeca errors. Use `code` to identify an error. The message text is not stable.\n */\nexport class InjecaError extends Error {\n readonly code: InjecaErrorCode\n\n constructor(code: InjecaErrorCode, message: string, options?: InjecaErrorOptions) {\n super(message, options)\n this.name = 'InjecaError'\n this.code = code\n }\n}\n\n/** A started container already has a current registration for the key. */\nexport class DuplicateServiceError extends InjecaError {\n declare readonly code: 'duplicate-service'\n\n constructor(message: string, options?: InjecaErrorOptions) {\n super('duplicate-service', message, options)\n this.name = 'DuplicateServiceError'\n }\n}\n\n/** A registration is no longer the current registration of its service. */\nexport class StaleRegistrationError extends InjecaError {\n declare readonly code: 'stale-registration'\n\n constructor(message: string, options?: InjecaErrorOptions) {\n super('stale-registration', message, options)\n this.name = 'StaleRegistrationError'\n }\n}\n\n/** A hook returned a decision from another run, or a kind that the hook does not accept. */\nexport class InvalidDecisionError extends InjecaError {\n declare readonly code: 'invalid-decision'\n\n constructor(message: string, options?: InjecaErrorOptions) {\n super('invalid-decision', message, options)\n this.name = 'InvalidDecisionError'\n }\n}\n\n/** A caller used a container or an instance after its disposal started. */\nexport class BuildAfterDisposeError extends InjecaError {\n declare readonly code: 'build-after-dispose'\n\n constructor(message: string, options?: InjecaErrorOptions) {\n super('build-after-dispose', message, options)\n this.name = 'BuildAfterDisposeError'\n }\n}\n\n/** `on` was called after `build` settled. */\nexport class HookOutsideBuildError extends InjecaError {\n declare readonly code: 'hook-outside-build'\n\n constructor(message: string, options?: InjecaErrorOptions) {\n super('hook-outside-build', message, options)\n this.name = 'HookOutsideBuildError'\n }\n}\n\n/** A transaction commit found a declared service without `provide`. */\nexport class TransactionIncompleteError extends InjecaError {\n declare readonly code: 'transaction-incomplete'\n\n constructor(message: string, options?: InjecaErrorOptions) {\n super('transaction-incomplete', message, options)\n this.name = 'TransactionIncompleteError'\n }\n}\n\n/** `dispose()` was called from the cleanup chain of the same container. */\nexport class ReentrantDisposeError extends InjecaError {\n declare readonly code: 'reentrant-dispose'\n\n constructor(message: string, options?: InjecaErrorOptions) {\n super('reentrant-dispose', message, options)\n this.name = 'ReentrantDisposeError'\n }\n}\n\n/** A `start` or `stop` hook did not settle before its configured timeout. */\nexport class HookTimeoutError extends InjecaError {\n declare readonly code: 'hook-timeout'\n readonly hook: 'start' | 'stop'\n readonly timeoutMs: number\n\n constructor(message: string, hook: 'start' | 'stop', timeoutMs: number, options?: InjecaErrorOptions) {\n super('hook-timeout', message, options)\n this.name = 'HookTimeoutError'\n this.hook = hook\n this.timeoutMs = timeoutMs\n }\n}\n\n/** `replace` was called on a service in `stopping`. */\nexport class ReplaceWhileStoppingError extends InjecaError {\n declare readonly code: 'replace-while-stopping'\n\n constructor(message: string, options?: InjecaErrorOptions) {\n super('replace-while-stopping', message, options)\n this.name = 'ReplaceWhileStoppingError'\n }\n}\n\n/** `start()` did not reach `running` for every service before its configured timeout. */\nexport class StartTimeoutError extends InjecaError {\n declare readonly code: 'start-timeout'\n /** One entry for each service or invocation that is not `running`. */\n readonly explanations: readonly ServiceExplanation[]\n\n constructor(message: string, explanations: readonly ServiceExplanation[], options?: InjecaErrorOptions) {\n super('start-timeout', message, options)\n this.name = 'StartTimeoutError'\n this.explanations = explanations\n }\n}\n\n/** `waitFor()` did not match before its configured timeout. */\nexport class WaitTimeoutError extends InjecaError {\n declare readonly code: 'wait-timeout'\n\n constructor(message: string, options?: InjecaErrorOptions) {\n super('wait-timeout', message, options)\n this.name = 'WaitTimeoutError'\n }\n}\n\n/** A caller passed a value with the wrong shape, or reading a caller-owned property failed. */\nexport class InvalidArgumentError extends InjecaError {\n declare readonly code: 'invalid-argument'\n\n constructor(message: string, options?: InjecaErrorOptions) {\n super('invalid-argument', message, options)\n this.name = 'InvalidArgumentError'\n }\n}\n","import { InvalidDecisionError } from '../diagnostics/errors'\n\n/** The hooks that `on` registers in `build` and in an invocation callback. */\nexport type ServiceHook = 'start' | 'stop' | 'dependency-change' | 'dependency-status-change'\n\n/** The hooks that run when a dependency changes. Their context has `dependsOn` and `changes`. */\nexport type DependencyHook = 'dependency-change' | 'dependency-status-change'\n\nexport interface DecisionOptions {\n readonly reason?: string\n}\n\nexport interface ExponentialBackoff {\n readonly kind: 'exponential'\n readonly initial: number\n readonly max: number\n readonly jitter?: number\n}\n\nexport interface RequeueOptions extends DecisionOptions {\n /** A fixed delay in milliseconds. */\n readonly after?: number\n readonly backoff?: ExponentialBackoff\n readonly maxAttempts?: number\n}\n\n/** The fields that injeca fills from the hook context. */\nexport interface DecisionSource {\n /** The name of the service, or of the invocation. */\n readonly service: string\n readonly instanceId: string\n readonly hook: ServiceHook\n readonly attempt: number\n /** The time of the container clock when `decide` created the decision. */\n readonly issuedAt: number\n}\n\nexport interface HandledDecision extends DecisionSource, DecisionOptions {\n readonly kind: 'handled'\n}\n\nexport interface RebuildDecision extends DecisionSource, DecisionOptions {\n readonly kind: 'rebuild'\n}\n\nexport interface RequeueDecision extends DecisionSource, RequeueOptions {\n readonly kind: 'requeue'\n}\n\nexport interface StopDecision extends DecisionSource, DecisionOptions {\n readonly kind: 'stop'\n}\n\nexport interface FailDecision extends DecisionSource, DecisionOptions {\n readonly kind: 'fail'\n readonly error: unknown\n}\n\n/**\n * The value that a hook returns to tell injeca what to do next. Only `decide` in the hook context creates it.\n */\nexport type Decision = HandledDecision | RebuildDecision | RequeueDecision | StopDecision | FailDecision\n\nexport type DecisionKind = Decision['kind']\n\n/** The decision kinds that a hook accepts. */\nexport type HookDecisionKind<H extends ServiceHook>\n = H extends 'start'\n ? 'handled' | 'requeue' | 'stop' | 'fail'\n : H extends 'stop'\n ? 'handled' | 'fail'\n : DecisionKind\n\n/** The decisions that a hook accepts. */\nexport type DecisionFor<H extends ServiceHook> = Extract<Decision, { readonly kind: HookDecisionKind<H> }>\n\n/**\n * The value that a hook handler can return.\n *\n * A decision tells injeca what to do next. Any other value, for example the result of `app.listen()`, selects the default of the hook.\n * So the shorthand `on('start', () => app.listen(port))` type-checks.\n * The `decide` of each hook has only the methods that the hook accepts. A decision from another run fails at run time with `invalid-decision`.\n */\nexport type HookResult<_H extends ServiceHook = ServiceHook> = unknown\n\n/** All decision constructors. A hook context has only the constructors that its hook accepts. */\nexport interface Decide {\n readonly handled: (options?: DecisionOptions) => HandledDecision\n readonly rebuild: (options?: DecisionOptions) => RebuildDecision\n readonly requeue: (options?: RequeueOptions) => RequeueDecision\n readonly stop: (options?: DecisionOptions) => StopDecision\n readonly fail: (error: unknown, options?: DecisionOptions) => FailDecision\n}\n\n/** The `decide` object of a hook context. A method that the hook does not accept is a type error. */\nexport type DecideFor<H extends ServiceHook> = Pick<Decide, HookDecisionKind<H>>\n\n/**\n * One run of one hook handler. Each decision is bound to the run that created it.\n *\n * @internal\n */\nexport interface HookRun {\n readonly service: string\n readonly instanceId: string\n readonly hook: ServiceHook\n readonly attempt: number\n readonly now: () => number\n}\n\nconst issued = new WeakMap<object, HookRun>()\n\nfunction reasonFrom(options: DecisionOptions | undefined): { reason?: string } {\n return options?.reason === undefined ? {} : { reason: options.reason }\n}\n\n/** Creates the `decide` object of one run. */\nexport function createDecide(run: HookRun): Decide {\n function issue<T extends Decision>(fields: Omit<T, keyof DecisionSource>): T {\n const decision = Object.freeze({\n ...fields,\n service: run.service,\n instanceId: run.instanceId,\n hook: run.hook,\n attempt: run.attempt,\n issuedAt: run.now(),\n }) as T\n issued.set(decision, run)\n return decision\n }\n\n return Object.freeze({\n handled: (options?: DecisionOptions) => issue<HandledDecision>({ kind: 'handled', ...reasonFrom(options) }),\n rebuild: (options?: DecisionOptions) => issue<RebuildDecision>({ kind: 'rebuild', ...reasonFrom(options) }),\n requeue: (options?: RequeueOptions) => issue<RequeueDecision>({\n kind: 'requeue',\n ...reasonFrom(options),\n ...(options?.after === undefined ? {} : { after: options.after }),\n ...(options?.backoff === undefined ? {} : { backoff: Object.freeze({ ...options.backoff }) }),\n ...(options?.maxAttempts === undefined ? {} : { maxAttempts: options.maxAttempts }),\n }),\n stop: (options?: DecisionOptions) => issue<StopDecision>({ kind: 'stop', ...reasonFrom(options) }),\n fail: (error: unknown, options?: DecisionOptions) => issue<FailDecision>({ kind: 'fail', error, ...reasonFrom(options) }),\n })\n}\n\nconst validKinds: Readonly<Record<ServiceHook, ReadonlySet<DecisionKind>>> = {\n 'start': new Set(['handled', 'requeue', 'stop', 'fail']),\n 'stop': new Set(['handled', 'fail']),\n 'dependency-change': new Set(['handled', 'rebuild', 'requeue', 'stop', 'fail']),\n 'dependency-status-change': new Set(['handled', 'rebuild', 'requeue', 'stop', 'fail']),\n}\n\n/** The decision for `undefined`. A `dependency-change` handler without a decision gets the default: rebuild. */\nexport function defaultDecision(run: HookRun): Decision {\n const decide = createDecide(run)\n return run.hook === 'dependency-change' ? decide.rebuild() : decide.handled()\n}\n\n/**\n * Checks the value that a hook handler returned. It gives `undefined` for a value that `decide` did not create, so that the caller applies the default.\n * This keeps a shorthand such as `on('start', () => app.listen(port))` valid.\n *\n * @throws {InvalidDecisionError} when another run created the decision, or when the hook does not accept its kind.\n */\nexport function validateDecision(run: HookRun, value: unknown): Decision | undefined {\n const owner = typeof value === 'object' && value !== null ? issued.get(value) : undefined\n if (!owner)\n return undefined\n if (owner !== run)\n throw new InvalidDecisionError(`The '${run.hook}' hook of '${run.service}' returned a decision from another run: the '${owner.hook}' hook of '${owner.service}', instance '${owner.instanceId}', attempt ${owner.attempt}.`)\n\n const decision = value as Decision\n if (!validKinds[run.hook].has(decision.kind))\n throw new InvalidDecisionError(`The '${run.hook}' hook cannot return a '${decision.kind}' decision.`)\n\n return decision\n}\n"],"mappings":";;;;AAuBA,IAAa,cAAb,cAAiC,MAAM;CACrC;CAEA,YAAY,MAAuB,SAAiB,SAA8B;EAChF,MAAM,SAAS,OAAO;EACtB,KAAK,OAAO;EACZ,KAAK,OAAO;CACd;AACF;;AAGA,IAAa,wBAAb,cAA2C,YAAY;CAGrD,YAAY,SAAiB,SAA8B;EACzD,MAAM,qBAAqB,SAAS,OAAO;EAC3C,KAAK,OAAO;CACd;AACF;;AAGA,IAAa,yBAAb,cAA4C,YAAY;CAGtD,YAAY,SAAiB,SAA8B;EACzD,MAAM,sBAAsB,SAAS,OAAO;EAC5C,KAAK,OAAO;CACd;AACF;;AAGA,IAAa,uBAAb,cAA0C,YAAY;CAGpD,YAAY,SAAiB,SAA8B;EACzD,MAAM,oBAAoB,S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B,IAAI,IAAI;EAAC;EAAW;EAAW;EAAW;EAAQ;CAAM,CAAC;CAC9E,4CAA4B,IAAI,IAAI;EAAC;EAAW;EAAW;EAAW;EAAQ;CAAM,CAAC;AACvF;;AAGA,SAAgB,gBAAgB,KAAwB;CACtD,MAAM,SAAS,aAAa,GAAG;CAC/B,OAAO,IAAI,SAAS,sBAAsB,OAAO,QAAQ,IAAI,OAAO,QAAQ;AAC9E;;;;;;;AAQA,SAAgB,iBAAiB,KAAc,OAAsC;CACnF,MAAM,QAAQ,OAAO,UAAU,YAAY,UAAU,OAAO,OAAO,IAAI,KAAK,IAAI,KAAA;CAChF,IAAI,CAAC,OACH,OAAO,KAAA;CACT,IAAI,UAAU,KACZ,MAAM,IAAI,qBAAqB,QAAQ,IAAI,KAAK,aAAa,IAAI,QAAQ,+CAA+C,MAAM,KAAK,aAAa,MAAM,QAAQ,eAAe,MAAM,WAAW,aAAa,MAAM,QAAQ,EAAE;CAE7N,MAAM,WAAW;CACjB,IAAI,CAAC,WAAW,IAAI,KAAK,CAAC,IAAI,SAAS,IAAI,GACzC,MAAM,IAAI,qBAAqB,QAAQ,IAAI,KAAK,0BAA0B,SAAS,KAAK,YAAY;CAEtG,OAAO;AACT"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { F as Container, Ft as Logger, I as ContainerOptions, O as EffectSetup, a as InvocationHandle, at as ConditionalDependency, ct as DependencyMap, d as ProvideOption, dt as KeyOptions, g as Registration, gt as ProvidedKey, h as ProvideOptionWithKeys, ht as ProvidedBy, lt as FallbackDependency, n as Transaction, o as InvokeOption, pt as OptionalDependency, st as DependencyDeclarations, u as InvokeOptionWithKeys, ut as InferProvided, vt as ResolutionCondition, wt as OwnerRecord, y as Graph, yt as ResolveDependencyDeclaration } from "./index-DXguNx2M.mjs";
|
|
2
|
+
import { setActiveSub } from "alien-signals";
|
|
3
|
+
/** The identity of an event. Any object with a string `id` is an event key, for example an eventa definition. */
|
|
4
|
+
interface EventKey {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
}
|
|
7
|
+
/** A frozen record on a listener, for example its priority, timeout, or failure policy. injeca does not read it. */
|
|
8
|
+
type ListenerMetadata = Readonly<Record<string, unknown>>;
|
|
9
|
+
/** One listener that `listeners()` returns. */
|
|
10
|
+
interface ListenerEntry<H> {
|
|
11
|
+
readonly handler: H;
|
|
12
|
+
/** The owner record of the container that owns the listener. */
|
|
13
|
+
readonly owner: OwnerRecord;
|
|
14
|
+
readonly metadata: ListenerMetadata;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Creates a container. With `options.parent`, the container is a child of that container.
|
|
18
|
+
*
|
|
19
|
+
* A child container does not start when it is created, even under a started parent. It waits for `start(child)`.
|
|
20
|
+
*/
|
|
21
|
+
declare function createContainer(options?: ContainerOptions): Container;
|
|
22
|
+
/**
|
|
23
|
+
* Runs `setup` and stores the returned disposer on the container. `dispose(container)` runs the disposers in reverse order.
|
|
24
|
+
*/
|
|
25
|
+
declare function effect$1(container: Container, setup: EffectSetup): void;
|
|
26
|
+
/**
|
|
27
|
+
* Withdraws the registrations and invocations of the container, disposes the child containers, and runs the container effects in reverse order.
|
|
28
|
+
*
|
|
29
|
+
* One error keeps its identity. More than one error becomes an `AggregateError`.
|
|
30
|
+
* A call from the cleanup chain of the same container rejects with `reentrant-dispose`. A second call from outside gets the same promise.
|
|
31
|
+
*/
|
|
32
|
+
declare function dispose(container: Container): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Registers an invocation: a service without a value. The callback runs when the required dependencies are available.
|
|
35
|
+
* Before `start()`, it waits for `start()`.
|
|
36
|
+
*/
|
|
37
|
+
declare function invoke$1<Deps extends DependencyDeclarations>(container: Container, option: InvokeOptionWithKeys<Deps>): InvocationHandle;
|
|
38
|
+
declare function invoke$1<D extends DependencyMap>(container: Container, option: InvokeOption<D>): InvocationHandle;
|
|
39
|
+
type KeyLike<T = unknown> = ProvidedKey<string, T, DependencyMap | undefined> | ProvidedBy<T>;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a typed key without a provider. A consumer can depend on it now. `provide(key, ...)` registers a provider later.
|
|
42
|
+
*
|
|
43
|
+
* With `options.exclusive`, the key can have only one provider in its resolution area. Two or more providers all go to `conflict`.
|
|
44
|
+
*/
|
|
45
|
+
declare function defineKey<T>(name: string, options?: KeyOptions): ProvidedKey<string, T, undefined>;
|
|
46
|
+
/**
|
|
47
|
+
* Marks a fallback dependency. It resolves to the first provider in the list whose effective status is available.
|
|
48
|
+
*
|
|
49
|
+
* A change of the selection is a change of identity: the dependent gets `dependency-change`.
|
|
50
|
+
* It is required, unless it is wrapped in `optional()`.
|
|
51
|
+
*/
|
|
52
|
+
declare function firstAvailable<const Keys extends readonly KeyLike[]>(...keys: Keys): FallbackDependency<InferProvided<Keys[number]>>;
|
|
53
|
+
/**
|
|
54
|
+
* Marks an optional dependency. The dependent gets `{ value, status }`, or `undefined` when no provider runs.
|
|
55
|
+
* It also accepts a `firstAvailable` marker and a `where` marker.
|
|
56
|
+
*/
|
|
57
|
+
declare function optional<T>(dependency: KeyLike<T> | FallbackDependency<T> | ConditionalDependency<T>): OptionalDependency<T>;
|
|
58
|
+
/**
|
|
59
|
+
* Adds a resolution condition to a dependency. The condition gets the key, the owner, and the metadata of the provider.
|
|
60
|
+
*
|
|
61
|
+
* If it returns `false`, the dependency does not resolve. A required dependency keeps the dependent `pending`. An optional one is `undefined`.
|
|
62
|
+
* The condition must be pure and synchronous. A thrown error counts as `false` and goes to the observe stream.
|
|
63
|
+
* Wrap the result in `optional()`, or pass an `optional()` marker, to combine the two.
|
|
64
|
+
*/
|
|
65
|
+
declare function where<T>(dependency: OptionalDependency<T>, condition: ResolutionCondition): OptionalDependency<T>;
|
|
66
|
+
declare function where<T>(dependency: KeyLike<T>, condition: ResolutionCondition): ConditionalDependency<T>;
|
|
67
|
+
interface StartOptions {
|
|
68
|
+
/** Rejects with `start-timeout` when not every service runs after this many milliseconds. There is no default. */
|
|
69
|
+
timeoutMs?: number;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Begins convergence of the container and of each child container that was started.
|
|
73
|
+
*
|
|
74
|
+
* Resolves when all their services and invocations are `running`. Rejects with the build and start errors.
|
|
75
|
+
* A service in `failed` tries again. A required key without a provider keeps the promise pending, unless `timeoutMs` is set.
|
|
76
|
+
* A child container that was never started does not start. Call `start(child)` for it.
|
|
77
|
+
*/
|
|
78
|
+
declare function start$1(container: Container, options?: StartOptions): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Stops all instances of the container and of each child container that was started. Dependents stop before their providers.
|
|
81
|
+
*
|
|
82
|
+
* The registrations stay. The container stays usable, and a later `start()` builds them again. `dispose()` clears the container.
|
|
83
|
+
* Rejects with the teardown errors.
|
|
84
|
+
*/
|
|
85
|
+
declare function stop$1(container: Container): Promise<void>;
|
|
86
|
+
/**
|
|
87
|
+
* Stores a listener for `eventKey`. The container owns it: `dispose(container)` removes it.
|
|
88
|
+
*
|
|
89
|
+
* injeca does not call the listener. The consumer reads the listeners with `listeners()` and runs them by its own rules.
|
|
90
|
+
* Returns a function that removes the listener.
|
|
91
|
+
*/
|
|
92
|
+
declare function listen<H extends (...args: never[]) => unknown>(container: Container, eventKey: EventKey, handler: H, metadata?: ListenerMetadata): () => void;
|
|
93
|
+
/**
|
|
94
|
+
* Registers a provider and returns its typed key with `withdraw()` and `replace()`.
|
|
95
|
+
*
|
|
96
|
+
* Before `start()`, a second provide of the same name replaces the first registration. After `start()`, it throws `duplicate-service`.
|
|
97
|
+
* After `start()`, a new service builds and starts when its dependencies are available.
|
|
98
|
+
*/
|
|
99
|
+
declare function provide$1<T, Key extends string, Deps extends DependencyDeclarations>(container: Container, name: Key, option: ProvideOptionWithKeys<T, Deps>): Registration<Key, T, ResolveDependencyDeclaration<Deps>>;
|
|
100
|
+
declare function provide$1<T, Key extends string, Deps extends DependencyDeclarations>(container: Container, option: ProvideOptionWithKeys<T, Deps> & {
|
|
101
|
+
autoNameStackIndex?: number;
|
|
102
|
+
}): Registration<Key, T, ResolveDependencyDeclaration<Deps>>;
|
|
103
|
+
declare function provide$1<D extends DependencyMap | undefined, T = any, Key extends string = string>(container: Container, name: Key, option: ProvideOption<T, D>): Registration<Key, T, D>;
|
|
104
|
+
declare function provide$1<D extends DependencyMap | undefined, T = any, Key extends string = string>(container: Container, option: ProvideOption<T, D> & {
|
|
105
|
+
autoNameStackIndex?: number;
|
|
106
|
+
}): Registration<Key, T, D>;
|
|
107
|
+
declare function provide$1<T, Key extends string, Deps extends DependencyDeclarations>(container: Container, key: ProvidedKey<Key, T, DependencyMap | undefined>, option: ProvideOptionWithKeys<NoInfer<T>, Deps>): Registration<Key, T, ResolveDependencyDeclaration<Deps>>;
|
|
108
|
+
declare function provide$1<T, Key extends string>(container: Container, key: ProvidedKey<Key, T, DependencyMap | undefined>, option: ProvideOption<NoInfer<T>, undefined>): Registration<Key, T, undefined>;
|
|
109
|
+
/**
|
|
110
|
+
* Waits until the dependencies are available and returns their values. Before `start()`, it builds and starts them.
|
|
111
|
+
*
|
|
112
|
+
* Rejects when a required dependency fails. The values can become stale after a run-time change. Use it for one-time code.
|
|
113
|
+
*/
|
|
114
|
+
declare function resolve$1<Deps extends DependencyDeclarations>(container: Container, dependsOn: Deps): Promise<ResolveDependencyDeclaration<Deps>>;
|
|
115
|
+
/**
|
|
116
|
+
* Creates a transaction for `container`.
|
|
117
|
+
*
|
|
118
|
+
* `declare` reserves services. `provide` and `replace` add services. Nothing is visible to consumers before `commit()`.
|
|
119
|
+
* `commit()` builds the services in dependency order and publishes them in one batch. Then the `start` hooks run.
|
|
120
|
+
* A failed build publishes nothing. A disposal of the container during the transaction aborts it.
|
|
121
|
+
*/
|
|
122
|
+
declare function transaction(container: Container): Transaction;
|
|
123
|
+
/**
|
|
124
|
+
* Returns the graph queries of `container`. They cover the container and its descendants. Each query is a pure read.
|
|
125
|
+
*
|
|
126
|
+
* A key resolves as for a consumer in the container: from the container, then from its ancestors. A key that only a descendant has resolves to the first such service.
|
|
127
|
+
*/
|
|
128
|
+
declare function graph(container: Container): Graph;
|
|
129
|
+
/**
|
|
130
|
+
* Returns the current listeners for `eventKey` in `container` and its descendants, in registration order.
|
|
131
|
+
*
|
|
132
|
+
* Each entry has the handler, the owner record of the container that owns it, and its metadata.
|
|
133
|
+
* `H` is the handler type that the caller expects. injeca does not check it.
|
|
134
|
+
*/
|
|
135
|
+
declare function listeners<H = (...args: never[]) => unknown>(container: Container, eventKey: EventKey): readonly ListenerEntry<H>[];
|
|
136
|
+
declare function setLogger(logger: Logger): void;
|
|
137
|
+
/** Returns the root container of the global functions. Pass it as `parent` to make a child container. */
|
|
138
|
+
declare function getContainer(): Container;
|
|
139
|
+
/** Replaces the global container with a new one. `getContainer()` then returns the new one. It does not stop or dispose the old container. */
|
|
140
|
+
declare function resetContainer(): void;
|
|
141
|
+
declare function provide<T, Key extends string, Deps extends DependencyDeclarations>(name: Key, option: ProvideOptionWithKeys<T, Deps>): Registration<Key, T, ResolveDependencyDeclaration<Deps>>;
|
|
142
|
+
declare function provide<D extends DependencyMap | undefined, T = any, Key extends string = string>(name: Key, option: ProvideOption<T, D>): Registration<Key, T, D>;
|
|
143
|
+
declare function provide<T, Key extends string, Deps extends DependencyDeclarations>(option: ProvideOptionWithKeys<T, Deps>): Registration<Key, T, ResolveDependencyDeclaration<Deps>>;
|
|
144
|
+
declare function provide<D extends DependencyMap | undefined, T = any, Key extends string = string>(option: ProvideOption<T, D>): Registration<Key, T, D>;
|
|
145
|
+
declare function provide<T, Key extends string, Deps extends DependencyDeclarations>(key: ProvidedKey<Key, T, DependencyMap | undefined>, option: ProvideOptionWithKeys<NoInfer<T>, Deps>): Registration<Key, T, ResolveDependencyDeclaration<Deps>>;
|
|
146
|
+
declare function provide<T, Key extends string>(key: ProvidedKey<Key, T, DependencyMap | undefined>, option: ProvideOption<NoInfer<T>, undefined>): Registration<Key, T, undefined>;
|
|
147
|
+
declare function invoke<Deps extends DependencyDeclarations>(option: InvokeOptionWithKeys<Deps>): InvocationHandle;
|
|
148
|
+
declare function invoke<D extends DependencyMap>(option: InvokeOption<D>): InvocationHandle;
|
|
149
|
+
declare function resolve<Deps extends DependencyDeclarations>(dependsOn: Deps): Promise<ResolveDependencyDeclaration<Deps>>;
|
|
150
|
+
declare function start(options?: StartOptions): Promise<void>;
|
|
151
|
+
declare function stop(): Promise<void>;
|
|
152
|
+
export { createContainer as C, ListenerEntry as D, EventKey as E, ListenerMetadata as O, invoke$1 as S, effect$1 as T, stop$1 as _, resolve as a, optional as b, stop as c, transaction as d, resolve$1 as f, start$1 as g, StartOptions as h, resetContainer as i, listeners as l, listen as m, invoke as n, setLogger as o, provide$1 as p, provide as r, start as s, getContainer as t, graph as u, defineKey as v, dispose as w, where as x, firstAvailable as y };
|
|
153
|
+
//# sourceMappingURL=global-B9Bk2EG2.d.mts.map
|