Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwdan committed Feb 7, 2016
0 parents commit 497a6bb
Show file tree
Hide file tree
Showing 16 changed files with 455 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/composer.lock
/vendor
17 changes: 17 additions & 0 deletions .travis.yml
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
21 changes: 21 additions & 0 deletions LICENSE
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.
69 changes: 69 additions & 0 deletions README.md
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";

```
34 changes: 34 additions & 0 deletions composer.json
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"
}
}
15 changes: 15 additions & 0 deletions examples/delay_foreach.php
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";
27 changes: 27 additions & 0 deletions examples/delay_next.php
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";
21 changes: 21 additions & 0 deletions examples/errors.php
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();
}





31 changes: 31 additions & 0 deletions examples/httpGet.php
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";
15 changes: 15 additions & 0 deletions examples/interval.php
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";
15 changes: 15 additions & 0 deletions examples/interval_take.php
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";
15 changes: 15 additions & 0 deletions examples/interval_timeout.php
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";
19 changes: 19 additions & 0 deletions phpunit.xml
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>
49 changes: 49 additions & 0 deletions src/functions.php
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 = [];
}
}
Loading

0 comments on commit 497a6bb

Please sign in to comment.