summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/location-store.js
blob: 96deb0a9912b99ebaaa9c92069ff2b3416147caa (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
/* 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 SOURCE_TOKEN = "<:>";

function LocationStore (store) {
  this._store = store || new Map();
}

/**
 * Method to get a promised location from the Store.
 * @param location
 * @returns Promise<Object>
 */
LocationStore.prototype.get = function (location) {
  this._safeAccessInit(location.url);
  return this._store.get(location.url).get(location);
};

/**
 * Method to set a promised location to the Store
 * @param location
 * @param promisedLocation
 */
LocationStore.prototype.set = function (location, promisedLocation = null) {
  this._safeAccessInit(location.url);
  this._store.get(location.url).set(serialize(location), promisedLocation);
};

/**
 * Utility method to verify if key exists in Store before accessing it.
 * If not, initializing it.
 * @param url
 * @private
 */
LocationStore.prototype._safeAccessInit = function (url) {
  if (!this._store.has(url)) {
    this._store.set(url, new Map());
  }
};

/**
 * Utility proxy method to Map.clear() method
 */
LocationStore.prototype.clear = function () {
  this._store.clear();
};

/**
 * Retrieves an object containing all locations to be resolved when `source-updated`
 * event is triggered.
 * @param url
 * @returns {Array<String>}
 */
LocationStore.prototype.getByURL = function (url){
  if (this._store.has(url)) {
    return [...this._store.get(url).keys()];
  }
  return [];
};

/**
 * Invalidates the stale location promises from the store when `source-updated`
 * event is triggered, and when FrameView unsubscribes from a location.
 * @param url
 */
LocationStore.prototype.clearByURL = function (url) {
  this._safeAccessInit(url);
  this._store.set(url, new Map());
};

exports.LocationStore = LocationStore;
exports.serialize = serialize;
exports.deserialize = deserialize;

/**
 * Utility method to serialize the source
 * @param source
 * @returns {string}
 */
function serialize(source) {
  let { url, line, column } = source;
  line = line || 0;
  column = column || 0;
  return `${url}${SOURCE_TOKEN}${line}${SOURCE_TOKEN}${column}`;
};

/**
 * Utility method to serialize the source
 * @param source
 * @returns Object
 */
function deserialize(source) {
  let [ url, line, column ] = source.split(SOURCE_TOKEN);
  line = parseInt(line);
  column = parseInt(column);
  if (column === 0) {
    return { url, line };
  }
  return { url, line, column };
};