Skip to content

Commit

Permalink
docs: Add notes on using private methods in services, clarify protoco…
Browse files Browse the repository at this point in the history
…l for remaining examples

Signed-off-by: Felicitas Pojtinger <felicitas@pojtinger.com>
  • Loading branch information
pojntfx committed Sep 19, 2024
1 parent 0cc0224 commit 567f63b
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 40 deletions.
38 changes: 22 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (s *coffeeMachine) BrewCoffee(
> - Methods must have `context.Context` as their first argument
> - Methods can not have variadic arguments
> - Methods must return either an error or a single value and an error
> - Methods must be public (private methods won't be callable as RPCs, but stay callable as regular methods)
To start turning the `BrewCoffee` method into an RPC, create an instance of the struct and pass it to a [panrpc Registry](https://pkg.go.dev/github.com/pojntfx/panrpc/go/pkg/rpc#Registry) like so:

Expand Down Expand Up @@ -995,7 +996,11 @@ To start with implementing the coffee machine server, create a new file `coffee-
import { ILocalContext } from "@pojntfx/panrpc";

class CoffeeMachine {
constructor(private supportedVariants: string[], private waterLevel: number) {
#waterLevel: number;

constructor(private supportedVariants: string[], waterLevel: number) {
this.#waterLevel = waterLevel;

this.BrewCoffee = this.BrewCoffee.bind(this);
}

Expand All @@ -1008,7 +1013,7 @@ class CoffeeMachine {
throw new Error("unsupported variant");
}

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

Expand All @@ -1018,9 +1023,9 @@ class CoffeeMachine {
setTimeout(r, 5000);
});

this.waterLevel -= size;
this.#waterLevel -= size;

return this.waterLevel;
return this.#waterLevel;
}
}
```
Expand All @@ -1029,6 +1034,7 @@ class CoffeeMachine {
>
> - Methods must have `ILocalContext` as their first argument
> - Methods can not have variadic arguments
> - Methods must be public (private methods - those that start with `#`, see [private properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_properties) - won't be callable as RPCs, but stay callable as regular methods)
To start turning the `BrewCoffee` method into an RPC, create an instance of the class and pass it to a [panrpc Registry](https://pojntfx.github.io/panrpc/classes/Registry.html) like so:
Expand Down Expand Up @@ -1543,7 +1549,7 @@ class CoffeeMachine {
throw new Error("unsupported variant");
}

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

Expand All @@ -1564,9 +1570,9 @@ class CoffeeMachine {
});
}

this.waterLevel -= size;
this.#waterLevel -= size;

return this.waterLevel;
return this.#waterLevel;
}
}
```
Expand Down Expand Up @@ -1679,7 +1685,7 @@ class CoffeeMachine {

// ..

return this.waterLevel;
return this.#waterLevel;
}
}
```
Expand Down Expand Up @@ -1817,15 +1823,15 @@ To make getting started with panrpc easier, take a look at the following example
- [<img alt="typescript" src="https://cdn.simpleicons.org/typescript" style="vertical-align: middle;" height="20" width="20" /> Valkey/Redis Server CLI Example](./ts/bin/panrpc-example-valkey-server-cli.ts)
- [<img alt="typescript" src="https://cdn.simpleicons.org/typescript" style="vertical-align: middle;" height="20" width="20" /> Valkey/Redis Client CLI Example](./ts/bin/panrpc-example-valkey-client-cli.ts)
- **Callbacks**
- [<img alt="Go" src="https://cdn.simpleicons.org/go" style="vertical-align: middle;" height="20" width="20" /> Callbacks Demo Server CLI Example](./go/cmd/panrpc-example-callbacks-callee-cli/main.go)
- [<img alt="Go" src="https://cdn.simpleicons.org/go" style="vertical-align: middle;" height="20" width="20" /> Callbacks Demo Client CLI Example](./go/cmd/panrpc-example-callbacks-caller-cli/main.go)
- [<img alt="typescript" src="https://cdn.simpleicons.org/typescript" style="vertical-align: middle;" height="20" width="20" /> Callbacks Demo Server CLI Example](./ts/bin/panrpc-example-callbacks-callee-cli.ts)
- [<img alt="typescript" src="https://cdn.simpleicons.org/typescript" style="vertical-align: middle;" height="20" width="20" /> Callbacks Demo Client CLI Example](./ts/bin/panrpc-example-callbacks-caller-cli.ts)
- [<img alt="Go" src="https://cdn.simpleicons.org/go" style="vertical-align: middle;" height="20" width="20" /> Callbacks Demo Server CLI Example](./go/cmd/panrpc-example-tcp-callbacks-callee-cli/main.go)
- [<img alt="Go" src="https://cdn.simpleicons.org/go" style="vertical-align: middle;" height="20" width="20" /> Callbacks Demo Client CLI Example](./go/cmd/panrpc-example-tcp-callbacks-caller-cli/main.go)
- [<img alt="typescript" src="https://cdn.simpleicons.org/typescript" style="vertical-align: middle;" height="20" width="20" /> Callbacks Demo Server CLI Example](./ts/bin/panrpc-example-tcp-callbacks-callee-cli.ts)
- [<img alt="typescript" src="https://cdn.simpleicons.org/typescript" style="vertical-align: middle;" height="20" width="20" /> Callbacks Demo Client CLI Example](./ts/bin/panrpc-example-tcp-callbacks-caller-cli.ts)
- **Closures**
- [<img alt="Go" src="https://cdn.simpleicons.org/go" style="vertical-align: middle;" height="20" width="20" /> Closures Demo Server CLI Example](./go/cmd/panrpc-example-closures-callee-cli/main.go)
- [<img alt="Go" src="https://cdn.simpleicons.org/go" style="vertical-align: middle;" height="20" width="20" /> Closures Demo Client CLI Example](./go/cmd/panrpc-example-closures-caller-cli/main.go)
- [<img alt="typescript" src="https://cdn.simpleicons.org/typescript" style="vertical-align: middle;" height="20" width="20" /> Closures Demo Server CLI Example](./ts/bin/panrpc-example-closures-callee-cli.ts)
- [<img alt="typescript" src="https://cdn.simpleicons.org/typescript" style="vertical-align: middle;" height="20" width="20" /> Closures Demo Client CLI Example](./ts/bin/panrpc-example-closures-caller-cli.ts)
- [<img alt="Go" src="https://cdn.simpleicons.org/go" style="vertical-align: middle;" height="20" width="20" /> Closures Demo Server CLI Example](./go/cmd/panrpc-example-tcp-closures-callee-cli/main.go)
- [<img alt="Go" src="https://cdn.simpleicons.org/go" style="vertical-align: middle;" height="20" width="20" /> Closures Demo Client CLI Example](./go/cmd/panrpc-example-tcp-closures-caller-cli/main.go)
- [<img alt="typescript" src="https://cdn.simpleicons.org/typescript" style="vertical-align: middle;" height="20" width="20" /> Closures Demo Server CLI Example](./ts/bin/panrpc-example-tcp-closures-callee-cli.ts)
- [<img alt="typescript" src="https://cdn.simpleicons.org/typescript" style="vertical-align: middle;" height="20" width="20" /> Closures Demo Client CLI Example](./ts/bin/panrpc-example-tcp-closures-caller-cli.ts)
- **Benchmarks**
- [<img alt="Go" src="https://cdn.simpleicons.org/go" style="vertical-align: middle;" height="20" width="20" /> Requests/Second Benchmark Server CLI Example](./go/cmd/panrpc-example-tcp-rps-server-cli/main.go)
- [<img alt="Go" src="https://cdn.simpleicons.org/go" style="vertical-align: middle;" height="20" width="20" /> Requests/Second Benchmark Client CLI Example](./go/cmd/panrpc-example-tcp-rps-client-cli/main.go)
Expand Down
6 changes: 3 additions & 3 deletions ts/bin/panrpc-example-pipe-server-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { JSONParser } from "@streamparser/json-whatwg";
import { ILocalContext, IRemoteContext, Registry } from "../index";

class Local {
private counter = 0;
#counter = 0;

constructor() {
this.Increment = this.Increment.bind(this);
Expand All @@ -19,9 +19,9 @@ class Local {
ctx.remoteID
);

this.counter += delta;
this.#counter += delta;

return this.counter;
return this.#counter;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Socket, createServer } from "net";
import { ILocalContext, IRemoteContext, Registry } from "../index";

class Local {
private counter = 0;
#counter = 0;

public forRemotes?: (
cb: (remoteID: string, remote: Remote) => Promise<void>
Expand All @@ -26,9 +26,9 @@ class Local {
}
});

this.counter += delta;
this.#counter += delta;

return this.counter;
return this.#counter;
}
}

Expand Down
6 changes: 3 additions & 3 deletions ts/bin/panrpc-example-tcp-nested-server-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TimeLocal {
}

class Local {
private counter = 0;
#counter = 0;

constructor(public Time: TimeLocal) {
this.Increment = this.Increment.bind(this);
Expand All @@ -35,9 +35,9 @@ class Local {
ctx.remoteID
);

this.counter += delta;
this.#counter += delta;

return this.counter;
return this.#counter;
}
}

Expand Down
6 changes: 3 additions & 3 deletions ts/bin/panrpc-example-tcp-server-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Socket, createServer } from "net";
import { ILocalContext, IRemoteContext, Registry } from "../index";

class Local {
private counter = 0;
#counter = 0;

constructor() {
this.Increment = this.Increment.bind(this);
Expand All @@ -22,9 +22,9 @@ class Local {
ctx.remoteID
);

this.counter += delta;
this.#counter += delta;

return this.counter;
return this.#counter;
}
}

Expand Down
8 changes: 6 additions & 2 deletions ts/bin/panrpc-example-tcp-throughput-server-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import { Socket, createServer } from "net";
import { ILocalContext, Registry } from "../index";

class Local {
constructor(private buf: number[]) {
#buf: number[];

constructor(buf: number[]) {
this.#buf = buf;

this.GetBytes = this.GetBytes.bind(this);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async GetBytes(ctx: ILocalContext): Promise<number[]> {
return this.buf;
return this.#buf;
}
}

Expand Down
6 changes: 3 additions & 3 deletions ts/bin/panrpc-example-unix-server-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { join } from "path";
import { ILocalContext, IRemoteContext, Registry } from "../index";

class Local {
private counter = 0;
#counter = 0;

constructor() {
this.Increment = this.Increment.bind(this);
Expand All @@ -24,9 +24,9 @@ class Local {
ctx.remoteID
);

this.counter += delta;
this.#counter += delta;

return this.counter;
return this.#counter;
}
}

Expand Down
12 changes: 8 additions & 4 deletions ts/bin/panrpc-example-websocket-coffee-server-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class CoffeeMachine {
cb: (remoteID: string, remote: RemoteControl) => Promise<void>
) => Promise<void>;

constructor(private supportedVariants: string[], private waterLevel: number) {
#waterLevel: number;

constructor(private supportedVariants: string[], waterLevel: number) {
this.#waterLevel = waterLevel;

this.BrewCoffee = this.BrewCoffee.bind(this);
}

Expand All @@ -41,7 +45,7 @@ class CoffeeMachine {
throw new Error("unsupported variant");
}

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

Expand Down Expand Up @@ -78,9 +82,9 @@ class CoffeeMachine {
});
}

this.waterLevel -= size;
this.#waterLevel -= size;

return this.waterLevel;
return this.#waterLevel;
}
}

Expand Down
6 changes: 3 additions & 3 deletions ts/bin/panrpc-example-websocket-server-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { WebSocket, WebSocketServer } from "ws";
import { ILocalContext, IRemoteContext, Registry } from "../index";

class Local {
private counter = 0;
#counter = 0;

constructor() {
this.Increment = this.Increment.bind(this);
Expand All @@ -23,9 +23,9 @@ class Local {
ctx.remoteID
);

this.counter += delta;
this.#counter += delta;

return this.counter;
return this.#counter;
}
}

Expand Down

0 comments on commit 567f63b

Please sign in to comment.