08 April 2020

RXJS Subject vs Behavior vs Replay vs Async

RXJS Subject vs Behavior vs Replay vs Async
rxjs-subject-vs-behavior-vs-replay-vs-async
Rxjs 2020
console.clear();
import * as Rx  from 'rxjs';

// Output: 'b'
const subject = new Rx.Subject();
subject.next('a'); // Không được phép
subject.subscribe(x => console.log('Subscriber A:', x));
subject.next('b');

// Output: 2, 3
const subject1 = new Rx.BehaviorSubject(1); // Có default value
subject1.next(2);
subject1.subscribe(x => console.log('Subscriber C:', x));
subject1.next(3);

// Output: '13', '14'
const bufferSize = 2;
const subject4 = new Rx.ReplaySubject<string>(bufferSize);
subject4.next("11");
subject4.next("12");
subject4.next("13");
subject4.next("14");
subject4.subscribe(x => console.log('Subscriber E:', x));

// Output: 'c'
const subject3 = new Rx.AsyncSubject<string>();
subject3.next("a");
subject3.next("b");
subject3.next("c");
subject3.subscribe(x => console.log('Subscriber D:', x));
subject3.complete(); // Cần có complete

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang