wtf.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2024 Google LLC. https://angular.io/
  5. * License: MIT
  6. */
  7. /**
  8. * @fileoverview
  9. * @suppress {missingRequire}
  10. */
  11. const _global = (typeof window === 'object' && window) || (typeof self === 'object' && self) || global;
  12. function patchWtf(Zone) {
  13. // Detect and setup WTF.
  14. let wtfTrace = null;
  15. let wtfEvents = null;
  16. const wtfEnabled = (function () {
  17. const wtf = _global['wtf'];
  18. if (wtf) {
  19. wtfTrace = wtf.trace;
  20. if (wtfTrace) {
  21. wtfEvents = wtfTrace.events;
  22. return true;
  23. }
  24. }
  25. return false;
  26. })();
  27. class WtfZoneSpec {
  28. constructor() {
  29. this.name = 'WTF';
  30. }
  31. static { this.forkInstance = wtfEnabled
  32. ? wtfEvents.createInstance('Zone:fork(ascii zone, ascii newZone)')
  33. : null; }
  34. static { this.scheduleInstance = {}; }
  35. static { this.cancelInstance = {}; }
  36. static { this.invokeScope = {}; }
  37. static { this.invokeTaskScope = {}; }
  38. onFork(parentZoneDelegate, currentZone, targetZone, zoneSpec) {
  39. const retValue = parentZoneDelegate.fork(targetZone, zoneSpec);
  40. WtfZoneSpec.forkInstance(zonePathName(targetZone), retValue.name);
  41. return retValue;
  42. }
  43. onInvoke(parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source) {
  44. const src = source || 'unknown';
  45. let scope = WtfZoneSpec.invokeScope[src];
  46. if (!scope) {
  47. scope = WtfZoneSpec.invokeScope[src] = wtfEvents.createScope(`Zone:invoke:${source}(ascii zone)`);
  48. }
  49. return wtfTrace.leaveScope(scope(zonePathName(targetZone)), parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source));
  50. }
  51. onHandleError(parentZoneDelegate, currentZone, targetZone, error) {
  52. return parentZoneDelegate.handleError(targetZone, error);
  53. }
  54. onScheduleTask(parentZoneDelegate, currentZone, targetZone, task) {
  55. const key = task.type + ':' + task.source;
  56. let instance = WtfZoneSpec.scheduleInstance[key];
  57. if (!instance) {
  58. instance = WtfZoneSpec.scheduleInstance[key] = wtfEvents.createInstance(`Zone:schedule:${key}(ascii zone, any data)`);
  59. }
  60. const retValue = parentZoneDelegate.scheduleTask(targetZone, task);
  61. instance(zonePathName(targetZone), shallowObj(task.data, 2));
  62. return retValue;
  63. }
  64. onInvokeTask(parentZoneDelegate, currentZone, targetZone, task, applyThis, applyArgs) {
  65. const source = task.source;
  66. let scope = WtfZoneSpec.invokeTaskScope[source];
  67. if (!scope) {
  68. scope = WtfZoneSpec.invokeTaskScope[source] = wtfEvents.createScope(`Zone:invokeTask:${source}(ascii zone)`);
  69. }
  70. return wtfTrace.leaveScope(scope(zonePathName(targetZone)), parentZoneDelegate.invokeTask(targetZone, task, applyThis, applyArgs));
  71. }
  72. onCancelTask(parentZoneDelegate, currentZone, targetZone, task) {
  73. const key = task.source;
  74. let instance = WtfZoneSpec.cancelInstance[key];
  75. if (!instance) {
  76. instance = WtfZoneSpec.cancelInstance[key] = wtfEvents.createInstance(`Zone:cancel:${key}(ascii zone, any options)`);
  77. }
  78. const retValue = parentZoneDelegate.cancelTask(targetZone, task);
  79. instance(zonePathName(targetZone), shallowObj(task.data, 2));
  80. return retValue;
  81. }
  82. }
  83. function shallowObj(obj, depth) {
  84. if (!obj || !depth)
  85. return null;
  86. const out = {};
  87. for (const key in obj) {
  88. if (obj.hasOwnProperty(key)) {
  89. // explicit : any due to https://github.com/microsoft/TypeScript/issues/33191
  90. let value = obj[key];
  91. switch (typeof value) {
  92. case 'object':
  93. const name = value && value.constructor && value.constructor.name;
  94. value = name == Object.name ? shallowObj(value, depth - 1) : name;
  95. break;
  96. case 'function':
  97. value = value.name || undefined;
  98. break;
  99. }
  100. out[key] = value;
  101. }
  102. }
  103. return out;
  104. }
  105. function zonePathName(zone) {
  106. let name = zone.name;
  107. let localZone = zone.parent;
  108. while (localZone != null) {
  109. name = localZone.name + '::' + name;
  110. localZone = localZone.parent;
  111. }
  112. return name;
  113. }
  114. Zone['wtfZoneSpec'] = !wtfEnabled ? null : new WtfZoneSpec();
  115. }
  116. patchWtf(Zone);