summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/watcher/WatchedDataHelpers.jsm
blob: 9acd5a612c1c87f24c86546d1b5889201ff7b4ea (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* 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";

/**
 * Helper module alongside WatcherRegistry, which focus on updating the "watchedData" object.
 * This object is shared across processes and threads and have to be maintained in all these runtimes.
 */

var EXPORTED_SYMBOLS = ["WatchedDataHelpers"];

// List of all arrays stored in `watchedData`, which are replicated across processes and threads
const SUPPORTED_DATA = {
  BREAKPOINTS: "breakpoints",
  RESOURCES: "resources",
  TARGETS: "targets",
};

// Optional function, if data isn't a primitive data type in order to produce a key
// for the given data entry
const DATA_KEY_FUNCTION = {
  [SUPPORTED_DATA.BREAKPOINTS]: function({
    location: { sourceUrl, sourceId, line, column },
  }) {
    if (!sourceUrl && !sourceId) {
      throw new Error(
        `Breakpoints expect to have either a sourceUrl or a sourceId.`
      );
    }
    if (sourceUrl && typeof sourceUrl != "string") {
      throw new Error(
        `Breakpoints expect to have sourceUrl string, got ${typeof sourceUrl} instead.`
      );
    }
    // sourceId may be undefined for some sources keyed by URL
    if (sourceId && typeof sourceId != "string") {
      throw new Error(
        `Breakpoints expect to have sourceId string, got ${typeof sourceId} instead.`
      );
    }
    if (typeof line != "number") {
      throw new Error(
        `Breakpoints expect to have line number, got ${typeof line} instead.`
      );
    }
    if (typeof column != "number") {
      throw new Error(
        `Breakpoints expect to have column number, got ${typeof column} instead.`
      );
    }
    return `${sourceUrl}:${sourceId}:${line}:${column}`;
  },
};

function idFunction(v) {
  if (typeof v != "string") {
    throw new Error(
      `Expect data entry values to be string, or be using custom data key functions. Got ${typeof v} type instead.`
    );
  }
  return v;
}

const WatchedDataHelpers = {
  SUPPORTED_DATA,

  /**
   * Add new values to the shared "watchedData" object.
   *
   * @param Object watchedData
   *               The data object to update.
   * @param string type
   *               The type of data to be added
   * @param Array<Object> entries
   *               The values to be added to this type of data
   */
  addWatchedDataEntry(watchedData, type, entries) {
    const toBeAdded = [];
    const keyFunction = DATA_KEY_FUNCTION[type] || idFunction;
    for (const entry of entries) {
      const alreadyExists = watchedData[type].some(existingEntry => {
        return keyFunction(existingEntry) === keyFunction(entry);
      });
      if (!alreadyExists) {
        toBeAdded.push(entry);
      }
    }
    watchedData[type].push(...toBeAdded);
  },

  /**
   * Remove values from the shared "watchedData" object.
   *
   * @param Object watchedData
   *               The data object to update.
   * @param string type
   *               The type of data to be remove
   * @param Array<Object> entries
   *               The values to be removed from this type of data
   * @return Boolean
   *               True, if at least one entries existed and has been removed.
   *               False, if none of the entries existed and none has been removed.
   */
  removeWatchedDataEntry(watchedData, type, entries) {
    let includesAtLeastOne = false;
    const keyFunction = DATA_KEY_FUNCTION[type] || idFunction;
    for (const entry of entries) {
      const idx = watchedData[type].findIndex(existingEntry => {
        return keyFunction(existingEntry) === keyFunction(entry);
      });
      if (idx !== -1) {
        watchedData[type].splice(idx, 1);
        includesAtLeastOne = true;
      }
    }
    if (!includesAtLeastOne) {
      return false;
    }

    return true;
  },
};