@@ -18,30 +18,37 @@ export const createCircuitWebWorker = async (
18
18
)
19
19
}
20
20
21
- const rawWorker = new Worker (
22
- configuration . webWorkerUrl ??
23
- "https://unpkg.com/@tscircuit/eval-webworker/dist/webworker/index.js" ,
24
- { type : "module" } ,
25
- )
26
- const webWorker = Comlink . wrap < InternalWebWorkerApi > ( rawWorker )
21
+ let workerBlobUrl =
22
+ configuration . webWorkerBlobUrl ?? configuration . webWorkerUrl
23
+
24
+ if ( ! workerBlobUrl ) {
25
+ const cdnUrl =
26
+ "https://cdn.jsdelivr.net/npm/@tscircuit/eval-webworker/dist/webworker/index.js"
27
+
28
+ const workerBlob = await fetch ( cdnUrl ) . then ( ( res ) => res . blob ( ) )
29
+ workerBlobUrl = URL . createObjectURL ( workerBlob )
30
+ }
31
+
32
+ const rawWorker = new Worker ( workerBlobUrl , { type : "module" } )
33
+ const comlinkWorker = Comlink . wrap < InternalWebWorkerApi > ( rawWorker )
27
34
28
35
if ( configuration . snippetsApiBaseUrl ) {
29
- await webWorker . setSnippetsApiBaseUrl ( configuration . snippetsApiBaseUrl )
36
+ await comlinkWorker . setSnippetsApiBaseUrl ( configuration . snippetsApiBaseUrl )
30
37
}
31
38
32
39
// Create a wrapper that handles events directly through circuit instance
33
40
const wrapper : CircuitWebWorker = {
34
- clearEventListeners : webWorker . clearEventListeners . bind ( webWorker ) ,
35
- execute : webWorker . execute . bind ( webWorker ) ,
36
- executeWithFsMap : webWorker . executeWithFsMap . bind ( webWorker ) ,
37
- renderUntilSettled : webWorker . renderUntilSettled . bind ( webWorker ) ,
38
- getCircuitJson : webWorker . getCircuitJson . bind ( webWorker ) ,
41
+ clearEventListeners : comlinkWorker . clearEventListeners . bind ( comlinkWorker ) ,
42
+ execute : comlinkWorker . execute . bind ( comlinkWorker ) ,
43
+ executeWithFsMap : comlinkWorker . executeWithFsMap . bind ( comlinkWorker ) ,
44
+ renderUntilSettled : comlinkWorker . renderUntilSettled . bind ( comlinkWorker ) ,
45
+ getCircuitJson : comlinkWorker . getCircuitJson . bind ( comlinkWorker ) ,
39
46
on : ( event : string , callback : ( ...args : any [ ] ) => void ) => {
40
47
const proxiedCallback = Comlink . proxy ( callback )
41
- webWorker . on ( event , proxiedCallback )
48
+ comlinkWorker . on ( event , proxiedCallback )
42
49
} ,
43
50
kill : async ( ) => {
44
- webWorker [ Comlink . releaseProxy ] ( )
51
+ comlinkWorker [ Comlink . releaseProxy ] ( )
45
52
rawWorker . terminate ( )
46
53
} ,
47
54
}
0 commit comments