-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
89 lines (71 loc) · 2.22 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
s1 n(r) n(x) s2 n(j) c n(s)
Subject
s1 ^-----r------x--------------j------|----------
s2 ---------------------^------j------|----------
AsyncSubject
s1 ^----------------------------------j|---------
s1 ---------------------^-------------j|---------
BehaviorSubject
s1 ^a----r------x--------------j------|----------
s2 ---------------------^x-----j------|----------
ReplaySubject
s1 ^-----r------x--------------j------|----------
s2 ---------------------^r-x---j------|----------
*/
console.clear();
import { Subject, AsyncSubject, BehaviorSubject, ReplaySubject } from 'rxjs';
const subject = new Subject();
const asyncSubject = new AsyncSubject();
const behaviorSubject = new BehaviorSubject('a');
const replaySubject = new ReplaySubject(2);
const subjects = [subject, asyncSubject, behaviorSubject, replaySubject];
const log = subjectType => e => console.log(`${subjectType}: ${e}`);
console.log('SUBSCRIBE 1');
subject.subscribe(log('s1 subject'));
asyncSubject.subscribe(log('s1 asyncSubject'));
behaviorSubject.subscribe(log('s1 behaviorSubject'));
replaySubject.subscribe(log('s1 replaySubject'));
console.log('\nNEXT(r)');
subjects.forEach(o => o.next('r'));
console.log('\nNEXT(x)');
subjects.forEach(o => o.next('x'));
console.log('\nSUBSCRIBE 2');
subject.subscribe(log('s2 subject'));
asyncSubject.subscribe(log('s2 asyncSubject'));
behaviorSubject.subscribe(log('s2 behaviorSubject'));
replaySubject.subscribe(log('s2 replaySubject'));
console.log('\nNEXT(j)');
subjects.forEach(o => o.next('j'));
console.log('\nCOMPLETE');
subjects.forEach(o => o.complete());
console.log('\nNEXT(s)');
subjects.forEach(o => o.next('s'));
/*
OUTPUT:
SUBSCRIBE 1
s1 behaviorSubject: a
NEXT(r)
s1 subject: r
s1 behaviorSubject: r
s1 replaySubject: r
NEXT(x)
s1 subject: x
s1 behaviorSubject: x
s1 replaySubject: x
SUBSCRIBE 2
s2 behaviorSubject: x
s2 replaySubject: r
s2 replaySubject: x
NEXT(j)
s1 subject: j
s2 subject: j
s1 behaviorSubject: j
s2 behaviorSubject: j
s1 replaySubject: j
s2 replaySubject: j
COMPLETE
s1 asyncSubject: j
s2 asyncSubject: j
NEXT(s)
*/