[dingus] Abstract document handling
This commit is contained in:
parent
bcf1bbb948
commit
3f51b83850
2 changed files with 50 additions and 69 deletions
105
dingus/dingus.js
105
dingus/dingus.js
|
|
@ -7,60 +7,57 @@ let next_document = null;
|
||||||
|
|
||||||
const STATUS = document.getElementById("status-line");
|
const STATUS = document.getElementById("status-line");
|
||||||
|
|
||||||
function submit_grammar(code) {
|
const DOC_CHAINS = {};
|
||||||
if (pending_grammar) {
|
function chain_document_submit(kind, editor, on_success) {
|
||||||
console.log("Grammar still pending, parking it");
|
let pending = null;
|
||||||
next_grammar = code;
|
let next = null;
|
||||||
} else {
|
|
||||||
pending_grammar = code;
|
|
||||||
worker.postMessage({kind: "grammar_module", data: code});
|
|
||||||
console.log("Grammar posted");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function submit_document(code) {
|
function do_submit(document) {
|
||||||
if (pending_document) {
|
if (pending) {
|
||||||
console.log("Document still pending, parking it");
|
console.log("Old still pending, parking it...");
|
||||||
next_document = code;
|
next = document;
|
||||||
} else {
|
} else {
|
||||||
pending_document = code;
|
pending = document;
|
||||||
worker.postMessage({kind: "document", data: code});
|
worker.postMessage({kind, data: document});
|
||||||
console.log("Document posted");
|
console.log("Document submitted");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function on_result(message) {
|
||||||
|
pending = null;
|
||||||
|
if (next) {
|
||||||
|
pending = next;
|
||||||
|
next = null;
|
||||||
|
|
||||||
|
worker.postMessage({kind, data: document});
|
||||||
|
console.log("Posted another document");
|
||||||
|
}
|
||||||
|
if (message.kind === "ok" && on_success) {
|
||||||
|
on_success(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DOC_CHAINS[kind] = on_result;
|
||||||
|
|
||||||
|
let change_timer_id = null;
|
||||||
|
editor.doc.on("change", () => {
|
||||||
|
clearTimeout(change_timer_id);
|
||||||
|
change_timer_id = setTimeout(() => {
|
||||||
|
change_timer_id = null;
|
||||||
|
do_submit(editor.doc.getValue());
|
||||||
|
}, 100);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const worker = new Worker('worker.js');
|
const worker = new Worker('worker.js');
|
||||||
worker.onmessage = (e) => {
|
worker.onmessage = (e) => {
|
||||||
const message = e.data;
|
const message = e.data;
|
||||||
if (message.kind === "grammar_status") {
|
|
||||||
STATUS.innerText = message.message;
|
|
||||||
|
|
||||||
if ((message.status === "ok") || (message.status === "error")) {
|
const chain = DOC_CHAINS[message.kind];
|
||||||
pending_grammar = null;
|
if (chain) {
|
||||||
if (next_grammar) {
|
chain(message);
|
||||||
pending_grammar = next_grammar;
|
|
||||||
next_grammar = null;
|
|
||||||
|
|
||||||
worker.postMessage({kind: "grammar_module", data: pending_grammar});
|
|
||||||
console.log("Posted another grammar");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message.kind === "doc_status") {
|
STATUS.innerText = message.message;
|
||||||
STATUS.innerText = message.message;
|
|
||||||
|
|
||||||
if ((message.status === "ok") || (message.status === "error")) {
|
|
||||||
pending_document = null;
|
|
||||||
if (next_document) {
|
|
||||||
pending_document = next_document;
|
|
||||||
next_document = null;
|
|
||||||
|
|
||||||
worker.postMessage({kind: "document", data: pending_document});
|
|
||||||
console.log("Posted another document");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -73,15 +70,7 @@ function setup_editors() {
|
||||||
value: "from parser import Grammar\n\nclass MyGrammar(Grammar):\n pass\n",
|
value: "from parser import Grammar\n\nclass MyGrammar(Grammar):\n pass\n",
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
chain_document_submit("grammar", grammar_editor);
|
||||||
let grammar_change_timer_id = null;
|
|
||||||
grammar_editor.doc.on("change", () => {
|
|
||||||
clearTimeout(grammar_change_timer_id);
|
|
||||||
grammar_change_timer_id = setTimeout(() => {
|
|
||||||
grammar_change_timer_id = null;
|
|
||||||
submit_grammar(grammar_editor.doc.getValue());
|
|
||||||
}, 100);
|
|
||||||
});
|
|
||||||
|
|
||||||
const input_editor = CodeMirror.fromTextArea(
|
const input_editor = CodeMirror.fromTextArea(
|
||||||
document.getElementById("input"),
|
document.getElementById("input"),
|
||||||
|
|
@ -89,15 +78,7 @@ function setup_editors() {
|
||||||
lineNumbers: true,
|
lineNumbers: true,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
chain_document_submit("input", input_editor);
|
||||||
let input_change_timer_id = null;
|
|
||||||
input_editor.doc.on("change", () => {
|
|
||||||
clearTimeout(input_change_timer_id);
|
|
||||||
input_change_timer_id = setTimeout(() => {
|
|
||||||
input_change_timer_id = null;
|
|
||||||
submit_document(input_editor.doc.getValue());
|
|
||||||
}, 100);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setup_editors();
|
setup_editors();
|
||||||
|
|
|
||||||
|
|
@ -22,18 +22,18 @@ function data_to_js(thing) {
|
||||||
const dingus_module = {
|
const dingus_module = {
|
||||||
post_grammar_status: function (message) {
|
post_grammar_status: function (message) {
|
||||||
console.log("Grammar Status:", message);
|
console.log("Grammar Status:", message);
|
||||||
postMessage({kind: "grammar_status", status: "loading", message});
|
postMessage({kind: "grammar", status: "loading", message});
|
||||||
},
|
},
|
||||||
|
|
||||||
post_grammar_loaded: function (name) {
|
post_grammar_loaded: function (name) {
|
||||||
const msg = "Grammar '" + name + "' loaded";
|
const msg = "Grammar '" + name + "' loaded";
|
||||||
console.log(msg);
|
console.log(msg);
|
||||||
postMessage({kind: "grammar_status", status: "ok", message: msg});
|
postMessage({kind: "grammar", status: "ok", message: msg});
|
||||||
},
|
},
|
||||||
|
|
||||||
post_grammar_error: function(error) {
|
post_grammar_error: function(error) {
|
||||||
console.log("Grammar Error:", error);
|
console.log("Grammar Error:", error);
|
||||||
postMessage({kind:"grammar_status", status: "error", message: error});
|
postMessage({kind:"grammar", status: "error", message: error});
|
||||||
},
|
},
|
||||||
|
|
||||||
post_doc_parse: function(tree, errors) {
|
post_doc_parse: function(tree, errors) {
|
||||||
|
|
@ -42,7 +42,7 @@ const dingus_module = {
|
||||||
|
|
||||||
console.log("Doc parse:", tree, errors);
|
console.log("Doc parse:", tree, errors);
|
||||||
postMessage({
|
postMessage({
|
||||||
kind: "doc_status",
|
kind: "input",
|
||||||
status: "ok",
|
status: "ok",
|
||||||
message: "Parsed",
|
message: "Parsed",
|
||||||
errors: errors,
|
errors: errors,
|
||||||
|
|
@ -52,7 +52,7 @@ const dingus_module = {
|
||||||
|
|
||||||
post_doc_error: function(error) {
|
post_doc_error: function(error) {
|
||||||
console.log("Doc Error:", error);
|
console.log("Doc Error:", error);
|
||||||
postMessage({kind:"doc_status", status: "error", message: error});
|
postMessage({kind:"input", status: "error", message: error});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -182,9 +182,9 @@ self.onmessage = async function(event) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { kind, data } = event.data;
|
const { kind, data } = event.data;
|
||||||
if (kind === "grammar_module") {
|
if (kind === "grammar") {
|
||||||
await load_grammar_module(data);
|
await load_grammar_module(data);
|
||||||
} else if (kind === "document") {
|
} else if (kind === "input") {
|
||||||
await parse_document(data);
|
await parse_document(data);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue