MCPcopy
hub / github.com/vitejs/vite / waitForSuccessfulPing

Function waitForSuccessfulPing

packages/vite/src/client/client.ts:360–412  ·  view source on GitHub ↗
(socketUrl: string)

Source from the content-addressed store, hash-verified

358}
359
360function waitForSuccessfulPing(socketUrl: string) {
361 if (typeof SharedWorker === 'undefined') {
362 const visibilityManager: VisibilityManager = {
363 currentState: document.visibilityState,
364 listeners: new Set(),
365 }
366 const onVisibilityChange = () => {
367 visibilityManager.currentState = document.visibilityState
368 for (const listener of visibilityManager.listeners) {
369 listener(visibilityManager.currentState)
370 }
371 }
372 document.addEventListener('visibilitychange', onVisibilityChange)
373 return waitForSuccessfulPingInternal(socketUrl, visibilityManager)
374 }
375
376 // needs to be inlined to
377 // - load the worker after the server is closed
378 // - make it work with backend integrations
379 const blob = new Blob(
380 [
381 '"use strict";',
382 `const waitForSuccessfulPingInternal = ${waitForSuccessfulPingInternal.toString()};`,
383 `const fn = ${pingWorkerContentMain.toString()};`,
384 `fn(${JSON.stringify(socketUrl)})`,
385 ],
386 { type: 'application/javascript' },
387 )
388 const objURL = URL.createObjectURL(blob)
389 const sharedWorker = new SharedWorker(objURL)
390 return new Promise<void>((resolve, reject) => {
391 const onVisibilityChange = () => {
392 sharedWorker.port.postMessage({ visibility: document.visibilityState })
393 }
394 document.addEventListener('visibilitychange', onVisibilityChange)
395
396 sharedWorker.port.addEventListener('message', (event) => {
397 document.removeEventListener('visibilitychange', onVisibilityChange)
398 sharedWorker.port.close()
399
400 const data: { type: 'success' } | { type: 'error'; error: Error } =
401 event.data
402 if (data.type === 'error') {
403 reject(data.error)
404 return
405 }
406 resolve()
407 })
408
409 onVisibilityChange()
410 sharedWorker.port.start()
411 })
412}
413
414type VisibilityManager = {
415 currentState: DocumentVisibilityState

Callers 1

handleMessageFunction · 0.85

Calls 4

onVisibilityChangeFunction · 0.85
closeMethod · 0.65
resolveFunction · 0.50

Tested by

no test coverage detected