effect 3.4.4 → 3.4.6
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/dist/cjs/Match.js.map +1 -1
- package/dist/cjs/Micro.js +481 -425
- package/dist/cjs/Micro.js.map +1 -1
- package/dist/cjs/internal/channel.js +5 -2
- package/dist/cjs/internal/channel.js.map +1 -1
- package/dist/cjs/internal/core-effect.js +14 -11
- package/dist/cjs/internal/core-effect.js.map +1 -1
- package/dist/cjs/internal/fiberRuntime.js +4 -9
- package/dist/cjs/internal/fiberRuntime.js.map +1 -1
- package/dist/cjs/internal/matcher.js.map +1 -1
- package/dist/cjs/internal/version.js +1 -1
- package/dist/dts/Match.d.ts +16 -16
- package/dist/dts/Match.d.ts.map +1 -1
- package/dist/dts/Micro.d.ts +344 -260
- package/dist/dts/Micro.d.ts.map +1 -1
- package/dist/dts/internal/core-effect.d.ts.map +1 -1
- package/dist/esm/Match.js.map +1 -1
- package/dist/esm/Micro.js +456 -402
- package/dist/esm/Micro.js.map +1 -1
- package/dist/esm/internal/channel.js +5 -2
- package/dist/esm/internal/channel.js.map +1 -1
- package/dist/esm/internal/core-effect.js +11 -9
- package/dist/esm/internal/core-effect.js.map +1 -1
- package/dist/esm/internal/fiberRuntime.js +4 -9
- package/dist/esm/internal/fiberRuntime.js.map +1 -1
- package/dist/esm/internal/matcher.js.map +1 -1
- package/dist/esm/internal/version.js +1 -1
- package/package.json +1 -1
- package/src/Match.ts +20 -20
- package/src/Micro.ts +715 -603
- package/src/internal/channel.ts +17 -19
- package/src/internal/core-effect.ts +14 -10
- package/src/internal/fiberRuntime.ts +4 -10
- package/src/internal/matcher.ts +20 -19
- package/src/internal/version.ts +1 -1
package/src/Micro.ts
CHANGED
|
@@ -64,7 +64,7 @@ export type runSymbol = typeof runSymbol
|
|
|
64
64
|
*/
|
|
65
65
|
export interface Micro<out A, out E = never, out R = never> extends Effect<A, E, R> {
|
|
66
66
|
readonly [TypeId]: Micro.Variance<A, E, R>
|
|
67
|
-
readonly [runSymbol]: (env: Env<any>,
|
|
67
|
+
readonly [runSymbol]: (env: Env<any>, onExit: (exit: MicroExit<A, E>) => void) => void
|
|
68
68
|
[Unify.typeSymbol]?: unknown
|
|
69
69
|
[Unify.unifySymbol]?: MicroUnify<this>
|
|
70
70
|
[Unify.ignoreSymbol]?: MicroUnifyIgnore
|
|
@@ -145,44 +145,50 @@ export interface MicroIterator<T extends Micro<any, any, any>> {
|
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
// ----------------------------------------------------------------------------
|
|
148
|
-
//
|
|
148
|
+
// MicroCause
|
|
149
149
|
// ----------------------------------------------------------------------------
|
|
150
150
|
|
|
151
151
|
/**
|
|
152
|
-
* @since 3.4.
|
|
152
|
+
* @since 3.4.6
|
|
153
153
|
* @experimental
|
|
154
|
-
* @category
|
|
154
|
+
* @category MicroCause
|
|
155
155
|
*/
|
|
156
|
-
export const
|
|
156
|
+
export const MicroCauseTypeId = Symbol.for("effect/Micro/MicroCause")
|
|
157
157
|
|
|
158
158
|
/**
|
|
159
|
-
* @since 3.4.
|
|
159
|
+
* @since 3.4.6
|
|
160
160
|
* @experimental
|
|
161
|
-
* @category
|
|
161
|
+
* @category MicroCause
|
|
162
162
|
*/
|
|
163
|
-
export type
|
|
163
|
+
export type MicroCauseTypeId = typeof MicroCauseTypeId
|
|
164
164
|
|
|
165
165
|
/**
|
|
166
|
-
* A Micro
|
|
166
|
+
* A Micro Cause is a data type that represents the different ways a Micro can fail.
|
|
167
167
|
*
|
|
168
|
-
* @since 3.4.
|
|
168
|
+
* @since 3.4.6
|
|
169
169
|
* @experimental
|
|
170
|
-
* @category
|
|
170
|
+
* @category MicroCause
|
|
171
171
|
*/
|
|
172
|
-
export type
|
|
172
|
+
export type MicroCause<E> = MicroCause.Die | MicroCause.Fail<E> | MicroCause.Interrupt
|
|
173
173
|
|
|
174
174
|
/**
|
|
175
|
-
* @since 3.4.
|
|
175
|
+
* @since 3.4.6
|
|
176
176
|
* @experimental
|
|
177
|
-
* @category
|
|
177
|
+
* @category MicroCause
|
|
178
178
|
*/
|
|
179
|
-
export declare namespace
|
|
179
|
+
export declare namespace MicroCause {
|
|
180
|
+
/**
|
|
181
|
+
* @since 3.4.6
|
|
182
|
+
* @experimental
|
|
183
|
+
*/
|
|
184
|
+
export type Error<T> = T extends MicroCause.Fail<infer E> ? E : never
|
|
185
|
+
|
|
180
186
|
/**
|
|
181
187
|
* @since 3.4.0
|
|
182
188
|
* @experimental
|
|
183
189
|
*/
|
|
184
190
|
export interface Proto<Tag extends string, E> extends Pipeable, globalThis.Error {
|
|
185
|
-
readonly [
|
|
191
|
+
readonly [MicroCauseTypeId]: {
|
|
186
192
|
_E: Covariant<E>
|
|
187
193
|
}
|
|
188
194
|
readonly _tag: Tag
|
|
@@ -190,37 +196,37 @@ export declare namespace Failure {
|
|
|
190
196
|
}
|
|
191
197
|
|
|
192
198
|
/**
|
|
193
|
-
* @since 3.4.
|
|
199
|
+
* @since 3.4.6
|
|
194
200
|
* @experimental
|
|
195
|
-
* @category
|
|
201
|
+
* @category MicroCause
|
|
196
202
|
*/
|
|
197
|
-
export interface
|
|
203
|
+
export interface Die extends Proto<"Die", never> {
|
|
198
204
|
readonly defect: unknown
|
|
199
205
|
}
|
|
200
206
|
|
|
201
207
|
/**
|
|
202
|
-
* @since 3.4.
|
|
208
|
+
* @since 3.4.6
|
|
203
209
|
* @experimental
|
|
204
|
-
* @category
|
|
210
|
+
* @category MicroCause
|
|
205
211
|
*/
|
|
206
|
-
export interface
|
|
212
|
+
export interface Fail<E> extends Proto<"Fail", E> {
|
|
207
213
|
readonly error: E
|
|
208
214
|
}
|
|
209
215
|
|
|
210
216
|
/**
|
|
211
|
-
* @since 3.4.
|
|
217
|
+
* @since 3.4.6
|
|
212
218
|
* @experimental
|
|
213
|
-
* @category
|
|
219
|
+
* @category MicroCause
|
|
214
220
|
*/
|
|
215
|
-
export interface
|
|
221
|
+
export interface Interrupt extends Proto<"Interrupt", never> {}
|
|
216
222
|
}
|
|
217
223
|
|
|
218
|
-
const
|
|
224
|
+
const microCauseVariance = {
|
|
219
225
|
_E: identity
|
|
220
226
|
}
|
|
221
227
|
|
|
222
|
-
abstract class
|
|
223
|
-
readonly [
|
|
228
|
+
abstract class MicroCauseImpl<Tag extends string, E> extends globalThis.Error implements MicroCause.Proto<Tag, E> {
|
|
229
|
+
readonly [MicroCauseTypeId]: {
|
|
224
230
|
_E: Covariant<E>
|
|
225
231
|
}
|
|
226
232
|
constructor(
|
|
@@ -228,19 +234,19 @@ abstract class FailureImpl<Tag extends string, E> extends globalThis.Error imple
|
|
|
228
234
|
originalError: unknown,
|
|
229
235
|
readonly traces: ReadonlyArray<string>
|
|
230
236
|
) {
|
|
231
|
-
const
|
|
237
|
+
const causeName = `MicroCause.${_tag}`
|
|
232
238
|
let name: string
|
|
233
239
|
let message: string
|
|
234
240
|
let stack: string
|
|
235
241
|
if (originalError instanceof globalThis.Error) {
|
|
236
|
-
name = `(${
|
|
242
|
+
name = `(${causeName}) ${originalError.name}`
|
|
237
243
|
message = originalError.message as string
|
|
238
244
|
const messageLines = message.split("\n").length
|
|
239
245
|
stack = originalError.stack
|
|
240
|
-
? `(${
|
|
246
|
+
? `(${causeName}) ${originalError.stack.split("\n").slice(0, messageLines + 3).join("\n")}`
|
|
241
247
|
: `${name}: ${message}`
|
|
242
248
|
} else {
|
|
243
|
-
name =
|
|
249
|
+
name = causeName
|
|
244
250
|
message = toStringUnknown(originalError, 0)
|
|
245
251
|
stack = `${name}: ${message}`
|
|
246
252
|
}
|
|
@@ -248,7 +254,7 @@ abstract class FailureImpl<Tag extends string, E> extends globalThis.Error imple
|
|
|
248
254
|
stack += `\n ${traces.join("\n ")}`
|
|
249
255
|
}
|
|
250
256
|
super(message)
|
|
251
|
-
this[
|
|
257
|
+
this[MicroCauseTypeId] = microCauseVariance
|
|
252
258
|
this.name = name
|
|
253
259
|
this.stack = stack
|
|
254
260
|
}
|
|
@@ -263,188 +269,211 @@ abstract class FailureImpl<Tag extends string, E> extends globalThis.Error imple
|
|
|
263
269
|
}
|
|
264
270
|
}
|
|
265
271
|
|
|
266
|
-
class
|
|
272
|
+
class FailImpl<E> extends MicroCauseImpl<"Fail", E> implements MicroCause.Fail<E> {
|
|
267
273
|
constructor(readonly error: E, traces: ReadonlyArray<string> = []) {
|
|
268
|
-
super("
|
|
274
|
+
super("Fail", error, traces)
|
|
269
275
|
}
|
|
270
276
|
}
|
|
271
277
|
|
|
272
278
|
/**
|
|
273
|
-
* @since 3.4.
|
|
279
|
+
* @since 3.4.6
|
|
274
280
|
* @experimental
|
|
275
|
-
* @category
|
|
281
|
+
* @category MicroCause
|
|
276
282
|
*/
|
|
277
|
-
export const
|
|
278
|
-
new FailureExpectedImpl(error, traces)
|
|
283
|
+
export const causeFail = <E>(error: E, traces: ReadonlyArray<string> = []): MicroCause<E> => new FailImpl(error, traces)
|
|
279
284
|
|
|
280
|
-
class
|
|
285
|
+
class DieImpl extends MicroCauseImpl<"Die", never> implements MicroCause.Die {
|
|
281
286
|
constructor(readonly defect: unknown, traces: ReadonlyArray<string> = []) {
|
|
282
|
-
super("
|
|
287
|
+
super("Die", defect, traces)
|
|
283
288
|
}
|
|
284
289
|
}
|
|
285
290
|
|
|
286
291
|
/**
|
|
287
|
-
* @since 3.4.
|
|
292
|
+
* @since 3.4.6
|
|
288
293
|
* @experimental
|
|
289
|
-
* @category
|
|
294
|
+
* @category MicroCause
|
|
290
295
|
*/
|
|
291
|
-
export const
|
|
292
|
-
new
|
|
296
|
+
export const causeDie = (defect: unknown, traces: ReadonlyArray<string> = []): MicroCause<never> =>
|
|
297
|
+
new DieImpl(defect, traces)
|
|
293
298
|
|
|
294
|
-
class
|
|
299
|
+
class InterruptImpl extends MicroCauseImpl<"Interrupt", never> implements MicroCause.Interrupt {
|
|
295
300
|
constructor(traces: ReadonlyArray<string> = []) {
|
|
296
|
-
super("
|
|
301
|
+
super("Interrupt", "interrupted", traces)
|
|
297
302
|
}
|
|
298
303
|
}
|
|
299
304
|
|
|
300
305
|
/**
|
|
301
|
-
* @since 3.4.
|
|
306
|
+
* @since 3.4.6
|
|
302
307
|
* @experimental
|
|
303
|
-
* @category
|
|
308
|
+
* @category MicroCause
|
|
304
309
|
*/
|
|
305
|
-
export const
|
|
310
|
+
export const causeInterrupt = (traces: ReadonlyArray<string> = []): MicroCause<never> => new InterruptImpl(traces)
|
|
306
311
|
|
|
307
312
|
/**
|
|
308
|
-
* @since 3.4.
|
|
313
|
+
* @since 3.4.6
|
|
309
314
|
* @experimental
|
|
310
|
-
* @category
|
|
315
|
+
* @category MicroCause
|
|
311
316
|
*/
|
|
312
|
-
export const
|
|
317
|
+
export const causeIsFail = <E>(self: MicroCause<E>): self is MicroCause.Fail<E> => self._tag === "Fail"
|
|
313
318
|
|
|
314
319
|
/**
|
|
315
|
-
* @since 3.4.
|
|
320
|
+
* @since 3.4.6
|
|
316
321
|
* @experimental
|
|
317
|
-
* @category
|
|
322
|
+
* @category MicroCause
|
|
318
323
|
*/
|
|
319
|
-
export const
|
|
324
|
+
export const causeIsDie = <E>(self: MicroCause<E>): self is MicroCause.Die => self._tag === "Die"
|
|
320
325
|
|
|
321
326
|
/**
|
|
322
|
-
* @since 3.4.
|
|
327
|
+
* @since 3.4.6
|
|
323
328
|
* @experimental
|
|
324
|
-
* @category
|
|
329
|
+
* @category MicroCause
|
|
325
330
|
*/
|
|
326
|
-
export const
|
|
331
|
+
export const causeIsInterrupt = <E>(self: MicroCause<E>): self is MicroCause.Interrupt => self._tag === "Interrupt"
|
|
327
332
|
|
|
328
333
|
/**
|
|
329
|
-
* @since 3.4.
|
|
334
|
+
* @since 3.4.6
|
|
330
335
|
* @experimental
|
|
331
|
-
* @category
|
|
336
|
+
* @category MicroCause
|
|
332
337
|
*/
|
|
333
|
-
export const
|
|
334
|
-
self._tag === "
|
|
338
|
+
export const causeSquash = <E>(self: MicroCause<E>): unknown =>
|
|
339
|
+
self._tag === "Fail" ? self.error : self._tag === "Die" ? self.defect : self
|
|
335
340
|
|
|
336
341
|
/**
|
|
337
|
-
* @since 3.4.
|
|
342
|
+
* @since 3.4.6
|
|
338
343
|
* @experimental
|
|
339
|
-
* @category
|
|
344
|
+
* @category MicroCause
|
|
340
345
|
*/
|
|
341
|
-
export const
|
|
342
|
-
(trace: string): <E>(self:
|
|
343
|
-
<E>(self:
|
|
344
|
-
} = dual(2, <E>(self:
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
346
|
+
export const causeWithTrace: {
|
|
347
|
+
(trace: string): <E>(self: MicroCause<E>) => MicroCause<E>
|
|
348
|
+
<E>(self: MicroCause<E>, trace: string): MicroCause<E>
|
|
349
|
+
} = dual(2, <E>(self: MicroCause<E>, trace: string): MicroCause<E> => {
|
|
350
|
+
const traces = [...self.traces, trace]
|
|
351
|
+
switch (self._tag) {
|
|
352
|
+
case "Die":
|
|
353
|
+
return causeDie(self.defect, traces)
|
|
354
|
+
case "Interrupt":
|
|
355
|
+
return causeInterrupt(traces)
|
|
356
|
+
case "Fail":
|
|
357
|
+
return causeFail(self.error, traces)
|
|
349
358
|
}
|
|
350
|
-
return FailureAborted([...self.traces, trace])
|
|
351
359
|
})
|
|
352
360
|
|
|
353
361
|
// ----------------------------------------------------------------------------
|
|
354
|
-
//
|
|
362
|
+
// MicroExit
|
|
355
363
|
// ----------------------------------------------------------------------------
|
|
356
364
|
|
|
357
365
|
/**
|
|
358
|
-
*
|
|
366
|
+
* @since 3.4.6
|
|
367
|
+
* @experimental
|
|
368
|
+
* @category MicroExit
|
|
369
|
+
*/
|
|
370
|
+
export declare namespace MicroExit {
|
|
371
|
+
/**
|
|
372
|
+
* @since 3.4.6
|
|
373
|
+
* @experimental
|
|
374
|
+
* @category MicroExit
|
|
375
|
+
*/
|
|
376
|
+
export type Success<A, E = never> = Either.Right<MicroCause<E>, A>
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* @since 3.4.6
|
|
380
|
+
* @experimental
|
|
381
|
+
* @category MicroExit
|
|
382
|
+
*/
|
|
383
|
+
export type Failure<A, E = never> = Either.Left<MicroCause<E>, A>
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* The MicroExit type is a data type that represents the result of a Micro
|
|
359
388
|
* computation.
|
|
360
389
|
*
|
|
361
390
|
* It uses the `Either` data type to represent the success and failure cases.
|
|
362
391
|
*
|
|
363
|
-
* @since 3.4.
|
|
392
|
+
* @since 3.4.6
|
|
364
393
|
* @experimental
|
|
365
|
-
* @category
|
|
394
|
+
* @category MicroExit
|
|
366
395
|
*/
|
|
367
|
-
export type
|
|
396
|
+
export type MicroExit<A, E = never> = MicroExit.Success<A, E> | MicroExit.Failure<A, E>
|
|
368
397
|
|
|
369
398
|
/**
|
|
370
|
-
* @since 3.4.
|
|
399
|
+
* @since 3.4.6
|
|
371
400
|
* @experimental
|
|
372
|
-
* @category
|
|
401
|
+
* @category MicroExit
|
|
373
402
|
*/
|
|
374
|
-
export const
|
|
403
|
+
export const exitInterrupt: MicroExit<never> = Either.left(causeInterrupt())
|
|
375
404
|
|
|
376
405
|
/**
|
|
377
|
-
* @since 3.4.
|
|
406
|
+
* @since 3.4.6
|
|
378
407
|
* @experimental
|
|
379
|
-
* @category
|
|
408
|
+
* @category MicroExit
|
|
380
409
|
*/
|
|
381
|
-
export const
|
|
410
|
+
export const exitSucceed: <A>(a: A) => MicroExit<A, never> = Either.right
|
|
382
411
|
|
|
383
412
|
/**
|
|
384
|
-
* @since 3.4.
|
|
413
|
+
* @since 3.4.6
|
|
385
414
|
* @experimental
|
|
386
|
-
* @category
|
|
415
|
+
* @category MicroExit
|
|
387
416
|
*/
|
|
388
|
-
export const
|
|
417
|
+
export const exitFail = <E>(e: E): MicroExit<never, E> => Either.left(causeFail(e))
|
|
389
418
|
|
|
390
419
|
/**
|
|
391
|
-
* @since 3.4.
|
|
420
|
+
* @since 3.4.6
|
|
392
421
|
* @experimental
|
|
393
|
-
* @category
|
|
422
|
+
* @category MicroExit
|
|
394
423
|
*/
|
|
395
|
-
export const
|
|
424
|
+
export const exitDie = (defect: unknown): MicroExit<never> => Either.left(causeDie(defect))
|
|
396
425
|
|
|
397
426
|
/**
|
|
398
|
-
* @since 3.4.
|
|
427
|
+
* @since 3.4.6
|
|
399
428
|
* @experimental
|
|
400
|
-
* @category
|
|
429
|
+
* @category MicroExit
|
|
401
430
|
*/
|
|
402
|
-
export const
|
|
431
|
+
export const exitFailCause: <E>(cause: MicroCause<E>) => MicroExit<never, E> = Either.left
|
|
403
432
|
|
|
404
433
|
/**
|
|
405
|
-
* @since 3.4.
|
|
434
|
+
* @since 3.4.6
|
|
406
435
|
* @experimental
|
|
407
|
-
* @category
|
|
436
|
+
* @category MicroExit
|
|
408
437
|
*/
|
|
409
|
-
export const
|
|
438
|
+
export const exitIsSuccess: <A, E>(self: MicroExit<A, E>) => self is MicroExit.Success<A, E> = Either.isRight
|
|
410
439
|
|
|
411
440
|
/**
|
|
412
|
-
* @since 3.4.
|
|
441
|
+
* @since 3.4.6
|
|
413
442
|
* @experimental
|
|
414
|
-
* @category
|
|
443
|
+
* @category MicroExit
|
|
415
444
|
*/
|
|
416
|
-
export const
|
|
445
|
+
export const exitIsFailure: <A, E>(self: MicroExit<A, E>) => self is MicroExit.Failure<A, E> = Either.isLeft
|
|
417
446
|
|
|
418
447
|
/**
|
|
419
|
-
* @since 3.4.
|
|
448
|
+
* @since 3.4.6
|
|
420
449
|
* @experimental
|
|
421
|
-
* @category
|
|
450
|
+
* @category MicroExit
|
|
422
451
|
*/
|
|
423
|
-
export const
|
|
424
|
-
|
|
452
|
+
export const exitIsInterrupt = <A, E>(self: MicroExit<A, E>): self is Either.Left<MicroCause.Interrupt, A> =>
|
|
453
|
+
exitIsFailure(self) && self.left._tag === "Interrupt"
|
|
425
454
|
|
|
426
455
|
/**
|
|
427
|
-
* @since 3.4.
|
|
456
|
+
* @since 3.4.6
|
|
428
457
|
* @experimental
|
|
429
|
-
* @category
|
|
458
|
+
* @category MicroExit
|
|
430
459
|
*/
|
|
431
|
-
export const
|
|
432
|
-
|
|
460
|
+
export const exitIsFail = <A, E>(self: MicroExit<A, E>): self is Either.Left<MicroCause.Fail<E>, A> =>
|
|
461
|
+
exitIsFailure(self) && self.left._tag === "Fail"
|
|
433
462
|
|
|
434
463
|
/**
|
|
435
|
-
* @since 3.4.
|
|
464
|
+
* @since 3.4.6
|
|
436
465
|
* @experimental
|
|
437
|
-
* @category
|
|
466
|
+
* @category MicroExit
|
|
438
467
|
*/
|
|
439
|
-
export const
|
|
440
|
-
|
|
468
|
+
export const exitIsDie = <A, E>(self: MicroExit<A, E>): self is Either.Left<MicroCause.Die, A> =>
|
|
469
|
+
exitIsFailure(self) && self.left._tag === "Die"
|
|
441
470
|
|
|
442
471
|
/**
|
|
443
|
-
* @since 3.4.
|
|
472
|
+
* @since 3.4.6
|
|
444
473
|
* @experimental
|
|
445
|
-
* @category
|
|
474
|
+
* @category MicroExit
|
|
446
475
|
*/
|
|
447
|
-
export const
|
|
476
|
+
export const exitVoid: MicroExit<void> = exitSucceed(void 0)
|
|
448
477
|
|
|
449
478
|
// ----------------------------------------------------------------------------
|
|
450
479
|
// env
|
|
@@ -582,8 +611,8 @@ export const envMutate: {
|
|
|
582
611
|
* @category environment
|
|
583
612
|
*/
|
|
584
613
|
export const service = <I, S>(tag: Context.Tag<I, S>): Micro<S, never, I> =>
|
|
585
|
-
make(function(env,
|
|
586
|
-
|
|
614
|
+
make(function(env, onExit) {
|
|
615
|
+
onExit(exitSucceed(Context.get(envGet(env, currentContext) as Context.Context<I>, tag as any) as S))
|
|
587
616
|
})
|
|
588
617
|
|
|
589
618
|
/**
|
|
@@ -598,8 +627,8 @@ export const service = <I, S>(tag: Context.Tag<I, S>): Micro<S, never, I> =>
|
|
|
598
627
|
* @category environment
|
|
599
628
|
*/
|
|
600
629
|
export const serviceOption = <I, S>(tag: Context.Tag<I, S>): Micro<Option.Option<S>> =>
|
|
601
|
-
make(function(env,
|
|
602
|
-
|
|
630
|
+
make(function(env, onExit) {
|
|
631
|
+
onExit(exitSucceed(Context.getOption(envGet(env, currentContext) as Context.Context<I>, tag)))
|
|
603
632
|
})
|
|
604
633
|
|
|
605
634
|
/**
|
|
@@ -610,7 +639,7 @@ export const serviceOption = <I, S>(tag: Context.Tag<I, S>): Micro<Option.Option
|
|
|
610
639
|
* @category environment
|
|
611
640
|
*/
|
|
612
641
|
export const getEnvRef = <A>(envRef: EnvRef<A>): Micro<A> =>
|
|
613
|
-
make((env,
|
|
642
|
+
make((env, onExit) => onExit(Either.right(envGet(env, envRef))))
|
|
614
643
|
|
|
615
644
|
/**
|
|
616
645
|
* Set the value of the given `EnvRef` for the duration of the effect.
|
|
@@ -625,7 +654,7 @@ export const locally: {
|
|
|
625
654
|
} = dual(
|
|
626
655
|
3,
|
|
627
656
|
<XA, E, R, A>(self: Micro<XA, E, R>, fiberRef: EnvRef<A>, value: A): Micro<XA, E, R> =>
|
|
628
|
-
make((env,
|
|
657
|
+
make((env, onExit) => self[runSymbol](envSet(env, fiberRef, value), onExit))
|
|
629
658
|
)
|
|
630
659
|
|
|
631
660
|
/**
|
|
@@ -650,10 +679,10 @@ export const provideContext: {
|
|
|
650
679
|
} = dual(
|
|
651
680
|
2,
|
|
652
681
|
<A, E, R, XR>(self: Micro<A, E, R>, provided: Context.Context<XR>): Micro<A, E, Exclude<R, XR>> =>
|
|
653
|
-
make(function(env,
|
|
682
|
+
make(function(env, onExit) {
|
|
654
683
|
const context = envGet(env, currentContext)
|
|
655
684
|
const nextEnv = envSet(env, currentContext, Context.merge(context, provided))
|
|
656
|
-
self[runSymbol](nextEnv,
|
|
685
|
+
self[runSymbol](nextEnv, onExit)
|
|
657
686
|
})
|
|
658
687
|
)
|
|
659
688
|
|
|
@@ -670,10 +699,10 @@ export const provideService: {
|
|
|
670
699
|
} = dual(
|
|
671
700
|
3,
|
|
672
701
|
<A, E, R, I, S>(self: Micro<A, E, R>, tag: Context.Tag<I, S>, service: S): Micro<A, E, Exclude<R, I>> =>
|
|
673
|
-
make(function(env,
|
|
702
|
+
make(function(env, onExit) {
|
|
674
703
|
const context = envGet(env, currentContext)
|
|
675
704
|
const nextEnv = envSet(env, currentContext, Context.add(context, tag, service))
|
|
676
|
-
self[runSymbol](nextEnv,
|
|
705
|
+
self[runSymbol](nextEnv, onExit)
|
|
677
706
|
})
|
|
678
707
|
)
|
|
679
708
|
|
|
@@ -681,11 +710,11 @@ export const provideService: {
|
|
|
681
710
|
* Create a service using the provided `Micro` effect, and add it to the
|
|
682
711
|
* current context.
|
|
683
712
|
*
|
|
684
|
-
* @since 3.4.
|
|
713
|
+
* @since 3.4.6
|
|
685
714
|
* @experimental
|
|
686
715
|
* @category environment
|
|
687
716
|
*/
|
|
688
|
-
export const
|
|
717
|
+
export const provideServiceEffect: {
|
|
689
718
|
<I, S, E2, R2>(
|
|
690
719
|
tag: Context.Tag<I, S>,
|
|
691
720
|
acquire: Micro<S, E2, R2>
|
|
@@ -827,36 +856,36 @@ const microDepthState = globalValue("effect/Micro/microDepthState", () => ({
|
|
|
827
856
|
maxDepthBeforeYield: currentMaxDepthBeforeYield.initial
|
|
828
857
|
}))
|
|
829
858
|
|
|
830
|
-
const unsafeMake = <A, E
|
|
831
|
-
run: (env: Env<R>,
|
|
859
|
+
const unsafeMake = <R, A, E>(
|
|
860
|
+
run: (env: Env<R>, onExit: (exit: MicroExit<A, E>) => void) => void
|
|
832
861
|
): Micro<A, E, R> => {
|
|
833
862
|
const self = Object.create(MicroProto)
|
|
834
863
|
self[runSymbol] = run
|
|
835
864
|
return self
|
|
836
865
|
}
|
|
837
866
|
|
|
838
|
-
const unsafeMakeOptions = <A, E
|
|
839
|
-
run: (env: Env<R>,
|
|
867
|
+
const unsafeMakeOptions = <R, A, E>(
|
|
868
|
+
run: (env: Env<R>, onExit: (exit: MicroExit<A, E>) => void) => void,
|
|
840
869
|
checkAbort: boolean
|
|
841
870
|
): Micro<A, E, R> =>
|
|
842
|
-
unsafeMake(function execute(env,
|
|
871
|
+
unsafeMake(function execute(env, onExit) {
|
|
843
872
|
if (
|
|
844
873
|
checkAbort && env.refs[currentInterruptible.key] !== false &&
|
|
845
874
|
(env.refs[currentAbortSignal.key] as AbortSignal).aborted
|
|
846
875
|
) {
|
|
847
|
-
return
|
|
876
|
+
return onExit(exitInterrupt)
|
|
848
877
|
}
|
|
849
878
|
microDepthState.depth++
|
|
850
879
|
if (microDepthState.depth === 1) {
|
|
851
880
|
microDepthState.maxDepthBeforeYield = envGet(env, currentMaxDepthBeforeYield)
|
|
852
881
|
}
|
|
853
882
|
if (microDepthState.depth >= microDepthState.maxDepthBeforeYield) {
|
|
854
|
-
yieldAdd(() => execute(env,
|
|
883
|
+
yieldAdd(() => execute(env, onExit))
|
|
855
884
|
} else {
|
|
856
885
|
try {
|
|
857
|
-
run(env,
|
|
886
|
+
run(env, onExit)
|
|
858
887
|
} catch (err) {
|
|
859
|
-
|
|
888
|
+
onExit(exitDie(err))
|
|
860
889
|
}
|
|
861
890
|
}
|
|
862
891
|
microDepthState.depth--
|
|
@@ -871,32 +900,32 @@ const unsafeMakeOptions = <A, E, R>(
|
|
|
871
900
|
* @experimental
|
|
872
901
|
* @category constructors
|
|
873
902
|
*/
|
|
874
|
-
export const make = <A, E
|
|
875
|
-
run: (env: Env<R>,
|
|
903
|
+
export const make = <R, A, E>(
|
|
904
|
+
run: (env: Env<R>, onExit: (exit: MicroExit<A, E>) => void) => void
|
|
876
905
|
): Micro<A, E, R> => unsafeMakeOptions(run, true)
|
|
877
906
|
|
|
878
907
|
/**
|
|
879
|
-
* Converts a `
|
|
908
|
+
* Converts a `MicroExit` into a `Micro` effect.
|
|
880
909
|
*
|
|
881
|
-
* @since 3.4.
|
|
910
|
+
* @since 3.4.6
|
|
882
911
|
* @experimental
|
|
883
912
|
* @category constructors
|
|
884
913
|
*/
|
|
885
|
-
export const
|
|
886
|
-
make(function(_env,
|
|
887
|
-
|
|
914
|
+
export const fromExit = <A, E>(self: MicroExit<A, E>): Micro<A, E> =>
|
|
915
|
+
make(function(_env, onExit) {
|
|
916
|
+
onExit(self)
|
|
888
917
|
})
|
|
889
918
|
|
|
890
919
|
/**
|
|
891
|
-
* Converts a lazy `
|
|
920
|
+
* Converts a lazy `MicroExit` into a `Micro` effect.
|
|
892
921
|
*
|
|
893
|
-
* @since 3.4.
|
|
922
|
+
* @since 3.4.6
|
|
894
923
|
* @experimental
|
|
895
924
|
* @category constructors
|
|
896
925
|
*/
|
|
897
|
-
export const
|
|
898
|
-
make(function(_env,
|
|
899
|
-
|
|
926
|
+
export const fromExitSync = <A, E>(self: LazyArg<MicroExit<A, E>>): Micro<A, E> =>
|
|
927
|
+
make(function(_env, onExit) {
|
|
928
|
+
onExit(self())
|
|
900
929
|
})
|
|
901
930
|
|
|
902
931
|
/**
|
|
@@ -906,7 +935,7 @@ export const fromResultSync = <A, E>(self: LazyArg<Result<A, E>>): Micro<A, E> =
|
|
|
906
935
|
* @experimental
|
|
907
936
|
* @category constructors
|
|
908
937
|
*/
|
|
909
|
-
export const succeed = <A>(a: A): Micro<A> =>
|
|
938
|
+
export const succeed = <A>(a: A): Micro<A> => fromExit(exitSucceed(a))
|
|
910
939
|
|
|
911
940
|
/**
|
|
912
941
|
* Creates a `Micro` effect that will succeed with `Option.Some` of the value.
|
|
@@ -929,19 +958,19 @@ export const succeedNone: Micro<Option.Option<never>> = succeed(Option.none())
|
|
|
929
958
|
/**
|
|
930
959
|
* Creates a `Micro` effect that will fail with the specified error.
|
|
931
960
|
*
|
|
932
|
-
* This will result in a `
|
|
961
|
+
* This will result in a `CauseFail`, where the error is tracked at the
|
|
933
962
|
* type level.
|
|
934
963
|
*
|
|
935
964
|
* @since 3.4.0
|
|
936
965
|
* @experimental
|
|
937
966
|
* @category constructors
|
|
938
967
|
*/
|
|
939
|
-
export const fail = <E>(e: E): Micro<never, E> =>
|
|
968
|
+
export const fail = <E>(e: E): Micro<never, E> => fromExit(exitFail(e))
|
|
940
969
|
|
|
941
970
|
/**
|
|
942
971
|
* Creates a `Micro` effect that will fail with the lazily evaluated error.
|
|
943
972
|
*
|
|
944
|
-
* This will result in a `
|
|
973
|
+
* This will result in a `CauseFail`, where the error is tracked at the
|
|
945
974
|
* type level.
|
|
946
975
|
*
|
|
947
976
|
* @since 3.4.0
|
|
@@ -949,54 +978,54 @@ export const fail = <E>(e: E): Micro<never, E> => fromResult(ResultFail(e))
|
|
|
949
978
|
* @category constructors
|
|
950
979
|
*/
|
|
951
980
|
export const failSync = <E>(e: LazyArg<E>): Micro<never, E> =>
|
|
952
|
-
make(function(_env,
|
|
953
|
-
|
|
981
|
+
make(function(_env, onExit) {
|
|
982
|
+
onExit(exitFail(e()))
|
|
954
983
|
})
|
|
955
984
|
|
|
956
985
|
/**
|
|
957
986
|
* Creates a `Micro` effect that will die with the specified error.
|
|
958
987
|
*
|
|
959
|
-
* This will result in a `
|
|
988
|
+
* This will result in a `CauseDie`, where the error is not tracked at
|
|
960
989
|
* the type level.
|
|
961
990
|
*
|
|
962
991
|
* @since 3.4.0
|
|
963
992
|
* @experimental
|
|
964
993
|
* @category constructors
|
|
965
994
|
*/
|
|
966
|
-
export const die = (defect: unknown): Micro<never> =>
|
|
995
|
+
export const die = (defect: unknown): Micro<never> => fromExit(exitDie(defect))
|
|
967
996
|
|
|
968
997
|
/**
|
|
969
|
-
* Creates a `Micro` effect that will fail with the specified `
|
|
998
|
+
* Creates a `Micro` effect that will fail with the specified `MicroCause`.
|
|
970
999
|
*
|
|
971
|
-
* @since 3.4.
|
|
1000
|
+
* @since 3.4.6
|
|
972
1001
|
* @experimental
|
|
973
1002
|
* @category constructors
|
|
974
1003
|
*/
|
|
975
|
-
export const
|
|
1004
|
+
export const failCause = <E>(cause: MicroCause<E>): Micro<never, E> => fromExit(exitFailCause(cause))
|
|
976
1005
|
|
|
977
1006
|
/**
|
|
978
|
-
* Creates a `Micro` effect that will fail with the lazily evaluated `
|
|
1007
|
+
* Creates a `Micro` effect that will fail with the lazily evaluated `MicroCause`.
|
|
979
1008
|
*
|
|
980
|
-
* @since 3.4.
|
|
1009
|
+
* @since 3.4.6
|
|
981
1010
|
* @experimental
|
|
982
1011
|
* @category constructors
|
|
983
1012
|
*/
|
|
984
|
-
export const
|
|
985
|
-
|
|
1013
|
+
export const failCauseSync = <E>(cause: LazyArg<MicroCause<E>>): Micro<never, E> =>
|
|
1014
|
+
fromExitSync(() => exitFailCause(cause()))
|
|
986
1015
|
|
|
987
1016
|
/**
|
|
988
1017
|
* Creates a `Micro` effect that will succeed with the lazily evaluated value.
|
|
989
1018
|
*
|
|
990
1019
|
* If the evaluation of the value throws an error, the effect will fail with
|
|
991
|
-
* `
|
|
1020
|
+
* `CauseDie`.
|
|
992
1021
|
*
|
|
993
1022
|
* @since 3.4.0
|
|
994
1023
|
* @experimental
|
|
995
1024
|
* @category constructors
|
|
996
1025
|
*/
|
|
997
1026
|
export const sync = <A>(evaluate: LazyArg<A>): Micro<A> =>
|
|
998
|
-
make(function(_env,
|
|
999
|
-
|
|
1027
|
+
make(function(_env, onExit) {
|
|
1028
|
+
onExit(exitSucceed(evaluate()))
|
|
1000
1029
|
})
|
|
1001
1030
|
|
|
1002
1031
|
/**
|
|
@@ -1009,8 +1038,8 @@ export const sync = <A>(evaluate: LazyArg<A>): Micro<A> =>
|
|
|
1009
1038
|
* @category constructors
|
|
1010
1039
|
*/
|
|
1011
1040
|
export const fromOption = <A>(option: Option.Option<A>): Micro<A, NoSuchElementException> =>
|
|
1012
|
-
make(function(_env,
|
|
1013
|
-
|
|
1041
|
+
make(function(_env, onExit) {
|
|
1042
|
+
onExit(option._tag === "Some" ? exitSucceed(option.value) : exitFail(new NoSuchElementException({})))
|
|
1014
1043
|
})
|
|
1015
1044
|
|
|
1016
1045
|
/**
|
|
@@ -1023,8 +1052,8 @@ export const fromOption = <A>(option: Option.Option<A>): Micro<A, NoSuchElementE
|
|
|
1023
1052
|
* @category constructors
|
|
1024
1053
|
*/
|
|
1025
1054
|
export const fromEither = <R, L>(either: Either.Either<R, L>): Micro<R, L> =>
|
|
1026
|
-
make(function(_env,
|
|
1027
|
-
|
|
1055
|
+
make(function(_env, onExit) {
|
|
1056
|
+
onExit(either._tag === "Right" ? either as MicroExit<R, never> : exitFail(either.left))
|
|
1028
1057
|
})
|
|
1029
1058
|
|
|
1030
1059
|
/**
|
|
@@ -1035,8 +1064,8 @@ export const fromEither = <R, L>(either: Either.Either<R, L>): Micro<R, L> =>
|
|
|
1035
1064
|
* @category constructors
|
|
1036
1065
|
*/
|
|
1037
1066
|
export const suspend = <A, E, R>(evaluate: LazyArg<Micro<A, E, R>>): Micro<A, E, R> =>
|
|
1038
|
-
make(function(env,
|
|
1039
|
-
evaluate()[runSymbol](env,
|
|
1067
|
+
make(function(env, onExit) {
|
|
1068
|
+
evaluate()[runSymbol](env, onExit)
|
|
1040
1069
|
})
|
|
1041
1070
|
|
|
1042
1071
|
const void_: Micro<void> = succeed(void 0)
|
|
@@ -1065,16 +1094,16 @@ export {
|
|
|
1065
1094
|
export const async = <A, E = never, R = never>(
|
|
1066
1095
|
register: (resume: (effect: Micro<A, E, R>) => void, signal: AbortSignal) => void | Micro<void, never, R>
|
|
1067
1096
|
): Micro<A, E, R> =>
|
|
1068
|
-
make(function(env,
|
|
1097
|
+
make(function(env, onExit) {
|
|
1069
1098
|
let resumed = false
|
|
1070
1099
|
const controller = register.length > 1 ? new AbortController() : undefined
|
|
1071
1100
|
const signal = envGet(env, currentAbortSignal)
|
|
1072
1101
|
let cleanup: Micro<void, never, R> | void = undefined
|
|
1073
1102
|
function onAbort() {
|
|
1074
1103
|
if (cleanup) {
|
|
1075
|
-
resume(uninterruptible(andThen(cleanup,
|
|
1104
|
+
resume(uninterruptible(andThen(cleanup, fromExit(exitInterrupt))))
|
|
1076
1105
|
} else {
|
|
1077
|
-
resume(
|
|
1106
|
+
resume(fromExit(exitInterrupt))
|
|
1078
1107
|
}
|
|
1079
1108
|
if (controller !== undefined) {
|
|
1080
1109
|
controller.abort()
|
|
@@ -1086,7 +1115,7 @@ export const async = <A, E = never, R = never>(
|
|
|
1086
1115
|
}
|
|
1087
1116
|
resumed = true
|
|
1088
1117
|
signal.removeEventListener("abort", onAbort)
|
|
1089
|
-
effect[runSymbol](env,
|
|
1118
|
+
effect[runSymbol](env, onExit)
|
|
1090
1119
|
}
|
|
1091
1120
|
cleanup = controller === undefined
|
|
1092
1121
|
? (register as any)(resume)
|
|
@@ -1099,11 +1128,11 @@ const try_ = <A, E>(options: {
|
|
|
1099
1128
|
try: LazyArg<A>
|
|
1100
1129
|
catch: (error: unknown) => E
|
|
1101
1130
|
}): Micro<A, E> =>
|
|
1102
|
-
make(function(_env,
|
|
1131
|
+
make(function(_env, onExit) {
|
|
1103
1132
|
try {
|
|
1104
|
-
|
|
1133
|
+
onExit(exitSucceed(options.try()))
|
|
1105
1134
|
} catch (err) {
|
|
1106
|
-
|
|
1135
|
+
onExit(exitFail(options.catch(err)))
|
|
1107
1136
|
}
|
|
1108
1137
|
})
|
|
1109
1138
|
export {
|
|
@@ -1127,7 +1156,7 @@ export {
|
|
|
1127
1156
|
|
|
1128
1157
|
/**
|
|
1129
1158
|
* Wrap a `Promise` into a `Micro` effect. Any errors will result in a
|
|
1130
|
-
* `
|
|
1159
|
+
* `CauseDie`.
|
|
1131
1160
|
*
|
|
1132
1161
|
* @since 3.4.0
|
|
1133
1162
|
* @experimental
|
|
@@ -1208,8 +1237,8 @@ const yieldAdd = (task: () => void) => {
|
|
|
1208
1237
|
* @experimental
|
|
1209
1238
|
* @category constructors
|
|
1210
1239
|
*/
|
|
1211
|
-
export const yieldNow: Micro<void> = make(function(_env,
|
|
1212
|
-
yieldAdd(() =>
|
|
1240
|
+
export const yieldNow: Micro<void> = make(function(_env, onExit) {
|
|
1241
|
+
yieldAdd(() => onExit(exitVoid))
|
|
1213
1242
|
})
|
|
1214
1243
|
|
|
1215
1244
|
/**
|
|
@@ -1252,7 +1281,7 @@ export const gen = <Self, Eff extends YieldWrap<Micro<any, any, any>>, AEff>(
|
|
|
1252
1281
|
[Eff] extends [never] ? never : [Eff] extends [YieldWrap<Micro<infer _A, infer E, infer _R>>] ? E : never,
|
|
1253
1282
|
[Eff] extends [never] ? never : [Eff] extends [YieldWrap<Micro<infer _A, infer _E, infer R>>] ? R : never
|
|
1254
1283
|
> =>
|
|
1255
|
-
make(function(env,
|
|
1284
|
+
make(function(env, onExit) {
|
|
1256
1285
|
const iterator: Generator<Eff, AEff, any> = args.length === 1 ? args[0]() : args[1].call(args[0])
|
|
1257
1286
|
let running = false
|
|
1258
1287
|
let value: any = undefined
|
|
@@ -1263,21 +1292,21 @@ export const gen = <Self, Eff extends YieldWrap<Micro<any, any, any>>, AEff>(
|
|
|
1263
1292
|
while (shouldContinue) {
|
|
1264
1293
|
const result = iterator.next(value)
|
|
1265
1294
|
if (result.done) {
|
|
1266
|
-
return
|
|
1295
|
+
return onExit(exitSucceed(result.value))
|
|
1267
1296
|
}
|
|
1268
1297
|
shouldContinue = false
|
|
1269
|
-
yieldWrapGet(result.value)[runSymbol](env, function(
|
|
1270
|
-
if (
|
|
1271
|
-
|
|
1298
|
+
yieldWrapGet(result.value)[runSymbol](env, function(exit) {
|
|
1299
|
+
if (exit._tag === "Left") {
|
|
1300
|
+
onExit(exit)
|
|
1272
1301
|
} else {
|
|
1273
1302
|
shouldContinue = true
|
|
1274
|
-
value =
|
|
1303
|
+
value = exit.right
|
|
1275
1304
|
if (!running) run()
|
|
1276
1305
|
}
|
|
1277
1306
|
})
|
|
1278
1307
|
}
|
|
1279
1308
|
} catch (err) {
|
|
1280
|
-
|
|
1309
|
+
onExit(exitDie(err))
|
|
1281
1310
|
}
|
|
1282
1311
|
running = false
|
|
1283
1312
|
}
|
|
@@ -1296,10 +1325,10 @@ export const gen = <Self, Eff extends YieldWrap<Micro<any, any, any>>, AEff>(
|
|
|
1296
1325
|
* @category mapping & sequencing
|
|
1297
1326
|
*/
|
|
1298
1327
|
export const flatten = <A, E, R, E2, R2>(self: Micro<Micro<A, E, R>, E2, R2>): Micro<A, E | E2, R | R2> =>
|
|
1299
|
-
make(function(env,
|
|
1328
|
+
make(function(env, onExit) {
|
|
1300
1329
|
self[runSymbol](
|
|
1301
1330
|
env,
|
|
1302
|
-
(
|
|
1331
|
+
(exit) => exit._tag === "Left" ? onExit(exit as MicroExit<never, E2>) : exit.right[runSymbol](env, onExit)
|
|
1303
1332
|
)
|
|
1304
1333
|
})
|
|
1305
1334
|
|
|
@@ -1315,9 +1344,9 @@ export const map: {
|
|
|
1315
1344
|
<A, B>(f: (a: A) => B): <E, R>(self: Micro<A, E, R>) => Micro<B, E, R>
|
|
1316
1345
|
<A, E, R, B>(self: Micro<A, E, R>, f: (a: A) => B): Micro<B, E, R>
|
|
1317
1346
|
} = dual(2, <A, E, R, B>(self: Micro<A, E, R>, f: (a: A) => B): Micro<B, E, R> =>
|
|
1318
|
-
make(function(env,
|
|
1319
|
-
self[runSymbol](env, function(
|
|
1320
|
-
|
|
1347
|
+
make(function(env, onExit) {
|
|
1348
|
+
self[runSymbol](env, function(exit) {
|
|
1349
|
+
onExit(exit._tag === "Left" ? exit as MicroExit<never, E> : exitSucceed(f(exit.right)))
|
|
1321
1350
|
})
|
|
1322
1351
|
}))
|
|
1323
1352
|
|
|
@@ -1341,7 +1370,7 @@ export const as: {
|
|
|
1341
1370
|
* @experimental
|
|
1342
1371
|
* @category mapping & sequencing
|
|
1343
1372
|
*/
|
|
1344
|
-
export const asSome = <A, E, R>(self: Micro<A, E, R>): Micro<Option.Option<A>, E, R> => map(self, Option.some)
|
|
1373
|
+
export const asSome = <A, E, R>(self: Micro<A, E, R>): Micro<Option.Option<A>, E, R> => map(self, Option.some)
|
|
1345
1374
|
|
|
1346
1375
|
/**
|
|
1347
1376
|
* Map the success value of this `Micro` effect to another `Micro` effect, then
|
|
@@ -1357,12 +1386,12 @@ export const flatMap: {
|
|
|
1357
1386
|
} = dual(
|
|
1358
1387
|
2,
|
|
1359
1388
|
<A, E, R, B, E2, R2>(self: Micro<A, E, R>, f: (a: A) => Micro<B, E2, R2>): Micro<B, E | E2, R | R2> =>
|
|
1360
|
-
make(function(env,
|
|
1361
|
-
self[runSymbol](env, function(
|
|
1362
|
-
if (
|
|
1363
|
-
return
|
|
1389
|
+
make(function(env, onExit) {
|
|
1390
|
+
self[runSymbol](env, function(exit) {
|
|
1391
|
+
if (exit._tag === "Left") {
|
|
1392
|
+
return onExit(exit as MicroExit<never, E>)
|
|
1364
1393
|
}
|
|
1365
|
-
f(
|
|
1394
|
+
f(exit.right)[runSymbol](env, onExit)
|
|
1366
1395
|
})
|
|
1367
1396
|
})
|
|
1368
1397
|
)
|
|
@@ -1375,7 +1404,7 @@ export const flatMap: {
|
|
|
1375
1404
|
* @category mapping & sequencing
|
|
1376
1405
|
*/
|
|
1377
1406
|
export const flip = <A, E, R>(self: Micro<A, E, R>): Micro<E, A, R> =>
|
|
1378
|
-
|
|
1407
|
+
matchEffect(self, {
|
|
1379
1408
|
onFailure: succeed,
|
|
1380
1409
|
onSuccess: fail
|
|
1381
1410
|
})
|
|
@@ -1417,18 +1446,18 @@ export const andThen: {
|
|
|
1417
1446
|
} = dual(
|
|
1418
1447
|
2,
|
|
1419
1448
|
<A, E, R, B, E2, R2>(self: Micro<A, E, R>, f: any): Micro<B, E | E2, R | R2> =>
|
|
1420
|
-
make(function(env,
|
|
1421
|
-
self[runSymbol](env, function(
|
|
1422
|
-
if (
|
|
1423
|
-
return
|
|
1449
|
+
make(function(env, onExit) {
|
|
1450
|
+
self[runSymbol](env, function(exit) {
|
|
1451
|
+
if (exit._tag === "Left") {
|
|
1452
|
+
return onExit(exit as MicroExit<never, E>)
|
|
1424
1453
|
} else if (envGet(env, currentAbortSignal).aborted) {
|
|
1425
|
-
return
|
|
1454
|
+
return onExit(exitInterrupt)
|
|
1426
1455
|
}
|
|
1427
|
-
const value = isMicro(f) ? f : typeof f === "function" ? f(
|
|
1456
|
+
const value = isMicro(f) ? f : typeof f === "function" ? f(exit.right) : f
|
|
1428
1457
|
if (isMicro(value)) {
|
|
1429
|
-
value[runSymbol](env,
|
|
1458
|
+
value[runSymbol](env, onExit)
|
|
1430
1459
|
} else {
|
|
1431
|
-
|
|
1460
|
+
onExit(exitSucceed(value))
|
|
1432
1461
|
}
|
|
1433
1462
|
})
|
|
1434
1463
|
})
|
|
@@ -1469,23 +1498,23 @@ export const tap: {
|
|
|
1469
1498
|
} = dual(
|
|
1470
1499
|
2,
|
|
1471
1500
|
<A, E, R, B, E2, R2>(self: Micro<A, E, R>, f: (a: A) => Micro<B, E2, R2>): Micro<A, E | E2, R | R2> =>
|
|
1472
|
-
make(function(env,
|
|
1473
|
-
self[runSymbol](env, function(
|
|
1474
|
-
if (
|
|
1475
|
-
return
|
|
1501
|
+
make(function(env, onExit) {
|
|
1502
|
+
self[runSymbol](env, function(selfExit) {
|
|
1503
|
+
if (selfExit._tag === "Left") {
|
|
1504
|
+
return onExit(selfExit as MicroExit<never, E>)
|
|
1476
1505
|
} else if (envGet(env, currentAbortSignal).aborted) {
|
|
1477
|
-
return
|
|
1506
|
+
return onExit(exitInterrupt)
|
|
1478
1507
|
}
|
|
1479
|
-
const value = isMicro(f) ? f : typeof f === "function" ? f(
|
|
1508
|
+
const value = isMicro(f) ? f : typeof f === "function" ? f(selfExit.right) : f
|
|
1480
1509
|
if (isMicro(value)) {
|
|
1481
|
-
value[runSymbol](env, function(
|
|
1482
|
-
if (
|
|
1483
|
-
return
|
|
1510
|
+
value[runSymbol](env, function(tapExit) {
|
|
1511
|
+
if (tapExit._tag === "Left") {
|
|
1512
|
+
return onExit(tapExit)
|
|
1484
1513
|
}
|
|
1485
|
-
|
|
1514
|
+
onExit(selfExit)
|
|
1486
1515
|
})
|
|
1487
1516
|
} else {
|
|
1488
|
-
|
|
1517
|
+
onExit(selfExit)
|
|
1489
1518
|
}
|
|
1490
1519
|
})
|
|
1491
1520
|
})
|
|
@@ -1501,28 +1530,28 @@ export const tap: {
|
|
|
1501
1530
|
export const asVoid = <A, E, R>(self: Micro<A, E, R>): Micro<void, E, R> => map(self, (_) => void 0)
|
|
1502
1531
|
|
|
1503
1532
|
/**
|
|
1504
|
-
* Access the `
|
|
1533
|
+
* Access the `MicroExit` of the given `Micro` effect.
|
|
1505
1534
|
*
|
|
1506
|
-
* @since 3.4.
|
|
1535
|
+
* @since 3.4.6
|
|
1507
1536
|
* @experimental
|
|
1508
1537
|
* @category mapping & sequencing
|
|
1509
1538
|
*/
|
|
1510
|
-
export const
|
|
1511
|
-
make(function(env,
|
|
1512
|
-
self[runSymbol](env, function(
|
|
1513
|
-
|
|
1539
|
+
export const exit = <A, E, R>(self: Micro<A, E, R>): Micro<MicroExit<A, E>, never, R> =>
|
|
1540
|
+
make(function(env, onExit) {
|
|
1541
|
+
self[runSymbol](env, function(exit) {
|
|
1542
|
+
onExit(exitSucceed(exit))
|
|
1514
1543
|
})
|
|
1515
1544
|
})
|
|
1516
1545
|
|
|
1517
1546
|
/**
|
|
1518
|
-
* Replace the error type of the given `Micro` with the full `
|
|
1547
|
+
* Replace the error type of the given `Micro` with the full `MicroCause` object.
|
|
1519
1548
|
*
|
|
1520
1549
|
* @since 3.4.0
|
|
1521
1550
|
* @experimental
|
|
1522
1551
|
* @category mapping & sequencing
|
|
1523
1552
|
*/
|
|
1524
|
-
export const sandbox = <A, E, R>(self: Micro<A, E, R>): Micro<A,
|
|
1525
|
-
|
|
1553
|
+
export const sandbox = <A, E, R>(self: Micro<A, E, R>): Micro<A, MicroCause<E>, R> =>
|
|
1554
|
+
catchAllCause(self, (cause) => fail(cause))
|
|
1526
1555
|
|
|
1527
1556
|
function forkSignal(env: Env<any>) {
|
|
1528
1557
|
const controller = new AbortController()
|
|
@@ -1552,26 +1581,26 @@ function forkSignal(env: Env<any>) {
|
|
|
1552
1581
|
export const raceAll = <Eff extends Micro<any, any, any>>(
|
|
1553
1582
|
all: Iterable<Eff>
|
|
1554
1583
|
): Micro<Micro.Success<Eff>, Micro.Error<Eff>, Micro.Context<Eff>> =>
|
|
1555
|
-
make(function(env,
|
|
1584
|
+
make(function(env, onExit) {
|
|
1556
1585
|
const [envWithSignal, onAbort] = forkSignal(env)
|
|
1557
1586
|
|
|
1558
1587
|
const effects = Array.from(all)
|
|
1559
1588
|
let len = effects.length
|
|
1560
1589
|
let index = 0
|
|
1561
1590
|
let done = 0
|
|
1562
|
-
let
|
|
1563
|
-
const
|
|
1564
|
-
function onDone(
|
|
1591
|
+
let exit: MicroExit<any, any> | undefined = undefined
|
|
1592
|
+
const causes: Array<MicroCause<any>> = []
|
|
1593
|
+
function onDone(exit_: MicroExit<any, any>) {
|
|
1565
1594
|
done++
|
|
1566
|
-
if (
|
|
1595
|
+
if (exit_._tag === "Right" && exit === undefined) {
|
|
1567
1596
|
len = index
|
|
1568
|
-
|
|
1597
|
+
exit = exit_
|
|
1569
1598
|
onAbort()
|
|
1570
|
-
} else if (
|
|
1571
|
-
|
|
1599
|
+
} else if (exit_._tag === "Left") {
|
|
1600
|
+
causes.push(exit_.left)
|
|
1572
1601
|
}
|
|
1573
1602
|
if (done >= len) {
|
|
1574
|
-
|
|
1603
|
+
onExit(exit ?? Either.left(causes[0]))
|
|
1575
1604
|
}
|
|
1576
1605
|
}
|
|
1577
1606
|
|
|
@@ -1592,24 +1621,24 @@ export const raceAll = <Eff extends Micro<any, any, any>>(
|
|
|
1592
1621
|
export const raceAllFirst = <Eff extends Micro<any, any, any>>(
|
|
1593
1622
|
all: Iterable<Eff>
|
|
1594
1623
|
): Micro<Micro.Success<Eff>, Micro.Error<Eff>, Micro.Context<Eff>> =>
|
|
1595
|
-
make(function(env,
|
|
1624
|
+
make(function(env, onExit) {
|
|
1596
1625
|
const [envWithSignal, onAbort] = forkSignal(env)
|
|
1597
1626
|
|
|
1598
1627
|
const effects = Array.from(all)
|
|
1599
1628
|
let len = effects.length
|
|
1600
1629
|
let index = 0
|
|
1601
1630
|
let done = 0
|
|
1602
|
-
let
|
|
1603
|
-
const
|
|
1604
|
-
function onDone(
|
|
1631
|
+
let exit: MicroExit<any, any> | undefined = undefined
|
|
1632
|
+
const causes: Array<MicroCause<any>> = []
|
|
1633
|
+
function onDone(exit_: MicroExit<any, any>) {
|
|
1605
1634
|
done++
|
|
1606
|
-
if (
|
|
1635
|
+
if (exit === undefined) {
|
|
1607
1636
|
len = index
|
|
1608
|
-
|
|
1637
|
+
exit = exit_
|
|
1609
1638
|
onAbort()
|
|
1610
1639
|
}
|
|
1611
1640
|
if (done >= len) {
|
|
1612
|
-
|
|
1641
|
+
onExit(exit ?? Either.left(causes[0]))
|
|
1613
1642
|
}
|
|
1614
1643
|
}
|
|
1615
1644
|
|
|
@@ -1722,7 +1751,7 @@ export const zipWith: {
|
|
|
1722
1751
|
|
|
1723
1752
|
/**
|
|
1724
1753
|
* Filter the specified effect with the provided function, failing with specified
|
|
1725
|
-
* `
|
|
1754
|
+
* `MicroCause` if the predicate fails.
|
|
1726
1755
|
*
|
|
1727
1756
|
* In addition to the filtering capabilities discussed earlier, you have the option to further
|
|
1728
1757
|
* refine and narrow down the type of the success channel by providing a
|
|
@@ -1731,26 +1760,30 @@ export const zipWith: {
|
|
|
1731
1760
|
* @experimental
|
|
1732
1761
|
* @category filtering & conditionals
|
|
1733
1762
|
*/
|
|
1734
|
-
export const
|
|
1763
|
+
export const filterOrFailCause: {
|
|
1735
1764
|
<A, B extends A, E2>(
|
|
1736
1765
|
refinement: Refinement<A, B>,
|
|
1737
|
-
orFailWith: (a: NoInfer<A>) =>
|
|
1766
|
+
orFailWith: (a: NoInfer<A>) => MicroCause<E2>
|
|
1738
1767
|
): <E, R>(self: Micro<A, E, R>) => Micro<B, E2 | E, R>
|
|
1739
1768
|
<A, E2>(
|
|
1740
1769
|
predicate: Predicate<NoInfer<A>>,
|
|
1741
|
-
orFailWith: (a: NoInfer<A>) =>
|
|
1770
|
+
orFailWith: (a: NoInfer<A>) => MicroCause<E2>
|
|
1742
1771
|
): <E, R>(self: Micro<A, E, R>) => Micro<A, E2 | E, R>
|
|
1743
1772
|
<A, E, R, B extends A, E2>(
|
|
1744
1773
|
self: Micro<A, E, R>,
|
|
1745
1774
|
refinement: Refinement<A, B>,
|
|
1746
|
-
orFailWith: (a: A) =>
|
|
1775
|
+
orFailWith: (a: A) => MicroCause<E2>
|
|
1747
1776
|
): Micro<B, E | E2, R>
|
|
1748
|
-
<A, E, R, E2>(
|
|
1777
|
+
<A, E, R, E2>(
|
|
1778
|
+
self: Micro<A, E, R>,
|
|
1779
|
+
predicate: Predicate<A>,
|
|
1780
|
+
orFailWith: (a: A) => MicroCause<E2>
|
|
1781
|
+
): Micro<A, E | E2, R>
|
|
1749
1782
|
} = dual((args) => isMicro(args[0]), <A, E, R, B extends A, E2>(
|
|
1750
1783
|
self: Micro<A, E, R>,
|
|
1751
1784
|
refinement: Refinement<A, B>,
|
|
1752
|
-
orFailWith: (a: A) =>
|
|
1753
|
-
): Micro<B, E | E2, R> => flatMap(self, (a) => refinement(a) ? succeed(a
|
|
1785
|
+
orFailWith: (a: A) => MicroCause<E2>
|
|
1786
|
+
): Micro<B, E | E2, R> => flatMap(self, (a) => refinement(a) ? succeed(a) : failCause(orFailWith(a))))
|
|
1754
1787
|
|
|
1755
1788
|
/**
|
|
1756
1789
|
* Filter the specified effect with the provided function, failing with specified
|
|
@@ -1782,7 +1815,7 @@ export const filterOrFail: {
|
|
|
1782
1815
|
self: Micro<A, E, R>,
|
|
1783
1816
|
refinement: Refinement<A, B>,
|
|
1784
1817
|
orFailWith: (a: A) => E2
|
|
1785
|
-
): Micro<B, E | E2, R> => flatMap(self, (a) => refinement(a) ? succeed(a
|
|
1818
|
+
): Micro<B, E | E2, R> => flatMap(self, (a) => refinement(a) ? succeed(a) : fail(orFailWith(a))))
|
|
1786
1819
|
|
|
1787
1820
|
/**
|
|
1788
1821
|
* The moral equivalent of `if (p) exp`.
|
|
@@ -1816,50 +1849,50 @@ export const when: {
|
|
|
1816
1849
|
* Repeat the given `Micro` using the provided options.
|
|
1817
1850
|
*
|
|
1818
1851
|
* The `while` predicate will be checked after each iteration, and can use the
|
|
1819
|
-
* fall `
|
|
1852
|
+
* fall `MicroExit` of the effect to determine if the repetition should continue.
|
|
1820
1853
|
*
|
|
1821
|
-
* @since 3.4.
|
|
1854
|
+
* @since 3.4.6
|
|
1822
1855
|
* @experimental
|
|
1823
1856
|
* @category repetition
|
|
1824
1857
|
*/
|
|
1825
|
-
export const
|
|
1858
|
+
export const repeatExit: {
|
|
1826
1859
|
<A, E>(options: {
|
|
1827
|
-
while: Predicate<
|
|
1860
|
+
while: Predicate<MicroExit<A, E>>
|
|
1828
1861
|
times?: number | undefined
|
|
1829
|
-
|
|
1862
|
+
schedule?: MicroSchedule | undefined
|
|
1830
1863
|
}): <R>(self: Micro<A, E, R>) => Micro<A, E, R>
|
|
1831
1864
|
<A, E, R>(self: Micro<A, E, R>, options: {
|
|
1832
|
-
while: Predicate<
|
|
1865
|
+
while: Predicate<MicroExit<A, E>>
|
|
1833
1866
|
times?: number | undefined
|
|
1834
|
-
|
|
1867
|
+
schedule?: MicroSchedule | undefined
|
|
1835
1868
|
}): Micro<A, E, R>
|
|
1836
1869
|
} = dual(2, <A, E, R>(self: Micro<A, E, R>, options: {
|
|
1837
|
-
while: Predicate<
|
|
1870
|
+
while: Predicate<MicroExit<A, E>>
|
|
1838
1871
|
times?: number | undefined
|
|
1839
|
-
|
|
1872
|
+
schedule?: MicroSchedule | undefined
|
|
1840
1873
|
}): Micro<A, E, R> =>
|
|
1841
|
-
make(function(env,
|
|
1842
|
-
const startedAt = options.
|
|
1874
|
+
make(function(env, onExit) {
|
|
1875
|
+
const startedAt = options.schedule ? Date.now() : 0
|
|
1843
1876
|
let attempt = 0
|
|
1844
|
-
self[runSymbol](env, function loop(
|
|
1845
|
-
if (options.while !== undefined && !options.while(
|
|
1846
|
-
return
|
|
1877
|
+
self[runSymbol](env, function loop(exit) {
|
|
1878
|
+
if (options.while !== undefined && !options.while(exit)) {
|
|
1879
|
+
return onExit(exit)
|
|
1847
1880
|
} else if (options.times !== undefined && attempt >= options.times) {
|
|
1848
|
-
return
|
|
1881
|
+
return onExit(exit)
|
|
1849
1882
|
}
|
|
1850
1883
|
attempt++
|
|
1851
1884
|
let delayEffect = yieldNow
|
|
1852
|
-
if (options.
|
|
1885
|
+
if (options.schedule !== undefined) {
|
|
1853
1886
|
const elapsed = Date.now() - startedAt
|
|
1854
|
-
const duration = options.
|
|
1887
|
+
const duration = options.schedule(attempt, elapsed)
|
|
1855
1888
|
if (Option.isNone(duration)) {
|
|
1856
|
-
return
|
|
1889
|
+
return onExit(exit)
|
|
1857
1890
|
}
|
|
1858
1891
|
delayEffect = sleep(duration.value)
|
|
1859
1892
|
}
|
|
1860
|
-
delayEffect[runSymbol](env, function(
|
|
1861
|
-
if (
|
|
1862
|
-
return
|
|
1893
|
+
delayEffect[runSymbol](env, function(exit) {
|
|
1894
|
+
if (exit._tag === "Left") {
|
|
1895
|
+
return onExit(exit as MicroExit<never, never>)
|
|
1863
1896
|
}
|
|
1864
1897
|
self[runSymbol](env, loop)
|
|
1865
1898
|
})
|
|
@@ -1879,7 +1912,7 @@ export const repeat: {
|
|
|
1879
1912
|
options?: {
|
|
1880
1913
|
while?: Predicate<A> | undefined
|
|
1881
1914
|
times?: number | undefined
|
|
1882
|
-
|
|
1915
|
+
schedule?: MicroSchedule | undefined
|
|
1883
1916
|
} | undefined
|
|
1884
1917
|
): <R>(self: Micro<A, E, R>) => Micro<A, E, R>
|
|
1885
1918
|
<A, E, R>(
|
|
@@ -1887,7 +1920,7 @@ export const repeat: {
|
|
|
1887
1920
|
options?: {
|
|
1888
1921
|
while?: Predicate<A> | undefined
|
|
1889
1922
|
times?: number | undefined
|
|
1890
|
-
|
|
1923
|
+
schedule?: MicroSchedule | undefined
|
|
1891
1924
|
} | undefined
|
|
1892
1925
|
): Micro<A, E, R>
|
|
1893
1926
|
} = dual((args) => isMicro(args[0]), <A, E, R>(
|
|
@@ -1895,12 +1928,12 @@ export const repeat: {
|
|
|
1895
1928
|
options?: {
|
|
1896
1929
|
while?: Predicate<A> | undefined
|
|
1897
1930
|
times?: number | undefined
|
|
1898
|
-
|
|
1931
|
+
schedule?: MicroSchedule | undefined
|
|
1899
1932
|
} | undefined
|
|
1900
1933
|
): Micro<A, E, R> =>
|
|
1901
|
-
|
|
1934
|
+
repeatExit(self, {
|
|
1902
1935
|
...options,
|
|
1903
|
-
while: (
|
|
1936
|
+
while: (exit) => exit._tag === "Right" && (options?.while === undefined || options.while(exit.right))
|
|
1904
1937
|
}))
|
|
1905
1938
|
|
|
1906
1939
|
/**
|
|
@@ -1913,89 +1946,135 @@ export const repeat: {
|
|
|
1913
1946
|
export const forever = <A, E, R>(self: Micro<A, E, R>): Micro<never, E, R> => repeat(self) as any
|
|
1914
1947
|
|
|
1915
1948
|
// ----------------------------------------------------------------------------
|
|
1916
|
-
//
|
|
1949
|
+
// scheduling
|
|
1917
1950
|
// ----------------------------------------------------------------------------
|
|
1918
1951
|
|
|
1919
1952
|
/**
|
|
1920
|
-
*
|
|
1921
|
-
* repeats.
|
|
1953
|
+
* The `MicroSchedule` type represents a function that can be used to calculate
|
|
1954
|
+
* the delay between repeats.
|
|
1922
1955
|
*
|
|
1923
|
-
* The function takes the current attempt number and the elapsed time since
|
|
1924
|
-
*
|
|
1925
|
-
*
|
|
1956
|
+
* The function takes the current attempt number and the elapsed time since the
|
|
1957
|
+
* first attempt, and returns the delay for the next attempt. If the function
|
|
1958
|
+
* returns `None`, the repetition will stop.
|
|
1926
1959
|
*
|
|
1927
|
-
* @since 3.4.
|
|
1960
|
+
* @since 3.4.6
|
|
1928
1961
|
* @experimental
|
|
1929
|
-
* @category
|
|
1962
|
+
* @category scheduling
|
|
1930
1963
|
*/
|
|
1931
|
-
export type
|
|
1964
|
+
export type MicroSchedule = (attempt: number, elapsed: number) => Option.Option<number>
|
|
1932
1965
|
|
|
1933
1966
|
/**
|
|
1934
|
-
* Create a `
|
|
1967
|
+
* Create a `MicroSchedule` that will stop repeating after the specified number
|
|
1968
|
+
* of attempts.
|
|
1935
1969
|
*
|
|
1936
|
-
* @since 3.4.
|
|
1970
|
+
* @since 3.4.6
|
|
1937
1971
|
* @experimental
|
|
1938
|
-
* @category
|
|
1972
|
+
* @category scheduling
|
|
1939
1973
|
*/
|
|
1940
|
-
export const
|
|
1941
|
-
Option.some(attempt ** factor * baseMillis)
|
|
1974
|
+
export const scheduleRecurs = (n: number): MicroSchedule => (attempt) => attempt <= n ? Option.some(0) : Option.none()
|
|
1942
1975
|
|
|
1943
1976
|
/**
|
|
1944
|
-
* Create a `
|
|
1977
|
+
* Create a `MicroSchedule` that will generate a constant delay.
|
|
1945
1978
|
*
|
|
1946
|
-
* @since 3.4.
|
|
1979
|
+
* @since 3.4.6
|
|
1980
|
+
* @experimental
|
|
1981
|
+
* @category scheduling
|
|
1982
|
+
*/
|
|
1983
|
+
export const scheduleSpaced = (millis: number): MicroSchedule => () => Option.some(millis)
|
|
1984
|
+
|
|
1985
|
+
/**
|
|
1986
|
+
* Create a `MicroSchedule` that will generate a delay with an exponential backoff.
|
|
1987
|
+
*
|
|
1988
|
+
* @since 3.4.6
|
|
1947
1989
|
* @experimental
|
|
1948
|
-
* @category
|
|
1990
|
+
* @category scheduling
|
|
1949
1991
|
*/
|
|
1950
|
-
export const
|
|
1992
|
+
export const scheduleExponential = (baseMillis: number, factor = 2): MicroSchedule => (attempt) =>
|
|
1993
|
+
Option.some(Math.pow(factor, attempt) * baseMillis)
|
|
1951
1994
|
|
|
1952
1995
|
/**
|
|
1953
|
-
*
|
|
1996
|
+
* Returns a new `MicroSchedule` with an added calculated delay to each delay
|
|
1997
|
+
* returned by this schedule.
|
|
1998
|
+
*
|
|
1999
|
+
* @since 3.4.6
|
|
2000
|
+
* @experimental
|
|
2001
|
+
* @category scheduling
|
|
2002
|
+
*/
|
|
2003
|
+
export const scheduleAddDelay: {
|
|
2004
|
+
(f: () => number): (self: MicroSchedule) => MicroSchedule
|
|
2005
|
+
(self: MicroSchedule, f: () => number): MicroSchedule
|
|
2006
|
+
} = dual(
|
|
2007
|
+
2,
|
|
2008
|
+
(self: MicroSchedule, f: () => number): MicroSchedule => (attempt, elapsed) =>
|
|
2009
|
+
Option.map(self(attempt, elapsed), (duration) => duration + f())
|
|
2010
|
+
)
|
|
2011
|
+
|
|
2012
|
+
/**
|
|
2013
|
+
* Transform a `MicroSchedule` to one that will have a delay that will never exceed
|
|
1954
2014
|
* the specified maximum.
|
|
1955
2015
|
*
|
|
1956
|
-
* @since 3.4.
|
|
2016
|
+
* @since 3.4.6
|
|
1957
2017
|
* @experimental
|
|
1958
|
-
* @category
|
|
2018
|
+
* @category scheduling
|
|
1959
2019
|
*/
|
|
1960
|
-
export const
|
|
1961
|
-
(max: number): (self:
|
|
1962
|
-
(self:
|
|
2020
|
+
export const scheduleWithMaxDelay: {
|
|
2021
|
+
(max: number): (self: MicroSchedule) => MicroSchedule
|
|
2022
|
+
(self: MicroSchedule, max: number): MicroSchedule
|
|
1963
2023
|
} = dual(
|
|
1964
2024
|
2,
|
|
1965
|
-
(self:
|
|
2025
|
+
(self: MicroSchedule, max: number): MicroSchedule => (attempt, elapsed) =>
|
|
1966
2026
|
Option.map(self(attempt, elapsed), (duration) => Math.min(duration, max))
|
|
1967
2027
|
)
|
|
1968
2028
|
|
|
1969
2029
|
/**
|
|
1970
|
-
* Transform a `
|
|
2030
|
+
* Transform a `MicroSchedule` to one that will stop repeating after the specified
|
|
1971
2031
|
* amount of time.
|
|
1972
2032
|
*
|
|
1973
|
-
* @since 3.4.
|
|
2033
|
+
* @since 3.4.6
|
|
1974
2034
|
* @experimental
|
|
1975
|
-
* @category
|
|
2035
|
+
* @category scheduling
|
|
1976
2036
|
*/
|
|
1977
|
-
export const
|
|
1978
|
-
(max: number): (self:
|
|
1979
|
-
(self:
|
|
2037
|
+
export const scheduleWithMaxElapsed: {
|
|
2038
|
+
(max: number): (self: MicroSchedule) => MicroSchedule
|
|
2039
|
+
(self: MicroSchedule, max: number): MicroSchedule
|
|
1980
2040
|
} = dual(
|
|
1981
2041
|
2,
|
|
1982
|
-
(self:
|
|
2042
|
+
(self: MicroSchedule, max: number): MicroSchedule => (attempt, elapsed) =>
|
|
2043
|
+
elapsed < max ? self(attempt, elapsed) : Option.none()
|
|
1983
2044
|
)
|
|
1984
2045
|
|
|
1985
2046
|
/**
|
|
1986
|
-
*
|
|
1987
|
-
*
|
|
2047
|
+
* Combines two `MicroSchedule`s, by recurring if either schedule wants to
|
|
2048
|
+
* recur, using the minimum of the two durations between recurrences.
|
|
1988
2049
|
*
|
|
1989
|
-
* @since 3.4.
|
|
2050
|
+
* @since 3.4.6
|
|
1990
2051
|
* @experimental
|
|
1991
|
-
* @category
|
|
2052
|
+
* @category scheduling
|
|
1992
2053
|
*/
|
|
1993
|
-
export const
|
|
1994
|
-
(
|
|
1995
|
-
(self:
|
|
2054
|
+
export const scheduleUnion: {
|
|
2055
|
+
(that: MicroSchedule): (self: MicroSchedule) => MicroSchedule
|
|
2056
|
+
(self: MicroSchedule, that: MicroSchedule): MicroSchedule
|
|
1996
2057
|
} = dual(
|
|
1997
2058
|
2,
|
|
1998
|
-
(self:
|
|
2059
|
+
(self: MicroSchedule, that: MicroSchedule): MicroSchedule => (attempt, elapsed) =>
|
|
2060
|
+
Option.zipWith(self(attempt, elapsed), that(attempt, elapsed), (d1, d2) => Math.min(d1, d2))
|
|
2061
|
+
)
|
|
2062
|
+
|
|
2063
|
+
/**
|
|
2064
|
+
* Combines two `MicroSchedule`s, by recurring only if both schedules want to
|
|
2065
|
+
* recur, using the maximum of the two durations between recurrences.
|
|
2066
|
+
*
|
|
2067
|
+
* @since 3.4.6
|
|
2068
|
+
* @experimental
|
|
2069
|
+
* @category scheduling
|
|
2070
|
+
*/
|
|
2071
|
+
export const scheduleIntersect: {
|
|
2072
|
+
(that: MicroSchedule): (self: MicroSchedule) => MicroSchedule
|
|
2073
|
+
(self: MicroSchedule, that: MicroSchedule): MicroSchedule
|
|
2074
|
+
} = dual(
|
|
2075
|
+
2,
|
|
2076
|
+
(self: MicroSchedule, that: MicroSchedule): MicroSchedule => (attempt, elapsed) =>
|
|
2077
|
+
Option.zipWith(self(attempt, elapsed), that(attempt, elapsed), (d1, d2) => Math.max(d1, d2))
|
|
1999
2078
|
)
|
|
2000
2079
|
|
|
2001
2080
|
// ----------------------------------------------------------------------------
|
|
@@ -2003,80 +2082,81 @@ export const delayWithRecurs: {
|
|
|
2003
2082
|
// ----------------------------------------------------------------------------
|
|
2004
2083
|
|
|
2005
2084
|
/**
|
|
2006
|
-
* Catch the full `
|
|
2007
|
-
* recover from any kind of
|
|
2085
|
+
* Catch the full `MicroCause` object of the given `Micro` effect, allowing you to
|
|
2086
|
+
* recover from any kind of cause.
|
|
2008
2087
|
*
|
|
2009
|
-
* @since 3.4.
|
|
2088
|
+
* @since 3.4.6
|
|
2010
2089
|
* @experimental
|
|
2011
2090
|
* @category error handling
|
|
2012
2091
|
*/
|
|
2013
|
-
export const
|
|
2092
|
+
export const catchAllCause: {
|
|
2014
2093
|
<E, B, E2, R2>(
|
|
2015
|
-
f: (
|
|
2094
|
+
f: (cause: NoInfer<MicroCause<E>>) => Micro<B, E2, R2>
|
|
2016
2095
|
): <A, R>(self: Micro<A, E, R>) => Micro<A | B, E2, R | R2>
|
|
2017
2096
|
<A, E, R, B, E2, R2>(
|
|
2018
2097
|
self: Micro<A, E, R>,
|
|
2019
|
-
f: (
|
|
2098
|
+
f: (cause: NoInfer<MicroCause<E>>) => Micro<B, E2, R2>
|
|
2020
2099
|
): Micro<A | B, E2, R | R2>
|
|
2021
2100
|
} = dual(
|
|
2022
2101
|
2,
|
|
2023
2102
|
<A, E, R, B, E2, R2>(
|
|
2024
2103
|
self: Micro<A, E, R>,
|
|
2025
|
-
f: (
|
|
2026
|
-
): Micro<A | B, E2, R | R2> =>
|
|
2104
|
+
f: (cause: NoInfer<MicroCause<E>>) => Micro<B, E2, R2>
|
|
2105
|
+
): Micro<A | B, E2, R | R2> => catchCauseIf(self, constTrue, f) as Micro<A | B, E2, R | R2>
|
|
2027
2106
|
)
|
|
2028
2107
|
|
|
2029
2108
|
/**
|
|
2030
|
-
* Selectively catch a `
|
|
2109
|
+
* Selectively catch a `MicroCause` object of the given `Micro` effect,
|
|
2031
2110
|
* using the provided predicate to determine if the failure should be caught.
|
|
2032
2111
|
*
|
|
2033
|
-
* @since 3.4.
|
|
2112
|
+
* @since 3.4.6
|
|
2034
2113
|
* @experimental
|
|
2035
2114
|
* @category error handling
|
|
2036
2115
|
*/
|
|
2037
|
-
export const
|
|
2038
|
-
<E, B, E2, R2, EB extends
|
|
2039
|
-
refinement: Refinement<
|
|
2040
|
-
f: (
|
|
2041
|
-
): <A, R>(self: Micro<A, E, R>) => Micro<A | B, E2, R | R2>
|
|
2116
|
+
export const catchCauseIf: {
|
|
2117
|
+
<E, B, E2, R2, EB extends MicroCause<E>>(
|
|
2118
|
+
refinement: Refinement<MicroCause<E>, EB>,
|
|
2119
|
+
f: (cause: EB) => Micro<B, E2, R2>
|
|
2120
|
+
): <A, R>(self: Micro<A, E, R>) => Micro<A | B, Exclude<E, MicroCause.Error<EB>> | E2, R | R2>
|
|
2042
2121
|
<E, B, E2, R2>(
|
|
2043
|
-
predicate: Predicate<
|
|
2044
|
-
f: (
|
|
2045
|
-
): <A, R>(self: Micro<A, E, R>) => Micro<A | B, E2, R | R2>
|
|
2046
|
-
<A, E, R, B, E2, R2, EB extends
|
|
2122
|
+
predicate: Predicate<MicroCause<NoInfer<E>>>,
|
|
2123
|
+
f: (cause: NoInfer<MicroCause<E>>) => Micro<B, E2, R2>
|
|
2124
|
+
): <A, R>(self: Micro<A, E, R>) => Micro<A | B, E | E2, R | R2>
|
|
2125
|
+
<A, E, R, B, E2, R2, EB extends MicroCause<E>>(
|
|
2047
2126
|
self: Micro<A, E, R>,
|
|
2048
|
-
refinement: Refinement<
|
|
2049
|
-
f: (
|
|
2050
|
-
): Micro<A | B, E2, R | R2>
|
|
2127
|
+
refinement: Refinement<MicroCause<E>, EB>,
|
|
2128
|
+
f: (cause: EB) => Micro<B, E2, R2>
|
|
2129
|
+
): Micro<A | B, Exclude<E, MicroCause.Error<EB>> | E2, R | R2>
|
|
2051
2130
|
<A, E, R, B, E2, R2>(
|
|
2052
2131
|
self: Micro<A, E, R>,
|
|
2053
|
-
predicate: Predicate<
|
|
2054
|
-
f: (
|
|
2055
|
-
): Micro<A | B, E2, R | R2>
|
|
2056
|
-
} = dual(3, <A, E, R, B, E2, R2
|
|
2132
|
+
predicate: Predicate<MicroCause<NoInfer<E>>>,
|
|
2133
|
+
f: (cause: NoInfer<MicroCause<E>>) => Micro<B, E2, R2>
|
|
2134
|
+
): Micro<A | B, E | E2, R | R2>
|
|
2135
|
+
} = dual(3, <A, E, R, B, E2, R2>(
|
|
2057
2136
|
self: Micro<A, E, R>,
|
|
2058
|
-
|
|
2059
|
-
f: (
|
|
2060
|
-
): Micro<A | B, E2, R | R2> =>
|
|
2061
|
-
make(function(env,
|
|
2062
|
-
self[runSymbol](env, function(
|
|
2063
|
-
if (
|
|
2064
|
-
|
|
2137
|
+
predicate: Predicate<MicroCause<E>>,
|
|
2138
|
+
f: (cause: MicroCause<E>) => Micro<B, E2, R2>
|
|
2139
|
+
): Micro<A | B, E | E2, R | R2> =>
|
|
2140
|
+
make(function(env, onExit) {
|
|
2141
|
+
self[runSymbol](env, function(exit) {
|
|
2142
|
+
if (exit._tag === "Right" || !predicate(exit.left)) {
|
|
2143
|
+
onExit(exit)
|
|
2144
|
+
} else {
|
|
2145
|
+
f(exit.left)[runSymbol](env, onExit)
|
|
2065
2146
|
}
|
|
2066
|
-
f(result.left)[runSymbol](env, onResult)
|
|
2067
2147
|
})
|
|
2068
2148
|
}))
|
|
2069
2149
|
|
|
2070
2150
|
/**
|
|
2071
2151
|
* Catch the error of the given `Micro` effect, allowing you to recover from it.
|
|
2072
2152
|
*
|
|
2073
|
-
* It only catches expected (`
|
|
2153
|
+
* It only catches expected (`MicroCause.Fail`) errors.
|
|
2074
2154
|
*
|
|
2075
|
-
* @since 3.4.
|
|
2155
|
+
* @since 3.4.6
|
|
2076
2156
|
* @experimental
|
|
2077
2157
|
* @category error handling
|
|
2078
2158
|
*/
|
|
2079
|
-
export const
|
|
2159
|
+
export const catchAll: {
|
|
2080
2160
|
<E, B, E2, R2>(
|
|
2081
2161
|
f: (e: NoInfer<E>) => Micro<B, E2, R2>
|
|
2082
2162
|
): <A, R>(self: Micro<A, E, R>) => Micro<A | B, E2, R | R2>
|
|
@@ -2086,17 +2166,17 @@ export const catchExpected: {
|
|
|
2086
2166
|
<A, E, R, B, E2, R2>(
|
|
2087
2167
|
self: Micro<A, E, R>,
|
|
2088
2168
|
f: (a: NoInfer<E>) => Micro<B, E2, R2>
|
|
2089
|
-
): Micro<A | B, E2, R | R2> =>
|
|
2169
|
+
): Micro<A | B, E2, R | R2> => catchAllCause(self, (cause) => causeIsFail(cause) ? f(cause.error) : failCause(cause))
|
|
2090
2170
|
)
|
|
2091
2171
|
|
|
2092
2172
|
/**
|
|
2093
2173
|
* Catch any unexpected errors of the given `Micro` effect, allowing you to recover from them.
|
|
2094
2174
|
*
|
|
2095
|
-
* @since 3.4.
|
|
2175
|
+
* @since 3.4.6
|
|
2096
2176
|
* @experimental
|
|
2097
2177
|
* @category error handling
|
|
2098
2178
|
*/
|
|
2099
|
-
export const
|
|
2179
|
+
export const catchAllDefect: {
|
|
2100
2180
|
<E, B, E2, R2>(
|
|
2101
2181
|
f: (defect: unknown) => Micro<B, E2, R2>
|
|
2102
2182
|
): <A, R>(self: Micro<A, E, R>) => Micro<A | B, E | E2, R | R2>
|
|
@@ -2104,92 +2184,94 @@ export const catchUnexpected: {
|
|
|
2104
2184
|
} = dual(
|
|
2105
2185
|
2,
|
|
2106
2186
|
<A, E, R, B, E2, R2>(self: Micro<A, E, R>, f: (defect: unknown) => Micro<B, E2, R2>): Micro<A | B, E | E2, R | R2> =>
|
|
2107
|
-
|
|
2187
|
+
catchCauseIf(self, causeIsDie, (die) => f(die.defect))
|
|
2108
2188
|
)
|
|
2109
2189
|
|
|
2110
2190
|
/**
|
|
2111
|
-
* Perform a side effect using the full `
|
|
2191
|
+
* Perform a side effect using the full `MicroCause` object of the given `Micro`.
|
|
2112
2192
|
*
|
|
2113
|
-
* @since 3.4.
|
|
2193
|
+
* @since 3.4.6
|
|
2114
2194
|
* @experimental
|
|
2115
2195
|
* @category error handling
|
|
2116
2196
|
*/
|
|
2117
|
-
export const
|
|
2197
|
+
export const tapErrorCause: {
|
|
2118
2198
|
<E, B, E2, R2>(
|
|
2119
|
-
f: (
|
|
2199
|
+
f: (cause: NoInfer<MicroCause<E>>) => Micro<B, E2, R2>
|
|
2120
2200
|
): <A, R>(self: Micro<A, E, R>) => Micro<A, E | E2, R | R2>
|
|
2121
|
-
<A, E, R, B, E2, R2>(
|
|
2201
|
+
<A, E, R, B, E2, R2>(
|
|
2202
|
+
self: Micro<A, E, R>,
|
|
2203
|
+
f: (cause: NoInfer<MicroCause<E>>) => Micro<B, E2, R2>
|
|
2204
|
+
): Micro<A, E | E2, R | R2>
|
|
2122
2205
|
} = dual(
|
|
2123
2206
|
2,
|
|
2124
2207
|
<A, E, R, B, E2, R2>(
|
|
2125
2208
|
self: Micro<A, E, R>,
|
|
2126
|
-
f: (
|
|
2127
|
-
): Micro<A, E | E2, R | R2> =>
|
|
2209
|
+
f: (cause: NoInfer<MicroCause<E>>) => Micro<B, E2, R2>
|
|
2210
|
+
): Micro<A, E | E2, R | R2> => tapErrorCauseIf(self, constTrue, f)
|
|
2128
2211
|
)
|
|
2129
2212
|
|
|
2130
2213
|
/**
|
|
2131
|
-
* Perform a side effect using if a `
|
|
2214
|
+
* Perform a side effect using if a `MicroCause` object matches the specified
|
|
2132
2215
|
* predicate.
|
|
2133
2216
|
*
|
|
2134
2217
|
* @since 3.4.0
|
|
2135
2218
|
* @experimental
|
|
2136
2219
|
* @category error handling
|
|
2137
2220
|
*/
|
|
2138
|
-
export const
|
|
2139
|
-
<E, B, E2, R2, EB extends
|
|
2140
|
-
refinement: Refinement<
|
|
2221
|
+
export const tapErrorCauseIf: {
|
|
2222
|
+
<E, B, E2, R2, EB extends MicroCause<E>>(
|
|
2223
|
+
refinement: Refinement<MicroCause<E>, EB>,
|
|
2141
2224
|
f: (a: EB) => Micro<B, E2, R2>
|
|
2142
2225
|
): <A, R>(self: Micro<A, E, R>) => Micro<A, E | E2, R | R2>
|
|
2143
2226
|
<E, B, E2, R2>(
|
|
2144
|
-
predicate: (
|
|
2145
|
-
f: (a: NoInfer<
|
|
2227
|
+
predicate: (cause: NoInfer<MicroCause<E>>) => boolean,
|
|
2228
|
+
f: (a: NoInfer<MicroCause<E>>) => Micro<B, E2, R2>
|
|
2146
2229
|
): <A, R>(self: Micro<A, E, R>) => Micro<A, E | E2, R | R2>
|
|
2147
|
-
<A, E, R, B, E2, R2, EB extends
|
|
2230
|
+
<A, E, R, B, E2, R2, EB extends MicroCause<E>>(
|
|
2148
2231
|
self: Micro<A, E, R>,
|
|
2149
|
-
refinement: Refinement<
|
|
2232
|
+
refinement: Refinement<MicroCause<E>, EB>,
|
|
2150
2233
|
f: (a: EB) => Micro<B, E2, R2>
|
|
2151
2234
|
): Micro<A, E | E2, R | R2>
|
|
2152
2235
|
<A, E, R, B, E2, R2>(
|
|
2153
2236
|
self: Micro<A, E, R>,
|
|
2154
|
-
predicate: (
|
|
2155
|
-
f: (a: NoInfer<
|
|
2237
|
+
predicate: (cause: NoInfer<MicroCause<E>>) => boolean,
|
|
2238
|
+
f: (a: NoInfer<MicroCause<E>>) => Micro<B, E2, R2>
|
|
2156
2239
|
): Micro<A, E | E2, R | R2>
|
|
2157
2240
|
} = dual(
|
|
2158
2241
|
3,
|
|
2159
|
-
<A, E, R, B, E2, R2, EB extends
|
|
2242
|
+
<A, E, R, B, E2, R2, EB extends MicroCause<E>>(
|
|
2160
2243
|
self: Micro<A, E, R>,
|
|
2161
|
-
refinement: Refinement<
|
|
2244
|
+
refinement: Refinement<MicroCause<E>, EB>,
|
|
2162
2245
|
f: (a: EB) => Micro<B, E2, R2>
|
|
2163
|
-
): Micro<A, E | E2, R | R2> =>
|
|
2164
|
-
catchFailureIf(self, refinement, (failure) => andThen(f(failure as any), failWith(failure)))
|
|
2246
|
+
): Micro<A, E | E2, R | R2> => catchCauseIf(self, refinement, (cause) => andThen(f(cause), failCause(cause)))
|
|
2165
2247
|
)
|
|
2166
2248
|
|
|
2167
2249
|
/**
|
|
2168
2250
|
* Perform a side effect from expected errors of the given `Micro`.
|
|
2169
2251
|
*
|
|
2170
|
-
* @since 3.4.
|
|
2252
|
+
* @since 3.4.6
|
|
2171
2253
|
* @experimental
|
|
2172
2254
|
* @category error handling
|
|
2173
2255
|
*/
|
|
2174
|
-
export const
|
|
2256
|
+
export const tapError: {
|
|
2175
2257
|
<E, B, E2, R2>(
|
|
2176
|
-
f: (
|
|
2258
|
+
f: (e: NoInfer<E>) => Micro<B, E2, R2>
|
|
2177
2259
|
): <A, R>(self: Micro<A, E, R>) => Micro<A, E | E2, R | R2>
|
|
2178
|
-
<A, E, R, B, E2, R2>(self: Micro<A, E, R>, f: (
|
|
2260
|
+
<A, E, R, B, E2, R2>(self: Micro<A, E, R>, f: (e: NoInfer<E>) => Micro<B, E2, R2>): Micro<A, E | E2, R | R2>
|
|
2179
2261
|
} = dual(
|
|
2180
2262
|
2,
|
|
2181
|
-
<A, E, R, B, E2, R2>(self: Micro<A, E, R>, f: (
|
|
2182
|
-
|
|
2263
|
+
<A, E, R, B, E2, R2>(self: Micro<A, E, R>, f: (e: NoInfer<E>) => Micro<B, E2, R2>): Micro<A, E | E2, R | R2> =>
|
|
2264
|
+
tapErrorCauseIf(self, causeIsFail, (fail) => f(fail.error))
|
|
2183
2265
|
)
|
|
2184
2266
|
|
|
2185
2267
|
/**
|
|
2186
2268
|
* Perform a side effect from unexpected errors of the given `Micro`.
|
|
2187
2269
|
*
|
|
2188
|
-
* @since 3.4.
|
|
2270
|
+
* @since 3.4.6
|
|
2189
2271
|
* @experimental
|
|
2190
2272
|
* @category error handling
|
|
2191
2273
|
*/
|
|
2192
|
-
export const
|
|
2274
|
+
export const tapDefect: {
|
|
2193
2275
|
<E, B, E2, R2>(
|
|
2194
2276
|
f: (defect: unknown) => Micro<B, E2, R2>
|
|
2195
2277
|
): <A, R>(self: Micro<A, E, R>) => Micro<A, E | E2, R | R2>
|
|
@@ -2197,7 +2279,7 @@ export const tapUnexpected: {
|
|
|
2197
2279
|
} = dual(
|
|
2198
2280
|
2,
|
|
2199
2281
|
<A, E, R, B, E2, R2>(self: Micro<A, E, R>, f: (defect: unknown) => Micro<B, E2, R2>): Micro<A, E | E2, R | R2> =>
|
|
2200
|
-
|
|
2282
|
+
tapErrorCauseIf(self, causeIsDie, (die) => f(die.defect))
|
|
2201
2283
|
)
|
|
2202
2284
|
|
|
2203
2285
|
/**
|
|
@@ -2233,10 +2315,10 @@ export const catchIf: {
|
|
|
2233
2315
|
predicate: Predicate<E>,
|
|
2234
2316
|
f: (e: E) => Micro<A2, E2, R2>
|
|
2235
2317
|
): Micro<A | A2, E | E2, R | R2> =>
|
|
2236
|
-
|
|
2318
|
+
catchCauseIf(
|
|
2237
2319
|
self,
|
|
2238
|
-
(f): f is
|
|
2239
|
-
(
|
|
2320
|
+
(f): f is MicroCause.Fail<E> => causeIsFail(f) && predicate(f.error),
|
|
2321
|
+
(fail) => f(fail.error)
|
|
2240
2322
|
)
|
|
2241
2323
|
)
|
|
2242
2324
|
|
|
@@ -2265,19 +2347,19 @@ export const catchTag: {
|
|
|
2265
2347
|
catchIf(self, isTagged(k) as Refinement<E, Extract<E, { _tag: K }>>, f) as any)
|
|
2266
2348
|
|
|
2267
2349
|
/**
|
|
2268
|
-
* Transform the full `
|
|
2350
|
+
* Transform the full `MicroCause` object of the given `Micro` effect.
|
|
2269
2351
|
*
|
|
2270
|
-
* @since 3.4.
|
|
2352
|
+
* @since 3.4.6
|
|
2271
2353
|
* @experimental
|
|
2272
2354
|
* @category error handling
|
|
2273
2355
|
*/
|
|
2274
|
-
export const
|
|
2275
|
-
<E, E2>(f: (
|
|
2276
|
-
<A, E, R, E2>(self: Micro<A, E, R>, f: (
|
|
2356
|
+
export const mapErrorCause: {
|
|
2357
|
+
<E, E2>(f: (e: MicroCause<E>) => MicroCause<E2>): <A, R>(self: Micro<A, E, R>) => Micro<A, E2, R>
|
|
2358
|
+
<A, E, R, E2>(self: Micro<A, E, R>, f: (e: MicroCause<E>) => MicroCause<E2>): Micro<A, E2, R>
|
|
2277
2359
|
} = dual(
|
|
2278
2360
|
2,
|
|
2279
|
-
<A, E, R, E2>(self: Micro<A, E, R>, f: (
|
|
2280
|
-
|
|
2361
|
+
<A, E, R, E2>(self: Micro<A, E, R>, f: (e: MicroCause<E>) => MicroCause<E2>): Micro<A, E2, R> =>
|
|
2362
|
+
catchAllCause(self, (cause) => failCause(f(cause)))
|
|
2281
2363
|
)
|
|
2282
2364
|
|
|
2283
2365
|
/**
|
|
@@ -2288,12 +2370,11 @@ export const mapFailure: {
|
|
|
2288
2370
|
* @category error handling
|
|
2289
2371
|
*/
|
|
2290
2372
|
export const mapError: {
|
|
2291
|
-
<E, E2>(f: (
|
|
2292
|
-
<A, E, R, E2>(self: Micro<A, E, R>, f: (
|
|
2373
|
+
<E, E2>(f: (e: E) => E2): <A, R>(self: Micro<A, E, R>) => Micro<A, E2, R>
|
|
2374
|
+
<A, E, R, E2>(self: Micro<A, E, R>, f: (e: E) => E2): Micro<A, E2, R>
|
|
2293
2375
|
} = dual(
|
|
2294
2376
|
2,
|
|
2295
|
-
<A, E, R, E2>(self: Micro<A, E, R>, f: (
|
|
2296
|
-
catchExpected(self, (error) => fail(f(error)))
|
|
2377
|
+
<A, E, R, E2>(self: Micro<A, E, R>, f: (e: E) => E2): Micro<A, E2, R> => catchAll(self, (error) => fail(f(error)))
|
|
2297
2378
|
)
|
|
2298
2379
|
|
|
2299
2380
|
/**
|
|
@@ -2304,7 +2385,7 @@ export const mapError: {
|
|
|
2304
2385
|
* @experimental
|
|
2305
2386
|
* @category error handling
|
|
2306
2387
|
*/
|
|
2307
|
-
export const orDie = <A, E, R>(self: Micro<A, E, R>): Micro<A, never, R> =>
|
|
2388
|
+
export const orDie = <A, E, R>(self: Micro<A, E, R>): Micro<A, never, R> => catchAll(self, die)
|
|
2308
2389
|
|
|
2309
2390
|
/**
|
|
2310
2391
|
* Recover from all errors by succeeding with the given value.
|
|
@@ -2318,7 +2399,7 @@ export const orElseSucceed: {
|
|
|
2318
2399
|
<A, E, R, B>(self: Micro<A, E, R>, f: LazyArg<B>): Micro<A | B, never, R>
|
|
2319
2400
|
} = dual(
|
|
2320
2401
|
2,
|
|
2321
|
-
<A, E, R, B>(self: Micro<A, E, R>, f: LazyArg<B>): Micro<A | B, never, R> =>
|
|
2402
|
+
<A, E, R, B>(self: Micro<A, E, R>, f: LazyArg<B>): Micro<A | B, never, R> => catchAll(self, (_) => sync(f))
|
|
2322
2403
|
)
|
|
2323
2404
|
|
|
2324
2405
|
/**
|
|
@@ -2329,7 +2410,7 @@ export const orElseSucceed: {
|
|
|
2329
2410
|
* @category error handling
|
|
2330
2411
|
*/
|
|
2331
2412
|
export const ignore = <A, E, R>(self: Micro<A, E, R>): Micro<void, never, R> =>
|
|
2332
|
-
|
|
2413
|
+
matchEffect(self, { onFailure: (_) => void_, onSuccess: (_) => void_ })
|
|
2333
2414
|
|
|
2334
2415
|
/**
|
|
2335
2416
|
* Ignore any expected errors of the given `Micro` effect, returning `void`.
|
|
@@ -2339,8 +2420,8 @@ export const ignore = <A, E, R>(self: Micro<A, E, R>): Micro<void, never, R> =>
|
|
|
2339
2420
|
* @category error handling
|
|
2340
2421
|
*/
|
|
2341
2422
|
export const ignoreLogged = <A, E, R>(self: Micro<A, E, R>): Micro<void, never, R> =>
|
|
2342
|
-
|
|
2343
|
-
onFailure: (
|
|
2423
|
+
matchEffect(self, {
|
|
2424
|
+
onFailure: (error) => sync(() => console.error(error)),
|
|
2344
2425
|
onSuccess: (_) => void_
|
|
2345
2426
|
})
|
|
2346
2427
|
|
|
@@ -2380,7 +2461,7 @@ export const retry: {
|
|
|
2380
2461
|
options?: {
|
|
2381
2462
|
while?: Predicate<E> | undefined
|
|
2382
2463
|
times?: number | undefined
|
|
2383
|
-
|
|
2464
|
+
schedule?: MicroSchedule | undefined
|
|
2384
2465
|
} | undefined
|
|
2385
2466
|
): <R>(self: Micro<A, E, R>) => Micro<A, E, R>
|
|
2386
2467
|
<A, E, R>(
|
|
@@ -2388,7 +2469,7 @@ export const retry: {
|
|
|
2388
2469
|
options?: {
|
|
2389
2470
|
while?: Predicate<E> | undefined
|
|
2390
2471
|
times?: number | undefined
|
|
2391
|
-
|
|
2472
|
+
schedule?: MicroSchedule | undefined
|
|
2392
2473
|
} | undefined
|
|
2393
2474
|
): Micro<A, E, R>
|
|
2394
2475
|
} = dual((args) => isMicro(args[0]), <A, E, R>(
|
|
@@ -2396,19 +2477,19 @@ export const retry: {
|
|
|
2396
2477
|
options?: {
|
|
2397
2478
|
while?: Predicate<E> | undefined
|
|
2398
2479
|
times?: number | undefined
|
|
2399
|
-
|
|
2480
|
+
schedule?: MicroSchedule | undefined
|
|
2400
2481
|
} | undefined
|
|
2401
2482
|
): Micro<A, E, R> =>
|
|
2402
|
-
|
|
2483
|
+
repeatExit(self, {
|
|
2403
2484
|
...options,
|
|
2404
|
-
while: (
|
|
2405
|
-
|
|
2406
|
-
(options?.while === undefined || options.while(
|
|
2485
|
+
while: (exit) =>
|
|
2486
|
+
exit._tag === "Left" && exit.left._tag === "Fail" &&
|
|
2487
|
+
(options?.while === undefined || options.while(exit.left.error))
|
|
2407
2488
|
}))
|
|
2408
2489
|
|
|
2409
2490
|
/**
|
|
2410
2491
|
* Add a stack trace to any failures that occur in the effect. The trace will be
|
|
2411
|
-
* added to the `traces` field of the `
|
|
2492
|
+
* added to the `traces` field of the `MicroCause` object.
|
|
2412
2493
|
*
|
|
2413
2494
|
* @since 3.4.0
|
|
2414
2495
|
* @experimental
|
|
@@ -2422,22 +2503,22 @@ export const withTrace: {
|
|
|
2422
2503
|
globalThis.Error.stackTraceLimit = 2
|
|
2423
2504
|
const error = new globalThis.Error()
|
|
2424
2505
|
globalThis.Error.stackTraceLimit = prevLimit
|
|
2425
|
-
function generate(name: string,
|
|
2506
|
+
function generate(name: string, cause: MicroCause<any>) {
|
|
2426
2507
|
const stack = error.stack
|
|
2427
2508
|
if (!stack) {
|
|
2428
|
-
return
|
|
2509
|
+
return cause
|
|
2429
2510
|
}
|
|
2430
2511
|
const line = stack.split("\n")[2]?.trim().replace(/^at /, "")
|
|
2431
2512
|
if (!line) {
|
|
2432
|
-
return
|
|
2513
|
+
return cause
|
|
2433
2514
|
}
|
|
2434
2515
|
const lineMatch = line.match(/\((.*)\)$/)
|
|
2435
|
-
return
|
|
2516
|
+
return causeWithTrace(cause, `at ${name} (${lineMatch ? lineMatch[1] : line})`)
|
|
2436
2517
|
}
|
|
2437
2518
|
const f = (name: string) => (self: Micro<any, any, any>) =>
|
|
2438
|
-
unsafeMakeOptions(function(env,
|
|
2439
|
-
self[runSymbol](env, function(
|
|
2440
|
-
|
|
2519
|
+
unsafeMakeOptions(function(env, onExit) {
|
|
2520
|
+
self[runSymbol](env, function(exit) {
|
|
2521
|
+
onExit(exit._tag === "Left" ? Either.left(generate(name, exit.left)) : exit)
|
|
2441
2522
|
})
|
|
2442
2523
|
}, false)
|
|
2443
2524
|
if (arguments.length === 2) {
|
|
@@ -2451,21 +2532,21 @@ export const withTrace: {
|
|
|
2451
2532
|
// ----------------------------------------------------------------------------
|
|
2452
2533
|
|
|
2453
2534
|
/**
|
|
2454
|
-
* @since 3.4.
|
|
2535
|
+
* @since 3.4.6
|
|
2455
2536
|
* @experimental
|
|
2456
2537
|
* @category pattern matching
|
|
2457
2538
|
*/
|
|
2458
|
-
export const
|
|
2539
|
+
export const matchCauseEffect: {
|
|
2459
2540
|
<E, A2, E2, R2, A, A3, E3, R3>(
|
|
2460
2541
|
options: {
|
|
2461
|
-
readonly onFailure: (
|
|
2542
|
+
readonly onFailure: (cause: MicroCause<E>) => Micro<A2, E2, R2>
|
|
2462
2543
|
readonly onSuccess: (a: A) => Micro<A3, E3, R3>
|
|
2463
2544
|
}
|
|
2464
2545
|
): <R>(self: Micro<A, E, R>) => Micro<A2 | A3, E2 | E3, R2 | R3 | R>
|
|
2465
2546
|
<A, E, R, A2, E2, R2, A3, E3, R3>(
|
|
2466
2547
|
self: Micro<A, E, R>,
|
|
2467
2548
|
options: {
|
|
2468
|
-
readonly onFailure: (
|
|
2549
|
+
readonly onFailure: (cause: MicroCause<E>) => Micro<A2, E2, R2>
|
|
2469
2550
|
readonly onSuccess: (a: A) => Micro<A3, E3, R3>
|
|
2470
2551
|
}
|
|
2471
2552
|
): Micro<A2 | A3, E2 | E3, R2 | R3 | R>
|
|
@@ -2474,38 +2555,38 @@ export const matchFailureMicro: {
|
|
|
2474
2555
|
<A, E, R, A2, E2, R2, A3, E3, R3>(
|
|
2475
2556
|
self: Micro<A, E, R>,
|
|
2476
2557
|
options: {
|
|
2477
|
-
readonly onFailure: (
|
|
2558
|
+
readonly onFailure: (cause: MicroCause<E>) => Micro<A2, E2, R2>
|
|
2478
2559
|
readonly onSuccess: (a: A) => Micro<A3, E3, R3>
|
|
2479
2560
|
}
|
|
2480
2561
|
): Micro<A2 | A3, E2 | E3, R2 | R3 | R> =>
|
|
2481
|
-
make(function(env,
|
|
2482
|
-
self[runSymbol](env, function(
|
|
2562
|
+
make(function(env, onExit) {
|
|
2563
|
+
self[runSymbol](env, function(exit) {
|
|
2483
2564
|
try {
|
|
2484
|
-
const next =
|
|
2485
|
-
next[runSymbol](env,
|
|
2565
|
+
const next = exit._tag === "Left" ? options.onFailure(exit.left) : options.onSuccess(exit.right)
|
|
2566
|
+
next[runSymbol](env, onExit)
|
|
2486
2567
|
} catch (err) {
|
|
2487
|
-
|
|
2568
|
+
onExit(exitDie(err))
|
|
2488
2569
|
}
|
|
2489
2570
|
})
|
|
2490
2571
|
})
|
|
2491
2572
|
)
|
|
2492
2573
|
|
|
2493
2574
|
/**
|
|
2494
|
-
* @since 3.4.
|
|
2575
|
+
* @since 3.4.6
|
|
2495
2576
|
* @experimental
|
|
2496
2577
|
* @category pattern matching
|
|
2497
2578
|
*/
|
|
2498
|
-
export const
|
|
2579
|
+
export const matchCause: {
|
|
2499
2580
|
<E, A2, A, A3>(
|
|
2500
2581
|
options: {
|
|
2501
|
-
readonly onFailure: (
|
|
2582
|
+
readonly onFailure: (cause: MicroCause<E>) => A2
|
|
2502
2583
|
readonly onSuccess: (a: A) => A3
|
|
2503
2584
|
}
|
|
2504
2585
|
): <R>(self: Micro<A, E, R>) => Micro<A2 | A3, never, R>
|
|
2505
2586
|
<A, E, R, A2, A3>(
|
|
2506
2587
|
self: Micro<A, E, R>,
|
|
2507
2588
|
options: {
|
|
2508
|
-
readonly onFailure: (
|
|
2589
|
+
readonly onFailure: (cause: MicroCause<E>) => A2
|
|
2509
2590
|
readonly onSuccess: (a: A) => A3
|
|
2510
2591
|
}
|
|
2511
2592
|
): Micro<A2 | A3, never, R>
|
|
@@ -2514,22 +2595,22 @@ export const matchFailure: {
|
|
|
2514
2595
|
<A, E, R, A2, A3>(
|
|
2515
2596
|
self: Micro<A, E, R>,
|
|
2516
2597
|
options: {
|
|
2517
|
-
readonly onFailure: (
|
|
2598
|
+
readonly onFailure: (cause: MicroCause<E>) => A2
|
|
2518
2599
|
readonly onSuccess: (a: A) => A3
|
|
2519
2600
|
}
|
|
2520
2601
|
): Micro<A2 | A3, never, R> =>
|
|
2521
|
-
|
|
2522
|
-
onFailure: (
|
|
2602
|
+
matchCauseEffect(self, {
|
|
2603
|
+
onFailure: (cause) => sync(() => options.onFailure(cause)),
|
|
2523
2604
|
onSuccess: (value) => sync(() => options.onSuccess(value))
|
|
2524
2605
|
})
|
|
2525
2606
|
)
|
|
2526
2607
|
|
|
2527
2608
|
/**
|
|
2528
|
-
* @since 3.4.
|
|
2609
|
+
* @since 3.4.6
|
|
2529
2610
|
* @experimental
|
|
2530
2611
|
* @category pattern matching
|
|
2531
2612
|
*/
|
|
2532
|
-
export const
|
|
2613
|
+
export const matchEffect: {
|
|
2533
2614
|
<E, A2, E2, R2, A, A3, E3, R3>(
|
|
2534
2615
|
options: {
|
|
2535
2616
|
readonly onFailure: (e: E) => Micro<A2, E2, R2>
|
|
@@ -2552,8 +2633,8 @@ export const matchMicro: {
|
|
|
2552
2633
|
readonly onSuccess: (a: A) => Micro<A3, E3, R3>
|
|
2553
2634
|
}
|
|
2554
2635
|
): Micro<A2 | A3, E2 | E3, R2 | R3 | R> =>
|
|
2555
|
-
|
|
2556
|
-
onFailure: (
|
|
2636
|
+
matchCauseEffect(self, {
|
|
2637
|
+
onFailure: (cause) => cause._tag === "Fail" ? options.onFailure(cause.error) : failCause(cause),
|
|
2557
2638
|
onSuccess: options.onSuccess
|
|
2558
2639
|
})
|
|
2559
2640
|
)
|
|
@@ -2586,7 +2667,7 @@ export const match: {
|
|
|
2586
2667
|
readonly onSuccess: (value: A) => A3
|
|
2587
2668
|
}
|
|
2588
2669
|
): Micro<A2 | A3, never, R> =>
|
|
2589
|
-
|
|
2670
|
+
matchEffect(self, {
|
|
2590
2671
|
onFailure: (error) => sync(() => options.onFailure(error)),
|
|
2591
2672
|
onSuccess: (value) => sync(() => options.onSuccess(value))
|
|
2592
2673
|
})
|
|
@@ -2657,6 +2738,26 @@ export const timeoutOrElse: {
|
|
|
2657
2738
|
raceFirst(self, andThen(interruptible(sleep(options.duration)), options.onTimeout))
|
|
2658
2739
|
)
|
|
2659
2740
|
|
|
2741
|
+
/**
|
|
2742
|
+
* Returns an effect that will timeout this effect, that will fail with a
|
|
2743
|
+
* `TimeoutException` if the timeout elapses before the effect has produced a
|
|
2744
|
+
* value.
|
|
2745
|
+
*
|
|
2746
|
+
* If the timeout elapses, the running effect will be safely interrupted.
|
|
2747
|
+
*
|
|
2748
|
+
* @since 3.4.0
|
|
2749
|
+
* @experimental
|
|
2750
|
+
* @category delays & timeouts
|
|
2751
|
+
*/
|
|
2752
|
+
export const timeout: {
|
|
2753
|
+
(millis: number): <A, E, R>(self: Micro<A, E, R>) => Micro<A, E | TimeoutException, R>
|
|
2754
|
+
<A, E, R>(self: Micro<A, E, R>, millis: number): Micro<A, E | TimeoutException, R>
|
|
2755
|
+
} = dual(
|
|
2756
|
+
2,
|
|
2757
|
+
<A, E, R>(self: Micro<A, E, R>, millis: number): Micro<A, E | TimeoutException, R> =>
|
|
2758
|
+
timeoutOrElse(self, { duration: millis, onTimeout: () => fail(new TimeoutException()) })
|
|
2759
|
+
)
|
|
2760
|
+
|
|
2660
2761
|
/**
|
|
2661
2762
|
* Returns an effect that will timeout this effect, succeeding with a `None`
|
|
2662
2763
|
* if the timeout elapses before the effect has produced a value; and `Some` of
|
|
@@ -2668,7 +2769,7 @@ export const timeoutOrElse: {
|
|
|
2668
2769
|
* @experimental
|
|
2669
2770
|
* @category delays & timeouts
|
|
2670
2771
|
*/
|
|
2671
|
-
export const
|
|
2772
|
+
export const timeoutOption: {
|
|
2672
2773
|
(millis: number): <A, E, R>(self: Micro<A, E, R>) => Micro<Option.Option<A>, E, R>
|
|
2673
2774
|
<A, E, R>(self: Micro<A, E, R>, millis: number): Micro<Option.Option<A>, E, R>
|
|
2674
2775
|
} = dual(
|
|
@@ -2705,7 +2806,7 @@ export type MicroScopeTypeId = typeof MicroScopeTypeId
|
|
|
2705
2806
|
*/
|
|
2706
2807
|
export interface MicroScope {
|
|
2707
2808
|
readonly [MicroScopeTypeId]: MicroScopeTypeId
|
|
2708
|
-
readonly addFinalizer: (finalizer: (
|
|
2809
|
+
readonly addFinalizer: (finalizer: (exit: MicroExit<unknown, unknown>) => Micro<void>) => Micro<void>
|
|
2709
2810
|
readonly fork: Micro<MicroScope.Closeable>
|
|
2710
2811
|
}
|
|
2711
2812
|
|
|
@@ -2721,7 +2822,7 @@ export declare namespace MicroScope {
|
|
|
2721
2822
|
* @category resources & finalization
|
|
2722
2823
|
*/
|
|
2723
2824
|
export interface Closeable extends MicroScope {
|
|
2724
|
-
readonly close: (
|
|
2825
|
+
readonly close: (exit: MicroExit<any, any>) => Micro<void>
|
|
2725
2826
|
}
|
|
2726
2827
|
}
|
|
2727
2828
|
|
|
@@ -2732,47 +2833,47 @@ export declare namespace MicroScope {
|
|
|
2732
2833
|
*/
|
|
2733
2834
|
export const MicroScope: Context.Tag<MicroScope, MicroScope> = Context.GenericTag<MicroScope>("effect/Micro/MicroScope")
|
|
2734
2835
|
|
|
2735
|
-
class
|
|
2836
|
+
class MicroScopeImpl implements MicroScope.Closeable {
|
|
2736
2837
|
readonly [MicroScopeTypeId]: MicroScopeTypeId
|
|
2737
2838
|
state: {
|
|
2738
2839
|
readonly _tag: "Open"
|
|
2739
|
-
readonly finalizers: Set<(
|
|
2840
|
+
readonly finalizers: Set<(exit: MicroExit<any, any>) => Micro<void>>
|
|
2740
2841
|
} | {
|
|
2741
2842
|
readonly _tag: "Closed"
|
|
2742
|
-
readonly
|
|
2843
|
+
readonly exit: MicroExit<any, any>
|
|
2743
2844
|
} = { _tag: "Open", finalizers: new Set() }
|
|
2744
2845
|
|
|
2745
2846
|
constructor() {
|
|
2746
2847
|
this[MicroScopeTypeId] = MicroScopeTypeId
|
|
2747
2848
|
}
|
|
2748
2849
|
|
|
2749
|
-
unsafeAddFinalizer(finalizer: (
|
|
2850
|
+
unsafeAddFinalizer(finalizer: (exit: MicroExit<any, any>) => Micro<void>): void {
|
|
2750
2851
|
if (this.state._tag === "Open") {
|
|
2751
2852
|
this.state.finalizers.add(finalizer)
|
|
2752
2853
|
}
|
|
2753
2854
|
}
|
|
2754
|
-
addFinalizer(finalizer: (
|
|
2855
|
+
addFinalizer(finalizer: (exit: MicroExit<any, any>) => Micro<void>): Micro<void> {
|
|
2755
2856
|
return suspend(() => {
|
|
2756
2857
|
if (this.state._tag === "Open") {
|
|
2757
2858
|
this.state.finalizers.add(finalizer)
|
|
2758
2859
|
return void_
|
|
2759
2860
|
}
|
|
2760
|
-
return finalizer(this.state.
|
|
2861
|
+
return finalizer(this.state.exit)
|
|
2761
2862
|
})
|
|
2762
2863
|
}
|
|
2763
|
-
unsafeRemoveFinalizer(finalizer: (
|
|
2864
|
+
unsafeRemoveFinalizer(finalizer: (exit: MicroExit<any, any>) => Micro<void>): void {
|
|
2764
2865
|
if (this.state._tag === "Open") {
|
|
2765
2866
|
this.state.finalizers.delete(finalizer)
|
|
2766
2867
|
}
|
|
2767
2868
|
}
|
|
2768
|
-
close(
|
|
2869
|
+
close(microExit: MicroExit<any, any>): Micro<void> {
|
|
2769
2870
|
return suspend(() => {
|
|
2770
2871
|
if (this.state._tag === "Open") {
|
|
2771
2872
|
const finalizers = Array.from(this.state.finalizers).reverse()
|
|
2772
|
-
this.state = { _tag: "Closed",
|
|
2873
|
+
this.state = { _tag: "Closed", exit: microExit }
|
|
2773
2874
|
return flatMap(
|
|
2774
|
-
forEach(finalizers, (finalizer) =>
|
|
2775
|
-
(
|
|
2875
|
+
forEach(finalizers, (finalizer) => exit(finalizer(microExit))),
|
|
2876
|
+
(exits) => asVoid(fromExit(Either.all(exits)))
|
|
2776
2877
|
)
|
|
2777
2878
|
}
|
|
2778
2879
|
return void_
|
|
@@ -2780,13 +2881,13 @@ class ScopeImpl implements MicroScope.Closeable {
|
|
|
2780
2881
|
}
|
|
2781
2882
|
get fork() {
|
|
2782
2883
|
return sync(() => {
|
|
2783
|
-
const newScope = new
|
|
2884
|
+
const newScope = new MicroScopeImpl()
|
|
2784
2885
|
if (this.state._tag === "Closed") {
|
|
2785
2886
|
newScope.state = this.state
|
|
2786
2887
|
return newScope
|
|
2787
2888
|
}
|
|
2788
|
-
function fin(
|
|
2789
|
-
return newScope.close(
|
|
2889
|
+
function fin(exit: MicroExit<any, any>) {
|
|
2890
|
+
return newScope.close(exit)
|
|
2790
2891
|
}
|
|
2791
2892
|
this.state.finalizers.add(fin)
|
|
2792
2893
|
newScope.unsafeAddFinalizer((_) => sync(() => this.unsafeRemoveFinalizer(fin)))
|
|
@@ -2800,14 +2901,14 @@ class ScopeImpl implements MicroScope.Closeable {
|
|
|
2800
2901
|
* @experimental
|
|
2801
2902
|
* @category resources & finalization
|
|
2802
2903
|
*/
|
|
2803
|
-
export const scopeMake: Micro<MicroScope.Closeable> = sync(() => new
|
|
2904
|
+
export const scopeMake: Micro<MicroScope.Closeable> = sync(() => new MicroScopeImpl())
|
|
2804
2905
|
|
|
2805
2906
|
/**
|
|
2806
2907
|
* @since 3.4.0
|
|
2807
2908
|
* @experimental
|
|
2808
2909
|
* @category resources & finalization
|
|
2809
2910
|
*/
|
|
2810
|
-
export const scopeUnsafeMake = (): MicroScope.Closeable => new
|
|
2911
|
+
export const scopeUnsafeMake = (): MicroScope.Closeable => new MicroScopeImpl()
|
|
2811
2912
|
|
|
2812
2913
|
/**
|
|
2813
2914
|
* Access the current `MicroScope`.
|
|
@@ -2844,8 +2945,8 @@ export const provideScope: {
|
|
|
2844
2945
|
*/
|
|
2845
2946
|
export const scoped = <A, E, R>(self: Micro<A, E, R>): Micro<A, E, Exclude<R, MicroScope>> =>
|
|
2846
2947
|
suspend(function() {
|
|
2847
|
-
const scope = new
|
|
2848
|
-
return
|
|
2948
|
+
const scope = new MicroScopeImpl()
|
|
2949
|
+
return onExit(provideService(self, MicroScope, scope), (exit) => scope.close(exit))
|
|
2849
2950
|
})
|
|
2850
2951
|
|
|
2851
2952
|
/**
|
|
@@ -2858,11 +2959,11 @@ export const scoped = <A, E, R>(self: Micro<A, E, R>): Micro<A, E, Exclude<R, Mi
|
|
|
2858
2959
|
*/
|
|
2859
2960
|
export const acquireRelease = <A, E, R>(
|
|
2860
2961
|
acquire: Micro<A, E, R>,
|
|
2861
|
-
release: (a: A,
|
|
2962
|
+
release: (a: A, exit: MicroExit<unknown, unknown>) => Micro<void>
|
|
2862
2963
|
): Micro<A, E, R | MicroScope> =>
|
|
2863
2964
|
uninterruptible(flatMap(
|
|
2864
2965
|
scope,
|
|
2865
|
-
(scope) => tap(acquire, (a) => scope.addFinalizer((
|
|
2966
|
+
(scope) => tap(acquire, (a) => scope.addFinalizer((exit) => release(a, exit)))
|
|
2866
2967
|
))
|
|
2867
2968
|
|
|
2868
2969
|
/**
|
|
@@ -2873,73 +2974,75 @@ export const acquireRelease = <A, E, R>(
|
|
|
2873
2974
|
* @category resources & finalization
|
|
2874
2975
|
*/
|
|
2875
2976
|
export const addFinalizer = (
|
|
2876
|
-
finalizer: (
|
|
2977
|
+
finalizer: (exit: MicroExit<unknown, unknown>) => Micro<void>
|
|
2877
2978
|
): Micro<void, never, MicroScope> => flatMap(scope, (scope) => scope.addFinalizer(finalizer))
|
|
2878
2979
|
|
|
2879
2980
|
/**
|
|
2880
2981
|
* When the `Micro` effect is completed, run the given finalizer effect with the
|
|
2881
|
-
* `
|
|
2982
|
+
* `MicroExit` of the executed effect.
|
|
2882
2983
|
*
|
|
2883
|
-
* @since 3.4.
|
|
2984
|
+
* @since 3.4.6
|
|
2884
2985
|
* @experimental
|
|
2885
2986
|
* @category resources & finalization
|
|
2886
2987
|
*/
|
|
2887
|
-
export const
|
|
2988
|
+
export const onExit: {
|
|
2888
2989
|
<A, E, XE, XR>(
|
|
2889
|
-
f: (
|
|
2990
|
+
f: (exit: MicroExit<A, E>) => Micro<void, XE, XR>
|
|
2890
2991
|
): <R>(self: Micro<A, E, R>) => Micro<A, E | XE, R | XR>
|
|
2891
|
-
<A, E, R, XE, XR>(self: Micro<A, E, R>, f: (
|
|
2992
|
+
<A, E, R, XE, XR>(self: Micro<A, E, R>, f: (exit: MicroExit<A, E>) => Micro<void, XE, XR>): Micro<A, E | XE, R | XR>
|
|
2892
2993
|
} = dual(
|
|
2893
2994
|
2,
|
|
2894
|
-
<A, E, R, XE, XR>(
|
|
2895
|
-
|
|
2995
|
+
<A, E, R, XE, XR>(
|
|
2996
|
+
self: Micro<A, E, R>,
|
|
2997
|
+
f: (exit: MicroExit<A, E>) => Micro<void, XE, XR>
|
|
2998
|
+
): Micro<A, E | XE, R | XR> => onExitIf(self, constTrue, f)
|
|
2896
2999
|
)
|
|
2897
3000
|
|
|
2898
3001
|
/**
|
|
2899
3002
|
* When the `Micro` effect is completed, run the given finalizer effect if it
|
|
2900
3003
|
* matches the specified predicate.
|
|
2901
3004
|
*
|
|
2902
|
-
* @since 3.4.
|
|
3005
|
+
* @since 3.4.6
|
|
2903
3006
|
* @experimental
|
|
2904
3007
|
* @category resources & finalization
|
|
2905
3008
|
*/
|
|
2906
|
-
export const
|
|
2907
|
-
<A, E, XE, XR, B extends
|
|
2908
|
-
refinement: Refinement<
|
|
2909
|
-
f: (
|
|
3009
|
+
export const onExitIf: {
|
|
3010
|
+
<A, E, XE, XR, B extends MicroExit<A, E>>(
|
|
3011
|
+
refinement: Refinement<MicroExit<A, E>, B>,
|
|
3012
|
+
f: (exit: B) => Micro<void, XE, XR>
|
|
2910
3013
|
): <R>(self: Micro<A, E, R>) => Micro<A, E | XE, R | XR>
|
|
2911
3014
|
<A, E, XE, XR>(
|
|
2912
|
-
predicate: Predicate<
|
|
2913
|
-
f: (
|
|
3015
|
+
predicate: Predicate<MicroExit<NoInfer<A>, NoInfer<E>>>,
|
|
3016
|
+
f: (exit: MicroExit<NoInfer<A>, NoInfer<E>>) => Micro<void, XE, XR>
|
|
2914
3017
|
): <R>(self: Micro<A, E, R>) => Micro<A, E | XE, R | XR>
|
|
2915
|
-
<A, E, R, XE, XR, B extends
|
|
3018
|
+
<A, E, R, XE, XR, B extends MicroExit<A, E>>(
|
|
2916
3019
|
self: Micro<A, E, R>,
|
|
2917
|
-
refinement: Refinement<
|
|
2918
|
-
f: (
|
|
3020
|
+
refinement: Refinement<MicroExit<A, E>, B>,
|
|
3021
|
+
f: (exit: B) => Micro<void, XE, XR>
|
|
2919
3022
|
): Micro<A, E | XE, R | XR>
|
|
2920
3023
|
<A, E, R, XE, XR>(
|
|
2921
3024
|
self: Micro<A, E, R>,
|
|
2922
|
-
predicate: Predicate<
|
|
2923
|
-
f: (
|
|
3025
|
+
predicate: Predicate<MicroExit<NoInfer<A>, NoInfer<E>>>,
|
|
3026
|
+
f: (exit: MicroExit<NoInfer<A>, NoInfer<E>>) => Micro<void, XE, XR>
|
|
2924
3027
|
): Micro<A, E | XE, R | XR>
|
|
2925
3028
|
} = dual(
|
|
2926
3029
|
3,
|
|
2927
|
-
<A, E, R, XE, XR, B extends
|
|
3030
|
+
<A, E, R, XE, XR, B extends MicroExit<A, E>>(
|
|
2928
3031
|
self: Micro<A, E, R>,
|
|
2929
|
-
refinement: Refinement<
|
|
2930
|
-
f: (
|
|
3032
|
+
refinement: Refinement<MicroExit<A, E>, B>,
|
|
3033
|
+
f: (exit: B) => Micro<void, XE, XR>
|
|
2931
3034
|
): Micro<A, E | XE, R | XR> =>
|
|
2932
3035
|
uninterruptibleMask((restore) =>
|
|
2933
|
-
make(function(env,
|
|
2934
|
-
restore(self)[runSymbol](env, function(
|
|
2935
|
-
if (!refinement(
|
|
2936
|
-
return
|
|
3036
|
+
make(function(env, onExit) {
|
|
3037
|
+
restore(self)[runSymbol](env, function(exit) {
|
|
3038
|
+
if (!refinement(exit)) {
|
|
3039
|
+
return onExit(exit)
|
|
2937
3040
|
}
|
|
2938
|
-
f(
|
|
2939
|
-
if (
|
|
2940
|
-
return
|
|
3041
|
+
f(exit)[runSymbol](env, function(finalizerExit) {
|
|
3042
|
+
if (finalizerExit._tag === "Left") {
|
|
3043
|
+
return onExit(finalizerExit as MicroExit<never, XE>)
|
|
2941
3044
|
}
|
|
2942
|
-
|
|
3045
|
+
onExit(exit)
|
|
2943
3046
|
})
|
|
2944
3047
|
})
|
|
2945
3048
|
})
|
|
@@ -2961,41 +3064,41 @@ export const ensuring: {
|
|
|
2961
3064
|
} = dual(
|
|
2962
3065
|
2,
|
|
2963
3066
|
<A, E, R, XE, XR>(self: Micro<A, E, R>, finalizer: Micro<void, XE, XR>): Micro<A, E | XE, R | XR> =>
|
|
2964
|
-
|
|
3067
|
+
onExit(self, (_) => finalizer)
|
|
2965
3068
|
)
|
|
2966
3069
|
|
|
2967
3070
|
/**
|
|
2968
3071
|
* When the `Micro` effect fails, run the given finalizer effect with the
|
|
2969
|
-
* `
|
|
3072
|
+
* `MicroCause` of the executed effect.
|
|
2970
3073
|
*
|
|
2971
|
-
* @since 3.4.
|
|
3074
|
+
* @since 3.4.6
|
|
2972
3075
|
* @experimental
|
|
2973
3076
|
* @category resources & finalization
|
|
2974
3077
|
*/
|
|
2975
|
-
export const
|
|
3078
|
+
export const onError: {
|
|
2976
3079
|
<A, E, XE, XR>(
|
|
2977
|
-
f: (
|
|
3080
|
+
f: (cause: MicroCause<NoInfer<E>>) => Micro<void, XE, XR>
|
|
2978
3081
|
): <R>(self: Micro<A, E, R>) => Micro<A, E | XE, R | XR>
|
|
2979
3082
|
<A, E, R, XE, XR>(
|
|
2980
3083
|
self: Micro<A, E, R>,
|
|
2981
|
-
f: (
|
|
3084
|
+
f: (cause: MicroCause<NoInfer<E>>) => Micro<void, XE, XR>
|
|
2982
3085
|
): Micro<A, E | XE, R | XR>
|
|
2983
3086
|
} = dual(
|
|
2984
3087
|
2,
|
|
2985
3088
|
<A, E, R, XE, XR>(
|
|
2986
3089
|
self: Micro<A, E, R>,
|
|
2987
|
-
f: (
|
|
2988
|
-
): Micro<A, E | XE, R | XR> =>
|
|
3090
|
+
f: (cause: MicroCause<NoInfer<E>>) => Micro<void, XE, XR>
|
|
3091
|
+
): Micro<A, E | XE, R | XR> => onExitIf(self, exitIsFailure, (exit) => f(exit.left))
|
|
2989
3092
|
)
|
|
2990
3093
|
|
|
2991
3094
|
/**
|
|
2992
3095
|
* If this `Micro` effect is aborted, run the finalizer effect.
|
|
2993
3096
|
*
|
|
2994
|
-
* @since 3.4.
|
|
3097
|
+
* @since 3.4.6
|
|
2995
3098
|
* @experimental
|
|
2996
3099
|
* @category resources & finalization
|
|
2997
3100
|
*/
|
|
2998
|
-
export const
|
|
3101
|
+
export const onInterrupt: {
|
|
2999
3102
|
<XE, XR>(
|
|
3000
3103
|
finalizer: Micro<void, XE, XR>
|
|
3001
3104
|
): <A, E, R>(self: Micro<A, E, R>) => Micro<A, E | XE, R | XR>
|
|
@@ -3003,7 +3106,7 @@ export const onAbort: {
|
|
|
3003
3106
|
} = dual(
|
|
3004
3107
|
2,
|
|
3005
3108
|
<A, E, R, XE, XR>(self: Micro<A, E, R>, finalizer: Micro<void, XE, XR>): Micro<A, E | XE, R | XR> =>
|
|
3006
|
-
|
|
3109
|
+
onExitIf(self, exitIsInterrupt, (_) => finalizer)
|
|
3007
3110
|
)
|
|
3008
3111
|
|
|
3009
3112
|
/**
|
|
@@ -3017,15 +3120,15 @@ export const onAbort: {
|
|
|
3017
3120
|
export const acquireUseRelease = <Resource, E, R, A, E2, R2, E3, R3>(
|
|
3018
3121
|
acquire: Micro<Resource, E, R>,
|
|
3019
3122
|
use: (a: Resource) => Micro<A, E2, R2>,
|
|
3020
|
-
release: (a: Resource,
|
|
3123
|
+
release: (a: Resource, exit: MicroExit<A, E2>) => Micro<void, E3, R3>
|
|
3021
3124
|
): Micro<A, E | E2 | E3, R | R2 | R3> =>
|
|
3022
3125
|
uninterruptibleMask((restore) =>
|
|
3023
3126
|
flatMap(
|
|
3024
3127
|
acquire,
|
|
3025
3128
|
(a) =>
|
|
3026
3129
|
flatMap(
|
|
3027
|
-
|
|
3028
|
-
(
|
|
3130
|
+
exit(restore(use(a))),
|
|
3131
|
+
(exit) => andThen(release(a, exit), fromExit(exit))
|
|
3029
3132
|
)
|
|
3030
3133
|
)
|
|
3031
3134
|
)
|
|
@@ -3037,14 +3140,14 @@ export const acquireUseRelease = <Resource, E, R, A, E2, R2, E3, R3>(
|
|
|
3037
3140
|
/**
|
|
3038
3141
|
* Abort the current `Micro` effect.
|
|
3039
3142
|
*
|
|
3040
|
-
* @since 3.4.
|
|
3143
|
+
* @since 3.4.6
|
|
3041
3144
|
* @experimental
|
|
3042
3145
|
* @category interruption
|
|
3043
3146
|
*/
|
|
3044
|
-
export const
|
|
3147
|
+
export const interrupt: Micro<never> = make(function(env, onExit) {
|
|
3045
3148
|
const controller = envGet(env, currentAbortController)
|
|
3046
3149
|
controller.abort()
|
|
3047
|
-
|
|
3150
|
+
onExit(exitInterrupt)
|
|
3048
3151
|
})
|
|
3049
3152
|
|
|
3050
3153
|
/**
|
|
@@ -3056,13 +3159,13 @@ export const abort: Micro<never> = make(function(env, onResult) {
|
|
|
3056
3159
|
* @category interruption
|
|
3057
3160
|
*/
|
|
3058
3161
|
export const uninterruptible = <A, E, R>(self: Micro<A, E, R>): Micro<A, E, R> =>
|
|
3059
|
-
unsafeMakeOptions(function(env,
|
|
3162
|
+
unsafeMakeOptions(function(env, onExit) {
|
|
3060
3163
|
const nextEnv = envMutate(env, function(env) {
|
|
3061
3164
|
env[currentInterruptible.key] = false
|
|
3062
3165
|
env[currentAbortSignal.key] = new AbortController().signal
|
|
3063
3166
|
return env
|
|
3064
3167
|
})
|
|
3065
|
-
self[runSymbol](nextEnv,
|
|
3168
|
+
self[runSymbol](nextEnv, onExit)
|
|
3066
3169
|
}, false)
|
|
3067
3170
|
|
|
3068
3171
|
/**
|
|
@@ -3087,7 +3190,7 @@ export const uninterruptible = <A, E, R>(self: Micro<A, E, R>): Micro<A, E, R> =
|
|
|
3087
3190
|
export const uninterruptibleMask = <A, E, R>(
|
|
3088
3191
|
f: (restore: <A, E, R>(effect: Micro<A, E, R>) => Micro<A, E, R>) => Micro<A, E, R>
|
|
3089
3192
|
): Micro<A, E, R> =>
|
|
3090
|
-
unsafeMakeOptions((env,
|
|
3193
|
+
unsafeMakeOptions((env, onExit) => {
|
|
3091
3194
|
const isInterruptible = envGet(env, currentInterruptible)
|
|
3092
3195
|
const effect = isInterruptible ? f(interruptible) : f(identity)
|
|
3093
3196
|
const nextEnv = isInterruptible ?
|
|
@@ -3097,7 +3200,7 @@ export const uninterruptibleMask = <A, E, R>(
|
|
|
3097
3200
|
return env
|
|
3098
3201
|
}) :
|
|
3099
3202
|
env
|
|
3100
|
-
effect[runSymbol](nextEnv,
|
|
3203
|
+
effect[runSymbol](nextEnv, onExit)
|
|
3101
3204
|
}, false)
|
|
3102
3205
|
|
|
3103
3206
|
/**
|
|
@@ -3109,7 +3212,7 @@ export const uninterruptibleMask = <A, E, R>(
|
|
|
3109
3212
|
* @category interruption
|
|
3110
3213
|
*/
|
|
3111
3214
|
export const interruptible = <A, E, R>(self: Micro<A, E, R>): Micro<A, E, R> =>
|
|
3112
|
-
make((env,
|
|
3215
|
+
make((env, onExit) => {
|
|
3113
3216
|
const isInterruptible = envGet(env, currentInterruptible)
|
|
3114
3217
|
let newEnv = env
|
|
3115
3218
|
if (!isInterruptible) {
|
|
@@ -3120,7 +3223,7 @@ export const interruptible = <A, E, R>(self: Micro<A, E, R>): Micro<A, E, R> =>
|
|
|
3120
3223
|
return env
|
|
3121
3224
|
})
|
|
3122
3225
|
}
|
|
3123
|
-
self[runSymbol](newEnv,
|
|
3226
|
+
self[runSymbol](newEnv, onExit)
|
|
3124
3227
|
})
|
|
3125
3228
|
|
|
3126
3229
|
// ========================================================================
|
|
@@ -3271,7 +3374,7 @@ export const forEach: {
|
|
|
3271
3374
|
readonly concurrency?: Concurrency | undefined
|
|
3272
3375
|
readonly discard?: boolean | undefined
|
|
3273
3376
|
}): Micro<any, E, R> =>
|
|
3274
|
-
make(function(env,
|
|
3377
|
+
make(function(env, onExit) {
|
|
3275
3378
|
const concurrencyOption = options?.concurrency === "inherit"
|
|
3276
3379
|
? envGet(env, currentConcurrency)
|
|
3277
3380
|
: options?.concurrency ?? 1
|
|
@@ -3283,7 +3386,7 @@ export const forEach: {
|
|
|
3283
3386
|
const [envWithSignal, onAbort] = forkSignal(env)
|
|
3284
3387
|
|
|
3285
3388
|
// iterate
|
|
3286
|
-
let
|
|
3389
|
+
let result: MicroExit<any, any> | undefined = undefined
|
|
3287
3390
|
const items = Array.from(iterable)
|
|
3288
3391
|
let length = items.length
|
|
3289
3392
|
const out: Array<B> | undefined = options?.discard ? undefined : new Array(length)
|
|
@@ -3299,26 +3402,26 @@ export const forEach: {
|
|
|
3299
3402
|
index++
|
|
3300
3403
|
inProgress++
|
|
3301
3404
|
try {
|
|
3302
|
-
f(item, currentIndex)[runSymbol](envWithSignal, function(
|
|
3303
|
-
if (
|
|
3304
|
-
if (
|
|
3305
|
-
|
|
3405
|
+
f(item, currentIndex)[runSymbol](envWithSignal, function(exit) {
|
|
3406
|
+
if (exit._tag === "Left") {
|
|
3407
|
+
if (result === undefined) {
|
|
3408
|
+
result = exit
|
|
3306
3409
|
length = index
|
|
3307
3410
|
onAbort()
|
|
3308
3411
|
}
|
|
3309
3412
|
} else if (out !== undefined) {
|
|
3310
|
-
out[currentIndex] =
|
|
3413
|
+
out[currentIndex] = exit.right
|
|
3311
3414
|
}
|
|
3312
3415
|
doneCount++
|
|
3313
3416
|
inProgress--
|
|
3314
3417
|
if (doneCount === length) {
|
|
3315
|
-
|
|
3418
|
+
onExit(result ?? Either.right(out))
|
|
3316
3419
|
} else if (!pumping && inProgress < concurrency) {
|
|
3317
3420
|
pump()
|
|
3318
3421
|
}
|
|
3319
3422
|
})
|
|
3320
3423
|
} catch (err) {
|
|
3321
|
-
|
|
3424
|
+
result = exitDie(err)
|
|
3322
3425
|
length = index
|
|
3323
3426
|
onAbort()
|
|
3324
3427
|
}
|
|
@@ -3345,7 +3448,7 @@ export const filter = <A, E, R>(iterable: Iterable<A>, f: (a: NoInfer<A>) => Mic
|
|
|
3345
3448
|
map(f(a), (pass) => {
|
|
3346
3449
|
pass = options?.negate ? !pass : pass
|
|
3347
3450
|
return pass ? Option.some(a) : Option.none()
|
|
3348
|
-
}))
|
|
3451
|
+
}), options)
|
|
3349
3452
|
|
|
3350
3453
|
/**
|
|
3351
3454
|
* Effectfully filter the elements of the provided iterable.
|
|
@@ -3471,13 +3574,13 @@ export type HandleTypeId = typeof HandleTypeId
|
|
|
3471
3574
|
*/
|
|
3472
3575
|
export interface Handle<A, E = never> {
|
|
3473
3576
|
readonly [HandleTypeId]: HandleTypeId
|
|
3474
|
-
readonly await: Micro<
|
|
3577
|
+
readonly await: Micro<MicroExit<A, E>>
|
|
3475
3578
|
readonly join: Micro<A, E>
|
|
3476
|
-
readonly
|
|
3477
|
-
readonly
|
|
3478
|
-
readonly addObserver: (observer: (
|
|
3479
|
-
readonly removeObserver: (observer: (
|
|
3480
|
-
readonly unsafePoll: () =>
|
|
3579
|
+
readonly interrupt: Micro<MicroExit<A, E>>
|
|
3580
|
+
readonly unsafeInterrupt: () => void
|
|
3581
|
+
readonly addObserver: (observer: (exit: MicroExit<A, E>) => void) => void
|
|
3582
|
+
readonly removeObserver: (observer: (exit: MicroExit<A, E>) => void) => void
|
|
3583
|
+
readonly unsafePoll: () => MicroExit<A, E> | null
|
|
3481
3584
|
}
|
|
3482
3585
|
|
|
3483
3586
|
/**
|
|
@@ -3491,8 +3594,8 @@ export const isHandle = (u: unknown): u is Handle<unknown, unknown> =>
|
|
|
3491
3594
|
class HandleImpl<A, E> implements Handle<A, E> {
|
|
3492
3595
|
readonly [HandleTypeId]: HandleTypeId
|
|
3493
3596
|
|
|
3494
|
-
readonly observers: Set<(
|
|
3495
|
-
private
|
|
3597
|
+
readonly observers: Set<(exit: MicroExit<A, E>) => void> = new Set()
|
|
3598
|
+
private _exit: MicroExit<A, E> | undefined = undefined
|
|
3496
3599
|
_controller: AbortController
|
|
3497
3600
|
readonly isRoot: boolean
|
|
3498
3601
|
|
|
@@ -3501,49 +3604,49 @@ class HandleImpl<A, E> implements Handle<A, E> {
|
|
|
3501
3604
|
this.isRoot = controller !== undefined
|
|
3502
3605
|
this._controller = controller ?? new AbortController()
|
|
3503
3606
|
if (!this.isRoot) {
|
|
3504
|
-
parentSignal.addEventListener("abort", this.
|
|
3607
|
+
parentSignal.addEventListener("abort", this.unsafeInterrupt)
|
|
3505
3608
|
}
|
|
3506
3609
|
}
|
|
3507
3610
|
|
|
3508
|
-
unsafePoll():
|
|
3509
|
-
return this.
|
|
3611
|
+
unsafePoll(): MicroExit<A, E> | null {
|
|
3612
|
+
return this._exit ?? null
|
|
3510
3613
|
}
|
|
3511
3614
|
|
|
3512
|
-
|
|
3615
|
+
unsafeInterrupt = () => {
|
|
3513
3616
|
this._controller.abort()
|
|
3514
3617
|
}
|
|
3515
3618
|
|
|
3516
|
-
emit(
|
|
3517
|
-
if (this.
|
|
3619
|
+
emit(exit: MicroExit<A, E>): void {
|
|
3620
|
+
if (this._exit) {
|
|
3518
3621
|
return
|
|
3519
3622
|
}
|
|
3520
|
-
this.
|
|
3623
|
+
this._exit = exit
|
|
3521
3624
|
if (!this.isRoot) {
|
|
3522
|
-
this.parentSignal.removeEventListener("abort", this.
|
|
3625
|
+
this.parentSignal.removeEventListener("abort", this.unsafeInterrupt)
|
|
3523
3626
|
}
|
|
3524
|
-
this.observers.forEach((observer) => observer(
|
|
3627
|
+
this.observers.forEach((observer) => observer(exit))
|
|
3525
3628
|
this.observers.clear()
|
|
3526
3629
|
}
|
|
3527
3630
|
|
|
3528
|
-
addObserver(observer: (
|
|
3529
|
-
if (this.
|
|
3530
|
-
return observer(this.
|
|
3631
|
+
addObserver(observer: (exit: MicroExit<A, E>) => void): void {
|
|
3632
|
+
if (this._exit) {
|
|
3633
|
+
return observer(this._exit)
|
|
3531
3634
|
}
|
|
3532
3635
|
this.observers.add(observer)
|
|
3533
3636
|
}
|
|
3534
3637
|
|
|
3535
|
-
removeObserver(observer: (
|
|
3638
|
+
removeObserver(observer: (exit: MicroExit<A, E>) => void): void {
|
|
3536
3639
|
this.observers.delete(observer)
|
|
3537
3640
|
}
|
|
3538
3641
|
|
|
3539
|
-
get await(): Micro<
|
|
3642
|
+
get await(): Micro<MicroExit<A, E>> {
|
|
3540
3643
|
return suspend(() => {
|
|
3541
|
-
if (this.
|
|
3542
|
-
return succeed(this.
|
|
3644
|
+
if (this._exit) {
|
|
3645
|
+
return succeed(this._exit)
|
|
3543
3646
|
}
|
|
3544
3647
|
return async((resume) => {
|
|
3545
|
-
function observer(
|
|
3546
|
-
resume(succeed(
|
|
3648
|
+
function observer(exit: MicroExit<A, E>) {
|
|
3649
|
+
resume(succeed(exit))
|
|
3547
3650
|
}
|
|
3548
3651
|
this.addObserver(observer)
|
|
3549
3652
|
return sync(() => {
|
|
@@ -3554,12 +3657,12 @@ class HandleImpl<A, E> implements Handle<A, E> {
|
|
|
3554
3657
|
}
|
|
3555
3658
|
|
|
3556
3659
|
get join(): Micro<A, E> {
|
|
3557
|
-
return flatMap(this.await,
|
|
3660
|
+
return flatMap(this.await, fromExit)
|
|
3558
3661
|
}
|
|
3559
3662
|
|
|
3560
|
-
get
|
|
3663
|
+
get interrupt(): Micro<MicroExit<A, E>> {
|
|
3561
3664
|
return suspend(() => {
|
|
3562
|
-
this.
|
|
3665
|
+
this.unsafeInterrupt()
|
|
3563
3666
|
return this.await
|
|
3564
3667
|
})
|
|
3565
3668
|
}
|
|
@@ -3576,7 +3679,7 @@ class HandleImpl<A, E> implements Handle<A, E> {
|
|
|
3576
3679
|
* @category handle & forking
|
|
3577
3680
|
*/
|
|
3578
3681
|
export const fork = <A, E, R>(self: Micro<A, E, R>): Micro<Handle<A, E>, never, R> =>
|
|
3579
|
-
make(function(env,
|
|
3682
|
+
make(function(env, onExit) {
|
|
3580
3683
|
const signal = envGet(env, currentAbortSignal)
|
|
3581
3684
|
const handle = new HandleImpl<A, E>(signal)
|
|
3582
3685
|
const nextEnv = envMutate(env, (map) => {
|
|
@@ -3585,11 +3688,11 @@ export const fork = <A, E, R>(self: Micro<A, E, R>): Micro<Handle<A, E>, never,
|
|
|
3585
3688
|
return map
|
|
3586
3689
|
})
|
|
3587
3690
|
yieldAdd(() => {
|
|
3588
|
-
self[runSymbol](nextEnv, (
|
|
3589
|
-
handle.emit(
|
|
3691
|
+
self[runSymbol](nextEnv, (exit) => {
|
|
3692
|
+
handle.emit(exit)
|
|
3590
3693
|
})
|
|
3591
3694
|
})
|
|
3592
|
-
|
|
3695
|
+
onExit(Either.right(handle))
|
|
3593
3696
|
})
|
|
3594
3697
|
|
|
3595
3698
|
/**
|
|
@@ -3603,7 +3706,7 @@ export const fork = <A, E, R>(self: Micro<A, E, R>): Micro<Handle<A, E>, never,
|
|
|
3603
3706
|
* @category handle & forking
|
|
3604
3707
|
*/
|
|
3605
3708
|
export const forkDaemon = <A, E, R>(self: Micro<A, E, R>): Micro<Handle<A, E>, never, R> =>
|
|
3606
|
-
make(function(env,
|
|
3709
|
+
make(function(env, onExit) {
|
|
3607
3710
|
const controller = new AbortController()
|
|
3608
3711
|
const handle = new HandleImpl<A, E>(controller.signal, controller)
|
|
3609
3712
|
const nextEnv = envMutate(env, (map) => {
|
|
@@ -3612,11 +3715,11 @@ export const forkDaemon = <A, E, R>(self: Micro<A, E, R>): Micro<Handle<A, E>, n
|
|
|
3612
3715
|
return map
|
|
3613
3716
|
})
|
|
3614
3717
|
yieldAdd(() => {
|
|
3615
|
-
self[runSymbol](nextEnv, (
|
|
3616
|
-
handle.emit(
|
|
3718
|
+
self[runSymbol](nextEnv, (exit) => {
|
|
3719
|
+
handle.emit(exit)
|
|
3617
3720
|
})
|
|
3618
3721
|
})
|
|
3619
|
-
|
|
3722
|
+
onExit(Either.right(handle))
|
|
3620
3723
|
})
|
|
3621
3724
|
|
|
3622
3725
|
/**
|
|
@@ -3638,8 +3741,8 @@ export const forkIn: {
|
|
|
3638
3741
|
uninterruptibleMask((restore) =>
|
|
3639
3742
|
flatMap(scope.fork, (scope) =>
|
|
3640
3743
|
tap(
|
|
3641
|
-
restore(forkDaemon(
|
|
3642
|
-
(fiber) => scope.addFinalizer((_) => asVoid(fiber.
|
|
3744
|
+
restore(forkDaemon(onExit(self, (exit) => scope.close(exit)))),
|
|
3745
|
+
(fiber) => scope.addFinalizer((_) => asVoid(fiber.interrupt))
|
|
3643
3746
|
))
|
|
3644
3747
|
)
|
|
3645
3748
|
)
|
|
@@ -3679,8 +3782,8 @@ export const forkScoped = <A, E, R>(self: Micro<A, E, R>): Micro<Handle<A, E>, n
|
|
|
3679
3782
|
* Micro.runFork
|
|
3680
3783
|
* )
|
|
3681
3784
|
*
|
|
3682
|
-
* handle.addObserver((
|
|
3683
|
-
* console.log(
|
|
3785
|
+
* handle.addObserver((exit) => {
|
|
3786
|
+
* console.log(exit)
|
|
3684
3787
|
* })
|
|
3685
3788
|
*/
|
|
3686
3789
|
export const runFork = <A, E>(
|
|
@@ -3695,17 +3798,17 @@ export const runFork = <A, E>(
|
|
|
3695
3798
|
refs[currentAbortSignal.key] = controller.signal
|
|
3696
3799
|
const env = envMake(refs)
|
|
3697
3800
|
const handle = new HandleImpl<A, E>(controller.signal, controller)
|
|
3698
|
-
effect[runSymbol](envSet(env, currentAbortSignal, handle._controller.signal), (
|
|
3699
|
-
handle.emit(
|
|
3801
|
+
effect[runSymbol](envSet(env, currentAbortSignal, handle._controller.signal), (exit) => {
|
|
3802
|
+
handle.emit(exit)
|
|
3700
3803
|
if (options?.signal) {
|
|
3701
|
-
options.signal.removeEventListener("abort", handle.
|
|
3804
|
+
options.signal.removeEventListener("abort", handle.unsafeInterrupt)
|
|
3702
3805
|
}
|
|
3703
3806
|
})
|
|
3704
3807
|
if (options?.signal) {
|
|
3705
3808
|
if (options.signal.aborted) {
|
|
3706
|
-
handle.
|
|
3809
|
+
handle.unsafeInterrupt()
|
|
3707
3810
|
} else {
|
|
3708
|
-
options.signal.addEventListener("abort", handle.
|
|
3811
|
+
options.signal.addEventListener("abort", handle.unsafeInterrupt, { once: true })
|
|
3709
3812
|
}
|
|
3710
3813
|
}
|
|
3711
3814
|
return handle
|
|
@@ -3713,18 +3816,18 @@ export const runFork = <A, E>(
|
|
|
3713
3816
|
|
|
3714
3817
|
/**
|
|
3715
3818
|
* Execute the `Micro` effect and return a `Promise` that resolves with the
|
|
3716
|
-
* `
|
|
3819
|
+
* `MicroExit` of the computation.
|
|
3717
3820
|
*
|
|
3718
|
-
* @since 3.4.
|
|
3821
|
+
* @since 3.4.6
|
|
3719
3822
|
* @experimental
|
|
3720
3823
|
* @category execution
|
|
3721
3824
|
*/
|
|
3722
|
-
export const
|
|
3825
|
+
export const runPromiseExit = <A, E>(
|
|
3723
3826
|
effect: Micro<A, E>,
|
|
3724
3827
|
options?: {
|
|
3725
3828
|
readonly signal?: AbortSignal | undefined
|
|
3726
3829
|
} | undefined
|
|
3727
|
-
): Promise<
|
|
3830
|
+
): Promise<MicroExit<A, E>> =>
|
|
3728
3831
|
new Promise((resolve, _reject) => {
|
|
3729
3832
|
const handle = runFork(effect, options)
|
|
3730
3833
|
handle.addObserver(resolve)
|
|
@@ -3744,33 +3847,33 @@ export const runPromise = <A, E>(
|
|
|
3744
3847
|
readonly signal?: AbortSignal | undefined
|
|
3745
3848
|
} | undefined
|
|
3746
3849
|
): Promise<A> =>
|
|
3747
|
-
|
|
3748
|
-
if (
|
|
3749
|
-
throw
|
|
3850
|
+
runPromiseExit(effect, options).then((exit) => {
|
|
3851
|
+
if (exit._tag === "Left") {
|
|
3852
|
+
throw exit.left
|
|
3750
3853
|
}
|
|
3751
|
-
return
|
|
3854
|
+
return exit.right
|
|
3752
3855
|
})
|
|
3753
3856
|
|
|
3754
3857
|
/**
|
|
3755
|
-
* Attempt to execute the `Micro` effect synchronously and return the `
|
|
3858
|
+
* Attempt to execute the `Micro` effect synchronously and return the `MicroExit`.
|
|
3756
3859
|
*
|
|
3757
3860
|
* If any asynchronous effects are encountered, the function will return a
|
|
3758
|
-
*
|
|
3861
|
+
* `CauseDie` containing the `Handle`.
|
|
3759
3862
|
*
|
|
3760
|
-
* @since 3.4.
|
|
3863
|
+
* @since 3.4.6
|
|
3761
3864
|
* @experimental
|
|
3762
3865
|
* @category execution
|
|
3763
3866
|
*/
|
|
3764
|
-
export const
|
|
3867
|
+
export const runSyncExit = <A, E>(effect: Micro<A, E>): MicroExit<A, E> => {
|
|
3765
3868
|
const handle = runFork(effect)
|
|
3766
3869
|
while (yieldState.tasks.length > 0) {
|
|
3767
3870
|
yieldRunTasks()
|
|
3768
3871
|
}
|
|
3769
|
-
const
|
|
3770
|
-
if (
|
|
3771
|
-
return
|
|
3872
|
+
const exit = handle.unsafePoll()
|
|
3873
|
+
if (exit === null) {
|
|
3874
|
+
return exitDie(handle)
|
|
3772
3875
|
}
|
|
3773
|
-
return
|
|
3876
|
+
return exit
|
|
3774
3877
|
}
|
|
3775
3878
|
|
|
3776
3879
|
/**
|
|
@@ -3782,11 +3885,11 @@ export const runSyncResult = <A, E>(effect: Micro<A, E>): Result<A, E> => {
|
|
|
3782
3885
|
* @category execution
|
|
3783
3886
|
*/
|
|
3784
3887
|
export const runSync = <A, E>(effect: Micro<A, E>): A => {
|
|
3785
|
-
const
|
|
3786
|
-
if (
|
|
3787
|
-
throw
|
|
3888
|
+
const exit = runSyncExit(effect)
|
|
3889
|
+
if (exit._tag === "Left") {
|
|
3890
|
+
throw exit.left
|
|
3788
3891
|
}
|
|
3789
|
-
return
|
|
3892
|
+
return exit.right
|
|
3790
3893
|
}
|
|
3791
3894
|
|
|
3792
3895
|
// ----------------------------------------------------------------------------
|
|
@@ -3804,14 +3907,14 @@ export interface YieldableError extends Pipeable, Inspectable, Readonly<Error> {
|
|
|
3804
3907
|
readonly [SinkTypeId]: Sink.VarianceStruct<never, unknown, never, this, never>
|
|
3805
3908
|
readonly [ChannelTypeId]: Channel.VarianceStruct<never, unknown, this, unknown, never, unknown, never>
|
|
3806
3909
|
readonly [TypeId]: Micro.Variance<never, this, never>
|
|
3807
|
-
readonly [runSymbol]: (env: Env<any>,
|
|
3910
|
+
readonly [runSymbol]: (env: Env<any>, onExit: (exit: MicroExit<never, this>) => void) => void
|
|
3808
3911
|
[Symbol.iterator](): MicroIterator<Micro<never, this, never>>
|
|
3809
3912
|
}
|
|
3810
3913
|
|
|
3811
3914
|
const YieldableError: new(message?: string) => YieldableError = (function() {
|
|
3812
3915
|
class YieldableError extends globalThis.Error {
|
|
3813
|
-
[runSymbol](_env: any,
|
|
3814
|
-
|
|
3916
|
+
[runSymbol](_env: any, onExit: any) {
|
|
3917
|
+
onExit(exitFail(this))
|
|
3815
3918
|
}
|
|
3816
3919
|
toString() {
|
|
3817
3920
|
return this.message ? `${this.name}: ${this.message}` : this.name
|
|
@@ -3875,3 +3978,12 @@ export const TaggedError = <Tag extends string>(tag: Tag): new<A extends Record<
|
|
|
3875
3978
|
* @category errors
|
|
3876
3979
|
*/
|
|
3877
3980
|
export class NoSuchElementException extends TaggedError("NoSuchElementException")<{ message?: string | undefined }> {}
|
|
3981
|
+
|
|
3982
|
+
/**
|
|
3983
|
+
* Represents a checked exception which occurs when a timeout occurs.
|
|
3984
|
+
*
|
|
3985
|
+
* @since 3.4.4
|
|
3986
|
+
* @experimental
|
|
3987
|
+
* @category errors
|
|
3988
|
+
*/
|
|
3989
|
+
export class TimeoutException extends TaggedError("TimeoutException") {}
|