Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RXJS on first emit

I'd like to map the value, but only the first time it's emitting. The other times it can pass through. Any ideas?

this.sourceStream$.takeUntil(this.onDestroy$).someFunc((data) => {
  return data['some tranformation']
}).subscribe(data => {

If there any way to do this without a variable to track if the first value has been received?

Thanks!

like image 505
nick Avatar asked Feb 10 '26 08:02

nick


2 Answers

The map operator has a second argument index that is automatically incremented:

this.sourceStream$.map((value, index) =>
  index === 0 ? whatever(value) : value
);
like image 140
martin Avatar answered Feb 13 '26 04:02

martin


What about using a combination of take skip and merge.

Something along these lines should do the trick, even if a bit verbose

const obs = Observable.interval(1000).take(4);

const obs1 = obs.take(1).map(val => 'I am the fist '+ val);
const obs2 = obs.skip(1);

merge(obs1, obs2).subscribe(console.log)
like image 43
Picci Avatar answered Feb 13 '26 04:02

Picci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!