From 6946665dc9beb70818fcda05f72800c69de3016b Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 16 May 2019 13:17:11 -0400 Subject: [PATCH] Add util/async.ts --- util/async.ts | 60 ++++++++++++++++++++++++++++++++++++++++++++++ util/async_test.ts | 27 +++++++++++++++++++++ util/test.ts | 1 + 3 files changed, 88 insertions(+) create mode 100644 util/async.ts create mode 100644 util/async_test.ts diff --git a/util/async.ts b/util/async.ts new file mode 100644 index 0000000000000..a4a38f43619af --- /dev/null +++ b/util/async.ts @@ -0,0 +1,60 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import { assert } from "../testing/asserts.ts"; + +// TODO(ry) It'd be better to make Deferred a class that inherits from +// Promise, rather than an interface. This is possible in ES2016, however +// typescript produces broken code when targeting ES5 code. +// See https://github.com/Microsoft/TypeScript/issues/15202 +// At the time of writing, the github issue is closed but the problem remains. +export interface Deferred extends Promise { + resolve: (value?: T | PromiseLike) => void; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + reject: (reason?: any) => void; +} + +/** Creates a Promise with the `reject` and `resolve` functions + * placed as methods on the promise object itself. It allows you to do: + * + * const p = deferred(); + * // ... + * p.resolve(42); + */ +export function deferred(): Deferred { + let methods; + const promise = new Promise( + (resolve, reject): void => { + methods = { resolve, reject }; + } + ); + return Object.assign(promise, methods) as Deferred; +} + +export class Latch { + // TODO(ry) Can this be done without using Arrays? + + // Array of `[resolve_function, value]` tuples. + private sendQueue: Array<[() => void, T]> = []; + // Array of `resolve_function` values. + private recvQueue: Array<(value: T) => void> = []; + + send(value: T): Promise { + const recvResolve = this.recvQueue.shift(); + if (recvResolve) { + recvResolve(value); + return Promise.resolve(); + } else { + return new Promise((resolve, _) => this.sendQueue.push([resolve, value])); + } + } + + recv(): Promise { + const s = this.sendQueue.shift(); + if (s) { + const [sendResolve, value] = s; + sendResolve(); + return Promise.resolve(value); + } else { + return new Promise((res, _) => this.recvQueue.push(res)); + } + } +} diff --git a/util/async_test.ts b/util/async_test.ts new file mode 100644 index 0000000000000..2da72075b0c9a --- /dev/null +++ b/util/async_test.ts @@ -0,0 +1,27 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import { test, runIfMain } from "../testing/mod.ts"; +import { assertEquals } from "../testing/asserts.ts"; +import { Latch, deferred } from "./async.ts"; + +test(async function asyncDeferred(): Promise { + const d = deferred(); + d.resolve(12); +}); + +async function send3(latch: Latch): Promise { + await latch.send(1); + await latch.send(2); + await latch.send(3); +} + +test(async function asyncLatch(): Promise { + const latch = new Latch(); + send3(latch); + + assertEquals(1, await latch.recv()); + assertEquals(2, await latch.recv()); + assertEquals(3, await latch.recv()); + let _lastPromise = latch.recv(); +}); + +runIfMain(import.meta); diff --git a/util/test.ts b/util/test.ts index a617c10ab36d6..ede984904f0d9 100644 --- a/util/test.ts +++ b/util/test.ts @@ -1 +1,2 @@ +import "./async_test.ts"; import "./deep_assign_test.ts";