【问题标题】:Why does scan accumulator value reset when new subscriptions are created but not when using shareReplay(1)?为什么创建新订阅时扫描累加器值会重置,但使用 shareReplay(1) 时不会重置?
【发布时间】:2020-11-02 16:59:53
【问题描述】:

我编写了以下代码,期望最后一次订阅将打印 ['emitted value 1', 'emitted value 2'],但只打印 ['emitted value 2']。这是有道理的,因为 BehaviorSubject 发出最后一个值,但为什么扫描累加器会重置为种子值?

我发现使用 shareReplay(1) 可以轻松解决此问题,但我真的不明白为什么。在这两种情况下,使用 shareReplay(1) 与不使用扫描累加器会发生什么?

const subject = new BehaviorSubject(null);
const action$ = subject.asObservable();

const obs$ = action$.pipe(scan((acc, curr) => [...acc, curr], []));
// const obs$ = action$.pipe(scan((acc, curr) => [...acc, curr], []), shareReplay(1));

obs$.subscribe(res => console.log('first subs', res));

subject.next('emitted value 1');

subject.next('emitter value 2');

obs$.subscribe(res => console.log('second subs', res));

This is the console output without shareReplay(1)

And with shareReplay(1)

【问题讨论】:

  • 那么您对下面的答案有任何疑问吗?
  • 不,我现在明白了。缓存这个词帮助我看清楚了。

标签: javascript rxjs behaviorsubject


【解决方案1】:

当您订阅 BehaviorSubject 时,您将收到最后发出的值。这就是为什么您在控制台中只收到:second subs ["emitter value 2"]。

shareReplay

来自doc:

为什么要使用 shareReplay? 当您有副作用或不希望计算繁重时,您通常希望使用 shareReplay 在多个订阅者之间执行。它也可能是有价值的 您知道您将有迟到的订阅者到流的情况 需要访问以前发出的值。这种重播能力 订阅的价值是 share 和 shareReplay 的区别。

这意味着您将收到所有发出的值而无需任何计算,它是一种缓存。这在控制台中也可见,您只有一个包含第二个订阅的所有值的打印: second subs [null, "emitted value 1", "emitter value 2"]

【讨论】:

    【解决方案2】:

    当您订阅 observable 时,它​​会因为缺少更好的词而“运行”。随着值随着时间的推移而分散,这可能会变得有点令人困惑,所以我们将坚持使用同步调用。考虑以下可观察的。

    const getRandom = () => Math.floor(Math.random() * 10);
    
    const stream$ = defer(() => from([getRandom(), getRandom(), getRandom()]));
    
    stream$.subscribe(console.log); // 4 6 2
    stream$.subscribe(console.log); // 4 4 2
    stream$.subscribe(console.log); // 1 8 6
    stream$.subscribe(console.log); // 8 6 7
    

    注意每个订阅如何获得不同的数字?他们都在重新运行getRandom(),并且他们的订阅获得不同的值。

    如果我们在stream$ 中放置一个累加器,每个订阅都将从一个新种子开始,并根据其流发出的内容获得结果。

    所以:

    const getRandom = () => Math.floor(Math.random() * 10);
    
    const stream$ = defer(
      () => from([getRandom(), getRandom(), getRandom()])
    ).pipe(
      reduce((acc, val) => acc + val, 0)
    );
    
    stream$.subscribe(console.log); // 12 (4+6+2) 
    stream$.subscribe(console.log); // 10 (4+4+2)
    stream$.subscribe(console.log); // 15 (1+8+6)
    stream$.subscribe(console.log); // 21 (8+6+7)
    

    查看每个订阅如何不添加到前一个订阅?它只是将自己的数字相加。

    他们不会在不同订阅之间共享累加器或值或任何东西。

    那么当你使用像shareReplay() 这样的东西时会发生什么?在这种情况下,stream$ 不会被订阅并“运行”4 次不同的时间,而是只订阅一次,并且无论之后有多少人订阅,单次订阅的结果都会共享。

    const getRandom = () => Math.floor(Math.random() * 10);
    
    const stream$ = defer(
      () => from([getRandom(), getRandom(), getRandom()])
    ).pipe(
      reduce((acc, val) => acc + val, 0),
      shareReplay(1)
    );
    
    stream$.subscribe(console.log); // 12 (4+6+2) 
    stream$.subscribe(console.log); // 12
    stream$.subscribe(console.log); // 12
    stream$.subscribe(console.log); // 12
    

    在这里,我们的累加器只运行一次。

    如果你像这样改变它:

    const getRandom = () => Math.floor(Math.random() * 10);
    
    const stream$ = defer(
      () => from([getRandom(), getRandom(), getRandom()])
    ).pipe(
      shareReplay(3),
      reduce((acc, val) => acc + val, 0)
    );
    
    stream$.subscribe(console.log); // 12 (4+6+2) 
    stream$.subscribe(console.log); // 12 (4+6+2) 
    stream$.subscribe(console.log); // 12 (4+6+2) 
    stream$.subscribe(console.log); // 12 (4+6+2) 
    

    然后累加器运行 4 次,但只调用 getRandom() 来创建前 3 个值,之后每个订阅都会获得相同的 3 个数字。


    所以,回答你的问题:

    如果您没有 shareReplay,每个订阅者都会在订阅时开始运行自己的 scan 流。 与 shareReply,scan 仅订阅一次。您将这些结果多播给所有未来的订阅者。这就是区别。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-09
      • 1970-01-01
      • 2016-07-24
      相关资源
      最近更新 更多