Skip to content

Instantly share code, notes, and snippets.

@makomweb
Created January 31, 2023 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makomweb/80193547f4b4836188db4759a3ae02e1 to your computer and use it in GitHub Desktop.
Save makomweb/80193547f4b4836188db4759a3ae02e1 to your computer and use it in GitHub Desktop.
Rx
<?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