0
# Observable Creation
1
2
Core methods for creating observable sequences from various sources including arrays, events, callbacks, and custom functions.
3
4
## Capabilities
5
6
### Create Observable
7
8
Creates an observable from a subscribe function.
9
10
```javascript { .api }
11
/**
12
* Creates an observable sequence from a subscribe function
13
* @param {function} subscribe - Function that accepts an observer and returns a disposable
14
* @returns {Observable} New observable sequence
15
*/
16
Rx.Observable.create = function(subscribe);
17
```
18
19
**Usage Example:**
20
21
```javascript
22
var source = Rx.Observable.create(function(observer) {
23
observer.onNext(42);
24
observer.onNext(56);
25
observer.onCompleted();
26
27
// Return cleanup function
28
return function() {
29
console.log('disposed');
30
};
31
});
32
```
33
34
### Defer
35
36
Defers observable creation until subscription time.
37
38
```javascript { .api }
39
/**
40
* Defers observable creation until subscription
41
* @param {function} observableFactory - Factory function that returns an observable
42
* @returns {Observable} Deferred observable
43
*/
44
Rx.Observable.defer = function(observableFactory);
45
```
46
47
### Empty Observable
48
49
Creates an empty observable that immediately completes.
50
51
```javascript { .api }
52
/**
53
* Creates an empty observable sequence
54
* @param {Scheduler} [scheduler] - Scheduler to send completion notification on
55
* @returns {Observable} Empty observable sequence
56
*/
57
Rx.Observable.empty = function(scheduler);
58
```
59
60
### Never Observable
61
62
Creates an observable that never emits or completes.
63
64
```javascript { .api }
65
/**
66
* Creates an observable sequence that never terminates
67
* @returns {Observable} Non-terminating observable sequence
68
*/
69
Rx.Observable.never = function();
70
```
71
72
### Return/Just Single Value
73
74
Creates an observable that emits a single value.
75
76
```javascript { .api }
77
/**
78
* Creates an observable that emits a single value then completes
79
* @param {*} value - Value to emit
80
* @param {Scheduler} [scheduler] - Scheduler to send value on
81
* @returns {Observable} Observable sequence containing the single value
82
*/
83
Rx.Observable.return = function(value, scheduler);
84
Rx.Observable.just = function(value, scheduler); // Alias for return
85
```
86
87
### Throw Error
88
89
Creates an observable that immediately emits an error.
90
91
```javascript { .api }
92
/**
93
* Creates an observable that immediately emits an error
94
* @param {*} error - Error to emit
95
* @param {Scheduler} [scheduler] - Scheduler to send error on
96
* @returns {Observable} Observable sequence that terminates with error
97
*/
98
Rx.Observable.throw = function(error, scheduler);
99
```
100
101
### Range
102
103
Creates an observable sequence of integers within a range.
104
105
```javascript { .api }
106
/**
107
* Creates an observable sequence of integers within a specified range
108
* @param {number} start - Start value of the range
109
* @param {number} count - Number of sequential integers to generate
110
* @param {Scheduler} [scheduler] - Scheduler to send values on
111
* @returns {Observable} Observable sequence containing range of integers
112
*/
113
Rx.Observable.range = function(start, count, scheduler);
114
```
115
116
**Usage Example:**
117
118
```javascript
119
var source = Rx.Observable.range(1, 5);
120
// Emits: 1, 2, 3, 4, 5
121
```
122
123
### Repeat
124
125
Creates an observable that repeats a value.
126
127
```javascript { .api }
128
/**
129
* Creates an observable that repeats the given value
130
* @param {*} value - Value to repeat
131
* @param {number} [repeatCount] - Number of times to repeat (infinite if not specified)
132
* @param {Scheduler} [scheduler] - Scheduler to send values on
133
* @returns {Observable} Observable sequence repeating the value
134
*/
135
Rx.Observable.repeat = function(value, repeatCount, scheduler);
136
```
137
138
### Generate
139
140
Generates an observable sequence by running a state-driven loop.
141
142
```javascript { .api }
143
/**
144
* Generates an observable sequence by running a state-driven loop
145
* @param {*} initialState - Initial state
146
* @param {function} condition - Condition to continue iteration
147
* @param {function} iterate - State update function
148
* @param {function} resultSelector - Result selection function
149
* @param {Scheduler} [scheduler] - Scheduler to send values on
150
* @returns {Observable} Generated observable sequence
151
*/
152
Rx.Observable.generate = function(initialState, condition, iterate, resultSelector, scheduler);
153
```
154
155
**Usage Example:**
156
157
```javascript
158
var source = Rx.Observable.generate(
159
0, // initial state
160
function(x) { return x < 3; }, // condition
161
function(x) { return x + 1; }, // iterate
162
function(x) { return x * x; } // result selector
163
);
164
// Emits: 0, 1, 4
165
```
166
167
### From Array
168
169
Creates an observable from an array.
170
171
```javascript { .api }
172
/**
173
* Creates an observable sequence from an array
174
* @param {Array} array - Array to convert to observable
175
* @param {Scheduler} [scheduler] - Scheduler to send values on
176
* @returns {Observable} Observable sequence containing array elements
177
*/
178
Rx.Observable.fromArray = function(array, scheduler);
179
```
180
181
### From Iterable
182
183
Creates an observable from an iterable.
184
185
```javascript { .api }
186
/**
187
* Creates an observable from an iterable object
188
* @param {Iterable} iterable - Iterable to convert
189
* @param {function} [mapFn] - Map function to transform each element
190
* @param {*} [thisArg] - Value to use as this when executing mapFn
191
* @param {Scheduler} [scheduler] - Scheduler to send values on
192
* @returns {Observable} Observable sequence from iterable
193
*/
194
Rx.Observable.from = function(iterable, mapFn, thisArg, scheduler);
195
```
196
197
### Of (From Arguments)
198
199
Creates an observable from the arguments.
200
201
```javascript { .api }
202
/**
203
* Creates an observable sequence from the arguments
204
* @param {...*} args - Arguments to emit as values
205
* @returns {Observable} Observable sequence containing the arguments
206
*/
207
Rx.Observable.of = function(...args);
208
209
/**
210
* Creates an observable sequence from arguments with scheduler
211
* @param {Scheduler} scheduler - Scheduler to send values on
212
* @param {...*} args - Arguments to emit as values
213
* @returns {Observable} Observable sequence containing the arguments
214
*/
215
Rx.Observable.ofWithScheduler = function(scheduler, ...args);
216
```
217
218
### Pairs (From Object)
219
220
Creates an observable from object key-value pairs.
221
222
```javascript { .api }
223
/**
224
* Creates an observable of key-value pairs from an object
225
* @param {Object} obj - Object to convert to key-value pairs
226
* @param {Scheduler} [scheduler] - Scheduler to send values on
227
* @returns {Observable} Observable sequence of [key, value] pairs
228
*/
229
Rx.Observable.pairs = function(obj, scheduler);
230
```
231
232
**Usage Example:**
233
234
```javascript
235
var source = Rx.Observable.pairs({a: 1, b: 2});
236
// Emits: ['a', 1], ['b', 2]
237
```
238
239
### Using (Resource Management)
240
241
Creates an observable with automatic resource management.
242
243
```javascript { .api }
244
/**
245
* Creates an observable with resource management
246
* @param {function} resourceFactory - Function that returns a disposable resource
247
* @param {function} observableFactory - Function that uses the resource to create an observable
248
* @returns {Observable} Observable sequence with automatic resource cleanup
249
*/
250
Rx.Observable.using = function(resourceFactory, observableFactory);
251
```
252
253
**Usage Example:**
254
255
```javascript
256
var source = Rx.Observable.using(
257
function() { return new FileHandle(); }, // resource factory
258
function(resource) { // observable factory
259
return Rx.Observable.fromCallback(resource.read.bind(resource));
260
}
261
);
262
```
263
264
### ForkJoin
265
266
Runs all observable sequences in parallel and collects their last emitted values.
267
268
```javascript { .api }
269
/**
270
* Runs all observable sequences in parallel and collects their last values
271
* @param {...Observable} sources - Observable sequences to combine
272
* @returns {Observable} Observable sequence with combined last values as array
273
*/
274
Rx.Observable.forkJoin = function(...sources);
275
276
/**
277
* Alternative signature with result selector
278
* @param {Array<Observable>} sources - Array of observable sequences
279
* @param {function} [resultSelector] - Function to transform the result array
280
* @returns {Observable} Observable sequence with combined result
281
*/
282
Rx.Observable.forkJoin = function(sources, resultSelector);
283
```
284
285
**Usage Example:**
286
287
```javascript
288
var source1 = Rx.Observable.timer(1000).map(function() { return 'A'; });
289
var source2 = Rx.Observable.timer(2000).map(function() { return 'B'; });
290
var combined = Rx.Observable.forkJoin(source1, source2);
291
// Emits: ['A', 'B'] after 2 seconds
292
```
293
294
### If (Conditional)
295
296
Returns one of two observables based on a condition.
297
298
```javascript { .api }
299
/**
300
* Returns one of two observables based on a condition
301
* @param {function} condition - Function that returns a boolean
302
* @param {Observable} thenSource - Observable to return if condition is true
303
* @param {Observable} [elseSource] - Observable to return if condition is false (defaults to empty)
304
* @returns {Observable} Conditional observable sequence
305
*/
306
Rx.Observable.if = function(condition, thenSource, elseSource);
307
```
308
309
**Usage Example:**
310
311
```javascript
312
var condition = function() { return Math.random() > 0.5; };
313
var source = Rx.Observable.if(
314
condition,
315
Rx.Observable.just('Heads'),
316
Rx.Observable.just('Tails')
317
);
318
```
319
320
### WhileDo
321
322
Repeats source observable while condition holds true.
323
324
```javascript { .api }
325
/**
326
* Repeats source observable while condition holds true
327
* @param {function} condition - Function that returns a boolean
328
* @param {Observable} source - Observable to repeat
329
* @returns {Observable} Observable sequence that repeats while condition is true
330
*/
331
Rx.Observable.whileDo = function(condition, source);
332
```
333
334
### OnErrorResumeNext (Static)
335
336
Continues with the next observable sequence on error.
337
338
```javascript { .api }
339
/**
340
* Continues with the next observable sequence on error for all sources
341
* @param {...Observable} sources - Observable sequences to chain
342
* @returns {Observable} Observable sequence that continues on error
343
*/
344
Rx.Observable.onErrorResumeNext = function(...sources);
345
```
346
347
### MergeDelayError
348
349
Merges all observable sequences but delays errors until all sources complete.
350
351
```javascript { .api }
352
/**
353
* Merges all sources but delays error propagation until all complete
354
* @param {...Observable} sources - Observable sequences to merge
355
* @returns {Observable} Merged observable sequence with delayed errors
356
*/
357
Rx.Observable.mergeDelayError = function(...sources);
358
```
359
360
### IsObservable
361
362
Checks if an object is an observable.
363
364
```javascript { .api }
365
/**
366
* Determines whether an object is an observable
367
* @param {*} obj - Object to test
368
* @returns {boolean} True if the object is an observable
369
*/
370
Rx.Observable.isObservable = function(obj);
371
```
372
373
**Usage Example:**
374
375
```javascript
376
var obs = Rx.Observable.just(42);
377
var isObs = Rx.Observable.isObservable(obs); // true
378
var notObs = Rx.Observable.isObservable("hello"); // false
379
```
380
381
### GenerateWithTime
382
383
Generates an observable sequence with time-based state iteration.
384
385
```javascript { .api }
386
/**
387
* Generates an observable sequence with absolute time
388
* @param {*} initialState - Initial state
389
* @param {function} condition - Condition to determine whether to continue iteration
390
* @param {function} iterate - Iteration step function
391
* @param {function} resultSelector - Function to select the result value
392
* @param {function} timeSelector - Function to select the time for each iteration
393
* @param {Scheduler} [scheduler] - Scheduler to run timers on
394
* @returns {Observable} Generated observable sequence
395
*/
396
Rx.Observable.generateWithAbsoluteTime = function(initialState, condition, iterate, resultSelector, timeSelector, scheduler);
397
398
/**
399
* Generates an observable sequence with relative time
400
* @param {*} initialState - Initial state
401
* @param {function} condition - Condition to determine whether to continue iteration
402
* @param {function} iterate - Iteration step function
403
* @param {function} resultSelector - Function to select the result value
404
* @param {function} timeSelector - Function to select the relative time for each iteration
405
* @param {Scheduler} [scheduler] - Scheduler to run timers on
406
* @returns {Observable} Generated observable sequence
407
*/
408
Rx.Observable.generateWithRelativeTime = function(initialState, condition, iterate, resultSelector, timeSelector, scheduler);
409
```