Skip to content

Commit

Permalink
use running-status-changed
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmaddock committed Nov 1, 2024
1 parent c766cfd commit 082dab0
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions packages/electron-chrome-extensions/src/browser/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,24 @@ class RoutingDelegate {
addObserver(observer: RoutingDelegateObserver) {
this.sessionMap.set(observer.session, observer)

// TODO(mv3): version-activated doesn't seem to always run before the
// version has started evaluating the script. using version-updated to
// catch the 'starting' lifecycle.
const maybeListenForWorkerEvents = ({ versionId }: any) => {
const worker = (observer.session as any).serviceWorkers.fromVersionID(versionId)
if (worker && worker.scope.startsWith('chrome-extension://') && !this.workers.has(worker)) {
debug(`listening to worker [versionId:${versionId}, scope:${worker.scope}]`)
this.workers.add(worker)
worker.ipc.handle('crx-msg', this.onRouterMessage)
worker.ipc.handle('crx-msg-remote', this.onRemoteMessage)
worker.ipc.on('crx-add-listener', this.onAddListener)
worker.ipc.on('crx-remove-listener', this.onRemoveListener)
// TODO(mv3): remove anys
const maybeListenForWorkerEvents = ({ runningStatus, versionId }: any) => {
if (runningStatus !== 'starting') return

const serviceWorker = (observer.session as any).serviceWorkers.fromVersionID(versionId)
if (
serviceWorker?.scope?.startsWith('chrome-extension://') &&
!this.workers.has(serviceWorker)
) {
debug(`listening to service worker [versionId:${versionId}, scope:${serviceWorker.scope}]`)
this.workers.add(serviceWorker)
serviceWorker.ipc.handle('crx-msg', this.onRouterMessage)
serviceWorker.ipc.handle('crx-msg-remote', this.onRemoteMessage)
serviceWorker.ipc.on('crx-add-listener', this.onAddListener)
serviceWorker.ipc.on('crx-remove-listener', this.onRemoveListener)
}
}
observer.session.serviceWorkers.on('version-updated' as any, maybeListenForWorkerEvents)
observer.session.serviceWorkers.on('version-activated' as any, maybeListenForWorkerEvents)
observer.session.serviceWorkers.on('running-status-changed' as any, maybeListenForWorkerEvents)
}

private onRouterMessage = async (
Expand Down Expand Up @@ -207,11 +209,13 @@ export class ExtensionRouter {
}
})

session.serviceWorkers.on('version-activated' as any, ({ versionId }: any) => {
const worker = (session as any).serviceWorkers.fromVersionID(versionId)
if (worker) {
debug(`storing reference to background worker [url:'${worker.scope}']`)
this.extensionWorkers.add(worker)
session.serviceWorkers.on('running-status-changed' as any, ({ runningStatus, versionId }: any) => {
if (runningStatus !== 'starting') return

const serviceWorker = (session as any).serviceWorkers.fromVersionID(versionId)
if (serviceWorker) {
debug(`storing reference to background service worker [url:'${serviceWorker.scope}']`)
this.extensionWorkers.add(serviceWorker)
}
})
}
Expand Down Expand Up @@ -363,27 +367,27 @@ export class ExtensionRouter {
return
}

let sentCount = 0;
let sentCount = 0
for (const { host } of eventListeners) {
const ipcName = `crx-${eventName}`
const send = () => {
if (host.isDestroyed()) {
console.error(`Unable to send '${eventName}' to extension host for ${extensionId}`)
return;
return
}
host.send(ipcName, ...args);
host.send(ipcName, ...args)
}

if (host.constructor.name === 'ServiceWorkerMain') {
host.startWorker().then(send);
host.startWorker().then(send)
} else {
send();
send()
}

sentCount++;
sentCount++
}

debug(`sent '${eventName}' event to ${sentCount} listeners`);
debug(`sent '${eventName}' event to ${sentCount} listeners`)
}

/** Broadcasts extension event to all extension hosts listening for it. */
Expand Down

0 comments on commit 082dab0

Please sign in to comment.