Reputation: 499
I am trying to do the following:
observable.pipe(
emit-value-to-subscriber(false),
map((value) => doSomethingWithValue(value)),
).subscribe(console.log);
Essentially intercept the value from observable and first emit a false value to the subscriber before piping the processed version of the value.
Is there a good way to do this in RXjs? My current implementation just involves using two subscribers:
observable.pipe(
map(() => false),
).subscribe(console.log);
observable.pipe(
map((value) => doSomethingWithValue(value)),
).subscribe(console.log);
But I have serious doubts about the reliability of doing it this way (order of events).
Upvotes: 0
Views: 2355
Reputation: 82
Solution without concat
and startsWith
- if your want to handle a stream, e.g. false, true, false, false, ...
, you will get a false, %mod false%, true, %mod true% ...
.
Observable.from(false)
.pipe(
flatMap(x$ => from([x$, doSomethingWithAValue(x$)])),
)
Upvotes: 1
Reputation: 8295
There are multiple ways of doing what you want.
Assuming doSomethingWithValue
returns a boolean, you can use startWith
operator:
observable.pipe(
map((value) => doSomethingWithValue(value)),
startWith(false),
).subscribe(console.log);
you can also use concat
:
concat(
of(false),
observable.pipe(map(value => doSomethingWithValue(value)),
).subscribe(console.log);
you can also use concat
inside switchMap
:
observable.pipe(
switchMap(value => concat(of(false), of(doSomethingWithValue(value)))),
).subscribe(console.log);
if there's a different observer, you can use tap
:
observable.pipe(
tap(_ => observer.next(false)),
map((value) => doSomethingWithValue(value)),
).subscribe(console.log);
Upvotes: 3