-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate out JS-execution code into a 'Kernel'
- Loading branch information
1 parent
3af920c
commit 03f7738
Showing
5 changed files
with
90 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// @ts-ignore - Load worker source code. | ||
import WORKER_SRC from "!raw-loader!ts-loader!./worker.ts"; | ||
const blob = new Blob([WORKER_SRC], { type: "application/javascript" }); | ||
|
||
import { javascript } from "@codemirror/lang-javascript"; | ||
import { Kernel, OutputLine } from "../kernel"; | ||
import { ExecutionRequest, ExecutionResponse } from "./types"; | ||
|
||
export class JavaScriptKernel implements Kernel { | ||
worker: Worker = new Worker(URL.createObjectURL(blob)); | ||
|
||
requestID: number = 0; | ||
getRequestID() { | ||
return this.requestID++; | ||
} | ||
|
||
run(code: string, onOutput: (line: OutputLine) => void, onDone: () => void) { | ||
// Generate a unique ID to track this execution request. | ||
const requestID = this.getRequestID(); | ||
|
||
const messageHandler = (e: MessageEvent<ExecutionResponse>) => { | ||
if (e.data.requestID != requestID) return; | ||
|
||
if (e.data.kind === "run-code-output") { | ||
onOutput(e.data.output); | ||
} else if (e.data.kind === "run-code-done") { | ||
this.worker.removeEventListener("message", messageHandler); | ||
onDone(); | ||
} | ||
}; | ||
|
||
this.worker.addEventListener("message", messageHandler); | ||
|
||
// Post the code to the worker. | ||
this.worker.postMessage({ | ||
kind: "run-code", | ||
code: code, | ||
requestID: requestID, | ||
} as ExecutionRequest); | ||
} | ||
|
||
getSyntaxHighlighter() { | ||
return javascript(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { OutputLine } from "../kernel"; | ||
|
||
export type ExecutionRequest = { | ||
kind: "run-code"; | ||
code: string; | ||
requestID: number; | ||
} | ||
|
||
export type ExecutionResponse = { | ||
kind: "run-code-output"; | ||
output: OutputLine; | ||
requestID: number; | ||
} | { | ||
kind: "run-code-done"; | ||
requestID: number; | ||
} | { | ||
kind: "run-code-waiting"; | ||
requestID: number; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { LanguageSupport } from "@codemirror/language"; | ||
|
||
export type OutputLine = { | ||
type: string; | ||
line: string; | ||
}; | ||
|
||
export interface Kernel { | ||
run(code: string, onOutput: (line: OutputLine) => void, onDone: () => void); | ||
getSyntaxHighlighter(): LanguageSupport; | ||
} |