RxPHP / RxHttp

Http Client for RxPHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pass composite items

ameoba32 opened this issue · comments

commented

Sorry if asked in wrong place, I found some help here before, hope for this time:

In the example below, how can I pass $url variable to subscribe function?

\Rx\Observable::fromArray(
    [
        'https://www.example.com/',
        'https://www.example.com/',
        'https://www.example.com/',
    ]
)->flatMap(
    function ($url) {
        return \Rx\React\Http::get($url);
    }
    ->subscribe(function ($data) {
        //  How to access $url variable here?
        echo $data;
    });

Thank you!

There are a couple of different ways that you can accomplish this. You can map in the url after you get the data from http or you can use combineLatest like this:

\Rx\Observable::fromArray([
    'https://www.example.com/',
    'https://www.example.com/',
    'https://www.example.com/',
])
    ->flatMap(function ($url) {
        return \Rx\React\Http::get($url)->combineLatest(Observable::of($url));
    })
    ->subscribe(function ($res) {
        list($data, $url) = $res;

        //  How to access $url variable here?
        echo $data;
    });
commented

David, it works :) Thank you very much! Looks like a voodoo magic for me, I spend 2 hours reading through Rx operators but had no idea which one to use.

Basically, I need this code to save data back to database. Imagine I have table with ID and URL, once I fetch data I need to update correct row by ID. Now I can pass ID to callable where data is ready.

We need to write some blog posts to explain how to do common things. The challenge is that there're almost an unlimited number of ways that operators can be composed.

Next time don't spend two hours, just join rxphp in irc and ask whatever questions you have 😄

commented

Very good idea having examples! I already started gathering, issues I faced, https://github.com/ameoba32/RxHTTP-examples