zones.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.runWithFinally = runWithFinally;
  6. exports.zones = void 0;
  7. var _stackTrace = require("./stackTrace");
  8. /**
  9. * Copyright (c) Microsoft Corporation.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. class ZoneManager {
  24. constructor() {
  25. this.lastZoneId = 0;
  26. this._zones = new Map();
  27. }
  28. run(type, data, func) {
  29. return new Zone(this, ++this.lastZoneId, type, data).run(func);
  30. }
  31. zoneData(type, rawStack) {
  32. for (const line of rawStack) {
  33. for (const zoneId of zoneIds(line)) {
  34. const zone = this._zones.get(zoneId);
  35. if (zone && zone.type === type) return zone.data;
  36. }
  37. }
  38. return null;
  39. }
  40. preserve(callback) {
  41. const rawStack = (0, _stackTrace.captureRawStack)();
  42. const refs = [];
  43. for (const line of rawStack) refs.push(...zoneIds(line));
  44. Object.defineProperty(callback, 'name', {
  45. value: `__PWZONE__[${refs.join(',')}]-refs`
  46. });
  47. return callback();
  48. }
  49. }
  50. function zoneIds(line) {
  51. const index = line.indexOf('__PWZONE__[');
  52. if (index === -1) return [];
  53. return line.substring(index + '__PWZONE__['.length, line.indexOf(']', index)).split(',').map(s => +s);
  54. }
  55. class Zone {
  56. constructor(manager, id, type, data) {
  57. this._manager = void 0;
  58. this.id = void 0;
  59. this.type = void 0;
  60. this.data = void 0;
  61. this.wallTime = void 0;
  62. this._manager = manager;
  63. this.id = id;
  64. this.type = type;
  65. this.data = data;
  66. this.wallTime = Date.now();
  67. }
  68. run(func) {
  69. this._manager._zones.set(this.id, this);
  70. Object.defineProperty(func, 'name', {
  71. value: `__PWZONE__[${this.id}]-${this.type}`
  72. });
  73. return runWithFinally(() => func(this.data), () => {
  74. this._manager._zones.delete(this.id);
  75. });
  76. }
  77. }
  78. function runWithFinally(func, finallyFunc) {
  79. try {
  80. const result = func();
  81. if (result instanceof Promise) {
  82. return result.then(r => {
  83. finallyFunc();
  84. return r;
  85. }).catch(e => {
  86. finallyFunc();
  87. throw e;
  88. });
  89. }
  90. finallyFunc();
  91. return result;
  92. } catch (e) {
  93. finallyFunc();
  94. throw e;
  95. }
  96. }
  97. const zones = exports.zones = new ZoneManager();