class ServiceWorkerPerfTools {
constructor() {
this.actionDoneCallback = null;
this.actionRequired = false;
this.action = null;
this.enabled = false;
}
enable() {
this.enabled = true;
}
stopWorkers() {
return this.performAction('stop-workers');
}
quit() {
return this.performAction('quit');
}
notifyActionDone() {
if (!this.actionDoneCallback)
throw new Error('There is no pending action!');
this.actionDoneCallback();
this.actionDoneCallback = null;
this.action = null;
this.actionRequired = false;
}
performAction(action) {
if (!this.enabled) {
throw new TypeError('ServiceWorkerPerfTools is not enabled,' +
' call enable() first!');
}
if (this.actionRequired) {
throw new Error('There is already a pending action:', this.action);
}
const promise = new Promise(resolve => {
this.actionDoneCallback = resolve;
});
this.action = action;
this.actionRequired = true;
return promise;
}
}
window.serviceWorkerPerfTools = new ServiceWorkerPerfTools;