puddlejumper26 / blogs

Personal Tech Blogs
4 stars 1 forks source link

Hot Observable和Cold Observable #185

Open puddlejumper26 opened 3 years ago

puddlejumper26 commented 3 years ago

Cold Observable

const source$ = new Observable(subscriber => {
  console.log('stream 開始');
  subscriber.next(1);
  subscriber.next(2);
  subscriber.next(3);
  subscriber.next(4);
  console.log('steam 結束');
  subscriber.complete();
});

source$.subscribe(data => console.log(`Observable 第一次訂閱: ${data}`));
// 1, 2, 3, 4
source$.subscribe(data => console.log(`Observable 第二次訂閱: ${data}`));
// 1, 2, 3, 4

image

Hot Observable

image

image

Cold Observable 和 Hot Observable 之间的相互转换

Source