RxPHP / RxHttp

Http Client for RxPHP
MIT License
22 stars 2 forks source link

->timeout() timing out #1

Closed alexsup closed 8 years ago

alexsup commented 8 years ago

Hello :)

When using RxHttp with ->timeout(), the timeout() behaves like a timer.

It simply hangs for the specific length of time in timeout() and then triggers the error state.

Here is example code:

$users = [
    "user_id_1",
    "user_id_2",
];

$loop = React\EventLoop\Factory::create();

$scheduler = new Rx\Scheduler\EventLoopScheduler($loop);

$observable = new Rx\Observable\ArrayObservable($users, $scheduler);

$selectManyObservable = $observable->flatMap(function($value) {
    return \Rx\React\Http::get("url{$value}");

}, $scheduler)
->timeout(5000);

$selectManyObservable->subscribe($createStdoutObserver(), $scheduler);

$loop->run();

Am I simply misunderstanding the use of timeout?

Thanks :)

AB

davidwdan commented 8 years ago

@alexsup

The issue is that you can only have one loop running at a time and since RxHttp uses a static loop you need to make sure that the scheduler and RxHttp use the same loop. You can do that two different ways. If you already have a loop you can set it as the static \EventLoop\setLoop($loop); or you can use the static loop in the EventLoopScheduler. I've updated your example to show how to do that:


$users = [
    "user_id_1",
    "user_id_2",
];

$loop = \EventLoop\getLoop();
$scheduler = new Rx\Scheduler\EventLoopScheduler($loop);

$observable = \Rx\Observable::fromArray($users);

$selectManyObservable = $observable
    ->flatMap(function ($value) {
        return \Rx\React\Http::get("http://www.google.com");
    })
    ->timeout(5000);

$selectManyObservable->subscribe($createStdoutObserver(), $scheduler);
alexsup commented 8 years ago

@davidwdan Thank you so much! All working now :)