clientInstrumentation.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createInstrumentation = createInstrumentation;
  6. /**
  7. * Copyright (c) Microsoft Corporation. All rights reserved.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. function createInstrumentation() {
  22. const listeners = [];
  23. return new Proxy({}, {
  24. get: (obj, prop) => {
  25. if (typeof prop !== 'string') return obj[prop];
  26. if (prop === 'addListener') return listener => listeners.push(listener);
  27. if (prop === 'removeListener') return listener => listeners.splice(listeners.indexOf(listener), 1);
  28. if (prop === 'removeAllListeners') return () => listeners.splice(0, listeners.length);
  29. if (!prop.startsWith('on')) return obj[prop];
  30. return async (...params) => {
  31. for (const listener of listeners) {
  32. var _prop, _ref;
  33. await ((_prop = (_ref = listener)[prop]) === null || _prop === void 0 ? void 0 : _prop.call(_ref, ...params));
  34. }
  35. };
  36. }
  37. });
  38. }