Skip to content

Instantly share code, notes, and snippets.

@hironytic
Last active December 13, 2015 15:14
Show Gist options
  • Select an option

  • Save hironytic/1de3e585d2f36f3ed3e0 to your computer and use it in GitHub Desktop.

Select an option

Save hironytic/1de3e585d2f36f3ed3e0 to your computer and use it in GitHub Desktop.
subscribeOn() v.s. observeOn()
import Rx from "rx-lite-extras";
const observable1 = Rx.Observable.create(observer => {
console.log("[A] call onNext(1)");
observer.onNext(1);
console.log("[A] called onNext(1)");
console.log("[B] call onNext(2)");
observer.onNext(2);
console.log("[B] called onNext(2)");
console.log("[C] call onCompleted()");
observer.onCompleted();
console.log("[C] called onCompleted()");
})
const observable2 = observable1
// .subscribeOn(Rx.Scheduler.async) // ...(1)
// .observeOn(Rx.Scheduler.async) // ...(2)
console.log("[D] call subscribe()");
observable2
.subscribe(value => {
console.log("next: " + value);
}, error => {
console.log("error: " + error);
}, () => {
console.log("completed");
})
console.log("[D] called subscribe()");

(1)subscribeOnも、(2)observeOnもない場合

const observable2 = observable1
//  .subscribeOn(Rx.Scheduler.async)    // ...(1)
//  .observeOn(Rx.Scheduler.async)      // ...(2)
[D] call subscribe()
[A] call onNext(1)
next: 1
[A] called onNext(1)
[B] call onNext(2)
next: 2
[B] called onNext(2)
[C] call onCompleted()
completed
[C] called onCompleted()
[D] called subscribe()

(1)subscribeOnがあって、(2)observeOnがない場合

const observable2 = observable1
  .subscribeOn(Rx.Scheduler.async)    // ...(1)
//  .observeOn(Rx.Scheduler.async)      // ...(2)
[D] call subscribe()
[D] called subscribe()
[A] call onNext(1)
next: 1
[A] called onNext(1)
[B] call onNext(2)
next: 2
[B] called onNext(2)
[C] call onCompleted()
completed
[C] called onCompleted()

(1)subscribeOnがなくて、(2)observeOnがある場合

const observable2 = observable1
//  .subscribeOn(Rx.Scheduler.async)    // ...(1)
  .observeOn(Rx.Scheduler.async)      // ...(2)
[D] call subscribe()
[A] call onNext(1)
[A] called onNext(1)
[B] call onNext(2)
[B] called onNext(2)
[C] call onCompleted()
[C] called onCompleted()
[D] called subscribe()
next: 1
next: 2
completed

(1)subscribeOnも、(2)observeOnもある場合

const observable2 = observable1
  .subscribeOn(Rx.Scheduler.async)    // ...(1)
  .observeOn(Rx.Scheduler.async)      // ...(2)
[D] call subscribe()
[D] called subscribe()
[A] call onNext(1)
[A] called onNext(1)
[B] call onNext(2)
[B] called onNext(2)
[C] call onCompleted()
[C] called onCompleted()
next: 1
next: 2
completed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment