Skip to content

Commit

Permalink
clear initializing to actually reconnect port
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed Jan 8, 2025
1 parent ebed94c commit cf3f955
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/js/port.js
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit cf3f955

Please sign in to comment.