fix(extension): await init in onMessage to fix lost click after SW sleeps (MV3 cold-start race)#24
Open
thiagomendonca-eu wants to merge 1 commit into
Conversation
…eeps The MV3 service worker is terminated when idle (the WebSocket idle timer disconnects after 10s, removing the last keep-alive). The next Option+Click wakes the SW via runtime.sendMessage, but the onMessage handler used elementSender / currentConfig directly while initialize() was still running its async config load. On a cold start the handler fires before init completes, so elementSender is undefined and the click is silently lost — users had to open the SW console (which keeps it alive) to make it work. Make initialization idempotent (ensureInitialized) and await it inside the listener before sending. The listener stays synchronous and returns true to keep the channel open for the async sendResponse, per MV3 messaging guidance. Fixes etsd-tech#19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes the MV3 service-worker cold-start race that drops
Option+Clickselections after the worker goes idle.Closes #19
Problem
ElementSenderServicedisconnects its WebSocket after 10s of inactivity, removing the last thing keeping the service worker alive, so Chrome terminates the SW. The nextOption+Clickwakes it viaruntime.sendMessage, but theonMessagehandler usedelementSender/currentConfigdirectly while the top-levelinitialize()was still awaiting the async config load:On a cold start the message arrives before
initialize()resolves, soelementSenderisundefined→TypeError→ the click is silently lost. This matches the diagnosis already in #19 ("initialize() hasn't completed when the message handler fires"). In practice, users had to keep the SW console open (which keeps the worker alive) for pointing to work.Fix
Make initialization idempotent (
ensureInitialized()returns a cached promise) andawaitit inside the listener before usingelementSender/currentConfig. Following MV3 messaging guidance, the listener stays synchronous, the async work runs in an inner IIFE, and it returnstrueto keep the channel open forsendResponse.Testing
inactive, the firstOption+Clickwas lost andget-pointed-elementreturned stale/empty data — unless the SW console was kept open.inactive, closed all devtools, thenOption+Click→ element was sent andget-pointed-elementreturned it immediately. No console needed.Single-file change; the listener contract (synchronous +
return true) is preserved.