Skip to content

Creation Functions - Observableの作成と結合

RxJSでは、Observableを作成するためのCreation Functionsと、既存のObservableを変換するPipeable Operatorsという2つの異なる形式があります。

このページでは、Creation Functionsの基本概念と、代表的な結合系Creation Functionsについて解説します。

Creation Functions とは

Creation Functionsは、新しいObservableを作成するための関数です。

typescript
import { of, from, interval } from 'rxjs';

// Creation Functionとして使用
const obs1$ = of(1, 2, 3);
const obs2$ = from([4, 5, 6]);
const obs3$ = interval(1000);

これらはrxjsパッケージから直接インポートし、関数として呼び出してObservableを生成します。

Pipeable Operator との違い

特徴Creation FunctionPipeable Operator
用途新しいObservableを作成既存のObservableを変換
インポート元rxjsrxjs/operators
使用方法関数として直接呼び出し.pipe() 内で使用
concat(obs1$, obs2$)obs1$.pipe(concatWith(obs2$))

Creation Function の例

typescript
import { concat, of } from 'rxjs';

const obs1$ = of(1, 2, 3);
const obs2$ = of(4, 5, 6);

// Creation Function として使用
concat(obs1$, obs2$).subscribe(console.log);
// 出力: 1, 2, 3, 4, 5, 6

Pipeable Operator の例

typescript
import { of } from 'rxjs';
import { concatWith } from 'rxjs';

const obs1$ = of(1, 2, 3);
const obs2$ = of(4, 5, 6);

// Pipeable Operator として使用
obs1$.pipe(
  concatWith(obs2$)
).subscribe(console.log);
// 出力: 1, 2, 3, 4, 5, 6

使い分けの基準

Creation Function を使うべき場合

  • 複数のObservableを同じレベルで結合する場合

    typescript
    concat(obs1$, obs2$, obs3$)
    merge(click$, hover$, scroll$)
  • 最初からObservableを作成する場合

    typescript
    of(1, 2, 3)
    from([1, 2, 3])
    interval(1000)

Pipeable Operator を使うべき場合

  • 既存のObservableに処理を追加する場合

    typescript
    obs1$.pipe(
      map(x => x * 2),
      concatWith(obs2$),
      filter(x => x > 5)
    )
  • パイプラインとして複数の操作を連鎖させる場合

基本的な Creation Functions(復習)

2章で学んだ基本的なCreation Functionsを振り返ります。

Function説明
of指定した値を順番に発行of(1, 2, 3)
from配列、Promise等から変換from([1, 2, 3])
interval指定間隔で連続発行interval(1000)
timer遅延後に発行開始timer(1000, 500)

詳細は Observableの作成方法 を参照してください。

結合系 Creation Functions

複数のObservableを結合するための主要なCreation Functionsです。

Function説明ユースケース
concat順次結合(前が完了後、次が開始)ステップバイステップ処理
merge並行結合(同時購読、発行順に出力)複数イベントの統合
combineLatest最新値を組み合わせフォーム入力の同期
zip対応する値をペア化リクエストとレスポンスの対応
race最初に発行したものを採用複数データソースの競争
forkJoinすべての完了を待って最終値を結合並列API呼び出しの完了待ち
partition条件で2つに分割成功/失敗の分岐処理

各Creation Functionの詳細は、リンクをクリックして参照してください。

Pipeable Operator との対応関係

多くの結合系Creation Functionsには、対応するPipeable Operatorが存在します。

Creation FunctionPipeable Operator備考
concat(a$, b$)a$.pipe(concatWith(b$))RxJS 7+
merge(a$, b$)a$.pipe(mergeWith(b$))RxJS 7+
zip(a$, b$)a$.pipe(zipWith(b$))RxJS 7+
combineLatest([a$, b$])a$.pipe(combineLatestWith(b$))RxJS 7+
race(a$, b$)a$.pipe(raceWith(b$))RxJS 7+

NOTE

RxJS 7以降、concatWith, mergeWith, zipWith, combineLatestWith, raceWith などの~With系オペレーターが追加され、Pipeable Operatorとしても使いやすくなりました。

どちらを使うべきか?

Creation Function を推奨

typescript
// ✅ 複数のObservableを同じレベルで結合
const combined$ = merge(
  fromEvent(button1, 'click'),
  fromEvent(button2, 'click'),
  fromEvent(button3, 'click')
);

Pipeable Operator を推奨

typescript
// ✅ パイプラインの一部として結合
const result$ = source$.pipe(
  map(x => x * 2),
  mergeWith(other$),
  filter(x => x > 10)
);

まとめ

  • Creation Functions: 新しいObservableを作成する関数
  • Pipeable Operators: 既存のObservableを変換する関数
  • 結合系Creation Functionsは用途に応じて使い分ける
  • パイプラインの中では~With系のPipeable Operatorを使う

次のセクションでは、各Creation Functionの詳細な使い方と実践例を学びます。

Released under the CC-BY-4.0 license.