-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathevents.js
More file actions
489 lines (453 loc) · 15 KB
/
Copy pathevents.js
File metadata and controls
489 lines (453 loc) · 15 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
let messageCounter = 0;
let targetOrigin = POSTMESSAGE_ORIGIN;
let RECEIVED_RESET = false;
function commSetup(config, messageCallback, gainControl, loseControl) {
const callbacks = {};
let callbackCounter = 0;
function sendRpc(data, resolve, reject) {
callbackCounter += 1;
const callbackId = `${callbackCounter}-${data.module}-${data.method}`
callbacks[callbackId] = [ resolve, reject ];
config.sendPort.postMessage({
protocol: "pyret-rpc",
data: { ...data, callbackId }
}, '*');
}
// state is optional; if not provided we use getCurrentState to fill it
// we make it optional because sometimes getCurrentState() is a little racy
// for example onInteraction triggers before the interaction is complete and
// can still see the just-run repl contents instead of empty repl contents
function sendEvent(data, description, state) {
if(!RECEIVED_RESET && data.type !== "pyret-init") {
console.log("Pyret skipping a message synthesized before initialization via 'reset':", data, getCurrentState(config));
return;
}
messageCounter += 1;
if(!state) {
state = getCurrentState(config)
}
state.messageNumber = messageCounter;
console.log("Sending from CPO ", data, state);
config.sendPort.postMessage(
{
protocol: "pyret",
timestamp: window.performance.now(),
data: data,
state,
description
},
targetOrigin
);
}
config.receivePort.onmessage = function (event) {
if (
typeof event.data === "string" &&
event.data.indexOf("setImmediate") === 0
) {
return;
}
if (event.data.type === "loseControl") {
loseControl(config);
return;
}
else if (event.data.type === "gainControl") {
gainControl(config);
return;
}
if (event.data.protocol === "pyret-rpc") {
if(!callbacks[event.data.data.callbackId]) {
console.error("No callback for ", event.data.data.callbackId, event, callbacks);
return;
}
else {
const [resolve, reject] = callbacks[event.data.data.callbackId];
delete callbacks[event.data.data.callbackId];
if(event.data.data.resultType === "exception") {
reject(event.data.data.exception);
}
else if(event.data.data.resultType === "value") {
resolve(event.data.data.result);
}
else {
const message = "Internal RPC error, unknown RPC response event (no result or exception)";
console.error(message, event);
reject({ event, message });
}
}
return;
}
if (event.data.protocol !== "pyret") {
return;
}
messageCallback(event.data.data, event.data.state);
};
return { sendEvent, sendRpc };
}
/**
* Every message goes on the queue *except* for run.
*
* Run empties the queue, stops the program, sets the current state, and runs the program
*
*/
let messageQueue = [];
let interactionsSinceLastRun = [];
// Sometimes there are interleavings of edits with running, so the currently-shown
// definitions isn't the one used for running the program.
// `false` indicates that the program has not been run yet
let definitionsAtLastRun = false;
function getCurrentState(config) {
return {
editorContents: config.CPO.editor.cm.getValue(),
definitionsAtLastRun,
interactionsSinceLastRun,
replContents: config.replCM().getValue()
};
}
function makeEvents(config) {
const editor = config.CPO.editor;
const replCM = function() { return config.CPO.replWidget.cm; }
config.replCM = replCM;
config.inControl = false;
var runButton = $("#runButton");
var runDropdown = $("#runDropdown");
var breakButton = $("#breakButton");
function sendControlWarning() {
comm.sendEvent({
type: "illegal-action"
}, "Tried to interact while not in control.");
}
function loseControl(config) {
config.inControl = false;
config.CPO.editor.cm.setOption('readOnly', 'nocursor');
config.CPO.replWidget.cm.setOption('readOnly', 'nocursor');
config.CPO.editor.cm.getWrapperElement().addEventListener('click', sendControlWarning);
config.replCM().getWrapperElement().addEventListener("click", sendControlWarning);
runButton.hide();
runDropdown.hide();
breakButton.hide();
}
function gainControl(config) {
config.inControl = true;
config.CPO.editor.cm.setOption('readOnly', false);
config.CPO.replWidget.cm.setOption('readOnly', false);
config.CPO.editor.cm.getWrapperElement().removeEventListener('click', sendControlWarning);
config.replCM().getWrapperElement().removeEventListener("click", sendControlWarning);
runButton.show();
runDropdown.show();
breakButton.show();
}
async function reset(state) {
interactionsSinceLastRun = [];
messageCounter = state.messageNumber;
definitionsAtLastRun = state.definitionsAtLastRun;
if(typeof state.definitionsAtLastRun === 'string') {
editorUpdate(state.definitionsAtLastRun);
await window.RUN_CODE(state.definitionsAtLastRun);
}
else if (state.definitionsAtLastRun === false) {
await window.RUN_CODE("");
}
const interactions = state.interactionsSinceLastRun;
for(let i = 0; i < interactions.length; i += 1) {
await runInteraction(interactions[i]);
}
editorUpdate(state.editorContents);
replUpdate(state.replContents);
}
config.CPO.events.onLoad(async function () {
replCM().on("change", function (instance, change) {
if (change.origin === thisAPI || change.origin === "setValue") {
return;
}
comm.sendEvent({
type: "changeRepl",
change: change,
}, "Edited the last interaction.");
});
comm.sendEvent({
type: "pyret-init"
});
if(config.initialState) {
addMessage({
process: async () => {
const result = await resetMessage({ state: config.initialState }, config.initialState);
gainControl(config);
return result;
}
});
}
});
const comm = commSetup(config, onmessage, gainControl, loseControl);
function editorUpdate(newCode) {
editor.cm.replaceRange(
newCode,
{ line: 0, ch: 0},
{ line: editor.cm.lastLine(), ch: 99999 },
thisAPI
);
editor.cm.refresh();
}
function replUpdate(newCode) {
replCM().replaceRange(
newCode,
{ line: 0, ch: 0},
{ line: replCM().lastLine(), ch: 99999 },
thisAPI
);
window.setTimeout(() => {
replCM().refresh();
if(config.inControl) {
replCM().focus();
}
}, 100);
}
// Thanks internet! https://github.com/codemirror/CodeMirror/issues/3691
const thisAPI = "@ignore-this-api";
editor.cm.on("change", function (instance, change) {
if (change.origin === thisAPI || change.origin === "setValue") {
return;
}
comm.sendEvent({
type: "change",
change: change,
}, "Made a change to the program.");
});
config.CPO.events.onRun(function () {
interactionsSinceLastRun = [];
definitionsAtLastRun = getCurrentState(config).editorContents;
comm.sendEvent({
type: "run"
}, "Ran the program.");
});
async function runProgram(state) {
interactionsSinceLastRun = [];
const code = state.editorContents
editorUpdate(code);
definitionsAtLastRun = code;
await window.RUN_CODE(code);
if(!config.inControl) {
replCM().display.input.blur();
replCM().setOption("readOnly", "noCursor");
}
}
config.CPO.events.onInteraction(function (interaction) {
interactionsSinceLastRun.push(interaction);
// When we get this event, the repl may or may not have been cleared,
// so we explicitly pass it as the empty string.
const state = { ...getCurrentState(config), replContents: "" };
comm.sendEvent({
type: "runInteraction"
}, `Ran the last interaction, ${interaction}.`, state);
});
async function clearInteractions(state) {
interactionsSinceLastRun = [];
$("#output").empty();
}
async function runInteraction(src, reportAnswer) {
// Because of a bug we introduced, a bunch of rooms ended up with states with null
// interactions. If left null, they will put the editor into an unrecoverable state.
// So, we simply run them as empty interactions, and rely on future
// runs/resets making things become eventually consistent
if(src === null) { src = ""; }
interactionsSinceLastRun.push(src);
if(!config.inControl) {
$(".repl-prompt")
.find(".CodeMirror")[0]
.CodeMirror.setOption("readOnly", "nocursor");
}
await window.RUN_INTERACTION(src);
// We do this again because the CPO infrastructure
// explicitly re-enables the prompt at the end of
// RUN_INTERACTION, but we don't want that here!
if(!config.inControl) {
$(".repl-prompt")
.find(".CodeMirror")[0]
.CodeMirror.setOption("readOnly", "nocursor");
replCM().display.input.blur();
}
if(typeof reportAnswer === 'string') {
const state = { ...getCurrentState(config), replContents: "" };
const elts = $("#output").children(".echo-container").last().nextAll();
const texts = elts.map((_, e) => $(e).text()).get();
const htmls = elts.map((_, e) => $(e).html()).get();
const textResult = JSON.stringify({ texts, htmls });
comm.sendEvent({
type: "interactionResult",
reportAnswer,
textResult,
}, `Reporting an interaction by request, ${src} ${reportAnswer}.`, state);
}
}
const initialState = {
editorContents: "use context starter2024\n\n",
interactionsSinceLastRun: [],
definitionsAtLastRun: "use context starter2024\n\n",
replContents: ""
};
function resetFromShare(link) {
var initialParams = url.parse(link); // https://code.pyret.org/editor#share=2390485
// initialParams = "share=2390485"
var params = url.parse("/?" + initialParams["hash"]);
if(params["get"]["share"]) {
editorUpdate("");
definitionsAtLastRun = "";
interactionsSinceLastRun = [];
const ran = window.RUN_CODE("");
const toLoad = ran.then(() => {
return CPO.storageAPI.then((api) => {
return api.getSharedFileById(params["get"]["share"]);
});
});
CPO.loadProgram(toLoad).then((text) => {
editorUpdate(text);
})
.catch((e) => {
console.error("Error loading initial state from share link: ", e, link);
});
}
}
async function resetMessage(message, state) {
console.log("Got explicit reset: ", message, "current state", getCurrentState(config));
RECEIVED_RESET = true;
if(message.state === "") {
return reset(initialState);
}
// This means we got a CPO link as the initial state.
if((typeof APP_BASE_URL === 'string' && APP_BASE_URL !== "" && message.state.startsWith(APP_BASE_URL)) || message.state.startsWith("https://" + window.APP_DOMAIN)) {
return resetFromShare(message.state);
}
try {
const state = JSON.parse(message.state);
return reset(state);
}
catch(e) {
console.error("Parse error in initial state, using default initial state");
return reset(initialState);
}
}
function stop() {
try {
config.CPO.replWidget.stop();
}
catch(e) {
console.log("runProgram caught as expected? ", e);
}
}
let pending = false;
function nextMessage() {
if(messageQueue.length === 0 ) { return; }
if(!pending) {
const { process } = messageQueue.pop();
pending = process();
pending.finally(() => {
pending = false;
nextMessage();
});
}
}
function addMessage(message) {
messageQueue.unshift(message);
nextMessage();
}
function onmessage(message, state) {
if(message.type === "reset") {
messageQueue = [];
stop();
addMessage({
process: async () => { return resetMessage(message, state); }
});
return;
}
if(!config.inControl && (state.messageNumber !== messageCounter + 1)) {
console.log("Messages received in a strange order: ", message, state, messageCounter, getCurrentState(config));
messageQueue = [];
stop();
addMessage({
process: async () => { return reset(state); }
});
return;
}
else {
messageCounter += 1;
}
switch (message.type) {
// Run events are special; they re-synchronize all clients and empty the pending queue.
// This makes it so clicking “run” will bring everyone back together, terminate their games, etc.
// Other cases should never reset the messageQueue
case "run":
messageQueue = [];
stop();
addMessage({ process: async () => { return runProgram(state); } });
break;
case "clearInteractions":
addMessage({ process: async () => { return clearInteractions(state); } });
break;
case "setContents":
addMessage({ process: async () => { return editorUpdate(message.text); } });
break;
case "change":
addMessage({
process: async () => {
editor.cm.replaceRange(
message.change.text,
message.change.from,
message.change.to,
thisAPI
);
if(!config.inControl && config.CPO.editor.cm.getValue() !== state.editorContents) {
console.log("Editor contents disagreed with message state, synchronizing.", config.CPO.editor.cm.getValue(), state.editorContents)
editorUpdate(state.editorContents);
}
}
})
break;
case "changeRepl":
addMessage({
process: async () => {
replCM().replaceRange(
message.change.text,
message.change.from,
message.change.to,
thisAPI
);
}
});
// These need to be separate messages to ensure that replCM() processes
// before asking it to refresh
addMessage({
process: async () => {
replCM().refresh();
if(config.inControl) {
replCM().focus();
}
}
});
addMessage({
process: async() => {
if(!config.inControl && replCM().getValue() !== state.replContents) {
console.log("REPL contents disagreed with message state, synchronizing.", replCM().getValue(), state.replContents)
replUpdate(state.replContents);
}
}
});
break;
case "runInteraction":
addMessage({
process: async () => {
const interactions = state.interactionsSinceLastRun;
const src = interactions[interactions.length - 1];
return runInteraction(src, message.reportAnswer);
}
});
break;
}
}
return {
sendRpc: (module, method, args) => {
const { promise, resolve, reject } = Promise.withResolvers();
const data = { module, method, args }
comm.sendRpc(data, resolve, reject);
return promise;
}
}
}