-
Notifications
You must be signed in to change notification settings - Fork 47
/
counter.ts
43 lines (37 loc) · 1.25 KB
/
counter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import * as df from "durable-functions";
import {
EntityHandler,
EntityContext,
OrchestrationContext,
OrchestrationHandler,
EntityId,
} from "durable-functions";
const counterEntityName = "counterEntity";
const counterEntity: EntityHandler<number> = async function (
context: EntityContext<number>
): Promise<void> {
await Promise.resolve();
let currentValue: number = context.df.getState(() => 0);
switch (context.df.operationName) {
case "add":
const amount: number = context.df.getInput();
currentValue += amount;
break;
case "reset":
currentValue = 0;
break;
case "get":
context.df.return(currentValue);
break;
}
context.df.setState(currentValue);
};
df.app.entity(counterEntityName, counterEntity);
const counterOrchestration: OrchestrationHandler = function* (context: OrchestrationContext) {
const entityId: EntityId = new df.EntityId(counterEntityName, "myCounter");
const currentValue: number = yield context.df.callEntity(entityId, "get");
if (currentValue < 10) {
yield context.df.callEntity(entityId, "add", 1);
}
};
df.app.orchestration("counterOrchestration", counterOrchestration);