injeca 0.2.0 → 0.4.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.
@@ -0,0 +1,217 @@
1
+ import { A as PeekValue, At as OwnerRecord, H as ContainerOptions, O as PeekDependency, P as EffectSetup, S as Graph, St as ProvidedKey, Tt as ResolveDependencyDeclaration, V as Container, Vt as Logger, _ as Registration, _t as KeyOptions, d as InvokeOptionWithKeys, dt as ConditionalDependency, f as ProvideOption, g as ProvideOptionWithKeys, gt as InferProvided, ht as FallbackDependency, k as PeekResult, mt as DependencyMap, n as Transaction, o as InvocationHandle, pt as DependencyDeclarations, s as InvokeOption, wt as ResolutionCondition, xt as ProvidedBy, yt as OptionalDependency } from "./index-DTYp044J.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
+ * When the disposal of the container started or ended, the disposer runs at once after `setup`, so that the resource does not leak.
26
+ * A disposer that throws throws to the caller. A disposer promise that rejects is reported outside the call, as an unhandled error.
27
+ */
28
+ declare function effect$1(container: Container, setup: EffectSetup): void;
29
+ interface DisposeOptions {
30
+ /**
31
+ * Ends the wait when the signal aborts, and rejects with the reason. The disposal continues in the background. There is no default.
32
+ * Each service that did not stop yet gets a `wait-aborted` record in the observe stream.
33
+ */
34
+ signal?: AbortSignal;
35
+ }
36
+ /**
37
+ * Withdraws the registrations and invocations of the container, disposes the child containers, and runs the container effects in reverse order.
38
+ *
39
+ * One error keeps its identity. More than one error becomes an `AggregateError`. A second call from outside without a signal gets the same promise.
40
+ *
41
+ * A call from the cleanup chain of the container or of a descendant cannot wait, because the disposal waits for that cleanup.
42
+ * The same is true for a cleanup of a service that holds a service of the subtree, directly or through other instances, or a consumer service of a `perConsumer` registration of it.
43
+ * injeca finds such a call by the signal of the cleanup, or by the synchronous part of the cleanup. It warns, sends a `reentrant-call` record, and resolves at once. The disposal continues.
44
+ *
45
+ * When `options.signal` aborts, the call rejects with the reason and stops waiting. The disposal continues: the container stays in `disposing`, and calls on it fail with `build-after-dispose`.
46
+ * A later `dispose()` waits for the same disposal and gets its result. An aborted signal starts the disposal, then rejects at once.
47
+ */
48
+ declare function dispose(container: Container, options?: DisposeOptions): Promise<void>;
49
+ /**
50
+ * Registers an invocation: a service without a value. The callback runs when the required dependencies are available.
51
+ * Before `start()`, it waits for `start()`.
52
+ */
53
+ declare function invoke$1<Deps extends DependencyDeclarations>(container: Container, option: InvokeOptionWithKeys<Deps>): InvocationHandle;
54
+ declare function invoke$1<D extends DependencyMap>(container: Container, option: InvokeOption<D>): InvocationHandle;
55
+ type KeyLike<T = unknown> = ProvidedKey<string, T, DependencyMap | undefined> | ProvidedBy<T>;
56
+ /**
57
+ * Creates a typed key without a provider. A consumer can depend on it now. `provide(key, ...)` registers a provider later.
58
+ *
59
+ * With `options.exclusive`, the key can have only one provider in its resolution area. Two or more providers all go to `conflict`.
60
+ */
61
+ declare function defineKey<T>(name: string, options?: KeyOptions): ProvidedKey<string, T, undefined>;
62
+ /**
63
+ * Marks a fallback dependency. It resolves to the first provider in the list whose effective status is available.
64
+ *
65
+ * A change of the selection is a change of identity: the dependent gets `dependency-change`.
66
+ * It is required, unless it is wrapped in `optional()`.
67
+ * A `where(key, condition)` marker in the list filters that candidate only. `where(firstAvailable(...), condition)` filters each candidate.
68
+ * The selection is the first available candidate that passes its conditions.
69
+ */
70
+ declare function firstAvailable<const Keys extends readonly (KeyLike | ConditionalDependency<unknown>)[]>(...keys: Keys): FallbackDependency<InferProvided<Keys[number]>>;
71
+ /**
72
+ * Marks an optional dependency. The dependent gets `{ value, status }`, or `undefined` when no provider runs.
73
+ * It also accepts a `firstAvailable` marker and a `where` marker, with their conditions.
74
+ */
75
+ declare function optional<T>(dependency: KeyLike<T> | FallbackDependency<T> | ConditionalDependency<T>): OptionalDependency<T>;
76
+ /**
77
+ * Adds a resolution condition to a dependency. The condition gets the key, the owner, and the metadata of the provider.
78
+ *
79
+ * If it returns `false`, the dependency does not resolve. A required dependency keeps the dependent `pending`. An optional one is `undefined`.
80
+ * The condition must be pure and synchronous. A thrown error counts as `false` and goes to the observe stream.
81
+ * Wrap the result in `optional()`, or pass an `optional()` marker, to combine the two.
82
+ *
83
+ * For a `firstAvailable` marker, the condition filters each candidate. The selection is the first available candidate that passes.
84
+ * A candidate that `firstAvailable(where(key, other), ...)` filters must pass both conditions.
85
+ */
86
+ declare function where<T>(dependency: OptionalDependency<T>, condition: ResolutionCondition): OptionalDependency<T>;
87
+ declare function where<T>(dependency: FallbackDependency<T>, condition: ResolutionCondition): FallbackDependency<T>;
88
+ declare function where<T>(dependency: KeyLike<T>, condition: ResolutionCondition): ConditionalDependency<T>;
89
+ interface StartOptions {
90
+ /**
91
+ * Ends the wait when the signal aborts. The services keep converging.
92
+ * A timeout reason (`name === 'TimeoutError'`) rejects with `start-timeout` and the explanations. Another reason rejects as it is. There is no default.
93
+ */
94
+ signal?: AbortSignal;
95
+ }
96
+ interface StopOptions {
97
+ /**
98
+ * Ends the wait when the signal aborts, and rejects with the reason. The cleanups that did not settle keep running.
99
+ * Each service that did not stop yet gets a `wait-aborted` record in the observe stream. There is no default.
100
+ */
101
+ signal?: AbortSignal;
102
+ }
103
+ /**
104
+ * Begins convergence of the container and of each child container that was started.
105
+ *
106
+ * Resolves when all their services and invocations are `running`. Rejects with the build and start errors.
107
+ * A service in `failed` tries again. A required key without a provider keeps the promise pending, unless `options.signal` aborts.
108
+ * A child container that was never started does not start. Call `start(child)` for it.
109
+ */
110
+ declare function start$1(container: Container, options?: StartOptions): Promise<void>;
111
+ /**
112
+ * Stops all instances of the container and of each child container that was started. Dependents stop before their providers.
113
+ *
114
+ * The registrations stay. The container stays usable, and a later `start()` builds them again. `dispose()` clears the container.
115
+ * Rejects with the teardown errors.
116
+ *
117
+ * When `options.signal` aborts, it rejects with the reason and stops waiting. The container stays stopped: a later `start()` builds again.
118
+ * The services that did not stop yet stay in `stopping` until their cleanups settle.
119
+ *
120
+ * A call from the cleanup chain of the container or of a descendant cannot wait, because the stop waits for that cleanup.
121
+ * The same is true for a cleanup of a service that holds a service of the subtree, directly or through other instances.
122
+ * injeca finds such a call by the signal of the cleanup, or by the synchronous part of the cleanup. It warns, sends a `reentrant-call` record, and resolves at once. The stop continues.
123
+ */
124
+ declare function stop$1(container: Container, options?: StopOptions): Promise<void>;
125
+ /**
126
+ * Stores a listener for `eventKey`. The container owns it: `dispose(container)` removes it.
127
+ *
128
+ * injeca does not call the listener. The consumer reads the listeners with `listeners()` and runs them by its own rules.
129
+ * Returns a function that removes the listener.
130
+ */
131
+ declare function listen<H extends (...args: never[]) => unknown>(container: Container, eventKey: EventKey, handler: H, metadata?: ListenerMetadata): () => void;
132
+ /**
133
+ * Registers a provider and returns its typed key with `withdraw()` and `replace()`.
134
+ *
135
+ * Before `start()`, a second provide of the same name replaces the first registration. After `start()`, it throws `duplicate-service`.
136
+ * After `start()`, a new service builds and starts when its dependencies are available.
137
+ */
138
+ declare function provide$1<T, Key extends string, Deps extends DependencyDeclarations>(container: Container, name: Key, option: ProvideOptionWithKeys<T, Deps>): Registration<Key, T, ResolveDependencyDeclaration<Deps>>;
139
+ declare function provide$1<T, Key extends string, Deps extends DependencyDeclarations>(container: Container, option: ProvideOptionWithKeys<T, Deps> & {
140
+ autoNameStackIndex?: number;
141
+ }): Registration<Key, T, ResolveDependencyDeclaration<Deps>>;
142
+ 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>;
143
+ declare function provide$1<D extends DependencyMap | undefined, T = any, Key extends string = string>(container: Container, option: ProvideOption<T, D> & {
144
+ autoNameStackIndex?: number;
145
+ }): Registration<Key, T, D>;
146
+ 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>>;
147
+ 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>;
148
+ interface ResolveOptions {
149
+ /**
150
+ * Ends the wait when the signal aborts. A timeout reason (`name === 'TimeoutError'`) rejects with `wait-timeout`, with the reason as `cause`.
151
+ * Another reason rejects as it is. Use `timeout(container, ms)` for a timeout on the container clock. There is no default.
152
+ */
153
+ signal?: AbortSignal;
154
+ }
155
+ /**
156
+ * Waits until the dependencies are available and returns their values. Before `start()`, it builds and starts them.
157
+ *
158
+ * Rejects when a required dependency fails. With `options.signal`, it rejects when the signal aborts. An aborted signal rejects at once.
159
+ * Each rejection removes the hidden resolver of the call. The values can become stale after a run-time change. Use it for one-time code.
160
+ */
161
+ declare function resolve$1<Deps extends DependencyDeclarations>(container: Container, dependsOn: Deps, options?: ResolveOptions): Promise<ResolveDependencyDeclaration<Deps>>;
162
+ /**
163
+ * Returns a signal that aborts after `ms` milliseconds of the clock of `container`.
164
+ *
165
+ * The reason is a `DOMException` with the name `TimeoutError`, as for `AbortSignal.timeout()`. Use it in place of `AbortSignal.timeout()`, because that one does not use the container clock.
166
+ * With the system clock, the timer does not keep a Node process alive.
167
+ */
168
+ declare function timeout(container: Container, ms: number): AbortSignal;
169
+ /** Returns a signal that aborts when `signal` aborts, or after `ms` milliseconds of the clock of `container`. Without `signal`, it is `timeout(container, ms)`. */
170
+ declare function withTimeout(signal: AbortSignal | undefined, ms: number, container: Container): AbortSignal;
171
+ /**
172
+ * Creates a transaction for `container`.
173
+ *
174
+ * `declare` reserves services. `provide` and `replace` add services. Nothing is visible to consumers before `commit()`.
175
+ * `commit()` builds the services in dependency order and publishes them in one batch. Then the `start` hooks run.
176
+ * A failed build publishes nothing. A disposal of the container during the transaction aborts it.
177
+ */
178
+ declare function transaction(container: Container): Transaction;
179
+ /**
180
+ * Returns the graph queries of `container`. They cover the container and its descendants. Each query is a pure read.
181
+ *
182
+ * A key resolves as for a consumer in the container: from the container and the exports of its children, then from each ancestor and the exports of its children.
183
+ * A key that only a descendant has resolves to the first such service.
184
+ */
185
+ declare function graph(container: Container): Graph;
186
+ /**
187
+ * Reads the value of a dependency now, without a wait. It is a pure read: it does not demand, build, or start a service.
188
+ *
189
+ * The dependency resolves as for a consumer in `container`: a key, `where(key, condition)`, or `firstAvailable(...)`. An `optional` marker has no effect.
190
+ * The result is the value with its status, or the reason why no value is available now. A `perConsumer` key has a value only after something demanded its service for `container`.
191
+ */
192
+ declare function peek<D extends PeekDependency>(container: Container, dependency: D): PeekResult<PeekValue<D>>;
193
+ /**
194
+ * Returns the current listeners for `eventKey` in `container` and its descendants, in registration order.
195
+ *
196
+ * Each entry has the handler, the owner record of the container that owns it, and its metadata.
197
+ * `H` is the handler type that the caller expects. injeca does not check it.
198
+ */
199
+ declare function listeners<H = (...args: never[]) => unknown>(container: Container, eventKey: EventKey): readonly ListenerEntry<H>[];
200
+ declare function setLogger(logger: Logger): void;
201
+ /** Returns the root container of the global functions. Pass it as `parent` to make a child container. */
202
+ declare function getContainer(): Container;
203
+ /** Replaces the global container with a new one. `getContainer()` then returns the new one. It does not stop or dispose the old container. */
204
+ declare function resetContainer(): void;
205
+ declare function provide<T, Key extends string, Deps extends DependencyDeclarations>(name: Key, option: ProvideOptionWithKeys<T, Deps>): Registration<Key, T, ResolveDependencyDeclaration<Deps>>;
206
+ declare function provide<D extends DependencyMap | undefined, T = any, Key extends string = string>(name: Key, option: ProvideOption<T, D>): Registration<Key, T, D>;
207
+ declare function provide<T, Key extends string, Deps extends DependencyDeclarations>(option: ProvideOptionWithKeys<T, Deps>): Registration<Key, T, ResolveDependencyDeclaration<Deps>>;
208
+ declare function provide<D extends DependencyMap | undefined, T = any, Key extends string = string>(option: ProvideOption<T, D>): Registration<Key, T, D>;
209
+ 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>>;
210
+ declare function provide<T, Key extends string>(key: ProvidedKey<Key, T, DependencyMap | undefined>, option: ProvideOption<NoInfer<T>, undefined>): Registration<Key, T, undefined>;
211
+ declare function invoke<Deps extends DependencyDeclarations>(option: InvokeOptionWithKeys<Deps>): InvocationHandle;
212
+ declare function invoke<D extends DependencyMap>(option: InvokeOption<D>): InvocationHandle;
213
+ declare function resolve<Deps extends DependencyDeclarations>(dependsOn: Deps, options?: ResolveOptions): Promise<ResolveDependencyDeclaration<Deps>>;
214
+ declare function start(options?: StartOptions): Promise<void>;
215
+ declare function stop(options?: StopOptions): Promise<void>;
216
+ export { dispose as A, defineKey as C, invoke$1 as D, where as E, EventKey as M, ListenerEntry as N, DisposeOptions as O, ListenerMetadata as P, stop$1 as S, optional as T, provide$1 as _, resolve as a, StopOptions as b, stop as c, graph as d, transaction as f, resolve$1 as g, ResolveOptions as h, resetContainer as i, effect$1 as j, createContainer as k, listeners as l, withTimeout as m, invoke as n, setLogger as o, timeout as p, provide as r, start as s, getContainer as t, peek as u, listen as v, firstAvailable as w, start$1 as x, StartOptions as y };
217
+ //# sourceMappingURL=global-DPfe26g5.d.mts.map
package/dist/global.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { a as setLogger, i as resolve, n as provide, o as start, r as resetContainer, s as stop, t as invoke } from "./global-BLY6_SI_.mjs";
2
- export { invoke, provide, resetContainer, resolve, setLogger, start, stop };
1
+ import { a as resolve, c as stop, i as resetContainer, n as invoke, o as setLogger, r as provide, s as start, t as getContainer } from "./global-DPfe26g5.mjs";
2
+ export { getContainer, invoke, provide, resetContainer, resolve, setLogger, start, stop };
package/dist/global.mjs CHANGED
@@ -1,3 +1 @@
1
- import { _ as start, f as invoke, g as setLogger, h as resolve, m as resetContainer, p as provide, v as stop } from "./src-UXkTj4Np.mjs";
2
-
3
- export { invoke, provide, resetContainer, resolve, setLogger, start, stop };
1
+ export { t as getContainer, n as invoke, r as provide, i as resetContainer, a as resolve, o as setLogger, s as start, c as stop } from "./global-B5N-d-TO.mjs";