Library for composing asynchronous and event-based operations in JavaScript using Observable sequences and fluent query operators
npx @tessl/cli install tessl/npm-rx@4.1.00
# RxJS (Reactive Extensions for JavaScript)
1
2
RxJS is a comprehensive library for composing asynchronous and event-based programs using observable sequences and fluent query operators. It unifies Promises, callbacks, and evented data sources into a consistent reactive programming model, enabling powerful composition patterns for handling complex asynchronous operations, error handling, cancellation, and synchronization in both browser and Node.js environments.
3
4
## Package Information
5
6
- **Package Name**: rx
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install rx`
10
11
## Core Imports
12
13
```javascript
14
var Rx = require('rx');
15
```
16
17
For browser environments:
18
19
```html
20
<script src="rx.all.js"></script>
21
```
22
23
For selective module loading:
24
25
```javascript
26
var Rx = require('rx');
27
require('rx/dist/rx.aggregates');
28
require('rx/dist/rx.async');
29
require('rx/dist/rx.time');
30
```
31
32
## Basic Usage
33
34
```javascript
35
var Rx = require('rx');
36
37
// Create an observable from an array
38
var source = Rx.Observable.fromArray([1, 2, 3, 4, 5]);
39
40
// Transform and filter the data
41
var subscription = source
42
.filter(function(x) { return x % 2 === 0; })
43
.map(function(x) { return x * x; })
44
.subscribe(
45
function(x) { console.log('Next: ' + x); },
46
function(err) { console.log('Error: ' + err); },
47
function() { console.log('Completed'); }
48
);
49
50
// Clean up subscription
51
subscription.dispose();
52
```
53
54
## Architecture
55
56
RxJS is built around several key components:
57
58
- **Observable**: Push-based collections that can emit multiple values over time
59
- **Observer**: Consumers of Observable sequences with onNext, onError, and onCompleted methods
60
- **Subjects**: Objects that are both Observable and Observer, enabling multicasting
61
- **Schedulers**: Centralized dispatchers to control concurrency and timing
62
- **Operators**: Composable functions for transforming, filtering, and combining observables
63
- **Disposables**: Resource management objects for cleaning up subscriptions
64
65
## Capabilities
66
67
### Observable Creation
68
69
Core methods for creating observable sequences from various sources including arrays, events, callbacks, and custom functions.
70
71
```javascript { .api }
72
// Static creation methods
73
Rx.Observable.create(function(observer) { /* ... */ });
74
Rx.Observable.fromArray([1, 2, 3]);
75
Rx.Observable.range(1, 10);
76
Rx.Observable.interval(1000);
77
Rx.Observable.timer(2000, 1000);
78
```
79
80
[Observable Creation](./observable-creation.md)
81
82
### Observable Transformation
83
84
Operators for transforming observable sequences including map, flatMap, scan, and buffer operations.
85
86
```javascript { .api }
87
// Instance transformation methods
88
observable.map(function(x) { return x * 2; });
89
observable.flatMap(function(x) { return Rx.Observable.range(0, x); });
90
observable.scan(function(acc, x) { return acc + x; }, 0);
91
observable.buffer(Rx.Observable.interval(1000));
92
```
93
94
[Observable Transformation](./observable-transformation.md)
95
96
### Observable Filtering
97
98
Operators for filtering observable sequences including filter, take, skip, distinct, and debounce operations.
99
100
```javascript { .api }
101
// Instance filtering methods
102
observable.filter(function(x) { return x > 5; });
103
observable.take(10);
104
observable.takeUntil(stopSignal);
105
observable.skip(5);
106
observable.distinctUntilChanged();
107
```
108
109
[Observable Filtering](./observable-filtering.md)
110
111
### Observable Combination
112
113
Operators for combining multiple observable sequences including merge, zip, combineLatest, and concat operations.
114
115
```javascript { .api }
116
// Static combination methods
117
Rx.Observable.merge(obs1, obs2, obs3);
118
Rx.Observable.zip(obs1, obs2, function(x, y) { return x + y; });
119
Rx.Observable.combineLatest(obs1, obs2, function(x, y) { return {x: x, y: y}; });
120
121
// Instance combination methods
122
observable.merge(otherObservable);
123
observable.concat(nextObservable);
124
```
125
126
[Observable Combination](./observable-combination.md)
127
128
### Observable Aggregation
129
130
Operators for aggregating observable sequences including reduce, count, min, max, and statistical operations.
131
132
```javascript { .api }
133
// Aggregation methods
134
observable.reduce(function(acc, x) { return acc + x; }, 0);
135
observable.count();
136
observable.min();
137
observable.max();
138
observable.average();
139
observable.sum();
140
```
141
142
[Observable Aggregation](./observable-aggregation.md)
143
144
### Async Operations
145
146
Methods for working with asynchronous patterns including callbacks, events, and promises.
147
148
```javascript { .api }
149
// Async creation methods
150
Rx.Observable.fromCallback(fs.readFile);
151
Rx.Observable.fromEvent(button, 'click');
152
Rx.Observable.fromPromise(fetch('/api/data'));
153
Rx.Observable.spawn(generatorFunction);
154
```
155
156
[Async Operations](./async-operations.md)
157
158
### Time-based Operations
159
160
Operators for time-based operations including delay, throttle, debounce, and interval scheduling.
161
162
```javascript { .api }
163
// Time-based methods
164
observable.delay(1000);
165
observable.debounce(500);
166
observable.throttle(100);
167
observable.timeout(5000);
168
observable.sample(Rx.Observable.interval(1000));
169
```
170
171
[Time-based Operations](./time-operations.md)
172
173
### Subject Types
174
175
Hot observables that can multicast values to multiple observers and act as both observable and observer.
176
177
```javascript { .api }
178
// Subject types
179
var subject = new Rx.Subject();
180
var behaviorSubject = new Rx.BehaviorSubject(initialValue);
181
var replaySubject = new Rx.ReplaySubject(bufferSize);
182
var asyncSubject = new Rx.AsyncSubject();
183
```
184
185
[Subject Types](./subjects.md)
186
187
### Schedulers
188
189
Control timing and concurrency of observable operations with different scheduling strategies.
190
191
```javascript { .api }
192
// Scheduler types
193
Rx.Scheduler.immediate;
194
Rx.Scheduler.currentThread;
195
Rx.Scheduler.default;
196
197
// Scheduling methods
198
scheduler.schedule(state, action);
199
scheduler.scheduleFuture(state, dueTime, action);
200
scheduler.scheduleRecursive(state, action);
201
```
202
203
[Schedulers](./schedulers.md)
204
205
### Testing Support
206
207
Testing utilities for unit testing reactive code with virtual time and marble testing.
208
209
```javascript { .api }
210
// Testing utilities
211
var testScheduler = new Rx.TestScheduler();
212
var hotObservable = testScheduler.createHotObservable(/* records */);
213
var coldObservable = testScheduler.createColdObservable(/* records */);
214
```
215
216
[Testing Support](./testing.md)
217
218
### Disposables
219
220
Resource management objects for cleaning up subscriptions and managing the lifetime of observables and observers.
221
222
```javascript { .api }
223
// Disposable types
224
var disposable = Rx.Disposable.create(action);
225
var composite = new Rx.CompositeDisposable();
226
var serial = new Rx.SerialDisposable();
227
var singleAssignment = new Rx.SingleAssignmentDisposable();
228
var refCount = new Rx.RefCountDisposable(disposable);
229
```
230
231
[Disposables](./disposables.md)
232
233
## Types
234
235
```javascript { .api }
236
// Core interfaces (documentation purposes - JavaScript runtime doesn't have these)
237
238
interface Observable<T> {
239
subscribe(observer: Observer<T>): Disposable;
240
subscribe(onNext?: function, onError?: function, onCompleted?: function): Disposable;
241
}
242
243
interface Observer<T> {
244
onNext(value: T): void;
245
onError(error: any): void;
246
onCompleted(): void;
247
}
248
249
interface Disposable {
250
dispose(): void;
251
}
252
253
interface Subject<T> extends Observable<T>, Observer<T> {
254
hasObservers(): boolean;
255
}
256
257
interface Scheduler {
258
now(): number;
259
schedule(state: any, action: function): Disposable;
260
scheduleFuture(state: any, dueTime: number, action: function): Disposable;
261
scheduleRecursive(state: any, action: function): Disposable;
262
schedulePeriodic(state: any, period: number, action: function): Disposable;
263
}
264
```