RxJS - Multicasting Operator pubblica
L'operatore publish () restituisce ConnectableObservable e deve utilizzare il metodo connect () per iscriversi agli osservabili.
Sintassi
publish()
Esempio
import { interval,Observable } from 'rxjs';
import { filter, publish } from 'rxjs/operators';
var observable = new Observable(function subscribe(subscriber) {
try {
subscriber.next(Math.random());
} catch (e) {
subscriber.error(e);
}
});
const observable1 = publish()(observable);
const subscribe_one = observable1.subscribe(
x => console.log("Value from Sub1 = "+x)
);
const subscribe_two = observable1.subscribe(
x => console.log("Value from Sub2 = "+x)
);
observable1.connect();