Skip to content

Commit

Permalink
Renamed IAsync to Async (#3906)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncave authored Sep 28, 2024
1 parent 7070748 commit a84cdd8
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 68 deletions.
3 changes: 2 additions & 1 deletion src/Fable.Transforms/Fable2Babel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -757,8 +757,9 @@ module Annotation =
// Move this to Replacements.tryEntity?
let tryNativeOrFableLibraryInterface com ctx genArgs (ent: Fable.Entity) =
match ent.FullName with
| Types.fsharpAsyncGeneric -> makeFableLibImportTypeAnnotation com ctx genArgs "AsyncBuilder" "IAsync" |> Some
| Types.fsharpAsyncGeneric -> makeFableLibImportTypeAnnotation com ctx genArgs "AsyncBuilder" "Async" |> Some
| _ when not ent.IsInterface -> None
// everything below is an interface
| Types.icollection -> makeNativeTypeAnnotation com ctx genArgs "Iterable" |> Some
// -> makeFableLibImportTypeAnnotation com ctx [Fable.Any] "Util" "ICollection"
| Types.icollectionGeneric -> makeNativeTypeAnnotation com ctx genArgs "Iterable" |> Some
Expand Down
8 changes: 4 additions & 4 deletions src/fable-library-py/fable_library/Async.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let defaultCancellationToken = new CancellationToken()
[<Erase>]
type Async =
static member StartWithContinuations
(computation: IAsync<'T>, continuation, exceptionContinuation, cancellationContinuation, ?cancellationToken)
(computation: Async<'T>, continuation, exceptionContinuation, cancellationContinuation, ?cancellationToken)
: unit
=
let trampoline = Trampoline()
Expand All @@ -31,7 +31,7 @@ type Async =
}
)

static member StartWithContinuations(computation: IAsync<'T>, ?cancellationToken) : unit =
static member StartWithContinuations(computation: Async<'T>, ?cancellationToken) : unit =
Async.StartWithContinuations(
computation,
emptyContinuation,
Expand All @@ -44,8 +44,8 @@ type Async =
Async.StartWithContinuations(computation, ?cancellationToken = cancellationToken)


static member StartImmediate(computation: IAsync<'T>, ?cancellationToken) =
static member StartImmediate(computation: Async<'T>, ?cancellationToken) =
Async.Start(computation, ?cancellationToken = cancellationToken)

let startImmediate (computation: IAsync<'T>) =
let startImmediate (computation: Async<'T>) =
Async.StartImmediate(computation, ?cancellationToken = None)
48 changes: 23 additions & 25 deletions src/fable-library-py/fable_library/AsyncBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ type IAsyncContext<'T> =
abstract member cancelToken: CancellationToken
abstract member trampoline: Trampoline

type IAsync<'T> = IAsyncContext<'T> -> unit
type Async<'T> = IAsyncContext<'T> -> unit

let protectedCont<'T> (f: IAsync<'T>) =
let protectedCont<'T> (f: Async<'T>) =
fun (ctx: IAsyncContext<'T>) ->
if ctx.cancelToken.IsCancelled then
ctx.onCancel (new OperationCanceledError())
Expand All @@ -93,7 +93,7 @@ let protectedCont<'T> (f: IAsync<'T>) =
with err ->
ctx.onError (err)

let protectedBind<'T, 'U> (computation: IAsync<'T>, binder: 'T -> IAsync<'U>) =
let protectedBind<'T, 'U> (computation: Async<'T>, binder: 'T -> Async<'U>) =
protectedCont (fun (ctx: IAsyncContext<'U>) ->
computation (
{ new IAsyncContext<'T> with
Expand All @@ -116,34 +116,32 @@ let protectedReturn<'T> (value: 'T) =
protectedCont (fun (ctx: IAsyncContext<'T>) -> ctx.onSuccess (value))

type IAsyncBuilder =
abstract member Bind<'T, 'U> : IAsync<'T> * ('T -> IAsync<'U>) -> IAsync<'U>
abstract member Bind<'T, 'U> : Async<'T> * ('T -> Async<'U>) -> Async<'U>

abstract member Combine<'T> : IAsync<unit> * IAsync<'T> -> IAsync<'T>
abstract member Combine<'T> : Async<unit> * Async<'T> -> Async<'T>

abstract member Delay<'T> : (unit -> IAsync<'T>) -> IAsync<'T>
abstract member Delay<'T> : (unit -> Async<'T>) -> Async<'T>

//abstract member Return<'T> : [<ParamArray>] values: 'T [] -> IAsync<'T>
abstract member Return<'T> : value: 'T -> IAsync<'T>
//abstract member Return<'T> : [<ParamArray>] values: 'T [] -> Async<'T>
abstract member Return<'T> : value: 'T -> Async<'T>

abstract member While: (unit -> bool) * IAsync<unit> -> IAsync<unit>
abstract member Zero: unit -> IAsync<unit>
abstract member While: (unit -> bool) * Async<unit> -> Async<unit>
abstract member Zero: unit -> Async<unit>


type AsyncBuilder() =
interface IAsyncBuilder with

member this.Bind<'T, 'U>(computation: IAsync<'T>, binder: 'T -> IAsync<'U>) =
protectedBind (computation, binder)
member this.Bind<'T, 'U>(computation: Async<'T>, binder: 'T -> Async<'U>) = protectedBind (computation, binder)

member this.Combine<'T>(computation1: IAsync<unit>, computation2: IAsync<'T>) =
member this.Combine<'T>(computation1: Async<unit>, computation2: Async<'T>) =
let self = this :> IAsyncBuilder
self.Bind(computation1, (fun () -> computation2))

member x.Delay<'T>(generator: unit -> IAsync<'T>) =
member x.Delay<'T>(generator: unit -> Async<'T>) =
protectedCont (fun (ctx: IAsyncContext<'T>) -> generator () (ctx))


// public For<T>(sequence: Iterable<T>, body: (x: T) => IAsync<void>) {
// public For<T>(sequence: Iterable<T>, body: (x: T) => Async<void>) {
// const iter = sequence[Symbol.iterator]();
// let cur = iter.next();
// return this.While(() => !cur.done, this.Delay(() => {
Expand All @@ -153,19 +151,19 @@ type AsyncBuilder() =
// }));
// }

member this.Return<'T>(value: 'T) : IAsync<'T> = protectedReturn (unbox value)
// member this.Return<'T>([<ParamArray>] value: 'T []) : IAsync<'T> =
member this.Return<'T>(value: 'T) : Async<'T> = protectedReturn (unbox value)
// member this.Return<'T>([<ParamArray>] value: 'T []) : Async<'T> =
// match value with
// | [||] -> protectedReturn (unbox null)
// | [| value |] -> protectedReturn value
// | _ -> failwith "Return takes zero or one argument."


// public ReturnFrom<T>(computation: IAsync<T>) {
// public ReturnFrom<T>(computation: Async<T>) {
// return computation;
// }

// public TryFinally<T>(computation: IAsync<T>, compensation: () => void) {
// public TryFinally<T>(computation: Async<T>, compensation: () => void) {
// return protectedCont((ctx: IAsyncContext<T>) => {
// computation({
// onSuccess: (x: T) => {
Expand All @@ -186,7 +184,7 @@ type AsyncBuilder() =
// });
// }

// public TryWith<T>(computation: IAsync<T>, catchHandler: (e: any) => IAsync<T>) {
// public TryWith<T>(computation: Async<T>, catchHandler: (e: any) => Async<T>) {
// return protectedCont((ctx: IAsyncContext<T>) => {
// computation({
// onSuccess: ctx.onSuccess,
Expand All @@ -204,20 +202,20 @@ type AsyncBuilder() =
// });
// }

// public Using<T extends IDisposable, U>(resource: T, binder: (x: T) => IAsync<U>) {
// public Using<T extends IDisposable, U>(resource: T, binder: (x: T) => Async<U>) {
// return this.TryFinally(binder(resource), () => resource.Dispose());
// }

member this.While(guard: unit -> bool, computation: IAsync<unit>) : IAsync<unit> =
member this.While(guard: unit -> bool, computation: Async<unit>) : Async<unit> =
let self = this :> IAsyncBuilder

if guard () then
self.Bind(computation, (fun () -> self.While(guard, computation)))
else
self.Return()

// member this.Bind<'T, 'U>(computation: IAsync<'T>, binder: 'T -> IAsync<'U>) = (this :> IAsyncBuilder).Bind(computation, binder)
member this.Zero() : IAsync<unit> =
// member this.Bind<'T, 'U>(computation: Async<'T>, binder: 'T -> Async<'U>) = (this :> IAsyncBuilder).Bind(computation, binder)
member this.Zero() : Async<unit> =
protectedCont (fun (ctx: IAsyncContext<unit>) -> ctx.onSuccess (()))

// }
Expand Down
44 changes: 20 additions & 24 deletions src/fable-library-ts/Async.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { OperationCanceledError, Trampoline } from "./AsyncBuilder.js";
import { Continuation, Continuations } from "./AsyncBuilder.js";
import { CancellationToken } from "./AsyncBuilder.js";
import { IAsync } from "./AsyncBuilder.js";
import { IAsyncContext } from "./AsyncBuilder.js";
import { protectedCont } from "./AsyncBuilder.js";
import { protectedBind } from "./AsyncBuilder.js";
import { protectedReturn } from "./AsyncBuilder.js";
import { Async, IAsyncContext, CancellationToken } from "./AsyncBuilder.js";
import { protectedCont, protectedBind, protectedReturn } from "./AsyncBuilder.js";
import { FSharpChoice$2_$union, Choice_makeChoice1Of2, Choice_makeChoice2Of2 } from "./Choice.js";
import { TimeoutException } from "./SystemException.js";

Expand All @@ -14,24 +10,24 @@ function emptyContinuation<T>(_x: T) {
}

// see AsyncBuilder.Delay
function delay<T>(generator: () => IAsync<T>) {
function delay<T>(generator: () => Async<T>) {
return protectedCont((ctx: IAsyncContext<T>) => generator()(ctx));
}

// MakeAsync: body:(AsyncActivation<'T> -> AsyncReturn) -> Async<'T>
export function makeAsync<T>(body: IAsync<T>) {
export function makeAsync<T>(body: Async<T>) {
return body;
}
// Invoke: computation: Async<'T> -> ctxt:AsyncActivation<'T> -> AsyncReturn
export function invoke<T>(computation: IAsync<T>, ctx: IAsyncContext<T>) {
export function invoke<T>(computation: Async<T>, ctx: IAsyncContext<T>) {
return computation(ctx);
}
// CallThenInvoke: ctxt:AsyncActivation<'T> -> result1:'U -> part2:('U -> Async<'T>) -> AsyncReturn
export function callThenInvoke<T, U>(ctx: IAsyncContext<T>, result1: U, part2: (x: U) => IAsync<T>) {
export function callThenInvoke<T, U>(ctx: IAsyncContext<T>, result1: U, part2: (x: U) => Async<T>) {
return part2(result1)(ctx);
}
// Bind: ctxt:AsyncActivation<'T> -> part1:Async<'U> -> part2:('U -> Async<'T>) -> AsyncReturn
export function bind<T, U>(ctx: IAsyncContext<T>, part1: IAsync<U>, part2: (x: U) => IAsync<T>) {
export function bind<T, U>(ctx: IAsyncContext<T>, part1: Async<U>, part2: (x: U) => Async<T>) {
return protectedBind(part1, part2)(ctx);
}

Expand Down Expand Up @@ -61,7 +57,7 @@ export function throwIfCancellationRequested(token: CancellationToken) {
}
}

function throwAfter(millisecondsDueTime: number) : IAsync<void> {
function throwAfter(millisecondsDueTime: number): Async<void> {
return protectedCont((ctx: IAsyncContext<void>) => {
let tokenId: number;
const timeoutId = setTimeout(() => {
Expand All @@ -75,7 +71,7 @@ function throwAfter(millisecondsDueTime: number) : IAsync<void> {
});
}

export function startChild<T>(computation: IAsync<T>, ms?: number): IAsync<IAsync<T>> {
export function startChild<T>(computation: Async<T>, ms?: number): Async<Async<T>> {
if (ms) {
const computationWithTimeout = protectedBind(
parallel2(
Expand Down Expand Up @@ -107,7 +103,7 @@ export function cancellationToken() {

export const defaultCancellationToken = new CancellationToken();

export function catchAsync<T>(work: IAsync<T>) {
export function catchAsync<T>(work: Async<T>) {
return protectedCont((ctx: IAsyncContext<FSharpChoice$2_$union<T, Error>>) => {
work({
onSuccess: (x) => ctx.onSuccess(Choice_makeChoice1Of2(x)),
Expand All @@ -124,21 +120,21 @@ export function fromContinuations<T>(f: (conts: Continuations<T>) => void) {
f([ctx.onSuccess, ctx.onError, ctx.onCancel]));
}

export function ignore<T>(computation: IAsync<T>) {
export function ignore<T>(computation: Async<T>) {
return protectedBind(computation, (_x) => protectedReturn(void 0));
}

export function parallel<T>(computations: Iterable<IAsync<T>>) {
export function parallel<T>(computations: Iterable<Async<T>>) {
return delay(() => awaitPromise(Promise.all(Array.from(computations, (w) => startAsPromise(w)))));
}

function parallel2<T, U>(a: IAsync<T>, b: IAsync<U>): IAsync<[T, U]> {
return delay(() => awaitPromise(Promise.all([ startAsPromise(a), startAsPromise(b) ])));
function parallel2<T, U>(a: Async<T>, b: Async<U>): Async<[T, U]> {
return delay(() => awaitPromise(Promise.all([startAsPromise(a), startAsPromise(b)])));
}

export function sequential<T>(computations: Iterable<IAsync<T>>) {
export function sequential<T>(computations: Iterable<Async<T>>) {

function _sequential<T>(computations: Iterable<IAsync<T>>): Promise<T[]> {
function _sequential<T>(computations: Iterable<Async<T>>): Promise<T[]> {
let pr: Promise<T[]> = Promise.resolve([]);
for (const c of computations) {
pr = pr.then(results => startAsPromise(c).then(r => results.concat([r])))
Expand Down Expand Up @@ -167,16 +163,16 @@ export function runSynchronously(): never {
throw new Error("Asynchronous code cannot be run synchronously in JS");
}

export function start<T>(computation: IAsync<T>, cancellationToken?: CancellationToken) {
export function start<T>(computation: Async<T>, cancellationToken?: CancellationToken) {
return startWithContinuations(computation, cancellationToken);
}

export function startImmediate<T>(computation: IAsync<T>, cancellationToken?: CancellationToken) {
export function startImmediate<T>(computation: Async<T>, cancellationToken?: CancellationToken) {
return start(computation, cancellationToken);
}

export function startWithContinuations<T>(
computation: IAsync<T>,
computation: Async<T>,
continuation?: Continuation<T> | CancellationToken,
exceptionContinuation?: Continuation<any>,
cancellationContinuation?: Continuation<any>,
Expand All @@ -195,7 +191,7 @@ export function startWithContinuations<T>(
});
}

export function startAsPromise<T>(computation: IAsync<T>, cancellationToken?: CancellationToken) {
export function startAsPromise<T>(computation: Async<T>, cancellationToken?: CancellationToken) {
return new Promise((resolve: Continuation<T>, reject: Continuation<any>) =>
startWithContinuations(computation, resolve, reject, reject,
cancellationToken ? cancellationToken : defaultCancellationToken));
Expand Down
24 changes: 12 additions & 12 deletions src/fable-library-ts/AsyncBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ export interface IAsyncContext<T> {
trampoline: Trampoline;
}

export type IAsync<T> = (x: IAsyncContext<T>) => void;
export type Async<T> = (x: IAsyncContext<T>) => void;

export function protectedCont<T>(f: IAsync<T>) {
export function protectedCont<T>(f: Async<T>) {
return (ctx: IAsyncContext<T>) => {
if (ctx.cancelToken.isCancelled) {
ctx.onCancel(new OperationCanceledError());
Expand All @@ -110,7 +110,7 @@ export function protectedCont<T>(f: IAsync<T>) {
};
}

export function protectedBind<T, U>(computation: IAsync<T>, binder: (x: T) => IAsync<U>) {
export function protectedBind<T, U>(computation: Async<T>, binder: (x: T) => Async<U>) {
return protectedCont((ctx: IAsyncContext<U>) => {
computation({
onSuccess: (x: T) => {
Expand All @@ -133,19 +133,19 @@ export function protectedReturn<T>(value: T) {
}

export class AsyncBuilder {
public Bind<T, U>(computation: IAsync<T>, binder: (x: T) => IAsync<U>) {
public Bind<T, U>(computation: Async<T>, binder: (x: T) => Async<U>) {
return protectedBind(computation, binder);
}

public Combine<T>(computation1: IAsync<void>, computation2: IAsync<T>) {
public Combine<T>(computation1: Async<void>, computation2: Async<T>) {
return this.Bind(computation1, () => computation2);
}

public Delay<T>(generator: () => IAsync<T>) {
public Delay<T>(generator: () => Async<T>) {
return protectedCont((ctx: IAsyncContext<T>) => generator()(ctx));
}

public For<T>(sequence: Iterable<T>, body: (x: T) => IAsync<void>) {
public For<T>(sequence: Iterable<T>, body: (x: T) => Async<void>) {
const iter = sequence[Symbol.iterator]();
let cur = iter.next();
return this.While(() => !cur.done, this.Delay(() => {
Expand All @@ -159,11 +159,11 @@ export class AsyncBuilder {
return protectedReturn(value);
}

public ReturnFrom<T>(computation: IAsync<T>) {
public ReturnFrom<T>(computation: Async<T>) {
return computation;
}

public TryFinally<T>(computation: IAsync<T>, compensation: () => void) {
public TryFinally<T>(computation: Async<T>, compensation: () => void) {
return protectedCont((ctx: IAsyncContext<T>) => {
computation({
onSuccess: (x: T) => {
Expand All @@ -184,7 +184,7 @@ export class AsyncBuilder {
});
}

public TryWith<T>(computation: IAsync<T>, catchHandler: (e: any) => IAsync<T>) {
public TryWith<T>(computation: Async<T>, catchHandler: (e: any) => Async<T>) {
return protectedCont((ctx: IAsyncContext<T>) => {
computation({
onSuccess: ctx.onSuccess,
Expand All @@ -202,11 +202,11 @@ export class AsyncBuilder {
});
}

public Using<T extends IDisposable, U>(resource: T, binder: (x: T) => IAsync<U>) {
public Using<T extends IDisposable, U>(resource: T, binder: (x: T) => Async<U>) {
return this.TryFinally(binder(resource), () => resource.Dispose());
}

public While(guard: () => boolean, computation: IAsync<void>): IAsync<void> {
public While(guard: () => boolean, computation: Async<void>): Async<void> {
if (guard()) {
return this.Bind(computation, () => this.While(guard, computation));
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/fable-library-ts/MailboxProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defaultCancellationToken } from "./Async.js";
import { fromContinuations } from "./Async.js";
import { startImmediate } from "./Async.js";
import { IAsync } from "./AsyncBuilder.js";
import { Async } from "./AsyncBuilder.js";
import { Continuation, Continuations } from "./AsyncBuilder.js";
import { CancellationToken } from "./AsyncBuilder.js";

Expand Down Expand Up @@ -41,7 +41,7 @@ class MailboxQueue<Msg> {
}
}

export type MailboxBody<Msg> = (m: MailboxProcessor<Msg>) => IAsync<void>;
export type MailboxBody<Msg> = (m: MailboxProcessor<Msg>) => Async<void>;

export interface AsyncReplyChannel<Reply> {
reply: (r: Reply) => void;
Expand Down

0 comments on commit a84cdd8

Please sign in to comment.