Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allows WorkerLike #31

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ for await (const data of reader) {
#### Worker

```typescript
/// <reference no-default-lib="true" />
/// <reference lib="deno.worker" />

import {
readableStreamFromWorker,
writableStreamFromWorker,
Expand All @@ -55,9 +58,8 @@ const decoder = new TextDecoder();
const encoder = new TextEncoder();

async function main(): Promise<void> {
const worker = self as unknown as Worker;
const reader = readableStreamFromWorker(worker);
const writer = writableStreamFromWorker(worker);
const reader = readableStreamFromWorker(self);
const writer = writableStreamFromWorker(self);
const w = writer.getWriter();

for await (const data of reader) {
Expand Down
3 changes: 2 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"exports": {
".": "./mod.ts",
"./readable_stream": "./readable_stream.ts",
"./writable_stream": "./writable_stream.ts"
"./writable_stream": "./writable_stream.ts",
"./types": "./types.ts"
},
"publish": {
"exclude": [
Expand Down
9 changes: 6 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
* #### Worker
*
* ```typescript
* /// <reference no-default-lib="true" />
* /// <reference lib="deno.worker" />
lambdalisue marked this conversation as resolved.
Show resolved Hide resolved
*
* import {
* readableStreamFromWorker,
* writableStreamFromWorker,
Expand All @@ -50,9 +53,8 @@
* const encoder = new TextEncoder();
*
* async function main(): Promise<void> {
* const worker = self as unknown as Worker;
* const reader = readableStreamFromWorker(worker);
* const writer = writableStreamFromWorker(worker);
* const reader = readableStreamFromWorker(self);
* const writer = writableStreamFromWorker(self);
* const w = writer.getWriter();
*
* for await (const data of reader) {
Expand All @@ -69,3 +71,4 @@
*/
export * from "./readable_stream.ts";
export * from "./writable_stream.ts";
export type * from "./types.ts";
4 changes: 3 additions & 1 deletion readable_stream.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { WorkerLike } from "./types.ts";

/**
* Options for creating a readable stream from a worker.
*/
Expand All @@ -16,7 +18,7 @@ export type ReadableStreamFromWorkerOptions = {
* @returns A readable stream that can be used to read the data.
*/
export function readableStreamFromWorker(
worker: Worker,
worker: WorkerLike,
options: ReadableStreamFromWorkerOptions = {},
): ReadableStream<Uint8Array> {
const {
Expand Down
8 changes: 5 additions & 3 deletions test_echo_server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const worker = self as unknown as Worker;
worker.onmessage = (ev) => {
worker.postMessage(ev.data);
/// <reference no-default-lib="true" />
/// <reference lib="deno.worker" />

self.onmessage = (ev) => {
self.postMessage(ev.data);
};
7 changes: 7 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** Worker like object. */
export type WorkerLike = {
// deno-lint-ignore no-explicit-any
onmessage: ((ev: MessageEvent) => any) | null;
Milly marked this conversation as resolved.
Show resolved Hide resolved
// deno-lint-ignore no-explicit-any
postMessage(message: any, transfer?: Transferable[]): void;
lambdalisue marked this conversation as resolved.
Show resolved Hide resolved
};
4 changes: 3 additions & 1 deletion writable_stream.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { WorkerLike } from "./types.ts";

/**
* Options for creating a writable stream from a worker.
*/
Expand All @@ -15,7 +17,7 @@ export type WritableStreamFromWorkerOptions = {
* @returns A new `WritableStream` instance.
*/
export function writableStreamFromWorker(
worker: Worker,
worker: WorkerLike,
options: WritableStreamFromWorkerOptions = {},
): WritableStream<Uint8Array> {
const {
Expand Down