source-map.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // @flow
  2. const {
  3. originalToGeneratedId,
  4. generatedToOriginalId,
  5. isGeneratedId,
  6. isOriginalId
  7. } = require("./source-map-util");
  8. function workerTask(worker, method) {
  9. return function(...args: any) {
  10. return new Promise((resolve, reject) => {
  11. const id = msgId++;
  12. worker.postMessage({ id, method, args });
  13. const listener = ({ data: result }) => {
  14. if (result.id !== id) {
  15. return;
  16. }
  17. worker.removeEventListener("message", listener);
  18. if (result.error) {
  19. reject(result.error);
  20. } else {
  21. resolve(result.response);
  22. }
  23. };
  24. worker.addEventListener("message", listener);
  25. });
  26. };
  27. }
  28. let sourceMapWorker;
  29. function restartWorker() {
  30. if (sourceMapWorker) {
  31. sourceMapWorker.terminate();
  32. }
  33. sourceMapWorker = new Worker(
  34. "resource://devtools/client/framework/source-map-worker.js"
  35. );
  36. if (Services.prefs.getBoolPref("devtools.debugger.client-source-maps-enabled")) {
  37. sourceMapWorker.postMessage({ id: 0, method: "enableSourceMaps" });
  38. }
  39. }
  40. restartWorker();
  41. function destroyWorker() {
  42. if (sourceMapWorker) {
  43. sourceMapWorker.terminate();
  44. sourceMapWorker = null;
  45. }
  46. }
  47. function shouldSourceMap() {
  48. return Services.prefs.getBoolPref("devtools.debugger.client-source-maps-enabled");
  49. }
  50. const getOriginalURLs = workerTask(sourceMapWorker, "getOriginalURLs");
  51. const getGeneratedLocation = workerTask(sourceMapWorker,
  52. "getGeneratedLocation");
  53. const getOriginalLocation = workerTask(sourceMapWorker,
  54. "getOriginalLocation");
  55. const getOriginalSourceText = workerTask(sourceMapWorker,
  56. "getOriginalSourceText");
  57. const applySourceMap = workerTask(sourceMapWorker, "applySourceMap");
  58. const clearSourceMaps = workerTask(sourceMapWorker, "clearSourceMaps");
  59. module.exports = {
  60. originalToGeneratedId,
  61. generatedToOriginalId,
  62. isGeneratedId,
  63. isOriginalId,
  64. getOriginalURLs,
  65. getGeneratedLocation,
  66. getOriginalLocation,
  67. getOriginalSourceText,
  68. applySourceMap,
  69. clearSourceMaps,
  70. destroyWorker,
  71. shouldSourceMap
  72. };