ReactiveX / reactivex.github.io

ReactiveX Website
Apache License 2.0
139 stars 139 forks source link

Document new hook management facility #262

Open DavidMGross opened 8 years ago

DavidMGross commented 8 years ago

See ReactiveX/RxJava#4007

As of the filing of this issue, it's still in the planning/proposal stage, but if it gets merged, it'll need some documentation.

Hooks are currently documented over at the RxJava wiki (https://github.com/ReactiveX/RxJava/wiki/Plugins) but it's probably time to consolidate the RxJava documentation over here (see also #238).

DavidMGross commented 8 years ago

From the 1.1.7 release notes:

PR #4007 introduced a new way of hooking into the lifecycle of the base reactive types (Observable, Single, Completable) and the Schedulers. The original RxJavaPlugins' design was too much focused on class-initialization time hooking and didn't properly allow hooking up different behavior after that. There is a reset() available on it but sometimes that doesn't work as expected.

The new class rx.plugins.RxJavaHooks allows changing the hooks at runtime, allowing tests to temporarily hook onto an internal behavior and then un-hook once the test completed.

RxJavaHooks.setOnObservableCreate(s -> { System.out.println("Observable created"); return s; });

Observable.just(1).subscribe(System.out::println);

RxJavaHooks.reset(); // or RxJavaHooks.setOnObservableCreate(null); It is now also possible to override what Schedulers the Schedulers.computation(), .io() and .newThread() returns without the need to fiddle with Schedulers' own reset behavior:

RxJavaHooks.setOnComputationScheduler(current -> Schedulers.immediate());

Observable.just(1).subscribeOn(Schedulers.computation()) .subscribe(v -> System.out.println(Thread.currentThread())); By default, all RxJavaHooks delegate to the original RxJavaPlugins callbacks so if you have hooks the old way, they still work. RxJavaHooks.reset() resets to this delegation and RxJavaHooks.clear() clears all hooks (i.e., everything becomes a pass-through hook).

DavidMGross commented 8 years ago

See also ReactiveX/RxJava#4173 for GenericScheduledExecutorService facility in RxJavaHooks