Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. ## Monkey patching common browser APIs
  2.  
  3. Here is an example of how browser APIs could be patched to take advantage of Zone propagation.
  4.  
  5. ### `setTimeout`
  6.  
  7. Ideally we would not need to do this, since the browser would do this for us.
  8.  
  9. ```javascript
  10. window.setTimeout = ((delegate) => {
  11. return function (fn, delay, var_args) {
  12. arguments[0] = Zone.current.wrap(fn, 'setTimeout');
  13. return delegate.apply(this, arguments);
  14. };
  15. })(window.setTimeout);
  16. ```
  17.  
  18.  
  19. ### `Promise.prototype.then`
  20.  
  21. Ideally we would not need to do this, since the Promise would do this for us. It could do this
  22. more efficiently by storing the `Zone`, and then invoking the callback using `zone.run(...)`
  23.  
  24. ```javascript
  25. window.Promise.prototype.then = ((then) => {
  26. return function (onResolve, onError) {
  27. // Wrap callbacks without error handling.
  28. arguments[0] = Zone.current.wrap(onResolve, 'Promise.then.onResolve', false);
  29. arguments[0] = Zone.current.wrap(onError, 'Promise.then.onError', false);
  30. return then.apply(this, arguments);
  31. };
  32. })(window.Promise.prototype.then);
  33. ```
  34.  
  35.  
  36. ### `addEventListener`
  37.  
  38. Ideally we would not need to do this, since the browser would do this for us.
  39.  
  40. ```javascript
  41. window.EventTarget.prototype.addEventListener = ((addEventListener) => {
  42. return function (eventName, handler, capture) {
  43. // Wrap callbacks without error handling.
  44. arguments[1] = Zone.current.wrap(handler, 'EventTarget.addEventListener');
  45. return addEventListener.apply(this, arguments);
  46. };
  47. })(window.EventTarget.prototype.addEventListener);
  48. ```
  49.  
  50.  
  51. ## Framework auto rendering
  52.  
  53. (Assuming that the browser / Promises propagate Zones)
  54.  
  55. In this example we have two applications on a page written with two frameworks, and we want to make
  56. sure that the right framework gets notified when the correct operation happens.
  57.  
  58. ```javascript
  59. var frameworkAZone = Zone.current.fork({
  60. name: 'frameworkA',
  61. onInvoke: (self, parent, zone, callback, applyThis, applyArgs) => {
  62. try {
  63. return parent.invoke(callback, applyThis, applyArgs);
  64. } finally {
  65. frameworkAMayNeedRendering();
  66. }
  67. }
  68. })
  69.  
  70. var frameworkBZone = Zone.current.fork({
  71. name: 'frameworkB',
  72. onInvoke: (self, parent, zone, callback, applyThis, applyArgs) => {
  73. try {
  74. return parent.invoke(callback, applyThis, applyArgs);
  75. } finally {
  76. frameworkBMayNeedRendering();
  77. }
  78. }
  79. })
  80.  
  81. frameworkAZone.run(() => {
  82. bootstrapApplicationA();
  83. });
  84.  
  85.  
  86. frameworkAZone.run(() => {
  87. bootstrapApplicationB();
  88. });
  89.  
  90. setInterval(() => {
  91. expect(Zone.current.name, '<root>');
  92. updateSomeThingNotPartOfApplication();
  93. }, 100);
  94.  
  95.  
  96. function bootstrapApplicationA() {
  97. expect(Zone.current, frameworkAZone);
  98. someDiv.addEventListener('click', (event) => {
  99. expect(Zone.current, frameworkAZone);
  100. setTimeout(() => {
  101. expect(Zone.current, frameworkAZone);
  102. new Promise().then(
  103. expect(Zone.current, frameworkAZone);
  104. // Set some value
  105. model.someProperty = 'New Value';
  106. // Expect that framework A will get notified of rendering.
  107. )
  108. }, 100);
  109. });
  110. }
  111.  
  112. function bootstrapApplicationB() {
  113. expect(Zone.current, frameworkBZone);
  114. someDiv.addEventListener('click', (event) => {
  115. expect(Zone.current, frameworkBZone);
  116. setTimeout(() => {
  117. expect(Zone.current, frameworkBZone);
  118. new Promise().then(
  119. expect(Zone.current, frameworkBZone);
  120. // Set some value
  121. model.someProperty = 'New Value';
  122. // Expect that framework B will get notified of rendering.
  123. )
  124. }, 100);
  125. });
  126. }
  127.  
  128.  
  129. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement