workers-state.js (1665B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { 8 START_WORKER, 9 UNREGISTER_WORKER, 10 UPDATE_CAN_DEBUG_WORKERS, 11 UPDATE_WORKERS, 12 } = require("resource://devtools/client/application/src/constants.js"); 13 14 function WorkersState() { 15 return { 16 // Array of all service worker registrations 17 list: [], 18 canDebugWorkers: false, 19 }; 20 } 21 22 function buildWorkerDataFromFronts({ registration, workers }) { 23 return { 24 id: registration.id, 25 lastUpdateTime: registration.lastUpdateTime, 26 registrationFront: registration, 27 scope: registration.scope, 28 workers: workers.map(worker => ({ 29 id: worker.id, 30 url: worker.url, 31 state: worker.state, 32 stateText: worker.stateText, 33 registrationFront: registration, 34 workerDescriptorFront: worker.workerDescriptorFront, 35 })), 36 }; 37 } 38 39 function workersReducer(state = WorkersState(), action) { 40 switch (action.type) { 41 case UPDATE_CAN_DEBUG_WORKERS: { 42 return Object.assign({}, state, { 43 canDebugWorkers: action.canDebugWorkers, 44 }); 45 } 46 case UPDATE_WORKERS: { 47 const { workers } = action; 48 return Object.assign({}, state, { 49 list: workers.map(buildWorkerDataFromFronts).flat(), 50 }); 51 } 52 // these actions don't change the state, but get picked up by the 53 // telemetry middleware 54 case START_WORKER: 55 case UNREGISTER_WORKER: 56 return state; 57 default: 58 return state; 59 } 60 } 61 62 module.exports = { 63 WorkersState, 64 workersReducer, 65 };