【发布时间】:2019-02-09 14:17:12
【问题描述】:
如果源 Observable 是使用 range 而不是 timer 创建的,我试图理解为什么 share RxJs 运算符的工作方式不同。
将原代码改为:
const source = range(1, 1)
.pipe(
share()
)
const example = source.pipe(
tap(() => console.log('***SIDE EFFECT***')),
mapTo('***RESULT***'),
)
const sharedExample = example
const subscribeThree = sharedExample.subscribe(val => console.log(val))
const subscribeFour = sharedExample.subscribe(val => console.log(val))
结果:
console.log src/pipeline/foo.spec.ts:223 副作用
console.log src/pipeline/foo.spec.ts:228 结果
console.log src/pipeline/foo.spec.ts:223 副作用
console.log src/pipeline/foo.spec.ts:229 结果
基本上,副作用会被多次调用。
据我所知,range 应该是一个冷的 observable,但据说 share 应该把冷的 observable 变成热的。
这种行为背后的解释是什么?
【问题讨论】: