hmn/src/templates/src/markdown_worker.js

42 lines
1.1 KiB
JavaScript
Raw Normal View History

importScripts('{{ static "go_wasm_exec.js" }}');
// wowee good javascript yeah
const global = Function('return this')();
2021-07-30 22:32:19 +00:00
/*
NOTE(ben): The structure here is a little funny but allows for some debouncing. Any postMessages
that got queued up can run all at once, then it can process the latest one.
*/
let wasmLoaded = false;
let jobs = {};
2021-07-30 22:32:19 +00:00
onmessage = ({ data }) => {
const { elementID, markdown, parserName } = data;
jobs[elementID] = { markdown, parserName };
2021-07-30 22:32:19 +00:00
setTimeout(doPreview, 0);
}
const go = new Go();
WebAssembly.instantiateStreaming(fetch('{{ static "parsing.wasm" }}'), go.importObject)
2021-07-30 22:32:19 +00:00
.then(result => {
go.run(result.instance); // don't await this; we want it to be continuously running
wasmLoaded = true;
2021-07-30 22:32:19 +00:00
setTimeout(doPreview, 0);
});
const doPreview = () => {
if (!wasmLoaded) {
2021-07-30 22:32:19 +00:00
return;
}
for (const [elementID, { markdown, parserName }] of Object.entries(jobs)) {
const html = global[parserName](markdown);
postMessage({
elementID: elementID,
html: html,
});
}
jobs = {};
2021-07-30 22:32:19 +00:00
}