Skip to content

Commit e858218

Browse files
committed
Clean up utility examples in the SDK
1 parent 1a110cb commit e858218

File tree

3 files changed

+55
-60
lines changed

3 files changed

+55
-60
lines changed

examples/example.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
1010
*/
1111

12-
/* eslint-disable no-console */
13-
1412
/*
1513
* A simple example program using the Restate dynamic RPC-based API.
1614
*
@@ -20,43 +18,45 @@
2018

2119
import * as restate from "../src/public_api";
2220

23-
// handler implementations
24-
25-
const doGreet = async (
26-
ctx: restate.RpcContext,
27-
name: string
28-
): Promise<string> => {
29-
const countSoFar = await ctx.rpc<apiType>({ path: "counter" }).count(name);
30-
31-
const message = `Hello ${name}! at the ${countSoFar + 1}th time`;
32-
33-
ctx.send(greeterApi).logger(message);
34-
ctx.sendDelayed(greeterApi, 100).logger("delayed " + message);
21+
//
22+
// The main entry point for the service, receiving the greeting request and name.
23+
//
24+
const greeter = restate.router({
25+
greet: async (ctx: restate.RpcContext, name: string) => {
26+
// blocking RPC call to a keyed service (here supplying type and path separately)
27+
const countSoFar = await ctx
28+
.rpc<counterApiType>({ path: "counter" })
29+
.count(name);
3530

36-
return message;
37-
};
31+
const message = `Hello ${name}, for the ${countSoFar + 1}th time!`;
3832

39-
const countKeeper = async (ctx: restate.RpcContext): Promise<number> => {
40-
const seen = (await ctx.get<number>("seen")) || 0;
41-
ctx.set("seen", seen + 1);
42-
return seen;
43-
};
33+
// sending messages to ourselves, immediately and delayed
34+
ctx.send(greeterApi).logger(message);
35+
ctx.sendDelayed(greeterApi, 100).logger("delayed " + message);
4436

45-
// routers (with some in-line handlers)
37+
return message;
38+
},
4639

47-
const greeter = restate.router({
48-
greet: doGreet,
4940
logger: async (ctx: restate.RpcContext, msg: string) => {
5041
ctx.console.log(" HEEEELLLLOOOOO! " + msg);
5142
},
5243
});
5344

45+
//
46+
// The stateful aux service that keeps the counts.
47+
// This could in principle be the same service as the greet service, we just separate
48+
// them here to have this multi-service setup for testing.
49+
//
5450
const counter = restate.keyedRouter({
55-
count: countKeeper,
51+
count: async (ctx: restate.RpcContext): Promise<number> => {
52+
const seen = (await ctx.get<number>("seen")) ?? 0;
53+
ctx.set("seen", seen + 1);
54+
return seen;
55+
},
5656
});
5757

58-
type apiType = typeof counter;
5958
const greeterApi: restate.ServiceApi<typeof greeter> = { path: "greeter" };
59+
type counterApiType = typeof counter;
6060

6161
// restate server
6262

examples/grpc_example.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ export class GreeterService implements TestGreeter {
3333
const ctx = restate.useContext(this);
3434

3535
// state
36-
let seen = (await ctx.get<number>("seen")) || 0;
36+
let seen = (await ctx.get<number>("seen")) ?? 0;
3737
seen += 1;
38-
39-
await ctx.set("seen", seen);
38+
ctx.set("seen", seen);
4039

4140
// return the final response
4241
return TestResponse.create({

examples/handler_example.ts

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,44 @@
99
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
1010
*/
1111

12-
/* eslint-disable no-console */
13-
1412
/*
15-
* A simple example program using the Restate's event handlers.
13+
* A simple example program showing how to let services listen to events produced
14+
* by systems like Kafka.
1615
*/
1716

1817
import * as restate from "../src/public_api";
1918

20-
const registration = async (ctx: restate.RpcContext, event: restate.Event) => {
21-
// store in state the user's information as coming from the registeration event
22-
const { name } = event.json<{ name: string }>();
23-
ctx.set("name", name);
24-
};
25-
26-
const email = async (ctx: restate.RpcContext, event: restate.Event) => {
27-
// store in state the user's information as coming from the email event
28-
const { email } = event.json<{ email: string }>();
29-
ctx.set("email", email);
30-
};
31-
3219
type UserProfile = {
3320
id: string;
3421
name: string;
3522
email: string;
3623
};
3724

38-
const get = async (
39-
ctx: restate.RpcContext,
40-
id: string
41-
): Promise<UserProfile> => {
42-
return {
43-
id,
44-
name: (await ctx.get<string>("name")) ?? "",
45-
email: (await ctx.get<string>("email")) ?? "",
46-
};
47-
};
48-
49-
const profile = restate.keyedRouter({
50-
registration: restate.keyedEventHandler(registration),
51-
email: restate.keyedEventHandler(email),
52-
get,
25+
const profileService = restate.keyedRouter({
26+
registration: restate.keyedEventHandler(
27+
async (ctx: restate.RpcContext, event: restate.Event) => {
28+
// store in state the user's information as coming from the registeration event
29+
const { name } = event.json<{ name: string }>();
30+
ctx.set("name", name);
31+
}
32+
),
33+
34+
email: restate.keyedEventHandler(
35+
async (ctx: restate.RpcContext, event: restate.Event) => {
36+
// store in state the user's information as coming from the email event
37+
const { email } = event.json<{ email: string }>();
38+
ctx.set("email", email);
39+
}
40+
),
41+
42+
get: async (ctx: restate.RpcContext, id: string): Promise<UserProfile> => {
43+
return {
44+
id,
45+
name: (await ctx.get<string>("name")) ?? "",
46+
email: (await ctx.get<string>("email")) ?? "",
47+
};
48+
},
5349
});
5450

5551
// restate server
56-
restate.createServer().bindKeyedRouter("profile", profile).listen(9080);
52+
restate.createServer().bindKeyedRouter("profile", profileService).listen(9080);

0 commit comments

Comments
 (0)