rxjs はリアクティブプログラミングのための公式ライブラリであり、Observable を使って非同期イベントストリームを処理します。rxjs-compat は、RxJS 5 から RxJS 6 への移行を容易にするために作られた互換性パッケージです。現在は RxJS 7 以降が主流であり、rxjs-compat はレガシーなコードベースの維持以外では使用されません。
rxjs と rxjs-compat は、同じ ReactiveX のエコシステムに属していますが、その役割と寿命は全く異なります。rxjs は現在も活発に開発されている本家ライブラリであり、rxjs-compat は過去のバージョン(RxJS 5)から新しいバージョン(RxJS 6)へ移行するための一時的な橋渡し役でした。現代のフロントエンド開発において、この二つを並べて比較することは、実は「現在進行形のツール」対「過去の遺産」を比較することに近いです。
rxjs は、非同期プログラミングを Observable パターンで処理するための標準ライブラリです。
// rxjs: 現在の標準的な使い方
import { of, fromEvent } from 'rxjs';
import { map } from 'rxjs/operators';
const source = of(1, 2, 3);
source.pipe(map(x => x * 2)).subscribe(console.log);
rxjs-compat は、RxJS 5 の書き方を RxJS 6 環境で動かすための互換性レイヤーです。
rxjs をラップして、古い import パスを通るようにしています。// rxjs-compat: 古い書き方(現在は非推奨)
import 'rxjs-compat'; // これだけで RxJS 5 のグローバル設定が有効になる
import { Observable } from 'rxjs/Observable'; // 古いパス
// 上記は現在の rxjs では動作しません
この二つの最大の違いは、コード内でどのように機能を読み込むかです。
rxjs では、ES6 モジュールの標準的な構文を使用します。
// rxjs: 明示的な import
import { BehaviorSubject } from 'rxjs';
import { filter } from 'rxjs/operators';
const subject = new BehaviorSubject(0);
rxjs-compat を使っていた頃は、RxJS 5 のような「すべてを一度に読み込む」スタイルが可能でした。
rxjs/Rx からの import を可能にしていました。// rxjs-compat: 古いスタイル(パッチ適用後)
import { Observable } from 'rxjs/Rx';
// 上記は rxjs-compat を入れていないとエラーになります
アーキテクチャの観点から、rxjs-compat を使い続けることは技術的負債を抱えることを意味します。
rxjs は、モダンなビルドプロセスと相性が良いです。
// rxjs: バンドル最適化の恩恵を受けられる
// 使っていない operator は最終ビルドから削除される
import { take } from 'rxjs/operators';
rxjs-compat は、互換性を保つために追加のコードをロードします。
rxjs に加えて、互換性レイヤー分のサイズが増えます。// rxjs-compat: 余分なレイヤーが存在する
// 内部で古い API を新しい API に変換する処理が走るためオーバーヘッドがある
これが最も重要な判断基準です。
rxjs は現在もアクティブに開発されています。
// rxjs: 最新の型定義が利用可能
const source: Observable<number> = of(1);
rxjs-compat は事実上廃止(Deprecated)されています。
// rxjs-compat: 型定義が古く、最新の TS と相性が悪い場合がある
// エラーが発生しても、互換性レイヤー経由のため原因特定が難しい
もしあなたが今 rxjs-compat を使っているなら、以下の手順で rxjs へ移行すべきです。
rxjs-compat をアンインストールする。import 'rxjs/Rx' などの古い記述を、import { ... } from 'rxjs' に書き換える。rxjs-marbles などのテストツールも最新版に合わせる。// 移行前 (rxjs-compat 使用)
import 'rxjs-compat';
Observable.of(1).subscribe();
// 移行後 (rxjs 使用)
import { of } from 'rxjs';
of(1).subscribe();
| 特徴 | rxjs | rxjs-compat |
|---|---|---|
| ステータス | ✅ アクティブ(推奨) | ❌ 廃止(非推奨) |
| 目的 | 非同期処理の標準実装 | RxJS 5→6 移行のためのつなぎ |
| Import 構文 | import { ... } from 'rxjs' | import 'rxjs/Rx' (パッチ) |
| バンドルサイズ | 最適化可能 | 互換性コードで増大 |
| RxJS 7 対応 | ✅ 対応 | ❌ 非対応 |
| 使用場面 | すべての新規・既存プロジェクト | 移行中のレガシーコードのみ |
rxjs を選ぶアーキテクトとしての明確な推奨は、rxjs 一択です。
rxjs-compat は、2018 年頃の RxJS 6 リリース時に発生した「破壊的変更」を吸収するための緊急措置でした。それから数年が経過し、エコシステムは完全に新しい構文に移行しています。今さら互換性レイヤーに依存することは、パフォーマンスの低下と、将来的なバージョンアップの困難さを招くだけです。
もしレガシーなコードベースを維持している場合でも、rxjs-compat に頼り続けるのではなく、少しずつでも標準的な rxjs の import 構文に書き換える「リファクタリングタスク」をバックログに追加すべきです。それが、長期的なプロジェクトの健全性を守ることになります。
新しいプロジェクトや既存プロジェクトの更新では、必ず rxjs を選択してください。これは公式の最新バージョンであり、すべての機能、パフォーマンス改善、セキュリティ修正が含まれています。モダンな import 構文(import { from } from 'rxjs')を使用することで、バンドルサイズも最適化されます。
このパッケージは、RxJS 5 のコード(import 'rxjs/Rx' など)を RxJS 6 に移行する間の「つなぎ」として設計されました。現在は RxJS 7 以降でサポートが終了しているため、新規プロジェクトでは絶対に使用しないでください。既存のレガシーシステムで、すぐに書き換えられない場合のみ一時的に検討しますが、早急な移行を推奨します。
RxJS: Reactive Extensions For JavaScriptCurious what's next for RxJS? Follow along with Issue 6367.
Reactive Extensions Library for JavaScript. This is a rewrite of Reactive-Extensions/RxJS and is the latest production-ready version of RxJS. This rewrite is meant to have better performance, better modularity, better debuggable call stacks, while staying mostly backwards compatible, with some breaking changes that reduce the API surface.
Most PRs should be made to master.
By contributing or commenting on issues in this repository, whether you've read them or not, you're agreeing to the Contributor Code of Conduct. Much like traffic laws, ignorance doesn't grant you immunity.
npm install rxjs
It's recommended to pull in the Observable creation methods you need directly from 'rxjs' as shown below with range.
If you're using RxJS version 7.2 or above, you can pull in any operator you need from the same spot, 'rxjs'.
import { range, filter, map } from 'rxjs';
range(1, 200)
.pipe(
filter(x => x % 2 === 1),
map(x => x + x)
)
.subscribe(x => console.log(x));
If you're using RxJS version below 7.2, you can pull in any operator you need from one spot, under 'rxjs/operators'.
import { range } from 'rxjs';
import { filter, map } from 'rxjs/operators';
range(1, 200)
.pipe(
filter(x => x % 2 === 1),
map(x => x + x)
)
.subscribe(x => console.log(x));
For CDN, you can use unpkg:
https://unpkg.com/rxjs@^7/dist/bundles/rxjs.umd.min.js
The global namespace for rxjs is rxjs:
const { range } = rxjs;
const { filter, map } = rxjs.operators;
range(1, 200)
.pipe(
filter(x => x % 2 === 1),
map(x => x + x)
)
.subscribe(x => console.log(x));
npm run compile build everythingnpm test run testsnpm run dtslint run dtslint testsWe appreciate all contributions to the documentation of any type. All of the information needed to get the docs app up and running locally as well as how to contribute can be found in the documentation directory.