0

AFAIK, observable is lazy.

import * as rxjs from 'rxjs'
const { filter, take, map } = rxjs.operators

function awesomeOpators() {
  return take(1);
}

const numbers$ = new rxjs.Subject<number>();
const start$ = numbers$.pipe(
  awesomeOpators(),
);


numbers$.next(1);

start$.subscribe((val) => {
  // outputs 2
  console.log(val)
})

numbers$.next(2)

How can I rewrite awesomeOpators such that beginWithLargeNumbe$ starts with 1?
https://stackblitz.com/edit/rxjs-zqkz7r?file=main.ts

Zen
  • 5,065
  • 8
  • 29
  • 49

1 Answers1

0

You can use a ReplaySubject instead:

const numbers$ = new rxjs.ReplaySubject<number>();

There's a bunch of other ways as well. You haven't told us which part you can or can't control, though, so this will do.

Ingo Bürk
  • 19,263
  • 6
  • 66
  • 100
  • Yeah, ReplaySubject should work. But I want to know how to pipe `numbers$` such that `start$` starts with 1. – Zen Feb 02 '18 at 15:29
  • Just multicast it through a separate ReplaySubject then which you immediately connect. You have to connect before the first value is emitted, otherwise the value is just lost. – Ingo Bürk Feb 02 '18 at 17:45
  • I've updated my question. Can you please tell me what code I should put into `function awesomeOpators`? – Zen Feb 03 '18 at 02:34