-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.picolabs.journal.krl
90 lines (68 loc) · 2.25 KB
/
io.picolabs.journal.krl
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
ruleset io.picolabs.journal {
meta {
shares __testing, getEntry
}
global {
__testing = { "queries":
[ { "name": "__testing" }
, { "name": "getEntry", "args": [ "title" ] }
] , "events":
[ { "domain": "journal", "type": "new_entry", "attrs": [ "title", "content" ] }
, { "domain": "journal", "type": "delete_entry", "attrs": [ "timestamp" ] }
, { "domain": "journal", "type": "edit_entry", "attrs": [ "timestamp", "newContent" ] }
]
}
getEntry = function(title) {
(title) => ent:entries.filter(function(x){
x{"title"} == title
})[0] | ent:entries;
}
app = {"name":"journal","version":"0.0"/* img: , pre: , ..*/};
bindings = function(){
{
//currently no bindings
};
}
}
// icon image from https://image.flaticon.com/icons/svg/201/201642.svg
rule discovery { select when manifold apps send_directive("app discovered...", {"app": app, "rid": meta:rid, "bindings": bindings(), "iconURL": "https://raw.githubusercontent.com/Picolab/JournalApp/master/logo.svg"} ); }
rule new_entry {
select when journal new_entry
pre {
entry = { "timestamp" : time:now(), "title" : event:attr("title"), "content" : event:attr("content") }
}
always {
ent:entries := ent:entries.defaultsTo([]).append(entry);
}
}
rule delete_entry {
select when journal delete_entry
pre {
timestamp = event:attr("timestamp");
toDelete = ent:entries.filter(function(x) {
x{"timestamp"} == timestamp
})[0];
toDeleteIndex = ent:entries.index(toDelete);
}
if (toDelete == -1) then noop();
notfired {
ent:entries := ent:entries.defaultsTo([]).splice(toDeleteIndex, 1);
}
}
rule edit_entry {
select when journal edit_entry
pre {
newContent = event:attr("newContent");
timestamp = event:attr("timestamp");
toChange = ent:entries.filter(function(x) {
x{"timestamp"} == timestamp
})[0];
}
if (toChange == -1) then noop();
notfired {
ent:entries := ent:entries.defaultsTo([]).map(function(x){
(x == toChange) => x.put("content", newContent) | x;
})
}
}
}