summaryrefslogtreecommitdiffstats
path: root/devtools/client/application/src/reducers/workers-state.js
blob: 004c25ddfa846acb51fb47d2d1c5cc8c3786b946 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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,
};