All files / vscode-extension/src/preview bootstrap.tsx

100% Statements 10/10
100% Branches 2/2
100% Functions 2/2
100% Lines 9/9

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                    2x 2x 1x 1x   1x   2x   2x     2x 2x    
import { createRoot, type Root } from 'react-dom/client';
import { flushSync } from 'react-dom';
import { App } from './App.js';
 
export interface PreviewHostApi {
  postMessage(message: unknown): void;
}
 
/** A running extension host can still produce the pre-React HTML after a VSIX update. */
export function mountPreview(api: PreviewHostApi): Root {
  let container = document.getElementById('root');
  if (!container) {
    container = document.createElement('div');
    container.id = 'root';
    // Dispose the legacy viewer and loading message rather than leaving a second canvas behind.
    document.body.replaceChildren(container);
  }
  const root = createRoot(container);
  // Install App's message listeners before the host can synchronously send the initial file.
  flushSync(() => root.render(<App />));
  // VS Code's acquired API posts only to this extension host.
  // oxlint-disable-next-line unicorn/require-post-message-target-origin
  api.postMessage({ type: 'ready' });
  return root;
}