function waitForState(worker, state, context) { returnnew Promise(resolve => { function onStateChange() { if (worker.state === state) {
worker.removeEventListener("statechange", onStateChange);
resolve(context);
}
}
// First add an event listener, so we won't miss any change that happens // before we check the current state.
worker.addEventListener("statechange", onStateChange);
// Now check if the worker is already in the desired state.
onStateChange();
});
}
/** *Helperforbrowserteststoissueregistercallsfromthecontentglobaland *waitfortheSWtoprogresstotheactivestate,asmosttestsdesire. *FromtheContentTask.spawn,usevia *`content.wrappedJSObject.registerAndWaitForActive`.
*/
async function registerAndWaitForActive(script, maybeScope) {
console.log("...calling register");
let opts = undefined; if (maybeScope) {
opts = { scope: maybeScope };
} const reg = await navigator.serviceWorker.register(script, opts); // Unless registration resurrection happens, the SW should be in the // installing slot.
console.log("...waiting for activation");
await waitForState(reg.installing, "activated", reg);
console.log("...activated!"); return reg;
}
async function fillStorage(cacheBytes, idbBytes) { // ## Fill Cache API Storage const cache = await caches.open("filler");
await cache.put("fill", new Response(makeRandomBlob(cacheBytes)));
// This method should ideally be called during a setup phase of the test to make // sure our BroadcastChannel is fully connected before anything that could cause // something to send a message to the channel can happen. Because IPC ordering // is more predictable these days (single channel per process pair), this is // primarily an issue of: // - Helping you not have to worry about there being a race here at all. // - Potentially be able to refactor this to run on the WPT infrastructure in // the future which likely cannot provide the same ordering guarantees. function setupMessagingChannel(name) { if (messagingChannels[name]) { return;
}
messagingChannels[name] = new BroadcastChannel(name);
}
function waitForBroadcastMessage(channelName, messageToWaitFor) { if (!messagingChannels[channelName]) { thrownew Error(`You forgot to call setupMessagingChannel(${channelName})`);
} returnnew Promise((resolve, reject) => { const channel = messagingChannels[channelName]; const listener = evt => { // Add `--setpref="devtools.console.stdout.content=true"` to your mach // invocation to get this to stdout for extra debugging.
console.log("Helper seeing message", evt.data, "on channel", channelName); if (evt.data === messageToWaitFor) {
resolve();
channel.removeEventListener("message", listener);
} elseif (evt.data?.error) { // Anything reporting an error means we should fail fast.
reject(evt.data);
channel.removeEventListener("message", listener);
}
};
channel.addEventListener("message", listener);
});
}
async function postMessageScopeAndWaitFor(
channelName,
scope,
messageToSend,
messageToWaitFor
) { // This will throw for us if the channel does not exist. const waitPromise = waitForBroadcastMessage(channelName, messageToWaitFor); const channel = messagingChannels[channelName];
const reg = await navigator.serviceWorker.getRegistration(scope); if (!reg) { thrownew Error(`Unable to find registration for scope: ${scope}`);
} if (!reg.active) { thrownew Error(`There is no active SW on the reg for scope: ${scope}`);
}
reg.active.postMessage(messageToSend);
await waitPromise;
}
async function broadcastAndWaitFor(
channelName,
messageToBroadcast,
messageToWaitFor
) { // This will throw for us if the channel does not exist. const waitPromise = waitForBroadcastMessage(channelName, messageToWaitFor); const channel = messagingChannels[channelName];
channel.postMessage(messageToBroadcast);
await waitPromise;
}
async function updateScopeAndWaitFor(channelName, scope, messageToWaitFor) { // This will throw for us if the channel does not exist. const waitPromise = waitForBroadcastMessage(channelName, messageToWaitFor); const channel = messagingChannels[channelName];
const reg = await navigator.serviceWorker.getRegistration(scope); if (!reg) { thrownew Error(`Unable to find registration for scope: ${scope}`);
}
reg.update();
await waitPromise;
}
Messung V0.5 in Prozent
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.11Bemerkung:
(vorverarbeitet am 2026-06-10)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.