(socketUrl: string)
| 417 | } |
| 418 | |
| 419 | function pingWorkerContentMain(socketUrl: string) { |
| 420 | self.addEventListener(class="st">'connect', (_event) => { |
| 421 | const event = _event as MessageEvent |
| 422 | const port = event.ports[0] |
| 423 | |
| 424 | if (!socketUrl) { |
| 425 | port.postMessage({ |
| 426 | type: class="st">'error', |
| 427 | error: new Error(class="st">'socketUrl not found'), |
| 428 | }) |
| 429 | return |
| 430 | } |
| 431 | |
| 432 | const visibilityManager: VisibilityManager = { |
| 433 | currentState: class="st">'visible', |
| 434 | listeners: new Set(), |
| 435 | } |
| 436 | port.addEventListener(class="st">'message', (event) => { |
| 437 | const { visibility } = event.data |
| 438 | visibilityManager.currentState = visibility |
| 439 | console.debug(class="st">'[vite] new window visibility', visibility) |
| 440 | for (const listener of visibilityManager.listeners) { |
| 441 | listener(visibility) |
| 442 | } |
| 443 | }) |
| 444 | port.start() |
| 445 | |
| 446 | console.debug(class="st">'[vite] connected from window') |
| 447 | waitForSuccessfulPingInternal(socketUrl, visibilityManager).then( |
| 448 | () => { |
| 449 | console.debug(class="st">'[vite] ping successful') |
| 450 | try { |
| 451 | port.postMessage({ type: class="st">'success' }) |
| 452 | } catch (error) { |
| 453 | port.postMessage({ type: class="st">'error', error }) |
| 454 | } |
| 455 | }, |
| 456 | (error) => { |
| 457 | console.debug(class="st">'[vite] error happened', error) |
| 458 | try { |
| 459 | port.postMessage({ type: class="st">'error', error }) |
| 460 | } catch (error) { |
| 461 | port.postMessage({ type: class="st">'error', error }) |
| 462 | } |
| 463 | }, |
| 464 | ) |
| 465 | }) |
| 466 | } |
| 467 | |
| 468 | async function waitForSuccessfulPingInternal( |
| 469 | socketUrl: string, |
nothing calls this directly
no test coverage detected