-
Notifications
You must be signed in to change notification settings - Fork 0
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 b1907aa
Showing
8 changed files
with
346 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,4 @@ | ||
# Composer ignores | ||
/vendor | ||
composer.phar | ||
composer.lock |
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,137 @@ | ||
# reactphp-framework-asyncify | ||
|
||
## install | ||
|
||
``` | ||
composer require reactphp-framework/asyncify -vvv | ||
``` | ||
|
||
## Usage | ||
|
||
promise | ||
|
||
```php | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Reactphp\Framework\Asyncify\Async; | ||
use React\EventLoop\Loop; | ||
use React\Promise\Deferred; | ||
|
||
$word = 'world'; | ||
Async::call(function () use ($word) { | ||
return [ | ||
'hello' => $word | ||
]; | ||
})->then(function ($data) { | ||
var_dump($data); | ||
}, function ($e) { | ||
var_dump($e->getMessage(), 'reject'); | ||
})->catch(function ($e) { | ||
var_dump($e->getMessage(), 'catch'); | ||
}); | ||
|
||
|
||
Async::call(function () { | ||
return file_get_contents(__DIR__ . '/test.txt'); | ||
})->then(function ($data) { | ||
var_dump($data); | ||
}, function ($e) { | ||
var_dump($e->getMessage(), 'reject'); | ||
})->catch(function ($e) { | ||
var_dump($e->getMessage(), 'catch'); | ||
}); | ||
|
||
Async::call(function () { | ||
$deferred = new Deferred(); | ||
Loop::addTimer(1, function () use ($deferred) { | ||
$deferred->resolve('hello world promise'); | ||
}); | ||
return $deferred->promise(); | ||
})->then(function ($data) { | ||
var_dump($data); | ||
}, function ($e) { | ||
var_dump($e->getMessage(), 'reject'); | ||
})->catch(function ($e) { | ||
var_dump($e->getMessage(), 'catch'); | ||
}); | ||
|
||
Loop::addTimer(2, function () { | ||
Async::terminate(); | ||
}); | ||
|
||
``` | ||
|
||
stream | ||
```php | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Reactphp\Framework\Asyncify\Async; | ||
use React\EventLoop\Loop; | ||
|
||
$word = 'world'; | ||
$stream = Async::call(function () use ($word) { | ||
return [ | ||
'hello' => $word | ||
]; | ||
}, true); | ||
|
||
$stream->on('data', function ($data) { | ||
var_dump($data); | ||
}); | ||
|
||
$stream->on('close', function () { | ||
|
||
}); | ||
|
||
$stream->on('error', function ($e) { | ||
var_dump($e->getMessage(), 'error'); | ||
}); | ||
|
||
|
||
Loop::addTimer(1, function () { | ||
Async::terminate(); | ||
}); | ||
``` | ||
```php | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Reactphp\Framework\Asyncify\Async; | ||
use React\Stream\ThroughStream; | ||
use React\EventLoop\Loop; | ||
|
||
$stream = Async::call(function (){ | ||
$stream = new ThroughStream(); | ||
$i = 0; | ||
$timer = Loop::addPeriodicTimer(1, function () use ($stream, &$i) { | ||
$i++; | ||
$stream->write('hello world:'. $i); | ||
}); | ||
Loop::addTimer(5, function () use ($timer, $stream) { | ||
Loop::cancelTimer($timer); | ||
$stream->end(); | ||
}); | ||
return $stream; | ||
}, true); | ||
|
||
$stream->on('data', function ($data) { | ||
var_dump($data); | ||
}); | ||
|
||
$stream->on('close', function () { | ||
|
||
}); | ||
|
||
$stream->on('error', function ($e) { | ||
var_dump($e->getMessage(), 'error'); | ||
}); | ||
|
||
Loop::addTimer(6, function () { | ||
Async::terminate(); | ||
}); | ||
``` |
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 @@ | ||
{ | ||
"name": "reactphp-framework/asyncify", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"Reactphp\\Framework\\Asyncify\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "wpjscc", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"reactphp-framework/process": "^1.0", | ||
"react/promise": "^3.1" | ||
} | ||
} |
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 | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Reactphp\Framework\Asyncify\Async; | ||
use React\EventLoop\Loop; | ||
use React\Promise\Deferred; | ||
|
||
$word = 'world'; | ||
Async::call(function () use ($word) { | ||
return [ | ||
'hello' => $word | ||
]; | ||
})->then(function ($data) { | ||
var_dump($data); | ||
}, function ($e) { | ||
var_dump($e->getMessage(), 'reject'); | ||
})->catch(function ($e) { | ||
var_dump($e->getMessage(), 'catch'); | ||
}); | ||
|
||
|
||
Async::call(function () { | ||
return file_get_contents(__DIR__ . '/test.txt'); | ||
})->then(function ($data) { | ||
var_dump($data); | ||
}, function ($e) { | ||
var_dump($e->getMessage(), 'reject'); | ||
})->catch(function ($e) { | ||
var_dump($e->getMessage(), 'catch'); | ||
}); | ||
|
||
Async::call(function () { | ||
$deferred = new Deferred(); | ||
Loop::addTimer(1, function () use ($deferred) { | ||
$deferred->resolve('hello world promise'); | ||
}); | ||
return $deferred->promise(); | ||
})->then(function ($data) { | ||
var_dump($data); | ||
}, function ($e) { | ||
var_dump($e->getMessage(), 'reject'); | ||
})->catch(function ($e) { | ||
var_dump($e->getMessage(), 'catch'); | ||
}); | ||
|
||
Loop::addTimer(2, function () { | ||
Async::terminate(); | ||
}); |
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,30 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Reactphp\Framework\Asyncify\Async; | ||
use React\EventLoop\Loop; | ||
|
||
$word = 'world'; | ||
$stream = Async::call(function () use ($word) { | ||
return [ | ||
'hello' => $word | ||
]; | ||
}, true); | ||
|
||
$stream->on('data', function ($data) { | ||
var_dump($data); | ||
}); | ||
|
||
$stream->on('close', function () { | ||
|
||
}); | ||
|
||
$stream->on('error', function ($e) { | ||
var_dump($e->getMessage(), 'error'); | ||
}); | ||
|
||
|
||
Loop::addTimer(1, function () { | ||
Async::terminate(); | ||
}); |
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,37 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Reactphp\Framework\Asyncify\Async; | ||
use React\Stream\ThroughStream; | ||
use React\EventLoop\Loop; | ||
|
||
$stream = Async::call(function (){ | ||
$stream = new ThroughStream(); | ||
$i = 0; | ||
$timer = Loop::addPeriodicTimer(1, function () use ($stream, &$i) { | ||
$i++; | ||
$stream->write('hello world:'. $i); | ||
}); | ||
Loop::addTimer(5, function () use ($timer, $stream) { | ||
Loop::cancelTimer($timer); | ||
$stream->end(); | ||
}); | ||
return $stream; | ||
}, true); | ||
|
||
$stream->on('data', function ($data) { | ||
var_dump($data); | ||
}); | ||
|
||
$stream->on('close', function () { | ||
|
||
}); | ||
|
||
$stream->on('error', function ($e) { | ||
var_dump($e->getMessage(), 'error'); | ||
}); | ||
|
||
Loop::addTimer(6, function () { | ||
Async::terminate(); | ||
}); |
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 @@ | ||
hello world test.txt |
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 @@ | ||
<?php | ||
|
||
namespace Reactphp\Framework\Asyncify; | ||
|
||
use Reactphp\Framework\Process\ProcessManager; | ||
use React\Stream\ReadableStreamInterface; | ||
use React\Promise\PromiseInterface; | ||
use React\Promise\Deferred; | ||
|
||
class Async | ||
{ | ||
static $number = 1; | ||
static $key = 'asyncify'; | ||
|
||
public static function call(callable $callable, $isStream = false): PromiseInterface | ReadableStreamInterface | ||
{ | ||
ProcessManager::instance(static::$key)->initProcessNumber(static::$number); | ||
$deferred = new Deferred(); | ||
$stream = ProcessManager::instance(static::$key)->callback(function ($stream) use ($callable) { | ||
$res = $callable(); | ||
if ($res instanceof ReadableStreamInterface) { | ||
$res->on('data', [$stream, 'write']); | ||
$res->on('close', [$stream, 'end']); | ||
$res->on('end', [$stream, 'end']); | ||
return $stream; | ||
} else if ($res instanceof PromiseInterface) { | ||
$res->then(function ($data) use ($stream) { | ||
$stream->end($data); | ||
}, function ($e) use ($stream) { | ||
$stream->emit('error', [$e]); | ||
})->catch(function ($e) use ($stream) { | ||
$stream->emit('error', [$e]); | ||
}); | ||
return $stream; | ||
} else { | ||
return $res; | ||
} | ||
}); | ||
|
||
if ($isStream) { | ||
return $stream; | ||
} | ||
|
||
$haveData = false; | ||
$stream->on('data', function ($buffer) use ($deferred, &$haveData) { | ||
$haveData = true; | ||
$deferred->resolve($buffer); | ||
}); | ||
|
||
$stream->on('close', function () use ($deferred, &$haveData) { | ||
if (!$haveData) { | ||
$deferred->resolve(null); | ||
} | ||
}); | ||
|
||
$stream->on('error', function ($e) use (&$data, &$isError, $deferred) { | ||
$isError = true; | ||
$data = null; | ||
$deferred->reject($e); | ||
}); | ||
|
||
return $deferred->promise(); | ||
} | ||
|
||
public static function __callStatic($method, $arguments) | ||
{ | ||
ProcessManager::instance(static::$key)->{$method}(...$arguments); | ||
} | ||
} |