zone-patch-promise-test.umd.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2025 Google LLC. https://angular.io/
  5. * License: MIT
  6. */
  7. (function (factory) {
  8. typeof define === 'function' && define.amd ? define(factory) :
  9. factory();
  10. })((function () {
  11. 'use strict';
  12. function patchPromiseTesting(Zone) {
  13. /**
  14. * Promise for async/fakeAsync zoneSpec test
  15. * can support async operation which not supported by zone.js
  16. * such as
  17. * it ('test jsonp in AsyncZone', async() => {
  18. * new Promise(res => {
  19. * jsonp(url, (data) => {
  20. * // success callback
  21. * res(data);
  22. * });
  23. * }).then((jsonpResult) => {
  24. * // get jsonp result.
  25. *
  26. * // user will expect AsyncZoneSpec wait for
  27. * // then, but because jsonp is not zone aware
  28. * // AsyncZone will finish before then is called.
  29. * });
  30. * });
  31. */
  32. Zone.__load_patch('promisefortest', function (global, Zone, api) {
  33. var symbolState = api.symbol('state');
  34. var UNRESOLVED = null;
  35. var symbolParentUnresolved = api.symbol('parentUnresolved');
  36. // patch Promise.prototype.then to keep an internal
  37. // number for tracking unresolved chained promise
  38. // we will decrease this number when the parent promise
  39. // being resolved/rejected and chained promise was
  40. // scheduled as a microTask.
  41. // so we can know such kind of chained promise still
  42. // not resolved in AsyncTestZone
  43. Promise[api.symbol('patchPromiseForTest')] = function patchPromiseForTest() {
  44. var oriThen = Promise[Zone.__symbol__('ZonePromiseThen')];
  45. if (oriThen) {
  46. return;
  47. }
  48. oriThen = Promise[Zone.__symbol__('ZonePromiseThen')] = Promise.prototype.then;
  49. Promise.prototype.then = function () {
  50. var chained = oriThen.apply(this, arguments);
  51. if (this[symbolState] === UNRESOLVED) {
  52. // parent promise is unresolved.
  53. var asyncTestZoneSpec = Zone.current.get('AsyncTestZoneSpec');
  54. if (asyncTestZoneSpec) {
  55. asyncTestZoneSpec.unresolvedChainedPromiseCount++;
  56. chained[symbolParentUnresolved] = true;
  57. }
  58. }
  59. return chained;
  60. };
  61. };
  62. Promise[api.symbol('unPatchPromiseForTest')] = function unpatchPromiseForTest() {
  63. // restore origin then
  64. var oriThen = Promise[Zone.__symbol__('ZonePromiseThen')];
  65. if (oriThen) {
  66. Promise.prototype.then = oriThen;
  67. Promise[Zone.__symbol__('ZonePromiseThen')] = undefined;
  68. }
  69. };
  70. });
  71. }
  72. patchPromiseTesting(Zone);
  73. }));