3
\$\begingroup\$

With RXJS library : I need to store values from the FIRST http request, but keep doing other http request on demand.

I have done this code so far, and that is working. But I think there is a more elegant way.

const Rx = require("rxjs");
let numHttp = 0;
const httpRequest = () => {
    let a = [];
    numHttp = numHttp + 1;
    for (let i = 0; i < 10; ++i) {
        a.push({ num: i, http: numHttp });
    };
    return new Rx.BehaviorSubject(a)
        .do(a => {
            console.log("antoher http request");
            if (getC.getValue() === undefined) {
                getC.next(a)
            }
        })
        // .do(n => { if (getB.) getB.next(n) })
}
const getB = () => {
    const a = new Rx.BehaviorSubject();
    return a
};
getC = getB();
httpRequest().subscribe(d => d);
httpRequest().subscribe(d => d);
getC.subscribe(data => console.log('cache', data))
httpRequest().subscribe(d => d);
httpRequest().subscribe(d => d);
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

It is really hard to solve the problem since the question is not providing all the necessary details.

Assuming you have a factory for your Http observable (call it createHttpObservable()) you may do something like this:

const observable = createHttpObservable()

  // Share the stream of events across all the subscribers.
  .publishReplay(1)
  .refCount()

  // Use `.scan()` against event stream (similarly to how we `.reduce()` against an array).
  .scan(
    (history, currentValue, valueIndex) => {
      history.push({ num: valueIndex, httpNum: currentValue });
      return history;
    },
    []
  )

  // Take the last event from the `history`
  .map(history => history[history.length - 1].httpNum)

  // Arbitrarily use `.do` for side effects.
  .do(console.log);


// Subscribe wherever you need.
observable.subscribe();
observable.subscribe();
observable.subscribe();

Here's a related, but not quite the same exact plunkr.

You can have more custom logic in the .scan() block, but frankly, I have no idea what are you trying to achieve and why is the first Http request's results are of any special interest.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.