Skip to content

cache behaving unexpected #1718

@Dorus

Description

@Dorus
const source = Rx.Observable.merge(
      Observable.interval(100),
      Observable.of(42).do(() => console.log("  effect A"))
  );

const source2 = Observable.of(52).do(() => console.log("  effect B"));
const o1 = source.cache(1);
const o2 = source2.cache(1);
const ob = (name) => ({next: x => console.log(name + x), complete: () => console.log(name + "c")})
o1.take(3).subscribe(ob("a1 "));
o2.take(1).subscribe(ob("a2 "));
setTimeout(() => {
  o1.take(3).subscribe(ob("b1 "));
  o2.take(1).subscribe(ob("b2 "));
}, 500);

// setTimeout(() => {
//   o1.take(3).subscribe(ob("c1 "));
//   o2.take(1).subscribe(ob("c2 "));
// }, 1000);

// setTimeout(() => {
//   o1.take(3).subscribe(ob("d1 "));
//   o2.take(1).subscribe(ob("d2 "));
// }, 1500);

result in:

"  effect A"
"a1 42"
"  effect B"
"a2 52"
"a2 c"
"a1 0"
"a1 1"
"a1 c"
"b1 1"
"  effect A"
"b1 42"
"b2 52"
"b2 c"
"b1 0"
"b1 c"

What causes effect B to only play once, but effect A to play twice? Playing around with the code randomly seems to cause side-effects to appear and disappear on the source.

Because cache works like publishReplay().refCount(), i would expect effect B to also play again after a2 c (a2 complete), but b2 does not seem to query the source again.

On a possibly unrelated note, cache combines poorly with cold observables (or startWith) because it replays later values and then subscribes again. But this behavior is rather random because of the bug above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions