wtf.js 4.7 KB

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