Skip to content

Commit

Permalink
feat: Broadcast coffee machine status to subscribed clients for TypeS…
Browse files Browse the repository at this point in the history
…cript version
  • Loading branch information
pojntfx committed Feb 23, 2024
1 parent 73c8565 commit 86027d6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 32 deletions.
1 change: 1 addition & 0 deletions go/cmd/panrpc-example-websocket-coffee-client-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func main() {
defer cancel()

clients := 0

registry := rpc.NewRegistry[coffeeMachine, json.RawMessage](
&remoteControl{},

Expand Down
2 changes: 1 addition & 1 deletion ts/bin/panrpc-example-callbacks-callee-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Local {
async Increment(ctx: ILocalContext, delta: number): Promise<number> {
const { remoteID: targetID } = ctx;

await registry.forRemotes(async (remoteID, remote) => {
await this.forRemotes?.(async (remoteID, remote) => {
if (remoteID === targetID) {
await remote.Println(undefined, `Incrementing counter by ${delta}`);
}
Expand Down
90 changes: 59 additions & 31 deletions ts/bin/panrpc-example-websocket-coffee-server-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import {
} from "../index";

class CoffeeMachine {
public forRemotes?: (
cb: (remoteID: string, remote: RemoteControl) => Promise<void>
) => Promise<void>;

constructor(private supportedVariants: string[], private waterLevel: number) {
this.BrewCoffee = this.BrewCoffee.bind(this);
}
Expand All @@ -22,38 +26,58 @@ class CoffeeMachine {
@remoteClosure
onProgress: (ctx: IRemoteContext, percentage: number) => Promise<void>
): Promise<number> {
if (!this.supportedVariants.includes(variant)) {
throw new Error("unsupported variant");
}

if (this.waterLevel - size < 0) {
throw new Error("not enough water");
const { remoteID: targetID } = ctx;

try {
await this.forRemotes?.(async (remoteID, remote) => {
if (remoteID === targetID) {
return;
}

await remote.SetCoffeeMachineBrewing(undefined, true);
});

if (!this.supportedVariants.includes(variant)) {
throw new Error("unsupported variant");
}

if (this.waterLevel - size < 0) {
throw new Error("not enough water");
}

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, 500);
});
await onProgress(undefined, 100);
} finally {
await this.forRemotes?.(async (remoteID, remote) => {
if (remoteID === targetID) {
return;
}

await remote.SetCoffeeMachineBrewing(undefined, false);
});
}

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, 500);
});
await onProgress(undefined, 100);

this.waterLevel -= size;

return this.waterLevel;
Expand All @@ -65,10 +89,12 @@ class RemoteControl {
async SetCoffeeMachineBrewing(ctx: IRemoteContext, brewing: boolean) {}
}

const service = new CoffeeMachine(["latte", "americano"], 1000);

let clients = 0;

const registry = new Registry(
new CoffeeMachine(["latte", "americano"], 1000),
service,
new RemoteControl(),

{
Expand All @@ -85,6 +111,8 @@ const registry = new Registry(
}
);

service.forRemotes = registry.forRemotes;

const server = new WebSocketServer({
host: "127.0.0.1",
port: 1337,
Expand Down

0 comments on commit 86027d6

Please sign in to comment.