From 95af1c6862218694d81e2fbcc7fda62db9dc17f2 Mon Sep 17 00:00:00 2001 From: Felicitas Pojtinger Date: Thu, 22 Feb 2024 23:00:33 +0100 Subject: [PATCH] feat: Add closure example to coffee server and client CLI in TypeScript --- ...rpc-example-websocket-coffee-client-cli.ts | 16 +++++++--- ...rpc-example-websocket-coffee-server-cli.ts | 31 +++++++++++++++++-- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/ts/bin/panrpc-example-websocket-coffee-client-cli.ts b/ts/bin/panrpc-example-websocket-coffee-client-cli.ts index 14ad6e0..07b2bc0 100644 --- a/ts/bin/panrpc-example-websocket-coffee-client-cli.ts +++ b/ts/bin/panrpc-example-websocket-coffee-client-cli.ts @@ -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 ): Promise { return 0; } @@ -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; @@ -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; diff --git a/ts/bin/panrpc-example-websocket-coffee-server-cli.ts b/ts/bin/panrpc-example-websocket-coffee-server-cli.ts index 04d1254..81fea1a 100644 --- a/ts/bin/panrpc-example-websocket-coffee-server-cli.ts +++ b/ts/bin/panrpc-example-websocket-coffee-server-cli.ts @@ -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) { @@ -13,7 +18,9 @@ class CoffeeMachine { async BrewCoffee( ctx: ILocalContext, variant: string, - size: number + size: number, + @remoteClosure + onProgress: (ctx: IRemoteContext, percentage: number) => Promise ): Promise { if (!this.supportedVariants.includes(variant)) { throw new Error("unsupported variant"); @@ -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;