import type Comlink from "comlink" ; import { KobraError } from "src/blocks/blockUtils" ; import { mlFunctions } from "src/blocks/ML_block" ; import type getCSVFromCache from "../utils/csvFetcher" ; import { addGlobals , addImportedBlocks , AsyncFunction , Function , makeAsyncFn , makeHighlightBlock , removeGlobals , removeImportedBlocks , RunError , serializeError } from "./shared" ; const comlink = require ( "comlink" ) ; const importedBlocks = [ require ( "./../blocks/DataFrame_block" ) , require ( "./../blocks/mock/DataView_block" ) , require ( "./../blocks/mock/misc_block" ) ] ; export type ProxiedFn < T extends Function > = Comlink . ProxyMarked & ( T extends AsyncFunction ? T : ( ... args : Parameters < T > ) => Promise < ReturnType < T > > ) ; declare global { var IS_WORKER ; var worker_getCSVFromCache ; var worker_currentlyRunningBlock ; var worker_comlinkExposed ; var MOCK_ML_MODELS ; } globalThis . IS_WORKER = true ; globalThis . MOCK_ML_MODELS = true ; const highlightBlockInner = makeHighlightBlock ( ( id ) => { globalThis . worker_currentlyRunningBlock = id ; } , "worker_currentlyRunningBlock" ) ; export type RunResult = { errors : RunError [ ] ; } ; export const RunnerWorker = { async run ( source , worker_getCSVFromCache ) { globalThis . worker_currentlyRunningBlock = undefined ; const collectedErrors = [ ] ; const highlightBlock = ( id , f ) => highlightBlockInner ( id , async ( ) => { try { return await f ( ) ; } catch ( error ) { collectedErrors . push ( { exception : error , blockId : globalThis . worker_currentlyRunningBlock } ) ; } } ) ; const globals = { worker_getCSVFromCache , mlFunctions , highlightBlock } ; addGlobals ( globals ) ; addImportedBlocks ( importedBlocks ) ; globalThis . modelsDb = [ ] ; await makeAsyncFn ( source ) ( ) ; removeImportedBlocks ( importedBlocks ) ; removeGlobals ( globals ) ; return { errors : collectedErrors . filter ( ( error ) => error . exception instanceof KobraError ) . map ( ( error ) => ( { ... error , exception : serializeError ( error . exception ) } ) ) } ; } } ; if ( ! globalThis . worker_comlinkExposed ) { comlink . expose ( RunnerWorker ) ; globalThis . worker_comlinkExposed = true ; }