-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathhistory.yaml
74 lines (70 loc) · 1.85 KB
/
history.yaml
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
name: history
doc: |-
A machine that remembers what is has seen.
ToDo: Support changing the limit.
Warning to developers: Be careful about emitting messages from this
machine's actions!
Dev note: Got "TypeError: Cannot extend Go slice" from push(). I
think that's because the 'hist' value becomes a Go slice (instead of
an array) at some point in the Go/Goja churning. Should look into
that. For now, I just construct a Javascript array from the slice.
paramspecs:
limit:
doc: The maximum number of messages to retain.
primitiveType: int
patternsyntax: json
nodes:
start:
branching:
branches:
- target: listen
listen:
branching:
type: message
branches:
- pattern: |
{"from":"history"}
doc: Skip our own output!
target: listen
- pattern: |
{"query":"?pattern"}
target: query
- pattern: |
"?heard"
target: heard
heard:
action:
interpreter: ecmascript
source: |-
var heard = _.bindings["?heard"];
delete _.bindings["?heard"];
var hist = _.bindings.hist;
if (!hist) {
hist = [];
} else {
hist = [].concat(hist);
}
var limit = _.bindings["?limit"]
if (!limit) {
limit = 10;
}
while (limit <= hist.length) {
hist.splice(0,1);
}
hist.push({ts: new Date(), message: heard});
_.bindings.hist = hist;
return _.bindings;
branching:
branches:
- target: listen
query:
action:
interpreter: ecmascript
source: |-
var bss = _.match(_.bindings["?pattern"], _.bindings.hist, {});
delete _.bindings["?pattern"];
_.out({found: bss, from: "history"});
return _.bindings;
branching:
branches:
- target: listen