Skip to content

Commit

Permalink
Merge pull request #1538 from DanielXMoore/comptime-playground
Browse files Browse the repository at this point in the history
Playground `comptime` restarts playground worker and resets toggle after editing
  • Loading branch information
edemaine authored Oct 29, 2024
2 parents 5b817f5 + 407d5f7 commit e3d5e48
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
15 changes: 9 additions & 6 deletions civet.dev/.vitepress/components/Playground.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const textareaEl = ref<HTMLTextAreaElement>();
// Compile on input
onMounted(fixTextareaSize);
watch(userCode, compile);
watch(userCode, codeChanged);
// Clear
watch(
Expand Down Expand Up @@ -55,14 +55,17 @@ const comptime = ref(false);
watch(comptime, compile);
const hasComptime = ref(false);
async function codeChanged() {
if (showComptime && comptime.value) {
comptime.value = false; // this triggers compile()
} else {
await compile();
}
}
async function compile() {
if (showComptime) {
hasComptime.value = /\bcomptime\b/.test(userCode.value);
if (comptime.value && !hasComptime.value) {
// If we remove comptime code, reset the checkbox to off
// to avoid accidental comptime in the future
comptime.value = userCode.value.includes('comptime');
}
}
const snippet = await compileCivetToHtml({
Expand Down
49 changes: 34 additions & 15 deletions civet.dev/.vitepress/utils/compileCivetToHtml.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
let playgroundWorker: Worker = {} as Worker;
let playgroundWorker: Worker;

interface WorkerResult {
inputHtml: string;
outputHtml?: string;
error?: string;
jsCode?: string;
}

const msgMap: Record<string, {
resolve: (r: WorkerResult) => void
restart: boolean
}> = {};

// @ts-ignore
if (!import.meta.env.SSR) {
playgroundWorker = new Worker('/playground.worker.js');
}
function startWorker() {
playgroundWorker = new Worker('/playground.worker.js')

playgroundWorker.onmessage = ({ data }) => {
const { resolve, restart } = msgMap[data.uid]
resolve(data)
delete msgMap[data.uid]
if (restart) {
playgroundWorker.terminate()
startWorker()
}
};
}

const msgMap: Record<string, any> = {};
playgroundWorker.onmessage = ({ data }) => {
msgMap[data.uid].resolve(data);
msgMap[data.uid] = null;
};
startWorker()
}

let uid = 0;

Expand All @@ -18,15 +38,14 @@ export function compileCivetToHtml({
prettierOutput = true,
jsOutput = false,
parseOptions = {},
}): Promise<{
inputHtml: string;
outputHtml?: string;
error?: string;
jsCode?: string;
}> {
}): Promise<WorkerResult> {
uid++;
playgroundWorker.postMessage({ uid, code, prettierOutput, jsOutput, parseOptions });
return new Promise((resolve) => {
msgMap[uid] = { resolve };
msgMap[uid] = {
resolve,
// Restart the worker whenever we run it with comptime: true
restart: Boolean((parseOptions as any).comptime),
};
});
}

0 comments on commit e3d5e48

Please sign in to comment.