From cf3f955f0a9b74b98fe76134d9d68ef9bdf9ea70 Mon Sep 17 00:00:00 2001 From: tophf Date: Wed, 8 Jan 2025 09:21:35 +0300 Subject: [PATCH] clear `initializing` to actually reconnect port --- src/js/port.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/js/port.js b/src/js/port.js index 402e9e6a74..75af759494 100644 --- a/src/js/port.js +++ b/src/js/port.js @@ -74,10 +74,13 @@ export function createPortExec(getTarget, {lock, once} = {}, target) { const ctx = {stack: new Error().stack}; const promise = new Promise((resolve, reject) => (ctx.rr = [resolve, reject])); + // Re-connect if disconnected unless already initializing if (!(exec[CONNECTED] = ref ? !!ref.deref() : !!port) && !initializing) initializing = initPort(); - if (!exec[CONNECTED]) // when initPort didn't complete synchronously - initializing = await initializing; + // If initPort didn't await inside, CONNECTED is true and we immediately clear `initializing`, + // otherwise we'll await first in this exec() and in the overlapped subsequent exec(s). + if (initializing) + initializing = !exec[CONNECTED] && await initializing; (once ? target : port || ref.deref()).postMessage( {args, id: ++lastId},