refactor: replace Bun.sleep with node:timers/promises sleep#18301
Conversation
Greptile SummaryThis PR is a targeted portability refactor that replaces three Key points:
Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Caller
participant workspaceEventLoop
participant Adaptor
participant sleep as sleep (node:timers/promises)
Caller->>workspaceEventLoop: startSyncing(project) — AbortSignal stop
loop while !stop.aborted
workspaceEventLoop->>Adaptor: fetch(space, "/event", { signal: stop })
alt fetch fails / non-OK / no body
workspaceEventLoop->>sleep: await sleep(1000)
sleep-->>workspaceEventLoop: resolves after 1 s
workspaceEventLoop->>workspaceEventLoop: continue (retry)
else SSE body received
workspaceEventLoop->>workspaceEventLoop: parseSSE(res.body, stop, cb)
workspaceEventLoop->>sleep: await sleep(250)
sleep-->>workspaceEventLoop: resolves after 250 ms
end
end
workspaceEventLoop-->>Caller: loop exits
Last reviewed commit: "refactor: replace Bu..." |
| const res = await adaptor.fetch(space, "/event", { method: "GET", signal: stop }).catch(() => undefined) | ||
| if (!res || !res.ok || !res.body) { | ||
| await Bun.sleep(1000) | ||
| await sleep(1000) |
There was a problem hiding this comment.
Opportunity to pass AbortSignal to sleep
Since node:timers/promises's setTimeout natively accepts an AbortSignal via its third argument (unlike Bun.sleep), this refactor is a good opportunity to make the event loop more responsive to cancellation. Without passing stop, if stop.abort() is called mid-sleep the loop can take up to 1 second to actually exit — only the next while (!stop.aborted) check ends it.
| await sleep(1000) | |
| await sleep(1000, undefined, { signal: stop }) |
Similarly the sleep(250) on line 131 could also receive the signal:
await sleep(250, undefined, { signal: stop })Note: wrapping the workspaceEventLoop call in a try/catch (already done at line 140) will naturally swallow the resulting AbortError, so no additional error handling is needed.
Summary
Bun.sleep()calls with portablesleepfromnode:timers/promisesin two files