wtf.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2025 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. name = 'WTF';
  29. static forkInstance = wtfEnabled
  30. ? wtfEvents.createInstance('Zone:fork(ascii zone, ascii newZone)')
  31. : null;
  32. static scheduleInstance = {};
  33. static cancelInstance = {};
  34. static invokeScope = {};
  35. static invokeTaskScope = {};
  36. onFork(parentZoneDelegate, currentZone, targetZone, zoneSpec) {
  37. const retValue = parentZoneDelegate.fork(targetZone, zoneSpec);
  38. WtfZoneSpec.forkInstance(zonePathName(targetZone), retValue.name);
  39. return retValue;
  40. }
  41. onInvoke(parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source) {
  42. const src = source || 'unknown';
  43. let scope = WtfZoneSpec.invokeScope[src];
  44. if (!scope) {
  45. scope = WtfZoneSpec.invokeScope[src] = wtfEvents.createScope(`Zone:invoke:${source}(ascii zone)`);
  46. }
  47. return wtfTrace.leaveScope(scope(zonePathName(targetZone)), parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source));
  48. }
  49. onHandleError(parentZoneDelegate, currentZone, targetZone, error) {
  50. return parentZoneDelegate.handleError(targetZone, error);
  51. }
  52. onScheduleTask(parentZoneDelegate, currentZone, targetZone, task) {
  53. const key = task.type + ':' + task.source;
  54. let instance = WtfZoneSpec.scheduleInstance[key];
  55. if (!instance) {
  56. instance = WtfZoneSpec.scheduleInstance[key] = wtfEvents.createInstance(`Zone:schedule:${key}(ascii zone, any data)`);
  57. }
  58. const retValue = parentZoneDelegate.scheduleTask(targetZone, task);
  59. instance(zonePathName(targetZone), shallowObj(task.data, 2));
  60. return retValue;
  61. }
  62. onInvokeTask(parentZoneDelegate, currentZone, targetZone, task, applyThis, applyArgs) {
  63. const source = task.source;
  64. let scope = WtfZoneSpec.invokeTaskScope[source];
  65. if (!scope) {
  66. scope = WtfZoneSpec.invokeTaskScope[source] = wtfEvents.createScope(`Zone:invokeTask:${source}(ascii zone)`);
  67. }
  68. return wtfTrace.leaveScope(scope(zonePathName(targetZone)), parentZoneDelegate.invokeTask(targetZone, task, applyThis, applyArgs));
  69. }
  70. onCancelTask(parentZoneDelegate, currentZone, targetZone, task) {
  71. const key = task.source;
  72. let instance = WtfZoneSpec.cancelInstance[key];
  73. if (!instance) {
  74. instance = WtfZoneSpec.cancelInstance[key] = wtfEvents.createInstance(`Zone:cancel:${key}(ascii zone, any options)`);
  75. }
  76. const retValue = parentZoneDelegate.cancelTask(targetZone, task);
  77. instance(zonePathName(targetZone), shallowObj(task.data, 2));
  78. return retValue;
  79. }
  80. }
  81. function shallowObj(obj, depth) {
  82. if (!obj || !depth)
  83. return null;
  84. const out = {};
  85. for (const key in obj) {
  86. if (obj.hasOwnProperty(key)) {
  87. // explicit : any due to https://github.com/microsoft/TypeScript/issues/33191
  88. let value = obj[key];
  89. switch (typeof value) {
  90. case 'object':
  91. const name = value && value.constructor && value.constructor.name;
  92. value = name == Object.name ? shallowObj(value, depth - 1) : name;
  93. break;
  94. case 'function':
  95. value = value.name || undefined;
  96. break;
  97. }
  98. out[key] = value;
  99. }
  100. }
  101. return out;
  102. }
  103. function zonePathName(zone) {
  104. let name = zone.name;
  105. let localZone = zone.parent;
  106. while (localZone != null) {
  107. name = localZone.name + '::' + name;
  108. localZone = localZone.parent;
  109. }
  110. return name;
  111. }
  112. Zone['wtfZoneSpec'] = !wtfEnabled ? null : new WtfZoneSpec();
  113. }
  114. patchWtf(Zone);