Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. 1 source, Three subscribers
  2. Subscriber 1 subscribes and source starts emitting
  3. Subscriber 2 subscribes halfway
  4. Subscriber 3 subscribes after source has completed.
  5.  
  6.  
  7. ```
  8. import Rx from 'rxjs/Rx';
  9.  
  10. var data = [];
  11.  
  12. var subject = new Rx.ReplaySubject(1);
  13. // var subject = new Rx.BehaviorSubject(data);
  14. // var subject = new Rx.Subject();
  15.  
  16. setTimeout(function () {
  17. // Clean up
  18. subject.complete();
  19. clearInterval(myInterval);
  20. }, 10000);
  21.  
  22. var subSubject1 = subject.subscribe(
  23. function (x) { console.log('Value published to observer 1: ' + x); },
  24. function (e) { console.log('onError: ' + e.message); },
  25. function () { console.log('onCompleted 1'); });
  26.  
  27. setTimeout(function () {
  28. var subSubject2 = subject.subscribe(
  29. function (x) { console.log('Value published to observer 2: ' + x); },
  30. function (e) { console.log('onError: ' + e.message); },
  31. function () { console.log('onCompleted 2'); }
  32. );
  33. }, 5000);
  34.  
  35. setTimeout(function () {
  36. var subSubject3 = subject.subscribe(
  37. function (x) { console.log('Value published to observer 3: ' + x); },
  38. function (e) { console.log('onError: ' + e.message); },
  39. function () { console.log('onCompleted 3'); }
  40. );
  41. }, 12000);
  42.  
  43. var myInterval = setInterval(function () {
  44. data.push(data.length);
  45. subject.next(data);
  46. }, 1000);
  47.  
  48. ```
  49.  
  50.  
  51. ## With var subject = new Rx.ReplaySubject(1);
  52.  
  53. **Value published to observer 1: 0**
  54. index.js:31 Value published to observer 1: 0,1
  55. index.js:31 Value published to observer 1: 0,1,2
  56. index.js:31 Value published to observer 1: 0,1,2,3
  57. index.js:61 Value published to observer 2: 0,1,2,3
  58. **index.js:31 Value published to observer 1: 0,1,2,3,4**
  59. **index.js:61 Value published to observer 2: 0,1,2,3,4**
  60. index.js:31 Value published to observer 1: 0,1,2,3,4,5
  61. index.js:61 Value published to observer 2: 0,1,2,3,4,5
  62. index.js:31 Value published to observer 1: 0,1,2,3,4,5,6
  63. index.js:61 Value published to observer 2: 0,1,2,3,4,5,6
  64. index.js:31 Value published to observer 1: 0,1,2,3,4,5,6,7
  65. index.js:61 Value published to observer 2: 0,1,2,3,4,5,6,7
  66. index.js:31 Value published to observer 1: 0,1,2,3,4,5,6,7,8
  67. index.js:61 Value published to observer 2: 0,1,2,3,4,5,6,7,8
  68. index.js:35 onCompleted 1
  69. index.js:65 onCompleted 2
  70. *index.js:71 Value published to observer #3: 0,1,2,3,4,5,6,7,8*
  71. index.js:75 onCompleted 3
  72.  
  73.  
  74. ## With var subject = new Rx.BehaviorSubject(data);
  75.  
  76. **Value published to observer 1:**
  77. index.js:31 Value published to observer 1: 0
  78. index.js:31 Value published to observer 1: 0,1
  79. index.js:31 Value published to observer 1: 0,1,2
  80. **index.js:31 Value published to observer 1: 0,1,2,3**
  81. **index.js:61 Value published to observer 2: 0,1,2,3**
  82. index.js:31 Value published to observer 1: 0,1,2,3,4
  83. index.js:61 Value published to observer 2: 0,1,2,3,4
  84. index.js:31 Value published to observer 1: 0,1,2,3,4,5
  85. index.js:61 Value published to observer 2: 0,1,2,3,4,5
  86. index.js:31 Value published to observer 1: 0,1,2,3,4,5,6
  87. index.js:61 Value published to observer 2: 0,1,2,3,4,5,6
  88. index.js:31 Value published to observer 1: 0,1,2,3,4,5,6,7
  89. index.js:61 Value published to observer 2: 0,1,2,3,4,5,6,7
  90. index.js:31 Value published to observer 1: 0,1,2,3,4,5,6,7,8
  91. index.js:61 Value published to observer 2: 0,1,2,3,4,5,6,7,8
  92. index.js:35 onCompleted 1
  93. index.js:65 onCompleted 2
  94. *index.js:75 onCompleted 3*
  95.  
  96.  
  97. ## With var subject = new Rx.Subject();
  98.  
  99. **Value published to observer 1: 0**
  100. index.js:31 Value published to observer 1: 0,1
  101. index.js:31 Value published to observer 1: 0,1,2
  102. index.js:31 Value published to observer 1: 0,1,2,3
  103. **index.js:31 Value published to observer 1: 0,1,2,3,4**
  104. **index.js:61 Value published to observer 2: 0,1,2,3,4**
  105. index.js:31 Value published to observer 1: 0,1,2,3,4,5
  106. index.js:61 Value published to observer 2: 0,1,2,3,4,5
  107. index.js:31 Value published to observer 1: 0,1,2,3,4,5,6
  108. index.js:61 Value published to observer 2: 0,1,2,3,4,5,6
  109. index.js:31 Value published to observer 1: 0,1,2,3,4,5,6,7
  110. index.js:61 Value published to observer 2: 0,1,2,3,4,5,6,7
  111. index.js:31 Value published to observer 1: 0,1,2,3,4,5,6,7,8
  112. index.js:61 Value published to observer 2: 0,1,2,3,4,5,6,7,8
  113. index.js:35 onCompleted 1
  114. index.js:65 onCompleted 2
  115. **index.js:75 onCompleted 3**
  116.  
  117.  
  118. ## ====
  119.  
  120. BehaviorSubject emits initial value (before next() happens)
  121. Subject, ReplaySubject emits on next(), not initial value
  122.  
  123. ReplaySubject returns value even after complete()
  124. Subject, BehaviorSubject do not
  125.  
  126. ReplaySubject, BehaviorSubject give the current value on subscription
  127. Subject emits on next()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement