1
1
import { downloadAssemblies } from './cache.js' ;
2
+ // this file handle the communication with the .NET worker.
2
3
3
4
const compilerWorker = new Worker ( 'worker.js' ) ; // first thing: we start the worker so it loads in parallel.
4
5
let runtimeWorker : Worker | undefined ;
@@ -11,21 +12,30 @@ compilerWorker.onmessage = async (ev) => {
11
12
message : string ;
12
13
} ;
13
14
switch ( msg . type ) {
14
- case 'setOutputText' : {
15
+ case 'setOutputText' : { // the worker is sending us some output
15
16
const parsed = JSON . parse ( msg . message ) ;
16
17
onOutputChange ( parsed [ 'OutputType' ] , parsed [ 'Text' ] , true ) ;
17
18
break ;
18
19
}
19
- case 'runtimeAssembly' : {
20
+ case 'runtimeAssembly' : { // the worker sent a compiled dll to run.
20
21
if ( runtimeWorker != undefined ) {
21
22
runtimeWorker . terminate ( ) ;
22
23
}
23
24
onOutputChange ( 'stdout' , 'Loading script\'s .NET Runtime...' , true ) ;
25
+
24
26
runtimeWorker = new Worker ( 'worker.js' ) ;
27
+
28
+ // our small dotnet wrapper around the compiler:
29
+ // listed all the assemblies that this dll will need
30
+ // and built a json that mono wasm understand to run a .NET dll.
31
+
32
+ // the small wrapepr part generate an ideal config file, this script here bring the reality back to it: dotnet js api is far from being usable.
25
33
const cfg = JSON . parse ( msg . message ) ;
26
34
console . log ( 'Starting worker with boot config:' ) ;
27
- cfg [ 'disableInterop' ] = true ;
35
+
36
+ cfg [ 'disableInterop' ] = true ; // we don't do js-dotnet interop in user dlls for now.
28
37
const assets = cfg [ 'assets' ] ;
38
+ // for some reason, mono js really need these two files. why does it doesn't do itself ? good question.
29
39
assets . unshift ( {
30
40
name : jsModuleNativeName ,
31
41
behavior : 'js-module-native' ,
@@ -34,8 +44,8 @@ compilerWorker.onmessage = async (ev) => {
34
44
name : jsModuleRuntimeName ,
35
45
behavior : 'js-module-runtime' ,
36
46
} ) ;
37
- await downloadAssemblies ( cfg ) ;
38
- runtimeWorker . postMessage ( cfg ) ;
47
+ await downloadAssemblies ( cfg ) ; // this download the assemblies by ourself, i explain in the function why we do that.
48
+ runtimeWorker . postMessage ( cfg ) ; // we send the config to the worker.
39
49
let shouldClean = true ;
40
50
runtimeWorker . onmessage = ( e ) => {
41
51
const runtimeMsg = e . data as {
@@ -84,9 +94,14 @@ export function unsubscribeOutputChange(listener: (arg: { outputType: string; va
84
94
}
85
95
86
96
export async function initDotnetWorkers ( initCode : string ) {
97
+ // msbuild generated a shiny file for dotnet js, it's called blazor even if we don't use blazor
87
98
const cfg = await ( await fetch ( '_framework/blazor.boot.json' ) ) . json ( ) ;
99
+ // dotnet js doesn't cache dlls, so we again use our download mechanism to cache them.
100
+ // also, there is multiple way to structure this config file, because why not.
101
+ // and the way msbuild generate this file, don't allow to use the buffer trick explained in cache.ts.
102
+ // so I recreate the config file, using this config structure that i understand.
88
103
console . log ( cfg ) ;
89
- const assets : unknown [ ] = Object . keys ( cfg . resources . assembly ) . map (
104
+ const assets : unknown [ ] = Object . keys ( cfg . resources . assembly ) . map ( // this rewrite the msbuild generated config into the config structure i use.
90
105
s => {
91
106
return {
92
107
'behavior' : 'assembly' ,
@@ -98,6 +113,11 @@ export async function initDotnetWorkers(initCode: string) {
98
113
'behavior' : 'dotnetwasm' ,
99
114
'name' : 'dotnet.native.wasm'
100
115
} ) ;
116
+
117
+ // if i remember correctly, the file structure they use have dedicated fields to specify theses 2 files
118
+ // since we don't use their file strucutre, we have to add them manually.
119
+ // Luckily, we can just pull the value from the msbuild generated file.
120
+
101
121
jsModuleNativeName = Object . keys ( cfg [ 'resources' ] [ 'jsModuleNative' ] ) [ 0 ] ;
102
122
assets . unshift ( {
103
123
name : jsModuleNativeName ,
0 commit comments