Messages
Which API to use
Use browser.* in all code. WXT 0.21 does not include webextension-polyfill. wxt/browser
gives globalThis.browser on Firefox and globalThis.chrome on Chromium, and WXT imports it
automatically. Each API that Gopher calls gives a promise on both browsers.
There is one important difference. A listener for runtime.onMessage must call sendResponse
and then return true when the reply comes later. If the listener returns a promise, Chromium
does not send the reply and gives no error.
Sequence
- The panel reads the cache with
useLiveQuery. This is a subscription, thus the panel does not read the cache again and again. - If the panel needs new data, it sends a message to the service worker. It never sends a
message to the runner. This is necessary because the active tab is frequently not a
patreon.comtab. In that condition there is no runner, and a direct message stops. - The service worker finds the runner, or adds it (see the service worker). The runner gets the data, normalizes it, and replies.
- The service worker writes the cache. The subscription of the panel then shows the new data.
- No component must be open for a different component to operate. The runner needs a listener in the service worker, and the worker starts when it receives a message.
Dexie in the extension origin is the one source of truth for entity data. The cache keeps the
result of each page, thus a second visit to the explorer does not get the data again. The
temporary state of the service worker is in browser.storage.session. That storage stays after
the worker stops, and the browser deletes it when the user closes the browser. This behaviour is
correct for a temporary limit.