zone-patch-promise-test.js 2.8 KB

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