Skip to content

Instantly share code, notes, and snippets.

@ochameau
Created December 11, 2018 16:17
Show Gist options
  • Save ochameau/f77dc017d8b308d1a2c1001fa7fc6f98 to your computer and use it in GitHub Desktop.
Save ochameau/f77dc017d8b308d1a2c1001fa7fc6f98 to your computer and use it in GitHub Desktop.
get all page workers thread clients
// parent here is the toolbox target front
// so it depends on what toolbox you are running from
// if regular web toolbox, it will be BrowsingContextTargetFront()
// if worker toolbox, WorkerTargetFront()
// if old xul addon toolbox, AddonTargetFront()
// if webextension toolbox, WebExtensionTargetFront()
// if browser content toolbox, ContentProcessTargetFront()
// if browser toolbox, BrowsingContextTargetFront()
//
// As of today, this is *FRONTS* not a target object
// I'm actively working to merge Target class (devtools/client/framework/target.js)
// and target fronts by using some inheritance.
// So that they all inherit from a base class which we can safely assume a couple
// of base method will exists. Today, they don't and you may have unexepected difference...
const parentTargetFront = threadClient._parent;
async function updateWorkers() {
const threadClients = [];
const { workers } = await parentTargetFront.listWorkers();
for (const workerTargetFront of workers) {
// You have to attach first in order to retriev the thread actor id (at least for workers...)
await workerTargetFront.attach();
// For workerTargetFront, this is how you retrieve the ThreadClient, but that may be different for other target fronts
// again... Target base class will simplify this.
const [, threadClient] = await workerTargetFront.attachThread();
console.log("thread client", threadClient.actor);
threadClients.push(threadClient);
}
return threadClients;
}
parentTargetFront.on("workerListChanged", updateWorkers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment