Skip to content

Commit

Permalink
feat: Add closure example to coffee server and client CLI in TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
pojntfx committed Feb 22, 2024
1 parent b2dec17 commit 95af1c6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
16 changes: 11 additions & 5 deletions ts/bin/panrpc-example-websocket-coffee-client-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class CoffeeMachine {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
variant: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
size: number
size: number,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onProgress: (ctx: ILocalContext, percentage: number) => Promise<void>
): Promise<number> {
return 0;
}
Expand Down Expand Up @@ -79,12 +81,14 @@ const registry = new Registry(
const res = await remote.BrewCoffee(
undefined,
"latte",
line === "1" ? 100 : 200
line === "1" ? 100 : 200,
async (ctx, percentage) =>
console.log(`Brewing Cafè Latte ... ${percentage}% done`)
);

console.log("Remaining water:", res, "ml");
} catch (e) {
console.error(`Couldn't brew coffee: ${e}`);
console.error(`Couldn't brew Cafè Latte: ${e}`);
}

break;
Expand All @@ -96,12 +100,14 @@ const registry = new Registry(
const res = await remote.BrewCoffee(
undefined,
"americano",
line === "3" ? 100 : 200
line === "3" ? 100 : 200,
async (ctx, percentage) =>
console.log(`Brewing Americano ... ${percentage}% done`)
);

console.log("Remaining water:", res, "ml");
} catch (e) {
console.error(`Couldn't brew coffee: ${e}`);
console.error(`Couldn't brew Americano: ${e}`);
}

break;
Expand Down
31 changes: 28 additions & 3 deletions ts/bin/panrpc-example-websocket-coffee-server-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import { JSONParser } from "@streamparser/json-whatwg";
// eslint-disable-next-line import/no-extraneous-dependencies
import { WebSocketServer } from "ws";
import { ILocalContext, IRemoteContext, Registry } from "../index";
import {
ILocalContext,
IRemoteContext,
Registry,
remoteClosure,
} from "../index";

class CoffeeMachine {
constructor(private supportedVariants: string[], private waterLevel: number) {
Expand All @@ -13,7 +18,9 @@ class CoffeeMachine {
async BrewCoffee(
ctx: ILocalContext,
variant: string,
size: number
size: number,
@remoteClosure
onProgress: (ctx: IRemoteContext, percentage: number) => Promise<void>
): Promise<number> {
if (!this.supportedVariants.includes(variant)) {
throw new Error("unsupported variant");
Expand All @@ -25,9 +32,27 @@ class CoffeeMachine {

console.log("Brewing coffee variant", variant, "in size", size, "ml");

await onProgress(undefined, 0);

await new Promise((r) => {
setTimeout(r, 500);
});
await onProgress(undefined, 25);

await new Promise((r) => {
setTimeout(r, 500);
});
await onProgress(undefined, 50);

await new Promise((r) => {
setTimeout(r, 500);
});
await onProgress(undefined, 75);

await new Promise((r) => {
setTimeout(r, 5000);
setTimeout(r, 500);
});
await onProgress(undefined, 100);

this.waterLevel -= size;

Expand Down

0 comments on commit 95af1c6

Please sign in to comment.