-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
70 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { RunFrame } from "lib/components/RunFrame" | ||
|
||
export default () => ( | ||
<RunFrame | ||
fsMap={{ | ||
"main.tsx": ` | ||
circuit.add( | ||
<resistor resistance="1k" /> | ||
) | ||
`, | ||
}} | ||
entrypoint="main.tsx" | ||
/> | ||
) |
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 |
---|---|---|
@@ -1,3 +1,55 @@ | ||
export const RunFrame = () => { | ||
return <div>hello</div> | ||
import { createCircuitWebWorker } from "@tscircuit/eval-webworker" | ||
import { CircuitJsonPreview } from "./CircuitJsonPreview" | ||
import { useEffect, useState } from "react" | ||
|
||
interface Props { | ||
/** | ||
* Map of filenames to file contents that will be available in the worker | ||
*/ | ||
fsMap: { [filename: string]: string } | ||
|
||
/** | ||
* The entry point file that will be executed first | ||
*/ | ||
entrypoint: string | ||
|
||
/** | ||
* Called when the circuit JSON changes | ||
*/ | ||
onCircuitJsonChange?: (circuitJson: any) => void | ||
|
||
/** | ||
* Called when rendering is finished | ||
*/ | ||
onRenderingFinished?: (params: { circuitJson: any }) => void | ||
|
||
/** | ||
* Called for each render event | ||
*/ | ||
onRenderEvent?: (event: any) => void | ||
|
||
/** | ||
* Called when an error occurs | ||
*/ | ||
onError?: (error: Error) => void | ||
} | ||
|
||
export const RunFrame = (props: Props) => { | ||
const [circuitJson, setCircuitJson] = useState<any>(null) | ||
|
||
useEffect(() => { | ||
async function runWorker() { | ||
const worker = await createCircuitWebWorker({}) | ||
const $finished = worker.executeWithFsMap({ | ||
entrypoint: props.entrypoint, | ||
fsMap: props.fsMap, | ||
}) | ||
setCircuitJson(await worker.getCircuitJson()) | ||
await $finished | ||
setCircuitJson(await worker.getCircuitJson()) | ||
} | ||
runWorker() | ||
}, [props.fsMap]) | ||
|
||
return <CircuitJsonPreview circuitJson={circuitJson} /> | ||
} |
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