Last active
September 22, 2017 01:52
-
-
Save sekky0905/b34aaeb6bf5676c502afefba1da16278 to your computer and use it in GitHub Desktop.
RxJSの基本をまとめてみた~基本的な概念編(Observable、Observer、Subscriptionなど)~ ref: http://qiita.com/Sekky0905/items/93bd4804a2003ed0aa8d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// observable.subscribeの戻り値として、subscriptionを返却 | |
// observable.subscribeの内部的に、Observerのオブジェクトが生成されていている。 | |
let subscription = observable.subscribe(val => console.log(val)); | |
// 進行中の実行をキャンセル | |
subscription.unsubscribe(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Observable} from "rxjs"; | |
// Observable.createは、ObservableのコンストラクタのエイリアスでObservableのオブジェクトを生成する | |
// 引数に、subscribe関数を渡す | |
// Observableはcreateで作れるけど、基本的にcreateを使って、Observableを生成することはあまりない | |
// ofとかの別のoperatorから生成する | |
let observable = Observable.create(function subscribe(observer) { | |
try { // 通常時 | |
// データを通知し、送信する | |
// 引数は、実際にObserverに送信されるデータを表している。 | |
observer.next(0); | |
observer.next(1); | |
observer.next(2); | |
observer.next(3); | |
observer.next(4); | |
observer.next(5); | |
// 正常に完了したことを通知する | |
// これが呼ばれた後にnext()しても意味がない | |
observer.complete(); | |
} catch (err) { // 異常時 | |
// エラーを通知する | |
observer.error(err); | |
} | |
}); | |
console.log('開始'); | |
// observable.subscribeの内部的に、Observerのオブジェクトが生成されている | |
observable.subscribe(val => console.log(val)); | |
console.log('終了'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
開始 | |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
終了 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment