diff options
Diffstat (limited to 'devtools/client/application/src/reducers/workers-state.js')
-rw-r--r-- | devtools/client/application/src/reducers/workers-state.js | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/devtools/client/application/src/reducers/workers-state.js b/devtools/client/application/src/reducers/workers-state.js new file mode 100644 index 0000000000..004c25ddfa --- /dev/null +++ b/devtools/client/application/src/reducers/workers-state.js @@ -0,0 +1,65 @@ +/* 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 { + START_WORKER, + UNREGISTER_WORKER, + UPDATE_CAN_DEBUG_WORKERS, + UPDATE_WORKERS, +} = require("resource://devtools/client/application/src/constants.js"); + +function WorkersState() { + return { + // Array of all service worker registrations + list: [], + canDebugWorkers: false, + }; +} + +function buildWorkerDataFromFronts({ registration, workers }) { + return { + id: registration.id, + lastUpdateTime: registration.lastUpdateTime, + registrationFront: registration, + scope: registration.scope, + workers: workers.map(worker => ({ + id: worker.id, + url: worker.url, + state: worker.state, + stateText: worker.stateText, + registrationFront: registration, + workerDescriptorFront: worker.workerDescriptorFront, + })), + }; +} + +function workersReducer(state = WorkersState(), action) { + switch (action.type) { + case UPDATE_CAN_DEBUG_WORKERS: { + return Object.assign({}, state, { + canDebugWorkers: action.canDebugWorkers, + }); + } + case UPDATE_WORKERS: { + const { workers } = action; + return Object.assign({}, state, { + list: workers.map(buildWorkerDataFromFronts).flat(), + }); + } + // these actions don't change the state, but get picked up by the + // telemetry middleware + case START_WORKER: + case UNREGISTER_WORKER: + return state; + default: + return state; + } +} + +module.exports = { + WorkersState, + workersReducer, +}; |