forked from gbishop/OS-DPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.js
141 lines (130 loc) · 3.35 KB
/
rules.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* rules.js
* implement actions
*/
import { log } from "./log";
import { State } from "./state";
import { evalInContext, Functions } from "./eval";
/**
* functions for updating states
*/
export class Rules {
/**
* @param {Rule[]} rules
* @param {State} state
*/
constructor(rules, state) {
this.rules = rules;
this.state = state;
this.last = {
/** @type {Rule|Null} */
rule: null,
/** @type {Object} */
data: {},
/** @type {string} */
event: "",
/** @type {string} */
origin: "",
};
this.doInit();
}
/** run the init rule if any
*/
doInit() {
this.applyRules("", "init", {});
}
/** @typedef {Object} eventQueueItem
* @property {string} origin
* @property {string} event
*/
/** @type {eventQueueItem[]} */
eventQueue = [];
/** queue an event from within an event handler
* @param {String} origin
* @param {String} event
*/
queueEvent(origin, event) {
this.eventQueue.push({ origin, event });
}
/**
* Attempt to apply a rule
*
* @param {string} origin - name of the originating element
* @param {string} event - type of event that occurred, i.e.press
* @param {Object} data - data associated with the event
*/
applyRules(origin, event, data) {
this.last = { origin, event, data, rule: null };
// first for the event then for any that got queued.
while (true) {
const context = { ...Functions, state: this.state, data };
for (const rule of this.rules) {
if (
(origin != rule.origin && rule.origin != "*") ||
event != rule.event
) {
continue;
}
const result = rule.conditions.every((restriction) =>
evalInContext(restriction, context)
);
if (result) {
this.last.rule = rule;
const patch = Object.fromEntries(
Object.entries(rule.updates).map(([$var, value]) => [
$var,
evalInContext(value, context),
])
);
this.state.update(patch);
break;
}
}
if (this.eventQueue.length == 0) break;
const item = this.eventQueue.pop();
if (item) {
origin = item.origin;
event = item.event;
}
data = {};
}
}
/**
* Pass event to rules
*
* @param {string} origin - name of the originating element
* @param {Object} data - data associated with the event
* @param {string} [event] - optional name for the event
* @return {(event:Event) => void}
*/
handler(origin, data, event) {
return (e) => {
let ev = event;
if (e instanceof PointerEvent && e.altKey) {
ev = "alt-" + event;
}
this.applyRules(origin, ev || e.type, data);
};
}
/** @param {(rule: Rule, index: number) => any} func */
map(func) {
return this.rules.map(func);
}
/** @returns {Set<string>} */
allStates() {
const result = new Set();
for (const rule of this.rules) {
for (const condition of rule.conditions) {
for (const [match] of condition.matchAll(/\$\w+/g)) {
result.add(match);
}
}
for (const [state, newValue] of Object.entries(rule.updates)) {
result.add(state);
for (const [match] of newValue.matchAll(/\$\w+/g)) {
result.add(match);
}
}
}
return result;
}
}