location-store.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "use strict";
  5. const SOURCE_TOKEN = "<:>";
  6. function LocationStore (store) {
  7. this._store = store || new Map();
  8. }
  9. /**
  10. * Method to get a promised location from the Store.
  11. * @param location
  12. * @returns Promise<Object>
  13. */
  14. LocationStore.prototype.get = function (location) {
  15. this._safeAccessInit(location.url);
  16. return this._store.get(location.url).get(location);
  17. };
  18. /**
  19. * Method to set a promised location to the Store
  20. * @param location
  21. * @param promisedLocation
  22. */
  23. LocationStore.prototype.set = function (location, promisedLocation = null) {
  24. this._safeAccessInit(location.url);
  25. this._store.get(location.url).set(serialize(location), promisedLocation);
  26. };
  27. /**
  28. * Utility method to verify if key exists in Store before accessing it.
  29. * If not, initializing it.
  30. * @param url
  31. * @private
  32. */
  33. LocationStore.prototype._safeAccessInit = function (url) {
  34. if (!this._store.has(url)) {
  35. this._store.set(url, new Map());
  36. }
  37. };
  38. /**
  39. * Utility proxy method to Map.clear() method
  40. */
  41. LocationStore.prototype.clear = function () {
  42. this._store.clear();
  43. };
  44. /**
  45. * Retrieves an object containing all locations to be resolved when `source-updated`
  46. * event is triggered.
  47. * @param url
  48. * @returns {Array<String>}
  49. */
  50. LocationStore.prototype.getByURL = function (url){
  51. if (this._store.has(url)) {
  52. return [...this._store.get(url).keys()];
  53. }
  54. return [];
  55. };
  56. /**
  57. * Invalidates the stale location promises from the store when `source-updated`
  58. * event is triggered, and when FrameView unsubscribes from a location.
  59. * @param url
  60. */
  61. LocationStore.prototype.clearByURL = function (url) {
  62. this._safeAccessInit(url);
  63. this._store.set(url, new Map());
  64. };
  65. exports.LocationStore = LocationStore;
  66. exports.serialize = serialize;
  67. exports.deserialize = deserialize;
  68. /**
  69. * Utility method to serialize the source
  70. * @param source
  71. * @returns {string}
  72. */
  73. function serialize(source) {
  74. let { url, line, column } = source;
  75. line = line || 0;
  76. column = column || 0;
  77. return `${url}${SOURCE_TOKEN}${line}${SOURCE_TOKEN}${column}`;
  78. };
  79. /**
  80. * Utility method to serialize the source
  81. * @param source
  82. * @returns Object
  83. */
  84. function deserialize(source) {
  85. let [ url, line, column ] = source.split(SOURCE_TOKEN);
  86. line = parseInt(line);
  87. column = parseInt(column);
  88. if (column === 0) {
  89. return { url, line };
  90. }
  91. return { url, line, column };
  92. };