-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathcounter.mo
50 lines (39 loc) · 889 Bytes
/
counter.mo
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
44
45
46
47
48
49
50
// A simple Counter actor.
actor class Counter(i : Int) {
var c = i;
// Decrement counter
public func dec() {
show("dec", c);
c -= 1;
};
// Read counter, asynchronous
public func read() : async Int { c };
};
// Dummy function to show intermediate value in trace.
func show(note : Text, c : Int) {};
// Create an actor.
let c = await Counter(10);
// Issue ten `dec` messages.
func testDec() : async () {
var i : Int = 10;
while (i > 0) {
c.dec();
i -= 1;
}
};
ignore testDec();
// Issue ten `dec` & `read` messages.
func testRead() : async () {
// Dummy function to show intermediate value in trace.
func showAsync(note : Text, a : async Int) {};
var i : Int = 10;
while (i > 0) {
c.dec();
let t = c.read();
showAsync("before", t);
show("await", await t);
showAsync("after", t);
i -= 1;
}
};
ignore testRead();