-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 497a6bb
Showing
16 changed files
with
455 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/composer.lock | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
language: php | ||
sudo: required | ||
|
||
php: | ||
- 5.6 | ||
- 7 | ||
- hhvm | ||
|
||
matrix: | ||
allow_failures: | ||
- php: hhvm | ||
|
||
install: | ||
- composer install | ||
|
||
script: | ||
- phpunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Voryx LLC | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Await for RxPHP | ||
|
||
|
||
This library will allow observables to block until complete. For use when combining observables with imperative code. | ||
|
||
|
||
It uses the [Voryx event-loop](https://github.com/voryx/event-loop) which behaves like the Javascript event-loop. ie. You don't need to start it. | ||
|
||
|
||
### Basic Example | ||
|
||
```PHP | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
//Do some aysnc craziness with observables | ||
$observable = \Rx\Observable::interval(1000); | ||
|
||
//Returns a `Generator` with the results of the observable | ||
$generator = \Rx\await($observable); | ||
|
||
//You can now use the results like a regular `Iterator` | ||
foreach ($generator as $item) { | ||
|
||
//Will block here until the observable completes | ||
echo $item, PHP_EOL; | ||
} | ||
|
||
|
||
``` | ||
|
||
|
||
### Timeout Example | ||
|
||
Since observables can return 1 to an infinite number of results, you'll need to make sure that you either limit the number of items you take or use a timeout or it could block forever. | ||
|
||
|
||
```PHP | ||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$source = \Rx\Observable::interval(1000) | ||
->takeUntil(\Rx\Observable::timer(10000)); //timeout after 10 seconds | ||
|
||
$generator = \Rx\await($source); | ||
|
||
foreach ($generator as $item) { | ||
echo $item, PHP_EOL; | ||
} | ||
|
||
echo "DONE"; | ||
|
||
``` | ||
|
||
|
||
|
||
```PHP | ||
|
||
$source = \Rx\Observable::interval(1000) | ||
->take(5); //Limit items to 5 | ||
|
||
$generator = \Rx\await($source); | ||
|
||
foreach ($generator as $item) { | ||
echo $item, PHP_EOL; | ||
} | ||
|
||
echo "DONE"; | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "rx/await", | ||
"type": "library", | ||
"description": "Provides await functionality for RxPHP", | ||
"keywords": [ | ||
"rxphp", | ||
"reactivex", | ||
"rx.php", | ||
"await", | ||
"reactphp" | ||
], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "David Dan", | ||
"email": "[email protected]", | ||
"role": "Developer" | ||
}, | ||
{ | ||
"name": "Matt Bonneau", | ||
"email": "[email protected]", | ||
"role": "Developer" | ||
} | ||
], | ||
"autoload": { | ||
"files": [ | ||
"src/functions.php" | ||
] | ||
}, | ||
"require": { | ||
"reactivex/rxphp": "^1.0", | ||
"voryx/event-loop": "^0.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
|
||
$source = \Rx\Observable::range(0, 5)->delay(1000); | ||
|
||
$generator = \Rx\await($source); | ||
|
||
|
||
foreach ($generator as $item) { | ||
echo $item, PHP_EOL; | ||
} | ||
|
||
echo "DONE"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
|
||
$source = \Rx\Observable::range(0, 5)->delay(1000); | ||
|
||
/** @var Generator $generator */ | ||
$generator = \Rx\await($source); | ||
|
||
|
||
echo $generator->current(), PHP_EOL; //0 | ||
|
||
$generator->next(); | ||
echo $generator->current(), PHP_EOL; //1 | ||
|
||
$generator->next(); | ||
echo $generator->current(), PHP_EOL; //2 | ||
|
||
$generator->next(); | ||
echo $generator->current(), PHP_EOL; //3 | ||
|
||
$generator->next(); | ||
echo $generator->current(), PHP_EOL; //4 | ||
|
||
|
||
echo "DONE"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
|
||
$source = \Rx\Observable::error(new Exception('some error')); | ||
|
||
$generator = \Rx\await($source); | ||
|
||
try { | ||
foreach ($generator as $item) { | ||
echo $item, PHP_EOL; | ||
} | ||
} catch (\Exception $e) { | ||
echo "caught error: ", $e->getMessage(); | ||
} | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
//This example requires https://github.com/RxPHP/RxHttp | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$terms = ["rxphp", "php", "make php great again"]; | ||
$search = \Rx\Observable::fromArray($terms) | ||
->map(function ($term) { | ||
return urlencode($term); | ||
}) | ||
->flatMap(function ($term) { | ||
return \Rx\React\Http::get("http://www.google.com/search?q={$term}") | ||
->map(function ($result) use ($term) { | ||
return [ | ||
"term" => $term, | ||
"page" => $result | ||
]; | ||
}); | ||
}); | ||
|
||
|
||
$generator = \Rx\await($search); | ||
|
||
echo "BLOCKING", PHP_EOL; | ||
|
||
foreach ($generator as $item) { | ||
echo "Result page for: {$item['term']}", PHP_EOL, $item['page']; | ||
} | ||
|
||
echo "DONE"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
|
||
$source = \Rx\Observable::interval(1000); | ||
|
||
$generator = \Rx\await($source); | ||
|
||
|
||
foreach ($generator as $item) { | ||
echo $item, PHP_EOL; | ||
} | ||
|
||
echo "DONE"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
|
||
$source = \Rx\Observable::interval(1000) | ||
->take(5); //Take the first 5 | ||
|
||
$generator = \Rx\await($source); | ||
|
||
foreach ($generator as $item) { | ||
echo $item, PHP_EOL; | ||
} | ||
|
||
echo "DONE"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
|
||
$source = \Rx\Observable::interval(1000) | ||
->takeUntil(\Rx\Observable::timer(10000)); //timeout after 10 seconds | ||
|
||
$generator = \Rx\await($source); | ||
|
||
foreach ($generator as $item) { | ||
echo $item, PHP_EOL; | ||
} | ||
|
||
echo "DONE"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<phpunit | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
bootstrap="tests/bootstrap.php"> | ||
<testsuites> | ||
<testsuite name="Rx/Await"> | ||
<directory>tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Rx; | ||
|
||
use Rx\Observer\CallbackObserver; | ||
use Rx\Scheduler\EventLoopScheduler; | ||
|
||
/** | ||
* Wait until observable completes. | ||
* | ||
* @param Observable|ObservableInterface $observable | ||
* @param EventLoopScheduler $scheduler | ||
* @return \Generator | ||
*/ | ||
function await(Observable $observable, EventLoopScheduler $scheduler = null) | ||
{ | ||
|
||
$completed = false; | ||
$results = []; | ||
$scheduler = $scheduler ?: new EventLoopScheduler(\EventLoop\getLoop()); | ||
|
||
$observable | ||
->subscribe(new CallbackObserver( | ||
function ($value) use (&$results, &$results) { | ||
$results[] = $value; | ||
|
||
\EventLoop\getLoop()->stop(); | ||
}, | ||
function ($e) use (&$completed) { | ||
$completed = true; | ||
throw $e; | ||
}, | ||
function () use (&$completed) { | ||
$completed = true; | ||
} | ||
|
||
), $scheduler); | ||
|
||
while (!$completed) { | ||
|
||
\EventLoop\getLoop()->run(); | ||
|
||
foreach ($results as $result) { | ||
yield $result; | ||
} | ||
|
||
$results = []; | ||
} | ||
} |
Oops, something went wrong.