Created
January 31, 2023 11:18
-
-
Save makomweb/80193547f4b4836188db4759a3ae02e1 to your computer and use it in GitHub Desktop.
Rx
This file contains hidden or 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
<?php | |
use React\EventLoop\Factory; | |
use Rx\Observable; | |
use Rx\Scheduler; | |
use Rx\Scheduler\EventLoopScheduler; | |
require_once __DIR__ . '/vendor/autoload.php'; | |
$loop = Factory::create(); | |
//You only need to set the default scheduler once | |
Scheduler::setDefaultFactory(function() use($loop){ | |
return new EventLoopScheduler($loop); | |
}); | |
$fruits = ['apple', 'banana', 'orange', 'raspberry']; | |
Observable::fromArray($fruits) | |
->subscribe( | |
function($value) { | |
if (strcmp($value, 'orange') === 0) { | |
throw new Exception('Oranges are not allowed!'); | |
} | |
printf('%s: %d' . PHP_EOL, $value, strlen($value)); | |
}, | |
function (Exception $error) { | |
printf('error: %s' . PHP_EOL, $error->getMessage()); | |
print('(completed)' . PHP_EOL); | |
}, | |
function() { | |
print('completed'. PHP_EOL); | |
} | |
); | |
$loop->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment