better-effect 0.5.0 → 0.6.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 +60 -6
- package/dist/adapters/iti.d.mts +1 -1
- package/dist/adapters/iti.d.mts.map +1 -1
- package/dist/adapters/iti.mjs +4 -3
- package/dist/adapters/iti.mjs.map +1 -1
- package/dist/errors-GR3K_nRu.mjs.map +1 -1
- package/dist/index-DMfjhNR_.d.mts +500 -0
- package/dist/index-DMfjhNR_.d.mts.map +1 -0
- package/dist/index.d.mts +57 -61
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +87 -112
- package/dist/index.mjs.map +1 -1
- package/dist/{internal-identity-BnZC3Au-.mjs → internal-identity-C6Awrc33.mjs} +4 -3
- package/dist/internal-identity-C6Awrc33.mjs.map +1 -0
- package/dist/runtime-CDcCF5cb.mjs +10 -0
- package/dist/runtime-CDcCF5cb.mjs.map +1 -0
- package/dist/testing.d.mts +1 -1
- package/dist/testing.d.mts.map +1 -1
- package/dist/testing.mjs +3 -2
- package/dist/testing.mjs.map +1 -1
- package/package.json +13 -5
- package/dist/index-D77AvuBl.d.mts +0 -510
- package/dist/index-D77AvuBl.d.mts.map +0 -1
- package/dist/internal-identity-BnZC3Au-.mjs.map +0 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { A as
|
|
1
|
+
import { A as AnyEffect, B as ServiceClass, C as ScopeClosedError, D as ServiceResolver, E as ServiceRuntimeNotConfiguredError, F as EffectSuccess, G as ServiceTag, H as ServiceIdentity, I as EffectYield, K as ServiceToken, L as ServiceRequirement, M as EffectError, N as EffectFromGenerator, O as ServiceRuntime, P as EffectRequirements, R as AnyService, S as ScopeCloseError, T as ServiceNotFoundError, U as ServiceInstance, V as ServiceContract, W as ServiceRequirements, _ as DisposableResource, a as DuplicateServiceError, b as ScopeOutcome, c as LayerRegistrationError, d as LayerRegistration, f as CompleteExecution, g as CleanupFailureDiagnostic, h as ProvidedEnvironment, i as RuntimeShutdownDiagnostic, j as Effect$1, k as Service, l as ServiceTagCollisionError, m as LayerInput, n as CleanupFailureObserver, o as LayerDisposeError, p as CompleteInput, q as ServiceTokenOf, r as RuntimeOptions, s as LayerGeneratorYieldError, t as LayerBackend, u as Layer, v as MaybePromise$1, w as ScopeRuntimeNotConfiguredError, x as ResourceNotDisposableError, y as ScopeFinalizer, z as AnyServiceToken } from "./index-DMfjhNR_.mjs";
|
|
2
2
|
import { Err, Result, UnhandledException } from "better-result";
|
|
3
3
|
//#region src/scope/scope.d.ts
|
|
4
4
|
/**
|
|
@@ -48,16 +48,27 @@ declare const Scope: {
|
|
|
48
48
|
*/
|
|
49
49
|
readonly run: <A>(program: (scope: Scope) => A | PromiseLike<A>) => Promise<Awaited<A>>;
|
|
50
50
|
};
|
|
51
|
+
/** Type-level aliases for Scope ownership, outcomes, and cleanup contracts. */
|
|
52
|
+
declare namespace Scope {
|
|
53
|
+
/** A Scope whose owner is responsible for calling `close()`. */
|
|
54
|
+
type Closeable = CloseableScope;
|
|
55
|
+
/** The outcome supplied to Scope finalizers and resource releases. */
|
|
56
|
+
type Outcome = ScopeOutcome;
|
|
57
|
+
/** A cleanup callback registered with a Scope. */
|
|
58
|
+
type Finalizer = ScopeFinalizer;
|
|
59
|
+
/** A value implementing a JavaScript disposal protocol. */
|
|
60
|
+
type Disposable = DisposableResource;
|
|
61
|
+
}
|
|
51
62
|
//#endregion
|
|
52
63
|
//#region src/effect/combinators.d.ts
|
|
53
|
-
type EffectInput<A, E, Requirements> =
|
|
64
|
+
type EffectInput<A, E, Requirements extends AnyService> = Effect$1<A, E, Requirements> | PromiseLike<Effect$1<A, E, Requirements>>;
|
|
54
65
|
type AnyEffectInput = EffectInput<any, any, any>;
|
|
55
|
-
type
|
|
56
|
-
type AnyAsyncEffectInput = PromiseLike<
|
|
66
|
+
type AnyEffectValue = Effect$1<any, any, any>;
|
|
67
|
+
type AnyAsyncEffectInput = PromiseLike<AnyEffectValue>;
|
|
57
68
|
type PreserveAsync<Input, Output> = Input extends PromiseLike<unknown> ? Promise<Output> : Output;
|
|
58
|
-
type MappedResult<Input, B> =
|
|
59
|
-
type ErrorMappedResult<Input, E2> =
|
|
60
|
-
type ChainedResult<First, Next> =
|
|
69
|
+
type MappedResult<Input, B> = Effect$1<B, EffectError<Input>, EffectRequirements<Input>>;
|
|
70
|
+
type ErrorMappedResult<Input, E2> = Effect$1<EffectSuccess<Input>, E2, EffectRequirements<Input>>;
|
|
71
|
+
type ChainedResult<First, Next> = Effect$1<EffectSuccess<Next>, EffectError<First> | EffectError<Next>, EffectRequirements<First> | EffectRequirements<Next>>;
|
|
61
72
|
type ChainedOutput<First, Next> = ChainedResult<First, Next>;
|
|
62
73
|
type AsyncChainedOutput<First, Next> = Promise<ChainedResult<First, Next>>;
|
|
63
74
|
type MapOperation<A, B> = {
|
|
@@ -67,7 +78,7 @@ type MapErrorOperation<E1, E2> = {
|
|
|
67
78
|
<Input>(effect: Input & EffectInput<any, E1, any>): PreserveAsync<Input, ErrorMappedResult<Input, E2>>;
|
|
68
79
|
};
|
|
69
80
|
type AndThenOperation<A, Next> = {
|
|
70
|
-
<Input>(effect: Input &
|
|
81
|
+
<Input>(effect: Input & Effect$1<A, any, any>): ChainedOutput<Input, Next>;
|
|
71
82
|
};
|
|
72
83
|
type AndThenAsyncOperation<A, Next> = {
|
|
73
84
|
<Input>(effect: Input & EffectInput<A, any, any>): AsyncChainedOutput<Input, Next>;
|
|
@@ -108,8 +119,8 @@ declare function mapError<Input, E2>(effect: Input & AnyEffectInput, fn: (error:
|
|
|
108
119
|
* const user = Effect.andThen(Result.ok('u1'), (id) => repository.find(id))
|
|
109
120
|
* ```
|
|
110
121
|
*/
|
|
111
|
-
declare function andThen<A, Next extends
|
|
112
|
-
declare function andThen<Input, Next extends
|
|
122
|
+
declare function andThen<A, Next extends AnyEffectValue>(next: (value: A) => Next): AndThenOperation<A, Next>;
|
|
123
|
+
declare function andThen<Input, Next extends AnyEffectValue>(effect: Input & AnyEffectValue, next: (value: EffectSuccess<Input>) => Next): ChainedOutput<Input, Next>;
|
|
113
124
|
/**
|
|
114
125
|
* Chain an asynchronous Result-producing operation after a successful result.
|
|
115
126
|
*
|
|
@@ -125,6 +136,7 @@ declare function andThenAsync<A, Next extends AnyAsyncEffectInput>(next: (value:
|
|
|
125
136
|
declare function andThenAsync<Input, Next extends AnyAsyncEffectInput>(effect: Input & AnyEffectInput, next: (value: EffectSuccess<Input>) => Next): AsyncChainedOutput<Input, Next>;
|
|
126
137
|
//#endregion
|
|
127
138
|
//#region src/effect/effect.d.ts
|
|
139
|
+
type Effect<A, E, R extends AnyService = never> = Effect$1<A, E, R>;
|
|
128
140
|
type AnyResult = Result<any, any>;
|
|
129
141
|
/**
|
|
130
142
|
* Compose `better-result` operations while preserving Service requirements in
|
|
@@ -197,24 +209,20 @@ declare const Effect: {
|
|
|
197
209
|
/** Chain an asynchronous Effect result. */
|
|
198
210
|
readonly andThenAsync: typeof andThenAsync;
|
|
199
211
|
};
|
|
212
|
+
/** Type-level aliases for inspecting Effect result channels and requirements. */
|
|
213
|
+
declare namespace Effect {
|
|
214
|
+
/** Extract the success channel from an Effect result or Promise. */
|
|
215
|
+
type Success<T> = EffectSuccess<T>;
|
|
216
|
+
/** Extract the error channel from an Effect result or Promise. */
|
|
217
|
+
type Error<T> = EffectError<T>;
|
|
218
|
+
/** Extract the Service requirements from an Effect result or Promise. */
|
|
219
|
+
type Requirements<T> = EffectRequirements<T>;
|
|
220
|
+
/** An Effect with erased success, error, and requirements. */
|
|
221
|
+
type Any = AnyEffect;
|
|
222
|
+
}
|
|
200
223
|
//#endregion
|
|
201
224
|
//#region src/function/pipe.d.ts
|
|
202
225
|
type Unary<A, B> = (value: A) => B;
|
|
203
|
-
/**
|
|
204
|
-
* Compose a value through a sequence of unary functions.
|
|
205
|
-
*
|
|
206
|
-
* `pipe` is deliberately independent of Effect, Result, Promise, Scope, and
|
|
207
|
-
* Service metadata.
|
|
208
|
-
*
|
|
209
|
-
* @example
|
|
210
|
-
* ```ts
|
|
211
|
-
* const label = pipe(
|
|
212
|
-
* 'alice',
|
|
213
|
-
* (name) => name.trim(),
|
|
214
|
-
* (name) => name.toUpperCase()
|
|
215
|
-
* )
|
|
216
|
-
* ```
|
|
217
|
-
*/
|
|
218
226
|
declare function pipe<A>(value: A): A;
|
|
219
227
|
declare function pipe<A, B>(value: A, ab: Unary<A, B>): B;
|
|
220
228
|
declare function pipe<A, B, C>(value: A, ab: Unary<A, B>, bc: Unary<B, C>): C;
|
|
@@ -263,31 +271,22 @@ type AcquireUseReleaseOptions<R, A, AcquireError, UseError> = {
|
|
|
263
271
|
};
|
|
264
272
|
//#endregion
|
|
265
273
|
//#region src/resource/resource.d.ts
|
|
274
|
+
declare const Resource: {
|
|
275
|
+
/** Acquire, use, and release a resource with deterministic error precedence. */
|
|
276
|
+
readonly acquireUseRelease: <R, A, AcquireError, UseError>({ name, acquire, use, release, onReleaseFailure }: AcquireUseReleaseOptions<R, A, AcquireError, UseError>) => Promise<Result<A, AcquireError | UseError | UnhandledException | ResourceReleaseFailure>>;
|
|
277
|
+
};
|
|
278
|
+
//#endregion
|
|
279
|
+
//#region src/runtime/types.d.ts
|
|
266
280
|
/**
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
* Acquisition, use, and release may be synchronous or asynchronous Result
|
|
270
|
-
* operations. If both use and release fail, the use error remains primary and
|
|
271
|
-
* `onReleaseFailure` receives the cleanup failure as a diagnostic.
|
|
272
|
-
*
|
|
273
|
-
* When `release` is omitted, `Symbol.asyncDispose` is preferred over
|
|
274
|
-
* `Symbol.dispose`.
|
|
281
|
+
* Name a Runtime type from a concrete Layer without repeating its provided
|
|
282
|
+
* branded Service instance union.
|
|
275
283
|
*
|
|
276
284
|
* @example
|
|
277
285
|
* ```ts
|
|
278
|
-
*
|
|
279
|
-
* name: 'database connection',
|
|
280
|
-
* acquire: () => connect(),
|
|
281
|
-
* use: (connection) => query(connection),
|
|
282
|
-
* release: (connection) => connection.close()
|
|
283
|
-
* })
|
|
286
|
+
* type AppRuntime = RuntimeFor<typeof AppLive>
|
|
284
287
|
* ```
|
|
285
288
|
*/
|
|
286
|
-
|
|
287
|
-
declare const Resource: {
|
|
288
|
-
/** Acquire, use, and release a resource with deterministic error precedence. */
|
|
289
|
-
readonly acquireUseRelease: typeof acquireUseRelease;
|
|
290
|
-
};
|
|
289
|
+
type RuntimeFor<L extends LayerInput> = Runtime<Layer.Provided<L>>;
|
|
291
290
|
//#endregion
|
|
292
291
|
//#region src/runtime/runtime.d.ts
|
|
293
292
|
/**
|
|
@@ -303,9 +302,9 @@ declare const Resource: {
|
|
|
303
302
|
* await runtime.dispose()
|
|
304
303
|
* ```
|
|
305
304
|
*
|
|
306
|
-
* @typeParam Provided The Service
|
|
305
|
+
* @typeParam Provided The branded Service instances supplied by the Layer.
|
|
307
306
|
*/
|
|
308
|
-
declare class Runtime<Provided extends
|
|
307
|
+
declare class Runtime<Provided extends AnyService = any> {
|
|
309
308
|
private readonly handle;
|
|
310
309
|
private constructor();
|
|
311
310
|
/**
|
|
@@ -318,14 +317,14 @@ declare class Runtime<Provided extends AnyServiceToken = AnyServiceToken> {
|
|
|
318
317
|
* await runtime.dispose()
|
|
319
318
|
* ```
|
|
320
319
|
*/
|
|
321
|
-
static make<L extends
|
|
320
|
+
static make<L extends LayerInput>(layer: L & CompleteInput<L>, backend: LayerBackend, options?: RuntimeOptions): Promise<Runtime<ProvidedEnvironment<L>>>;
|
|
322
321
|
/**
|
|
323
322
|
* Run one program and dispose its Layer resources before resolving.
|
|
324
323
|
*
|
|
325
324
|
* This is convenient for request-style or command-style execution where a
|
|
326
325
|
* Runtime should not outlive the operation.
|
|
327
326
|
*/
|
|
328
|
-
static run<A, L extends
|
|
327
|
+
static run<A, L extends LayerInput>(layer: L & CompleteInput<L>, backend: LayerBackend, program: CompleteExecution<ProvidedEnvironment<L>, A>, options?: RuntimeOptions): Promise<Awaited<A>>;
|
|
329
328
|
/** Run one execution in this Runtime's child Scope. */
|
|
330
329
|
run<A>(program: CompleteExecution<Provided, A>): Promise<Awaited<A>>;
|
|
331
330
|
private runUnchecked;
|
|
@@ -333,18 +332,15 @@ declare class Runtime<Provided extends AnyServiceToken = AnyServiceToken> {
|
|
|
333
332
|
dispose(): Promise<void>;
|
|
334
333
|
private disposeWithOutcome;
|
|
335
334
|
}
|
|
335
|
+
/** Type-level aliases for naming Runtime handles and shutdown options. */
|
|
336
|
+
declare namespace Runtime {
|
|
337
|
+
/** Name a Runtime type from a concrete Layer. */
|
|
338
|
+
type For<L extends LayerInput> = RuntimeFor<L>;
|
|
339
|
+
/** Optional Runtime shutdown configuration. */
|
|
340
|
+
type Options = RuntimeOptions;
|
|
341
|
+
/** Diagnostic reported for aggregated Runtime shutdown cleanup failures. */
|
|
342
|
+
type ShutdownDiagnostic = RuntimeShutdownDiagnostic;
|
|
343
|
+
}
|
|
336
344
|
//#endregion
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* Name a Runtime type from a concrete Layer without repeating its provided
|
|
340
|
-
* Service constructor union.
|
|
341
|
-
*
|
|
342
|
-
* @example
|
|
343
|
-
* ```ts
|
|
344
|
-
* type AppRuntime = RuntimeFor<typeof AppLive>
|
|
345
|
-
* ```
|
|
346
|
-
*/
|
|
347
|
-
type RuntimeFor<L extends AnyLayer> = Runtime<LayerProvided<L>>;
|
|
348
|
-
//#endregion
|
|
349
|
-
export { type AcquireUseReleaseOptions, type AnyEffectResult, type AnyServiceToken, type AsyncResult, type CleanupFailureDiagnostic, type CleanupFailureObserver, type CloseableScope, type DisposableResource, DuplicateServiceError, Effect, type EffectError, type EffectRequirements, type EffectResult, type EffectSuccess, type EffectYield, Layer, type LayerBackend, LayerDisposeError, LayerGeneratorYieldError, type LayerMissing, type LayerProvided, type LayerRawRequired, type LayerRegistration, LayerRegistrationError, type LayerSpecs, type ReleaseFailureObserver, type ReleaseOutcome, Resource, ResourceNotDisposableError, ResourceReleaseFailure, Runtime, type RuntimeFor, type RuntimeOptions, type RuntimeShutdownDiagnostic, Scope, ScopeCloseError, ScopeClosedError, type ScopeFinalizer, type ScopeOutcome, ScopeRuntimeNotConfiguredError, Service, type ServiceClass, type ServiceInstance, ServiceNotFoundError, type ServiceRequirement, type ServiceRequirements, type ServiceResolver, ServiceRuntime, ServiceRuntimeNotConfiguredError, type ServiceTag, ServiceTagCollisionError, type ServiceToken, pipe };
|
|
345
|
+
export { type AcquireUseReleaseOptions, type AnyEffect, type AnyService, type AnyServiceToken, type AsyncResult, type CleanupFailureDiagnostic, type CleanupFailureObserver, type CloseableScope, type DisposableResource, DuplicateServiceError, Effect, type EffectError, type EffectRequirements, type EffectSuccess, type EffectYield, Layer, type LayerBackend, LayerDisposeError, LayerGeneratorYieldError, type LayerRegistration, LayerRegistrationError, type ReleaseFailureObserver, type ReleaseOutcome, Resource, ResourceNotDisposableError, ResourceReleaseFailure, Runtime, type RuntimeFor, type RuntimeOptions, type RuntimeShutdownDiagnostic, Scope, ScopeCloseError, ScopeClosedError, type ScopeFinalizer, type ScopeOutcome, ScopeRuntimeNotConfiguredError, Service, type ServiceClass, type ServiceContract, type ServiceIdentity, type ServiceInstance, ServiceNotFoundError, type ServiceRequirement, type ServiceRequirements, type ServiceResolver, ServiceRuntime, ServiceRuntimeNotConfiguredError, type ServiceTag, ServiceTagCollisionError, type ServiceToken, type ServiceTokenOf, pipe };
|
|
350
346
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/scope/scope.ts","../src/effect/combinators.ts","../src/effect/effect.ts","../src/function/pipe.ts","../src/resource/errors.ts","../src/resource/types.ts","../src/resource/resource.ts","../src/runtime/
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/scope/scope.ts","../src/effect/combinators.ts","../src/effect/effect.ts","../src/function/pipe.ts","../src/resource/errors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;;WAG9B,MAAM,UAAU,MAAM,YAAY,GAAG;;WAGrC,WAAW,UAAU,MAAM,aAAa;;;;;WAMxC,mBAAmB;;;;cCqBjB;;WA3Bc,oBAAA,GAAG,GAAG,cAAc,YAAQ,MAAA,SAAA,KAAA,SAAA,oBAMpD,yBAAyB,GAAG,GAAG,cAAc,cAAY,QAC1D,OAAW,GAAG,eAAe,WAAW,qBAAqB;;;;;;;;;;;;;KCvBnD,WAAW,UAAU,cAAc,QAAQ,MAAM,SAAS;;;;;;;;;;;;;;;;;;cCqBzD,QAAQ,iBAAiB;mBACC;UAA9B;;;;;;;;;;;SAYM,KAAK,UAAU,YAC1B,OAAO,IAAI,cAAc,IACzB,SAAS,cACT,UAAS,iBACR,QAAQ,QAAQ,oBAAoB;;;;;;;SAY1B,IAAI,GAAG,UAAU,YAC5B,OAAO,IAAI,cAAc,IACzB,SAAS,cACT,SAAS,kBAAkB,oBAAoB,IAAI,IACnD,UAAS,iBACR,QAAQ,QAAQ;;EAqDnB,IAAI,GAAG,SAAS,kBAAkB,UAAU,KAAK,QAAQ,QAAQ;UAIzD;;EAMR,WAAW;UAIH;;;kBAMe;;OAEX,IAAI,UAAU,cAAc,WAAW;;OAGvC,UAAU;;OAGV,qBAAqB"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { a as ServiceTagCollisionError, i as LayerRegistrationError, n as LayerDisposeError, o as ServiceNotFoundError, r as LayerGeneratorYieldError, s as ServiceRuntimeNotConfiguredError, t as DuplicateServiceError } from "./errors-GR3K_nRu.mjs";
|
|
2
|
+
import { t as isPromiseLike } from "./runtime-CDcCF5cb.mjs";
|
|
2
3
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
4
|
import { Result, TaggedError } from "better-result";
|
|
4
5
|
//#region src/service/runtime.ts
|
|
@@ -136,101 +137,48 @@ var Layer = class Layer {
|
|
|
136
137
|
const defaultAcquire = () => {
|
|
137
138
|
return new service();
|
|
138
139
|
};
|
|
140
|
+
const normalizedAcquire = normalizeAcquire(acquire ?? defaultAcquire);
|
|
139
141
|
return new Layer([{
|
|
140
142
|
service,
|
|
141
|
-
acquire:
|
|
143
|
+
acquire: normalizedAcquire
|
|
142
144
|
}]);
|
|
143
145
|
}
|
|
144
|
-
/**
|
|
145
|
-
* Create a Layer from an already-constructed Service instance.
|
|
146
|
-
*
|
|
147
|
-
* The instance is returned as-is whenever the Service is resolved.
|
|
148
|
-
*
|
|
149
|
-
* @example
|
|
150
|
-
* ```ts
|
|
151
|
-
* const DatabaseLive = Layer.succeed(Database, database)
|
|
152
|
-
* ```
|
|
153
|
-
*/
|
|
146
|
+
/** Create a Layer from an already-constructed Service instance. */
|
|
154
147
|
static succeed(service, instance) {
|
|
155
|
-
|
|
148
|
+
const normalizedAcquire = normalizeAcquire(() => instance);
|
|
149
|
+
return new Layer([{
|
|
150
|
+
service,
|
|
151
|
+
acquire: normalizedAcquire
|
|
152
|
+
}]);
|
|
156
153
|
}
|
|
157
|
-
/**
|
|
158
|
-
* Define a provider with Runtime-root cleanup.
|
|
159
|
-
*
|
|
160
|
-
* The release callback intentionally keeps its compatibility-friendly
|
|
161
|
-
* one-argument shape and runs when the owning Runtime is disposed. Use
|
|
162
|
-
* `scopedGen` when acquisition needs contextual Services or cleanup needs
|
|
163
|
-
* `ScopeOutcome`.
|
|
164
|
-
*
|
|
165
|
-
* @example
|
|
166
|
-
* ```ts
|
|
167
|
-
* const DatabaseLive = Layer.scoped(
|
|
168
|
-
* Database,
|
|
169
|
-
* () => openDatabase(),
|
|
170
|
-
* (database) => database.close()
|
|
171
|
-
* )
|
|
172
|
-
* ```
|
|
173
|
-
*/
|
|
154
|
+
/** Define a provider with Runtime-root cleanup. */
|
|
174
155
|
static scoped(service, acquire, release) {
|
|
175
156
|
return new Layer([{
|
|
176
157
|
service,
|
|
177
|
-
acquire,
|
|
178
|
-
release: (instance) =>
|
|
158
|
+
acquire: normalizeAcquire(acquire),
|
|
159
|
+
release: (instance) => {
|
|
160
|
+
return release(instance);
|
|
161
|
+
}
|
|
179
162
|
}]);
|
|
180
163
|
}
|
|
181
|
-
/**
|
|
182
|
-
* Define a provider whose acquisition can yield contextual Services.
|
|
183
|
-
*
|
|
184
|
-
* The release callback receives the acquired instance and the final
|
|
185
|
-
* `ScopeOutcome` selected by the owning Runtime.
|
|
186
|
-
*
|
|
187
|
-
* @example
|
|
188
|
-
* ```ts
|
|
189
|
-
* const RepositoryLive = Layer.scopedGen(
|
|
190
|
-
* UserRepository,
|
|
191
|
-
* async function* () {
|
|
192
|
-
* const database = yield* Database
|
|
193
|
-
* return new UserRepository(database)
|
|
194
|
-
* },
|
|
195
|
-
* (repository, outcome) => repository.close(outcome)
|
|
196
|
-
* )
|
|
197
|
-
* ```
|
|
198
|
-
*/
|
|
164
|
+
/** Define a provider whose acquisition can yield contextual Services. */
|
|
199
165
|
static scopedGen(service, factory, release) {
|
|
200
166
|
return new Layer([{
|
|
201
167
|
service,
|
|
202
168
|
acquire: () => runLayerGenerator(service, factory),
|
|
203
|
-
release: (instance, outcome) =>
|
|
169
|
+
release: (instance, outcome) => {
|
|
170
|
+
return release(instance, outcome);
|
|
171
|
+
}
|
|
204
172
|
}]);
|
|
205
173
|
}
|
|
206
|
-
/**
|
|
207
|
-
* Define a provider whose acquisition can yield contextual Services.
|
|
208
|
-
*
|
|
209
|
-
* Unlike `scopedGen`, this variant has no release callback. Use it for
|
|
210
|
-
* providers whose lifetime is managed elsewhere or that need no cleanup.
|
|
211
|
-
*
|
|
212
|
-
* @example
|
|
213
|
-
* ```ts
|
|
214
|
-
* const RepositoryLive = Layer.gen(UserRepository, async function* () {
|
|
215
|
-
* const database = yield* Database
|
|
216
|
-
* return new UserRepository(database)
|
|
217
|
-
* })
|
|
218
|
-
* ```
|
|
219
|
-
*/
|
|
174
|
+
/** Define a provider whose acquisition can yield contextual Services. */
|
|
220
175
|
static gen(service, factory) {
|
|
221
|
-
return Layer
|
|
176
|
+
return new Layer([{
|
|
177
|
+
service,
|
|
178
|
+
acquire: () => runLayerGenerator(service, factory)
|
|
179
|
+
}]);
|
|
222
180
|
}
|
|
223
|
-
/**
|
|
224
|
-
* Compose Layers without replacing providers.
|
|
225
|
-
*
|
|
226
|
-
* Each Service tag may appear only once. Duplicate tags are rejected at
|
|
227
|
-
* runtime; use `override` when replacement is intentional.
|
|
228
|
-
*
|
|
229
|
-
* @example
|
|
230
|
-
* ```ts
|
|
231
|
-
* const AppLive = Layer.merge(DatabaseLive, RepositoryLive)
|
|
232
|
-
* ```
|
|
233
|
-
*/
|
|
181
|
+
/** Compose Layers without replacing providers. */
|
|
234
182
|
static merge(...layers) {
|
|
235
183
|
const providers = /* @__PURE__ */ new Map();
|
|
236
184
|
for (const layer of layers) for (const provider of layer.providers) {
|
|
@@ -244,19 +192,7 @@ var Layer = class Layer {
|
|
|
244
192
|
}
|
|
245
193
|
return new Layer([...providers.values()]);
|
|
246
194
|
}
|
|
247
|
-
/**
|
|
248
|
-
* Replace providers in a base Layer, using tag identity and compatible
|
|
249
|
-
* instance contracts.
|
|
250
|
-
*
|
|
251
|
-
* Overrides are applied from left to right; the last compatible provider for
|
|
252
|
-
* a tag wins. Incompatible same-tag replacements remain visible as a type
|
|
253
|
-
* diagnostic and cannot be passed as a complete Layer.
|
|
254
|
-
*
|
|
255
|
-
* @example
|
|
256
|
-
* ```ts
|
|
257
|
-
* const TestLive = Layer.override(AppLive, Layer.succeed(Database, fakeDb))
|
|
258
|
-
* ```
|
|
259
|
-
*/
|
|
195
|
+
/** Replace providers in a base Layer, using tag identity and compatible contracts. */
|
|
260
196
|
static override(base, ...overrides) {
|
|
261
197
|
const providers = /* @__PURE__ */ new Map();
|
|
262
198
|
for (const provider of base.providers) providers.set(provider.service.serviceTag, provider);
|
|
@@ -264,6 +200,9 @@ var Layer = class Layer {
|
|
|
264
200
|
return new Layer([...providers.values()]);
|
|
265
201
|
}
|
|
266
202
|
};
|
|
203
|
+
const normalizeAcquire = (acquire) => () => {
|
|
204
|
+
return acquire();
|
|
205
|
+
};
|
|
267
206
|
//#endregion
|
|
268
207
|
//#region src/scope/errors.ts
|
|
269
208
|
/** Thrown when Scope context is accessed outside an active Scope execution. */
|
|
@@ -303,9 +242,9 @@ const SCOPE_SUCCESS$2 = { status: "success" };
|
|
|
303
242
|
const getDisposeFinalizer = (resource) => {
|
|
304
243
|
const candidate = Object(resource);
|
|
305
244
|
const asyncDispose = candidate[Symbol.asyncDispose];
|
|
306
|
-
if (
|
|
245
|
+
if (asyncDispose instanceof Function) return () => asyncDispose.call(resource);
|
|
307
246
|
const dispose = candidate[Symbol.dispose];
|
|
308
|
-
if (
|
|
247
|
+
if (dispose instanceof Function) return () => dispose.call(resource);
|
|
309
248
|
};
|
|
310
249
|
/** Dispose a value immediately when it implements a disposal protocol. */
|
|
311
250
|
const disposeResource = (resource) => {
|
|
@@ -504,40 +443,75 @@ const Scope = {
|
|
|
504
443
|
};
|
|
505
444
|
//#endregion
|
|
506
445
|
//#region src/effect/combinators.ts
|
|
507
|
-
const
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
const andThenResult = (result, next) =>
|
|
514
|
-
const
|
|
446
|
+
const mapResult = (result, fn) => {
|
|
447
|
+
return Result.map(result, fn);
|
|
448
|
+
};
|
|
449
|
+
const mapErrorResult = (result, fn) => {
|
|
450
|
+
return Result.mapError(result, fn);
|
|
451
|
+
};
|
|
452
|
+
const andThenResult = (result, next) => {
|
|
453
|
+
const resultNext = next;
|
|
454
|
+
return Result.andThen(result, resultNext);
|
|
455
|
+
};
|
|
456
|
+
const andThenAsyncResult = (result, next) => {
|
|
457
|
+
const resultNext = (value) => {
|
|
458
|
+
return Promise.resolve(next(value));
|
|
459
|
+
};
|
|
460
|
+
return Result.andThenAsync(result, resultNext);
|
|
461
|
+
};
|
|
515
462
|
function map(first, second) {
|
|
516
|
-
if (
|
|
463
|
+
if (first instanceof Function && second === void 0) {
|
|
464
|
+
const callback = first;
|
|
465
|
+
return (effect) => {
|
|
466
|
+
return map(effect, callback);
|
|
467
|
+
};
|
|
468
|
+
}
|
|
517
469
|
const fn = second;
|
|
518
|
-
if (isPromiseLike(first)) return Promise.resolve(first).then((result) =>
|
|
470
|
+
if (isPromiseLike(first)) return Promise.resolve(first).then((result) => {
|
|
471
|
+
return mapResult(result, fn);
|
|
472
|
+
});
|
|
519
473
|
return mapResult(first, fn);
|
|
520
474
|
}
|
|
521
475
|
function mapError(first, second) {
|
|
522
|
-
if (
|
|
476
|
+
if (first instanceof Function && second === void 0) {
|
|
477
|
+
const callback = first;
|
|
478
|
+
return (effect) => {
|
|
479
|
+
return mapError(effect, callback);
|
|
480
|
+
};
|
|
481
|
+
}
|
|
523
482
|
const fn = second;
|
|
524
|
-
if (isPromiseLike(first)) return Promise.resolve(first).then((result) =>
|
|
483
|
+
if (isPromiseLike(first)) return Promise.resolve(first).then((result) => {
|
|
484
|
+
return mapErrorResult(result, fn);
|
|
485
|
+
});
|
|
525
486
|
return mapErrorResult(first, fn);
|
|
526
487
|
}
|
|
527
488
|
function andThen(first, second) {
|
|
528
|
-
if (
|
|
489
|
+
if (first instanceof Function && second === void 0) {
|
|
490
|
+
const callback = first;
|
|
491
|
+
return (effect) => {
|
|
492
|
+
return andThen(effect, callback);
|
|
493
|
+
};
|
|
494
|
+
}
|
|
529
495
|
return andThenResult(first, second);
|
|
530
496
|
}
|
|
531
497
|
function andThenAsync(first, second) {
|
|
532
|
-
if (
|
|
498
|
+
if (first instanceof Function && second === void 0) {
|
|
499
|
+
const callback = first;
|
|
500
|
+
return (effect) => {
|
|
501
|
+
return andThenAsync(effect, callback);
|
|
502
|
+
};
|
|
503
|
+
}
|
|
533
504
|
const next = second;
|
|
534
|
-
if (isPromiseLike(first)) return Promise.resolve(first).then((result) =>
|
|
505
|
+
if (isPromiseLike(first)) return Promise.resolve(first).then((result) => {
|
|
506
|
+
return andThenAsyncResult(result, next);
|
|
507
|
+
});
|
|
535
508
|
return andThenAsyncResult(first, next);
|
|
536
509
|
}
|
|
537
510
|
//#endregion
|
|
538
511
|
//#region src/effect/effect.ts
|
|
539
512
|
function gen(body) {
|
|
540
|
-
|
|
513
|
+
const runResultGenerator = Result.gen;
|
|
514
|
+
return runResultGenerator(body);
|
|
541
515
|
}
|
|
542
516
|
/**
|
|
543
517
|
* Acquire a resource in the current Scope and register its release callback.
|
|
@@ -680,7 +654,10 @@ const Resource = {
|
|
|
680
654
|
acquireUseRelease };
|
|
681
655
|
//#endregion
|
|
682
656
|
//#region src/runtime/outcome.ts
|
|
683
|
-
const isResultLike = (value) =>
|
|
657
|
+
const isResultLike = (value) => {
|
|
658
|
+
const candidate = Object(value);
|
|
659
|
+
return Object.prototype.toString.call(value) !== "[object Function]" && "status" in candidate && (candidate.status === "ok" || candidate.status === "error");
|
|
660
|
+
};
|
|
684
661
|
const classifyRuntimeOutcome = (value) => {
|
|
685
662
|
if (isResultLike(value) && Result.isError(value)) return {
|
|
686
663
|
status: "failure",
|
|
@@ -825,9 +802,7 @@ const createRuntimeHandle = async (layer, backend, options = {}) => {
|
|
|
825
802
|
error: shutdownError
|
|
826
803
|
});
|
|
827
804
|
}
|
|
828
|
-
|
|
829
|
-
if (cleanupCauses.length === 1) cleanupCause = cleanupCauses[0];
|
|
830
|
-
else if (cleanupCauses.length > 1) cleanupCause = new LayerDisposeError(cleanupCauses.flatMap(normalizeDisposeCauses));
|
|
805
|
+
const cleanupCause = cleanupCauses.length === 1 ? cleanupCauses[0] : cleanupCauses.length > 1 ? new LayerDisposeError(cleanupCauses.flatMap(normalizeDisposeCauses)) : void 0;
|
|
831
806
|
throw new LayerRegistrationError(current?.service, registrationCause, cleanupCause);
|
|
832
807
|
}
|
|
833
808
|
return new RuntimeHandleImpl(backend, rootScope, options.onCleanupFailure);
|
|
@@ -847,7 +822,7 @@ const createRuntimeHandle = async (layer, backend, options = {}) => {
|
|
|
847
822
|
* await runtime.dispose()
|
|
848
823
|
* ```
|
|
849
824
|
*
|
|
850
|
-
* @typeParam Provided The Service
|
|
825
|
+
* @typeParam Provided The branded Service instances supplied by the Layer.
|
|
851
826
|
*/
|
|
852
827
|
var Runtime = class Runtime {
|
|
853
828
|
handle;
|