0.0.124 • Published 10 months ago

@tscircuit/eval v0.0.124

Weekly downloads
-
License
-
Repository
-
Last release
10 months ago

@tscircuit/eval

Evaluate code in a full tscircuit runtime environment, including babel transpilation and execution, so you just need to send the code to be executed with automatic handling of imports from @tsci/*

The circuit object from @tscircuit/core is already exposed on the global scope. All imports from @tsci/* are automatically handled.

Usage Options

1. Using CircuitWebWorker (Web Worker)

import { createCircuitWebWorker } from "@tscircuit/eval"

const circuitWebWorker = createCircuitWebWorker()

await circuitWebWorker.execute(`
import { RedLed } from "@tsci/seveibar.red-led"

circuit.add(
  <board width="10mm" height="10mm">
    <RedLed />
  </board>
)
`)

await circuitWebWorker.renderUntilSettled()

const circuitJson = await circuitWebWorker.getCircuitJson()

2. Using CircuitRunner Directly

For simple cases where you don't need web worker isolation, you can use CircuitRunner directly in the main thread:

import { CircuitRunner } from "@tscircuit/eval"

const circuitRunner = new CircuitRunner()

await circuitRunner.execute(`
import { RedLed } from "@tsci/seveibar.red-led"

circuit.add(
  <board width="10mm" height="10mm">
    <RedLed name="LED1" />
  </board>
)`)

await circuitRunner.renderUntilSettled()

const circuitJson = await circuitRunner.getCircuitJson()
// Validate circuit elements
const led = circuitJson.find((el) => el.name === "LED1")

3. Using Virtual Filesystem

You can also execute code using a virtual filesystem, which is useful when you have multiple files or components:

import { createCircuitWebWorker } from "@tscircuit/eval"

const circuitWebWorker = createCircuitWebWorker()

await circuitWebWorker.executeWithFsMap({
  fsMap: {
    "entrypoint.tsx": `
      import { MyLed } from "./myled.tsx"
      
      circuit.add(
        <board width="10mm" height="10mm">
          <MyLed name="LED1" />
        </board>
      )
    `,
    "myled.tsx": `
      import { RedLed } from "@tsci/seveibar.red-led"
      
      export const MyLed = ({ name }) => {
        return <RedLed name={name} />
      }
    `,
  },
  entrypoint: "entrypoint.tsx",
})

await circuitWebWorker.renderUntilSettled()

const circuitJson = await circuitWebWorker.getCircuitJson()

When to Use Which Approach

CircuitRunner (Direct Execution)

  • ✅ Simple debugging
  • ✅ No worker setup required
  • ❌ Blocks main thread
  • ❌ No isolation from host environment

CircuitWebWorker (Web Worker)

  • ✅ Non-blocking execution
  • ✅ Isolated environment
  • ✅ Better for production use
  • ❌ More complex setup
  • ❌ Comlink overhead for communication

Why use a web worker?

tscircuit can block the ui thread in a browser. In addition, tscircuit sometimes freezes during the render loop due to autorouting or other computationally intensive operations. Executing tscircuit code in a web worker allows the ui to display the rendering process without freezing, and stop rendering if it goes on for too long.

Execution Implementation

  1. The execution code is scanned for imports, these imports are then loaded via fetch from the CDN and added to a global import map.
  2. The code is transpiled. Imports/requires automatically check the import map.
  3. The transpiled code is executed with a circuit object added to the global scope.
  4. When a user calls circuitWebWorker.renderUntilSettled(), the web worker the webworker runs circuit.renderUntilSettled()
0.0.124

10 months ago

0.0.123

10 months ago

0.0.122

10 months ago

0.0.121

10 months ago

0.0.117

11 months ago

0.0.116

11 months ago

0.0.115

11 months ago

0.0.114

11 months ago

0.0.119

11 months ago

0.0.118

11 months ago

0.0.120

10 months ago

0.0.113

11 months ago

0.0.112

11 months ago

0.0.111

11 months ago

0.0.110

11 months ago

0.0.109

11 months ago

0.0.108

11 months ago

0.0.106

11 months ago

0.0.105

11 months ago

0.0.104

11 months ago

0.0.103

11 months ago

0.0.107

11 months ago

0.0.102

11 months ago

0.0.101

11 months ago

0.0.100

11 months ago

0.0.99

11 months ago

0.0.98

11 months ago

0.0.97

11 months ago

0.0.96

11 months ago

0.0.95

11 months ago

0.0.94

11 months ago

0.0.93

11 months ago

0.0.92

11 months ago

0.0.91

11 months ago

0.0.90

11 months ago

0.0.89

11 months ago

0.0.88

11 months ago

0.0.87

12 months ago

0.0.86

12 months ago

0.0.85

12 months ago

0.0.84

12 months ago

0.0.83

12 months ago