All files / web/src/lib/flowcharts example-generator.worker.ts

0% Statements 0/72
0% Branches 0/1
0% Functions 0/1
0% Lines 0/72

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73                                                                                                                                                 
/**
 * Web Worker for generating flowchart examples off the main thread.
 *
 * This worker handles the CPU-intensive example generation computation
 * so it doesn't block UI interactions like dice dragging.
 *
 * Supports two modes:
 * 1. Full generation: Generate all examples (single worker)
 * 2. Partial generation: Generate examples for a subset of paths (parallel workers)
 */

import type { ExecutableFlowchart } from './schema'
import type { GeneratedExample, GenerationConstraints } from './loader'
import { generateDiverseExamples, generateExamplesForPaths } from './loader'

export interface GenerateExamplesRequest {
  type: 'generate'
  requestId: string // Unique ID to match response to request
  flowchart: ExecutableFlowchart
  count: number
  constraints: GenerationConstraints
}

export interface GenerateExamplesPartialRequest {
  type: 'generate-partial'
  requestId: string // Unique ID to match response to request
  flowchart: ExecutableFlowchart
  pathIndices: number[] // Which paths this worker should process
  constraints: GenerationConstraints
}

export interface GenerateExamplesResponse {
  type: 'result'
  requestId: string // Echo back the request ID
  examples: GeneratedExample[]
}

export interface GenerateExamplesError {
  type: 'error'
  requestId: string // Echo back the request ID
  message: string
}

export type WorkerMessage = GenerateExamplesRequest | GenerateExamplesPartialRequest
export type WorkerResponse = GenerateExamplesResponse | GenerateExamplesError

// Handle incoming messages
self.onmessage = (event: MessageEvent<WorkerMessage>) => {
  const data = event.data
  const { requestId } = data

  try {
    if (data.type === 'generate') {
      // Full generation mode
      const examples = generateDiverseExamples(data.flowchart, data.count, data.constraints)
      const response: GenerateExamplesResponse = { type: 'result', requestId, examples }
      self.postMessage(response)
    } else if (data.type === 'generate-partial') {
      // Partial generation mode - only process assigned paths
      const examples = generateExamplesForPaths(data.flowchart, data.pathIndices, data.constraints)
      const response: GenerateExamplesResponse = { type: 'result', requestId, examples }
      self.postMessage(response)
    }
  } catch (error) {
    const response: GenerateExamplesError = {
      type: 'error',
      requestId,
      message: error instanceof Error ? error.message : 'Unknown error',
    }
    self.postMessage(response)
  }
}