-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriterQueue.ts
42 lines (42 loc) · 1.05 KB
/
WriterQueue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Copyright (c) 2018-present, heineiuo.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { WriteBatch } from "./WriteBatch.ts";
export class Writer {
batch!: WriteBatch | void;
sync = false;
done = false;
resolve!: () => void;
signal(): void {
if (this.resolve)
this.resolve();
delete this.resolve;
}
wait(): Promise<void> {
delete this.resolve;
return new Promise((resolve) => (this.resolve = resolve));
}
}
export class WriterQueue {
private list: Writer[] = [];
public push(writer: Writer): void {
this.list.push(writer);
}
public front(): Writer | void {
return this.list[0];
}
public popFront(): void {
this.list.shift();
}
get length(): number {
return this.list.length;
}
*iterator(start = 0): IterableIterator<Writer> {
for (let i = start; i < this.list.length; i++) {
yield this.list[i];
}
}
}