-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.ts
39 lines (36 loc) 路 1.16 KB
/
writer.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
/**
* A `WorkerWriter` is a `Deno.Writer` that defines a data channel of a `Worker` and writes data to it.
*
* It notifies the peer that the data channel has finished writing, by sending a `null` message.
*/
export class WorkerWriter implements Deno.Writer, Deno.Closer {
#worker: Worker;
/**
* Constructs a new `WorkerWriter` with the given `Worker`.
*
* @param worker The `Worker` to write to.
*/
constructor(worker: Worker) {
this.#worker = worker;
}
/**
* Writes `p.byteLength` bytes of data to the `Worker` using `postMessage`.
*
* Note that this method does NOT guarantee that the data has been posted to the peer.
* Please refer to the following link for more information:
* https://github.com/lambdalisue/deno-workerio/issues/5
*
* @param p The buffer to write.
* @returns A promise resolving to the number of bytes written.
*/
write(p: Uint8Array): Promise<number> {
this.#worker.postMessage(p, [p.buffer]);
return Promise.resolve(p.length);
}
/**
* Notify the peer that the data channel has finished writing, by sending a `null` message.
*/
close(): void {
this.#worker.postMessage(null);
}
}