skip to content

materialize - dematerialize

 

To manipulate error and completion events — we can turn them into ordinary value emissions, using materialize.
Then we’ll be able to apply any operator we want: delay, map, filter, etc.
After we're done, we can turn things back to normal using dematerialize:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const { rxObserver } = require('api/v0.3');
const { timer, throwError, Notification } = require('rxjs');
const { switchMap, materialize, dematerialize, delay, map } = require('rxjs/operators');


const source$ = timer(5).pipe(
  switchMap(() => throwError('Err!'))
);

const result$ = source$.pipe(
  // turn all events on stream into Notifications
  materialize(),
  // delay error by 5ms
  delay(5),
  // turn error into a value
  map(n => new Notification('N', n.error, undefined)),
  // turn Notifications back to events on stream
  dematerialize()
);

source$.subscribe(rxObserver('source$'));
result$.subscribe(rxObserver('result$'));

0mssource$startErr!!result$startcompleteErr!Err!