You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.6 KiB

2 weeks ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ApiProxy = void 0;
  4. const const_js_1 = require("./const.js");
  5. const time_js_1 = require("./time.js");
  6. class ApiProxy {
  7. constructor(plugin, hook) {
  8. this.target = null;
  9. this.targetQueue = [];
  10. this.onQueue = [];
  11. this.plugin = plugin;
  12. this.hook = hook;
  13. const defaultSettings = {};
  14. if (plugin.settings) {
  15. for (const id in plugin.settings) {
  16. const item = plugin.settings[id];
  17. defaultSettings[id] = item.defaultValue;
  18. }
  19. }
  20. const localSettingsSaveId = `__vue-devtools-plugin-settings__${plugin.id}`;
  21. let currentSettings = Object.assign({}, defaultSettings);
  22. try {
  23. const raw = localStorage.getItem(localSettingsSaveId);
  24. const data = JSON.parse(raw);
  25. Object.assign(currentSettings, data);
  26. }
  27. catch (e) {
  28. // noop
  29. }
  30. this.fallbacks = {
  31. getSettings() {
  32. return currentSettings;
  33. },
  34. setSettings(value) {
  35. try {
  36. localStorage.setItem(localSettingsSaveId, JSON.stringify(value));
  37. }
  38. catch (e) {
  39. // noop
  40. }
  41. currentSettings = value;
  42. },
  43. now() {
  44. return (0, time_js_1.now)();
  45. },
  46. };
  47. if (hook) {
  48. hook.on(const_js_1.HOOK_PLUGIN_SETTINGS_SET, (pluginId, value) => {
  49. if (pluginId === this.plugin.id) {
  50. this.fallbacks.setSettings(value);
  51. }
  52. });
  53. }
  54. this.proxiedOn = new Proxy({}, {
  55. get: (_target, prop) => {
  56. if (this.target) {
  57. return this.target.on[prop];
  58. }
  59. else {
  60. return (...args) => {
  61. this.onQueue.push({
  62. method: prop,
  63. args,
  64. });
  65. };
  66. }
  67. },
  68. });
  69. this.proxiedTarget = new Proxy({}, {
  70. get: (_target, prop) => {
  71. if (this.target) {
  72. return this.target[prop];
  73. }
  74. else if (prop === 'on') {
  75. return this.proxiedOn;
  76. }
  77. else if (Object.keys(this.fallbacks).includes(prop)) {
  78. return (...args) => {
  79. this.targetQueue.push({
  80. method: prop,
  81. args,
  82. resolve: () => { },
  83. });
  84. return this.fallbacks[prop](...args);
  85. };
  86. }
  87. else {
  88. return (...args) => {
  89. return new Promise((resolve) => {
  90. this.targetQueue.push({
  91. method: prop,
  92. args,
  93. resolve,
  94. });
  95. });
  96. };
  97. }
  98. },
  99. });
  100. }
  101. async setRealTarget(target) {
  102. this.target = target;
  103. for (const item of this.onQueue) {
  104. this.target.on[item.method](...item.args);
  105. }
  106. for (const item of this.targetQueue) {
  107. item.resolve(await this.target[item.method](...item.args));
  108. }
  109. }
  110. }
  111. exports.ApiProxy = ApiProxy;