-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support async/Promise-returning functions
- Loading branch information
Showing
4 changed files
with
63 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,58 @@ | ||
import { ArgumentTypes } from './types'; | ||
import { | ||
Thunk, | ||
UnwrapThunk, | ||
UnwrapThunkDeep, | ||
isThunk, | ||
toThunk, | ||
ThunkOrValue, | ||
} from './thunk'; | ||
|
||
export type Cont<A extends any[], R> = (...args: A) => Thunk<UnwrapThunk<R>>; | ||
export type UnwrapPromise<T> = T extends Promise<infer U> ? Exclude<U, Promise<T>> : T; | ||
|
||
export type Unbox<T> = UnwrapThunkDeep<UnwrapPromise<T>>; | ||
|
||
export type Cont<A extends any[], R> = (...args: A) => Thunk<Unbox<R>>; | ||
|
||
export interface Trampoline<F extends ((...args: any[]) => any)> { | ||
(...args: ArgumentTypes<F>): UnwrapThunk<ReturnType<F>>; | ||
(...args: ArgumentTypes<F>): Unbox<ReturnType<F>>; | ||
cont: Cont<ArgumentTypes<F>, ReturnType<F>>; | ||
} | ||
|
||
export interface TrampolineAsync<F extends ((...args: any[]) => any)> { | ||
(...args: ArgumentTypes<F>): Promise<Unbox<ReturnType<F>>>; | ||
cont: Cont<ArgumentTypes<F>, ReturnType<F>>; | ||
} | ||
|
||
export const trampoline = <F extends ((...args: any[]) => any)>(fn: F): Trampoline<F> => { | ||
const trampolineFunction = (...args: ArgumentTypes<F>): UnwrapThunk<ReturnType<F>> => { | ||
let result: ThunkOrValue<ReturnType<F>> = fn(...args); | ||
const cont = (...args: ArgumentTypes<F>) => toThunk(() => fn(...args)); | ||
|
||
return Object.assign( | ||
(...args: ArgumentTypes<F>): Unbox<ReturnType<F>> => { | ||
let result: ThunkOrValue<ReturnType<F>> = fn(...args); | ||
|
||
while (isThunk<ReturnType<F>>(result)) { | ||
result = result(); | ||
} | ||
|
||
return result; | ||
}, | ||
{ cont }, | ||
); | ||
}; | ||
|
||
while (isThunk<ReturnType<F>>(result)) { | ||
result = result(); | ||
} | ||
export const trampolineAsync = <F extends ((...args: any[]) => any)>(fn: F): TrampolineAsync<F> => { | ||
const cont = (...args: ArgumentTypes<F>) => toThunk(() => fn(...args)); | ||
|
||
return result; | ||
}; | ||
return Object.assign( | ||
async (...args: ArgumentTypes<F>): Promise<Unbox<ReturnType<F>>> => { | ||
let result: ThunkOrValue<ReturnType<F>> = await fn(...args); | ||
|
||
trampolineFunction.cont = (...args: ArgumentTypes<F>) => toThunk(() => fn(...args)); | ||
while (isThunk<ReturnType<F>>(result)) { | ||
result = await result(); | ||
} | ||
|
||
return trampolineFunction; | ||
return result; | ||
}, | ||
{ cont }, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters