Skip to content

Commit

Permalink
feat: Port rest of panrpc examples to context/signal-aware API in Typ…
Browse files Browse the repository at this point in the history
…eScript version

Signed-off-by: Felicitas Pojtinger <felicitas@pojtinger.com>
  • Loading branch information
pojntfx committed Aug 15, 2024
1 parent 767017b commit 526cb38
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ts/bin/panrpc-example-websocket-client-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ if (listen) {
console.error("Client disconnected with error:", e);
});

const linkSignal = new AbortController();

const encoder = new WritableStream({
write(chunk) {
socket.send(JSON.stringify(chunk));
Expand Down Expand Up @@ -149,9 +151,12 @@ if (listen) {
socket.addEventListener("close", () => {
parserReader.cancel();
parserWriter.abort();
linkSignal.abort();
});

registry.linkStream(
linkSignal.signal,

encoder,
decoder,

Expand All @@ -176,6 +181,8 @@ if (listen) {
socket.addEventListener("error", rej);
});

const linkSignal = new AbortController();

const encoder = new WritableStream({
write(chunk) {
socket.send(JSON.stringify(chunk));
Expand Down Expand Up @@ -215,9 +222,12 @@ if (listen) {
socket.addEventListener("close", () => {
parserReader.cancel();
parserWriter.abort();
linkSignal.abort();
});

registry.linkStream(
linkSignal.signal,

encoder,
decoder,

Expand Down
5 changes: 5 additions & 0 deletions ts/bin/panrpc-example-websocket-client-web/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export default function Home() {
socket.addEventListener("error", rej);
});

const linkSignal = new AbortController();

const encoder = new WritableStream({
write(chunk) {
socket.send(JSON.stringify(chunk));
Expand Down Expand Up @@ -91,9 +93,12 @@ export default function Home() {
socket.addEventListener("close", () => {
parserReader.cancel();
parserWriter.abort();
linkSignal.abort();
});

registry.linkStream(
linkSignal.signal,

encoder,
decoder,

Expand Down
Binary file modified ts/bin/panrpc-example-websocket-client-web/bun.lockb
Binary file not shown.
5 changes: 5 additions & 0 deletions ts/bin/panrpc-example-websocket-coffee-client-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ await new Promise<void>((res, rej) => {
socket.addEventListener("error", rej);
});

const linkSignal = new AbortController();

// Set up streaming JSON encoder
const encoder = new WritableStream({
write(chunk) {
Expand Down Expand Up @@ -173,9 +175,12 @@ socket.addEventListener("message", (m) => parserWriter.write(m.data as string));
socket.addEventListener("close", () => {
parserReader.cancel();
parserWriter.abort();
linkSignal.abort();
});

registry.linkStream(
linkSignal.signal,

encoder,
decoder,

Expand Down
5 changes: 5 additions & 0 deletions ts/bin/panrpc-example-websocket-coffee-server-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ server.on("connection", (socket) => {
console.error("Remote control disconnected with error:", e);
});

const linkSignal = new AbortController();

// Set up streaming JSON encoder
const encoder = new WritableStream({
write(chunk) {
Expand Down Expand Up @@ -165,9 +167,12 @@ server.on("connection", (socket) => {
socket.addEventListener("close", () => {
parserReader.cancel();
parserWriter.abort();
linkSignal.abort();
});

registry.linkStream(
linkSignal.signal,

encoder,
decoder,

Expand Down
10 changes: 10 additions & 0 deletions ts/bin/panrpc-example-websocket-server-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ if (listen) {
console.error("Client disconnected with error:", e);
});

const linkSignal = new AbortController();

const encoder = new WritableStream({
write(chunk) {
socket.send(JSON.stringify(chunk));
Expand Down Expand Up @@ -145,9 +147,12 @@ if (listen) {
socket.addEventListener("close", () => {
parserReader.cancel();
parserWriter.abort();
linkSignal.abort();
});

registry.linkStream(
linkSignal.signal,

encoder,
decoder,

Expand All @@ -172,6 +177,8 @@ if (listen) {
socket.addEventListener("error", rej);
});

const linkSignal = new AbortController();

const encoder = new WritableStream({
write(chunk) {
socket.send(JSON.stringify(chunk));
Expand Down Expand Up @@ -211,9 +218,12 @@ if (listen) {
socket.addEventListener("close", () => {
parserReader.cancel();
parserWriter.abort();
linkSignal.abort();
});

registry.linkStream(
linkSignal.signal,

encoder,
decoder,

Expand Down

0 comments on commit 526cb38

Please sign in to comment.