Created
December 14, 2018 15:57
-
-
Save juliandescottes/e19e6761faa82480025f4f7b89c3f859 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This Source Code Form is subject to the terms of the Mozilla Public | |
* License, v. 2.0. If a copy of the MPL was not distributed with this | |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
"use strict"; | |
const { | |
DEBUG_TARGETS, | |
REQUEST_WORKERS_SUCCESS, | |
SERVICE_WORKER_FETCH_STATES, | |
SERVICE_WORKER_STATUSES, | |
} = require("../constants"); | |
/** | |
* This middleware converts workers object that get from DebuggerClient.listAllWorkers() | |
* to data which is used in DebugTargetItem. | |
*/ | |
const workerComponentDataMiddleware = store => next => action => { | |
switch (action.type) { | |
case REQUEST_WORKERS_SUCCESS: { | |
action.otherWorkers = toComponentData(action.otherWorkers); | |
action.serviceWorkers = toComponentData(action.serviceWorkers, true); | |
action.sharedWorkers = toComponentData(action.sharedWorkers); | |
break; | |
} | |
} | |
return next(action); | |
}; | |
function getServiceWorkerStatus(isActive, isRunning) { | |
if (isActive && isRunning) { | |
return SERVICE_WORKER_STATUSES.RUNNING; | |
} else if (isActive) { | |
return SERVICE_WORKER_STATUSES.STOPPED; | |
} | |
// We cannot get service worker registrations unless the registration is in | |
// ACTIVE state. Unable to know the actual state ("installing", "waiting"), we | |
// display a custom state "registering" for now. See Bug 1153292. | |
return SERVICE_WORKER_STATUSES.REGISTERING; | |
} | |
function getServiceWorkerDetails(worker) { | |
const isActive = worker.active; | |
const isRunning = !!worker.workerTargetFront; | |
return { | |
fetch: worker.fetch ? SERVICE_WORKER_FETCH_STATES.LISTENING | |
: SERVICE_WORKER_FETCH_STATES.NOT_LISTENING, | |
isActive, | |
isRunning, | |
registrationActor: worker.registrationActor, | |
scope: worker.scope, | |
status: getServiceWorkerStatus(isActive, isRunning), | |
subscription: worker.subscription, | |
}; | |
} | |
function toComponentData(workers, isServiceWorker) { | |
return workers.map(worker => { | |
// Here `worker` is the worker object created by RootFront.listAllWorkers | |
const type = DEBUG_TARGETS.WORKER; | |
const icon = "chrome://devtools/skin/images/debugging-workers.svg"; | |
const { name, registrationActor, workerTargetFront } = worker; | |
// For registering service workers, workerTargetFront will not be available. | |
// The only valid identifier we can use at that point is the actorID for the | |
// service worker registration. | |
const id = workerTargetFront ? workerTargetFront.actorID : registrationActor; | |
return { | |
details: isServiceWorker ? getServiceWorkerDetails(worker) : {}, | |
icon, | |
id, | |
name, | |
type, | |
}; | |
}); | |
} | |
module.exports = workerComponentDataMiddleware; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment