Materialize/Dematerialize

represent both the items emitted and the notifications sent as emitted items, or reverse this process

Materialize

A well-formed, finite Observable will invoke its observer’s onNext method zero or more times, and then will invoke either the onCompleted or onError method exactly once. The Materialize operator converts this series of invocations — both the original onNext notifications and the terminal onCompleted or onError notification — into a series of items emitted by an Observable.

Dematerialize

The Dematerialize operator reverses this process. It operates on an Observable that has previously been transformed by Materialize and returns it to its original form.

See Also

Language-Specific Information:

materialize

In RxGroovy, materialize transforms the notifications from the source Observable into Notification objects and emits them as the emissions from the Observable it returns. For example:

Sample Code

numbers = Observable.from([1, 2, 3]);

numbers.materialize().subscribe(
  { if(rx.Notification.Kind.OnNext == it.kind) { println("Next: " + it.value); }
    else if(rx.Notification.Kind.OnCompleted == it.kind) { println("Completed"); }
    else if(rx.Notification.Kind.OnError == it.kind) { println("Error: " + it.exception); } },
  { println("Error: " + it.getMessage()); }, // onError
  { println("Sequence complete"); }          // onCompleted
);
Next: 1
Next: 2
Next: 3
Completed
Sequence complete

materialize does not by default operate on any particular Scheduler.

dematerialize

dematerialize reverses this process: converting the emitted Notification objects from the source Observable into notifications from the resulting Observable. The following example dematerializes the materialized Observable from the previous section:

Sample Code

numbers = Observable.from([1, 2, 3]);

numbers.materialize().dematerialize().subscribe(
  { println(it); },                          // onNext
  { println("Error: " + it.getMessage()); }, // onError
  { println("Sequence complete"); }          // onCompleted
);
1
2
3
Sequence complete

dematerialize does not by default operate on any particular Scheduler.

materialize

In RxJava, materialize transforms the notifications from the source Observable into Notification objects and emits them as the emissions from the Observable it returns.

materialize does not by default operate on any particular Scheduler.

dematerialize

dematerialize reverses this process: converting the emitted Notification objects from the source Observable into notifications from the resulting Observable.

dematerialize does not by default operate on any particular Scheduler.

dematerialize

RxJS only implements the dematerialize operator. If you want a “materialized” Observable, you have to assemble it by hand by manually creating and emitting the Notification objects that represent Observable notification calls.

Sample Code

var source = Rx.Observable
    .fromArray([
        Rx.Notification.createOnNext(42),
        Rx.Notification.createOnError(new Error('woops'))
    ])
    .dematerialize();

var subscription = source.subscribe(
    function (x) { console.log('Next: ' + x.toString()); },
    function (err) { console.log('Error: ' + err); },
    function () { console.log('Completed'); });
Next: 42
Error: Error: woops

dematerialize is found in each of the following distributions:

  • rx.js
  • rx.all.js
  • rx.all.compat.js
  • rx.compat.js
  • rx.lite.js
  • rx.lite.compat.js

RxPHP implements this operator as materialize.

Materializes the implicit notifications of an observable sequence as explicit notifications.

RxPHP also has an operator dematerialize.

Dematerializes the explicit notification values of an observable sequence as implicit notifications.