RxJS Lite (Legacy v4)
rx-lite is a lightweight distribution of the Reactive Extensions for JavaScript (RxJS) version 4.x, primarily used for composing asynchronous and event-based operations in JavaScript. At version 4.0.8, it provided a core set of observable operators for handling events, promises, callbacks, and time-based operations in both modern browser environments (IE9+) and Node.js. The library was part of an earlier generation of RxJS, with its development repository transitioning to 'RxJS vNext' (which became RxJS v5 and later) after the 4.x series. This version prioritizes a compact bundle (`rx.lite.js`) for everyday use cases, differing from the more modular, tree-shakable approach adopted in subsequent major RxJS releases. It had an irregular release cadence, focusing on bug fixes and performance improvements within the 3.x and 4.x lines before the major architectural shift to modern RxJS.
Common errors
-
TypeError: (0, _rxLite.Observable) is not a constructor
cause Attempting to use ES module import syntax (`import { Observable } from 'rx-lite';`) with a CommonJS-only package.fixUse CommonJS `require`: `const Rx = require('rx-lite'); const Observable = Rx.Observable;` -
ReferenceError: require is not defined
cause Using `require('rx-lite')` directly in a browser environment without a CommonJS bundler (like Webpack or Browserify).fixFor browser usage, include the `rx.lite.js` file via a `<script>` tag. For bundled applications, ensure your bundler is correctly configured to handle CommonJS modules. -
ReferenceError: Rx is not defined
cause Trying to use `Rx` in the browser without loading the `rx.lite.js` script, or in Node.js without `require('rx-lite')`.fixEnsure `rx.lite.js` is loaded via a `<script>` tag before your code, or add `const Rx = require('rx-lite');` at the top of your Node.js file.
Warnings
- breaking This `rx-lite` package is for RxJS v4. Development transitioned to 'RxJS vNext' (modern `rxjs` v5+), which introduced significant breaking changes, API rewrites, and a new modular import system. `rx-lite` is not compatible with modern RxJS.
- gotcha RxJS v4, including `rx-lite`, primarily targets CommonJS for Node.js and global script tags for browsers. It does not support native ES Modules (ESM) `import` syntax, which will lead to errors.
- breaking In versions prior to 4.0.6, `Rx.Observable.fromPromise(promise)` was known to swallow errors originating from Observable instances within the promise chain, leading to silent failures.
- deprecated The package name `rx-lite` is an artifact of RxJS v4. Modern RxJS uses the package name `rxjs` and does not differentiate between 'lite' and 'core' versions in the same way, as it is tree-shakable by default.
Install
-
npm install rx-lite -
yarn add rx-lite -
pnpm add rx-lite
Imports
- Rx
import Rx from 'rx-lite';
const Rx = require('rx-lite'); - Observable
import { Observable } from 'rx-lite';const Rx = require('rx-lite'); const Observable = Rx.Observable; - fromPromise
import { fromPromise } from 'rx-lite';const Rx = require('rx-lite'); const myObservable = Rx.Observable.fromPromise(somePromise);
Quickstart
const Rx = require('rx-lite');
const source = Rx.Observable.fromArray([1, 2, 3, 4, 5]);
const subscription = source
.map(x => x * 2)
.filter(x => x > 5)
.subscribe(
x => console.log('Next: ' + x),
err => console.error('Error: ' + err),
() => console.log('Completed')
);
// Example with a promise
const somePromise = Promise.resolve('Hello from Promise!');
Rx.Observable.fromPromise(somePromise)
.subscribe(
data => console.log('Promise data: ' + data),
err => console.error('Promise error: ' + err)
);
// To stop listening (important for long-lived observables)
// subscription.dispose();