Repository navigation
Expand file tree
/
Copy pathrules.js
More file actions
215 lines (198 loc) · 5.66 KB
/
Copy pathrules.js
File metadata and controls
215 lines (198 loc) · 5.66 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* rules.js
* implement actions
*/
import { log } from "./log";
import { State } from "./state";
/**
* functions for updating states
*/
export class Rules {
Functions = {
append: (/** @type {any} */ value) => (/** @type {any[]} */ old) =>
[...(old || []), value],
empty: () => () => [],
increment: (/** @type {number} */ value) => (/** @type {number} */ old) =>
old + value,
add_word: (/** @type {string} */ value) => (/** @type {string} */ old) =>
old ? old + " " + value : value,
add_letter: (/** @type {string} */ value) => (/** @type {string} */ old) =>
old ? old + value : value,
replace_last:
(/** @type {string} */ newWord) => (/** @type {string} */ old) =>
[...old.split(" ").slice(0, -1), newWord].join(" "),
};
/**
* @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", {});
}
/** translate an expression from Excel-like to Javascript
* @param {string} expression
* @return {string}
*/
translate(expression) {
/* translate the expression from the excel like form to javascript
this is a hack, we should have a parser
*/
// translate single = to ==
let exp = expression.replaceAll(/(?<![=<>!])=/g, "==");
// translate $name into state references
exp = exp.replaceAll(/\$\w+/g, "state.get('$&')");
// translate #name into field references
exp = exp.replaceAll(/#(\w+)/g, "data.$1");
// log("eic", expression, exp);
return exp;
}
/**
* validate an expression string
* @param {string} expression
* @return {boolean}
*/
validateExpression(expression) {
const exp = this.translate(expression);
try {
Function(`return ${exp};`);
} catch (error) {
log("validate", error);
return false;
}
return true;
}
/**
* evaluate a string as an expression in a given context
*
* @param {string} expression - expression to evaluate
* @param {Object} context - context for the evaluation
* @return {boolean} value returned by the expression
*/
evalInContext(expression, context) {
const variables = Object.keys(context);
const values = Object.values(context);
const exp = this.translate(expression);
// log("eic", expression, exp);
// log("variables", variables);
const func = Function(...variables, `return ${exp}`);
return func(...values);
}
/** @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) {
log({ origin, event, data });
this.last = { origin, event, data, rule: null };
// first for the event then for any that got queued.
while (true) {
const context = { ...this.Functions, state: this.state, data };
log("applyRules", origin, event, data);
log("context", context);
for (const rule of this.rules) {
log("rule", rule);
if (
(origin != rule.origin && rule.origin != "*") ||
event != rule.event
) {
continue;
}
const result = rule.conditions.every((restriction) =>
this.evalInContext(restriction, context)
);
if (result) {
log("got it");
this.last.rule = rule;
const patch = Object.fromEntries(
Object.entries(rule.updates).map(([$var, value]) => [
$var,
this.evalInContext(value, context),
])
);
log("patch", patch);
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;
}
// log("handler", e, origin, event, data);
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;
}
}