LoginSignup
1
1

More than 5 years have passed since last update.

Rxjs6.3.3の仕様についての自分用メモ

Last updated at Posted at 2018-10-12

内容

このコード、helloが先に呼ばれます。
ofで初期streamを渡した場合、それらは同期的に宣言した所ですぐにsubscriberが実行されます。

const {
    Observable,
    Subject,  // subscribeしてからの値しか流れないsubject
    BehaviorSubject,  // subscribeした瞬間から以前の値が流れてくるsubject
    of,
} = rxjs;

// 同期的に流れる
of(5, 1).subscribe(value => console.log('hello'));
console.log('world');
// 結果: hello world

const subject = new Subject();
subject.next(1);
subject.subscribe(v => console.log(v));
// consoleには何も出力されない
subject.next(1);
// 1

const behaviorSubject = new BehaviorSubject(500);
const subscriber = behaviorSubject.subscribe(v => console.log(`${v}だよ`));
behaviorSubject.next(1);
subscriber.unsubscribe();
behaviorSubject.subscribe(v => console.log(`${v}は最後の状態だよ`));
// 500だよ
// 1だよ
// 1は最後の状態だよ

DEMO

最後に

前から気になってたのでスッキリ。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1