Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

optional AbortSignal apis using Function.length #566

Merged
merged 5 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nasty-forks-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/io": minor
---

optional AbortSignal apis - remove \*Interrupt variants
113 changes: 28 additions & 85 deletions docs/modules/Effect.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ Added in v1.0.0
- [async](#async)
- [asyncEffect](#asynceffect)
- [asyncEither](#asynceither)
- [asyncInterrupt](#asyncinterrupt)
- [asyncOption](#asyncoption)
- [die](#die)
- [dieMessage](#diemessage)
Expand All @@ -77,7 +76,6 @@ Added in v1.0.0
- [never](#never)
- [none](#none)
- [promise](#promise)
- [promiseInterrupt](#promiseinterrupt)
- [succeed](#succeed)
- [succeedNone](#succeednone)
- [succeedSome](#succeedsome)
Expand Down Expand Up @@ -150,9 +148,7 @@ Added in v1.0.0
- [try](#try)
- [tryMap](#trymap)
- [tryMapPromise](#trymappromise)
- [tryMapPromiseInterrupt](#trymappromiseinterrupt)
- [tryPromise](#trypromise)
- [tryPromiseInterrupt](#trypromiseinterrupt)
- [unsandbox](#unsandbox)
- [execution](#execution)
- [runCallback](#runcallback)
Expand Down Expand Up @@ -1154,14 +1150,17 @@ The callback function `Effect<R, E, A> => void` must be called at most once.
If an Effect is returned by the registration function, it will be executed
if the fiber executing the effect is interrupted.

The registration function can also receive an `AbortSignal` if required for
interruption.

The `FiberId` of the fiber that may complete the async callback may be
provided to allow for better diagnostics.

**Signature**

```ts
export declare const async: <R, E, A>(
register: (callback: (_: Effect<R, E, A>) => void) => void | Effect<R, never, void>,
register: (callback: (_: Effect<R, E, A>) => void, signal: AbortSignal) => void | Effect<R, never, void>,
blockingOn?: FiberId.FiberId
) => Effect<R, E, A>
```
Expand Down Expand Up @@ -1212,28 +1211,6 @@ export declare const asyncEither: <R, E, A>(

Added in v1.0.0

## asyncInterrupt

Imports an asynchronous side-effect into a pure `Effect` value.
The callback function `Effect<R, E, A> => void` must be called at most once.

The registration function receives an AbortSignal that can be used to handle
interruption.

The `FiberId` of the fiber that may complete the async callback may be
provided to allow for better diagnostics.

**Signature**

```ts
export declare const asyncInterrupt: <R, E, A>(
register: (callback: (_: Effect<R, E, A>) => void, signal: AbortSignal) => void,
blockingOn?: FiberId.FiberId
) => Effect<R, E, A>
```

Added in v1.0.0

## asyncOption

Imports an asynchronous effect into a pure `Effect` value, possibly returning
Expand Down Expand Up @@ -1376,22 +1353,15 @@ Added in v1.0.0

Like `tryPromise` but produces a defect in case of errors.

**Signature**

```ts
export declare const promise: <A>(evaluate: LazyArg<Promise<A>>) => Effect<never, never, A>
```

Added in v1.0.0

## promiseInterrupt

Like `promise` but allows for interruption via AbortSignal
An optional `AbortSignal` can be provided to allow for interruption of the
wrapped Promise api.

**Signature**

```ts
export declare const promiseInterrupt: <A>(evaluate: (signal: AbortSignal) => Promise<A>) => Effect<never, never, A>
export declare const promise: <A>(
evaluate: LazyArg<Promise<A>> | ((signal: AbortSignal) => Promise<A>)
) => Effect<never, never, A>
```

Added in v1.0.0
Expand Down Expand Up @@ -2507,37 +2477,23 @@ Added in v1.0.0
Returns an effect whose success is mapped by the specified side effecting
`f` function, translating any promise rejections into typed failed effects.

**Signature**

```ts
export declare const tryMapPromise: {
<A, B, E1>(options: { readonly try: (a: A) => Promise<B>; readonly catch: (error: unknown) => E1 }): <R, E>(
self: Effect<R, E, A>
) => Effect<R, E1 | E, B>
<R, E, A, B, E1>(
self: Effect<R, E, A>,
options: { readonly try: (a: A) => Promise<B>; readonly catch: (error: unknown) => E1 }
): Effect<R, E | E1, B>
}
```

Added in v1.0.0

## tryMapPromiseInterrupt

Like `tryMapPromise` but allows for interruption via AbortSignal
An optional `AbortSignal` can be provided to allow for interruption of the
wrapped Promise api.

**Signature**

```ts
export declare const tryMapPromiseInterrupt: {
<A, B, E1>(options: {
readonly try: (a: A, signal: AbortSignal) => Promise<B>
readonly catch: (error: unknown) => E1
}): <R, E>(self: Effect<R, E, A>) => Effect<R, E1 | E, B>
export declare const tryMapPromise: {
<A, B, E1>(
options:
| { readonly try: (a: A, signal: AbortSignal) => Promise<B>; readonly catch: (error: unknown) => E1 }
| { readonly try: (a: A) => Promise<B>; readonly catch: (error: unknown) => E1 }
): <R, E>(self: Effect<R, E, A>) => Effect<R, E1 | E, B>
<R, E, A, B, E1>(
self: Effect<R, E, A>,
options: { readonly try: (a: A, signal: AbortSignal) => Promise<B>; readonly catch: (error: unknown) => E1 }
options:
| { readonly try: (a: A, signal: AbortSignal) => Promise<B>; readonly catch: (error: unknown) => E1 }
| { readonly try: (a: A) => Promise<B>; readonly catch: (error: unknown) => E1 }
): Effect<R, E | E1, B>
}
```
Expand All @@ -2549,31 +2505,18 @@ Added in v1.0.0
Create an `Effect` that when executed will construct `promise` and wait for
its result, errors will produce failure as `unknown`.

**Signature**

```ts
export declare const tryPromise: {
<A, E>(options: { readonly try: LazyArg<Promise<A>>; readonly catch: (error: unknown) => E }): Effect<never, E, A>
<A>(try_: LazyArg<Promise<A>>): Effect<never, unknown, A>
}
```

Added in v1.0.0

## tryPromiseInterrupt

Like `tryPromise` but allows for interruption via AbortSignal
An optional `AbortSignal` can be provided to allow for interruption of the
wrapped Promise api.

**Signature**

```ts
export declare const tryPromiseInterrupt: {
<A, E>(options: { readonly try: (signal: AbortSignal) => Promise<A>; readonly catch: (error: unknown) => E }): Effect<
never,
E,
A
>
<A>(try_: (signal: AbortSignal) => Promise<A>): Effect<never, unknown, A>
export declare const tryPromise: {
<A, E>(options: {
readonly try: LazyArg<Promise<A>> | ((signal: AbortSignal) => Promise<A>)
readonly catch: (error: unknown) => E
}): Effect<never, E, A>
<A>(try_: LazyArg<Promise<A>> | ((signal: AbortSignal) => Promise<A>)): Effect<never, unknown, A>
}
```

Expand Down
91 changes: 27 additions & 64 deletions src/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,14 +891,17 @@ export const validateFirst: {
* If an Effect is returned by the registration function, it will be executed
* if the fiber executing the effect is interrupted.
*
* The registration function can also receive an `AbortSignal` if required for
* interruption.
*
* The `FiberId` of the fiber that may complete the async callback may be
* provided to allow for better diagnostics.
*
* @since 1.0.0
* @category constructors
*/
export const async: <R, E, A>(
register: (callback: (_: Effect<R, E, A>) => void) => void | Effect<R, never, void>,
register: (callback: (_: Effect<R, E, A>) => void, signal: AbortSignal) => void | Effect<R, never, void>,
blockingOn?: FiberId.FiberId
) => Effect<R, E, A> = core.async

Expand All @@ -915,24 +918,6 @@ export const asyncEffect: <R, E, A, R2, E2, X>(
register: (callback: (_: Effect<R, E, A>) => void) => Effect<R2, E2, X>
) => Effect<R | R2, E | E2, A> = _runtime.asyncEffect

/**
* Imports an asynchronous side-effect into a pure `Effect` value.
* The callback function `Effect<R, E, A> => void` must be called at most once.
*
* The registration function receives an AbortSignal that can be used to handle
* interruption.
*
* The `FiberId` of the fiber that may complete the async callback may be
* provided to allow for better diagnostics.
*
* @since 1.0.0
* @category constructors
*/
export const asyncInterrupt: <R, E, A>(
register: (callback: (_: Effect<R, E, A>) => void, signal: AbortSignal) => void,
blockingOn?: FiberId.FiberId
) => Effect<R, E, A> = core.asyncInterrupt

/**
* Imports an asynchronous effect into a pure `Effect` value, possibly returning
* the value synchronously.
Expand Down Expand Up @@ -1332,19 +1317,15 @@ export const none: <R, E, A>(self: Effect<R, E, Option.Option<A>>) => Effect<R,
/**
* Like `tryPromise` but produces a defect in case of errors.
*
* @since 1.0.0
* @category constructors
*/
export const promise: <A>(evaluate: LazyArg<Promise<A>>) => Effect<never, never, A> = effect.promise

/**
* Like `promise` but allows for interruption via AbortSignal
* An optional `AbortSignal` can be provided to allow for interruption of the
* wrapped Promise api.
*
* @since 1.0.0
* @category constructors
*/
export const promiseInterrupt: <A>(evaluate: (signal: AbortSignal) => Promise<A>) => Effect<never, never, A> =
effect.promiseInterrupt
export const promise: <A>(
evaluate: LazyArg<Promise<A>> | ((signal: AbortSignal) => Promise<A>)
) => Effect<never, never, A> = effect.promise

/**
* @since 1.0.0
Expand Down Expand Up @@ -1779,71 +1760,53 @@ export const tryMap: {
* Returns an effect whose success is mapped by the specified side effecting
* `f` function, translating any promise rejections into typed failed effects.
*
* An optional `AbortSignal` can be provided to allow for interruption of the
* wrapped Promise api.
*
* @since 1.0.0
* @category error handling
*/
export const tryMapPromise: {
<A, B, E1>(
options: {
readonly try: (a: A) => Promise<B>
readonly try: (a: A, signal: AbortSignal) => Promise<B>
readonly catch: (error: unknown) => E1
}
): <R, E>(self: Effect<R, E, A>) => Effect<R, E1 | E, B>
<R, E, A, B, E1>(
self: Effect<R, E, A>,
options: {
} | {
readonly try: (a: A) => Promise<B>
readonly catch: (error: unknown) => E1
}
): Effect<R, E | E1, B>
} = effect.tryMapPromise

/**
* Like `tryMapPromise` but allows for interruption via AbortSignal
*
* @since 1.0.0
* @category error handling
*/
export const tryMapPromiseInterrupt: {
<A, B, E1>(
options: {
readonly try: (a: A, signal: AbortSignal) => Promise<B>
readonly catch: (error: unknown) => E1
}
): <R, E>(self: Effect<R, E, A>) => Effect<R, E1 | E, B>
<R, E, A, B, E1>(
self: Effect<R, E, A>,
options: {
readonly try: (a: A, signal: AbortSignal) => Promise<B>
readonly catch: (error: unknown) => E1
} | {
readonly try: (a: A) => Promise<B>
readonly catch: (error: unknown) => E1
}
): Effect<R, E | E1, B>
} = effect.tryMapPromiseInterrupt
} = effect.tryMapPromise

/**
* Create an `Effect` that when executed will construct `promise` and wait for
* its result, errors will produce failure as `unknown`.
*
* @since 1.0.0
* @category error handling
*/
export const tryPromise: {
<A, E>(options: { readonly try: LazyArg<Promise<A>>; readonly catch: (error: unknown) => E }): Effect<never, E, A>
<A>(try_: LazyArg<Promise<A>>): Effect<never, unknown, A>
} = effect.tryPromise

/**
* Like `tryPromise` but allows for interruption via AbortSignal
* An optional `AbortSignal` can be provided to allow for interruption of the
* wrapped Promise api.
*
* @since 1.0.0
* @category error handling
*/
export const tryPromiseInterrupt: {
export const tryPromise: {
<A, E>(
options: { readonly try: (signal: AbortSignal) => Promise<A>; readonly catch: (error: unknown) => E }
options: {
readonly try: LazyArg<Promise<A>> | ((signal: AbortSignal) => Promise<A>)
readonly catch: (error: unknown) => E
}
): Effect<never, E, A>
<A>(try_: (signal: AbortSignal) => Promise<A>): Effect<never, unknown, A>
} = effect.tryPromiseInterrupt
<A>(try_: LazyArg<Promise<A>> | ((signal: AbortSignal) => Promise<A>)): Effect<never, unknown, A>
} = effect.tryPromise

/**
* The inverse operation `sandbox(effect)`
Expand Down
Loading
Loading