0
# Subject Types
1
2
Hot observables that can multicast values to multiple observers and act as both observable and observer.
3
4
## Capabilities
5
6
### Subject
7
8
Basic subject that multicasts values to all subscribed observers.
9
10
```javascript { .api }
11
/**
12
* Creates a subject that multicasts values to multiple observers
13
* @constructor
14
* @returns {Subject} New subject instance
15
*/
16
function Subject();
17
18
// Static creation method
19
/**
20
* Creates an anonymous subject with separate observer and observable
21
* @param {Observer} [observer] - Observer to send values to
22
* @param {Observable} [observable] - Observable to subscribe to
23
* @returns {AnonymousSubject} Anonymous subject instance
24
*/
25
Rx.Subject.create = function(observer, observable);
26
```
27
28
**Usage Example:**
29
30
```javascript
31
var subject = new Rx.Subject();
32
33
// Subscribe multiple observers
34
subject.subscribe(function(x) { console.log('Observer 1: ' + x); });
35
subject.subscribe(function(x) { console.log('Observer 2: ' + x); });
36
37
// Emit values to all observers
38
subject.onNext(42);
39
subject.onNext(56);
40
subject.onCompleted();
41
```
42
43
### Behavior Subject
44
45
Subject that requires an initial value and emits current value to new subscribers.
46
47
```javascript { .api }
48
/**
49
* Creates a behavior subject with an initial value
50
* @constructor
51
* @param {*} initialValue - Initial value to emit to subscribers
52
* @returns {BehaviorSubject} New behavior subject instance
53
*/
54
function BehaviorSubject(initialValue);
55
56
/**
57
* Gets the current value of the behavior subject
58
* @returns {*} Current value
59
*/
60
BehaviorSubject.prototype.getValue = function();
61
```
62
63
**Usage Example:**
64
65
```javascript
66
var behaviorSubject = new Rx.BehaviorSubject(42);
67
68
behaviorSubject.subscribe(function(x) {
69
console.log('Subscriber 1: ' + x);
70
});
71
// Immediately emits: Subscriber 1: 42
72
73
behaviorSubject.onNext(56);
74
// Emits: Subscriber 1: 56
75
76
behaviorSubject.subscribe(function(x) {
77
console.log('Subscriber 2: ' + x);
78
});
79
// Immediately emits: Subscriber 2: 56
80
```
81
82
### Replay Subject
83
84
Subject that replays a specified number of values to new subscribers.
85
86
```javascript { .api }
87
/**
88
* Creates a replay subject that buffers values for new subscribers
89
* @constructor
90
* @param {number} [bufferSize] - Maximum number of values to buffer (unlimited if not specified)
91
* @param {number} [windowSize] - Maximum time window in milliseconds to buffer values
92
* @param {Scheduler} [scheduler] - Scheduler to use for time-based operations
93
* @returns {ReplaySubject} New replay subject instance
94
*/
95
function ReplaySubject(bufferSize, windowSize, scheduler);
96
```
97
98
**Usage Example:**
99
100
```javascript
101
var replaySubject = new Rx.ReplaySubject(2); // Buffer last 2 values
102
103
replaySubject.onNext(1);
104
replaySubject.onNext(2);
105
replaySubject.onNext(3);
106
107
replaySubject.subscribe(function(x) {
108
console.log('Late subscriber: ' + x);
109
});
110
// Immediately emits: Late subscriber: 2, Late subscriber: 3
111
```
112
113
### Async Subject
114
115
Subject that only emits the last value when the sequence completes.
116
117
```javascript { .api }
118
/**
119
* Creates an async subject that emits only the last value on completion
120
* @constructor
121
* @returns {AsyncSubject} New async subject instance
122
*/
123
function AsyncSubject();
124
```
125
126
**Usage Example:**
127
128
```javascript
129
var asyncSubject = new Rx.AsyncSubject();
130
131
asyncSubject.subscribe(function(x) {
132
console.log('Result: ' + x);
133
});
134
135
asyncSubject.onNext(1);
136
asyncSubject.onNext(2);
137
asyncSubject.onNext(3);
138
// Nothing emitted yet
139
140
asyncSubject.onCompleted();
141
// Now emits: Result: 3
142
```
143
144
### Anonymous Subject
145
146
Subject created with separate observer and observable components.
147
148
```javascript { .api }
149
/**
150
* Anonymous subject with separate observer and observable
151
* @constructor
152
* @param {Observer} observer - Observer to send values to
153
* @param {Observable} observable - Observable to subscribe to
154
* @returns {AnonymousSubject} Anonymous subject instance
155
*/
156
function AnonymousSubject(observer, observable);
157
```
158
159
## Subject Interface Methods
160
161
All subjects implement both Observable and Observer interfaces:
162
163
### Observer Methods
164
165
```javascript { .api }
166
/**
167
* Sends a value to all subscribed observers
168
* @param {*} value - Value to emit
169
*/
170
subject.onNext = function(value);
171
172
/**
173
* Sends an error to all subscribed observers and terminates the sequence
174
* @param {*} error - Error to emit
175
*/
176
subject.onError = function(error);
177
178
/**
179
* Signals completion to all subscribed observers and terminates the sequence
180
*/
181
subject.onCompleted = function();
182
```
183
184
### Observable Methods
185
186
```javascript { .api }
187
/**
188
* Subscribes an observer to the subject
189
* @param {Observer|function} observer - Observer or onNext callback
190
* @param {function} [onError] - Error callback
191
* @param {function} [onCompleted] - Completion callback
192
* @returns {Disposable} Subscription disposable
193
*/
194
subject.subscribe = function(observer, onError, onCompleted);
195
```
196
197
### Subject-Specific Methods
198
199
```javascript { .api }
200
/**
201
* Checks if the subject has any observers
202
* @returns {boolean} True if subject has observers
203
*/
204
subject.hasObservers = function();
205
206
/**
207
* Disposes the subject and releases all observers
208
*/
209
subject.dispose = function();
210
```
211
212
## Hot vs Cold Observables
213
214
Subjects are **hot** observables, meaning they emit values regardless of whether anyone is subscribed:
215
216
**Usage Example:**
217
218
```javascript
219
// Cold observable - creates new sequence for each subscriber
220
var cold = Rx.Observable.interval(1000);
221
222
// Hot observable - shares single sequence among all subscribers
223
var subject = new Rx.Subject();
224
Rx.Observable.interval(1000).subscribe(subject);
225
226
// Multiple subscriptions to subject share the same sequence
227
subject.subscribe(observer1);
228
subject.subscribe(observer2);
229
```
230
231
## Multicast Patterns
232
233
Subjects enable converting cold observables to hot using multicast operators:
234
235
```javascript { .api }
236
/**
237
* Multicasts the source sequence using a subject
238
* @param {Subject|function} subjectOrSelector - Subject or subject factory function
239
* @param {function} [selector] - Selector function for connection lifetime
240
* @returns {ConnectableObservable} Connectable observable that multicasts through subject
241
*/
242
observable.multicast = function(subjectOrSelector, selector);
243
244
/**
245
* Shares the source sequence using a subject (shorthand for multicast + refCount)
246
* @returns {Observable} Observable sequence shared among multiple subscribers
247
*/
248
observable.share = function();
249
```