Skip to content

Faster preview on no change, future hot reload#192

Merged
ScarletKuro merged 15 commits intomainfrom
hot-reaload
Apr 10, 2026
Merged

Faster preview on no change, future hot reload#192
ScarletKuro merged 15 commits intomainfrom
hot-reaload

Conversation

@ScarletKuro
Copy link
Copy Markdown
Member

@ScarletKuro ScarletKuro commented Feb 25, 2026

Fixes #198

Previously, every compilation triggered a full iframe navigation, even when the code hadn’t changed.

Now, if there’s no code change, the preview updates instantly:
trymud
(you need to run it with no debugger).

This code also gives some future improvements, once web workers are shipped: dotnet/aspnetcore#65037

CompilationService could run on a worker thread. The pipeline would become:

Editor onChange (debounced ~300ms)
→ Worker thread: CompilationService.CompileAsync()
→ postMessage(dllBytes) back to main thread
→ updateUserComponentsDll + hotReloadIframe
→ iframe: AssemblyLoadContext swap
→ DynamicComponent re-renders

Everything from updateUserComponentsDll downward is already in place. The only missing piece is moving compilation
off-thread.

Rough prototype shape

  // main.js - spawn worker on init
  const compilationWorker = new Worker('compilation-worker.js');

  compilationWorker.onmessage = (e) => {
      if (e.data.type === 'compiled') {
          Try.CodeExecution.updateUserComponentsDll(e.data.dllBytes);
          Try.CodeExecution.hotReloadIframe('user-page-window', '/__main');
      }
  };

  // On editor change (debounced)
  editor.onDidChangeModelContent(debounce(() => {
      compilationWorker.postMessage({ type: 'compile', files: getCurrentFiles() });
  }, 300));

  // compilation-worker.js
  importScripts('dotnet.js'); // hypothetical - load .NET runtime in worker

  onmessage = async (e) => {
      if (e.data.type === 'compile') {
          const dllBytes = await dotnetRuntime.invokeAsync(
              'CompilationService.CompileAsync', e.data.files);
          postMessage({ type: 'compiled', dllBytes });
      }
  };

This comment was marked as resolved.

@ScarletKuro
Copy link
Copy Markdown
Member Author

I will merge this in few days if nobody else wants to test this manually.
P.S. ignore the failing CI

Copy link
Copy Markdown
Contributor

@versile2 versile2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have loaded and ran the sample, it ran without issue. I did not look at deep detail of the code as it's so far from dev due to other updates. If you would like me to let me know and I should have time early next week. That being said it seems to work great, recognizes when code has changed etc. One thing I would note is that only the "hot reload" should use that logic, I should be able to click "Play" and cause a relaod so I can handle or test stuff that happens only onafterrender oninit.

@sonarqubecloud
Copy link
Copy Markdown

@ScarletKuro ScarletKuro merged commit 8372b35 into main Apr 10, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't use System.Linq Queryable extension methods

3 participants