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

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