Skip to content

Commit

Permalink
Rename Latch to Channel
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed May 17, 2019
1 parent 1ab72b3 commit 79065b4
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 25 deletions.
8 changes: 4 additions & 4 deletions http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BufReader, BufState, BufWriter } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
import { STATUS_TEXT } from "./http_status.ts";
import { assert } from "../testing/asserts.ts";
import { Latch, deferred, Deferred } from "../util/async.ts";
import { Channel, deferred, Deferred } from "../util/async.ts";

interface HttpConn extends Conn {
// When read by a newly created request B, lastId is the id pointing to a previous
Expand Down Expand Up @@ -292,7 +292,7 @@ async function readRequest(
export class Server implements AsyncIterableIterator<ServerRequest> {
private closing = false;
private looping = false;
private latch = new Latch<ServerRequest>();
private channel = new Channel<ServerRequest>();

constructor(public listener: Listener) {}

Expand Down Expand Up @@ -326,12 +326,12 @@ export class Server implements AsyncIterableIterator<ServerRequest> {
break;
}

await this.latch.send(req);
await this.channel.send(req);
}
}

async next(): Promise<IteratorResult<ServerRequest>> {
const req = await this.latch.recv();
const req = await this.channel.recv();
return { done: false, value: req };
}

Expand Down
4 changes: 2 additions & 2 deletions prettier/testdata/opts/0.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
console.log(0)
console.log([function foo() {}, function baz() {}, a => {}])
console.log(0);
console.log([function foo() {}, function baz() {}, (a) => {}]);
2 changes: 1 addition & 1 deletion prettier/testdata/opts/1.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log ("1")
console.log('1');
2 changes: 1 addition & 1 deletion prettier/testdata/opts/2.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log({a:1})
console.log({a: 1});
8 changes: 3 additions & 5 deletions util/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ export function deferred<T>(): Deferred<T> {
return Object.assign(promise, methods) as Deferred<T>;
}

export class Latch<T> {
// TODO(ry) Can this be done without using Arrays?

// Array of `[resolve_function, value]` tuples.
/** Sends objects between asynchronous tasks, with backpressure. */
export class Channel<T> {
// TODO(ry) Can Channel be implemented without using Arrays?
private sendQueue: Array<[() => void, T]> = [];
// Array of `resolve_function` values.
private recvQueue: Array<(value: T) => void> = [];

send(value: T): Promise<void> {
Expand Down
24 changes: 12 additions & 12 deletions util/async_test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +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";
import { Channel, deferred } from "./async.ts";

test(async function asyncDeferred(): Promise<void> {
const d = deferred<number>();
d.resolve(12);
});

async function send3(latch: Latch<number>): Promise<void> {
await latch.send(1);
await latch.send(2);
await latch.send(3);
async function send3(channel: Channel<number>): Promise<void> {
await channel.send(1);
await channel.send(2);
await channel.send(3);
}

test(async function asyncLatch(): Promise<void> {
const latch = new Latch<number>();
send3(latch);
test(async function asyncChannel(): Promise<void> {
const channel = new Channel<number>();
send3(channel);

assertEquals(1, await latch.recv());
assertEquals(2, await latch.recv());
assertEquals(3, await latch.recv());
let _lastPromise = latch.recv();
assertEquals(1, await channel.recv());
assertEquals(2, await channel.recv());
assertEquals(3, await channel.recv());
let _lastPromise = channel.recv();
});

runIfMain(import.meta);

0 comments on commit 79065b4

Please sign in to comment.