Skip to content

Commit

Permalink
feat: Coroutine构造注册函数增加coroutineId入参
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Oct 8, 2024
1 parent 1e05629 commit 277e2cf
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 16 deletions.
8 changes: 6 additions & 2 deletions src/Utils/Coroutine/Coroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

/**
* @method mixed origin()
* @method string|int id()
*/
class Coroutine
{
Expand All @@ -42,15 +43,18 @@ class Coroutine

/**
* 构造方法
*/
/**
*
* @param Closure $func
* @link CoroutineInterface::__construct
*/
public function __construct(Closure $func)
{
$this->_interface = new (self::$_handlers[Factory::getCurrentEventLoop()] ?? DefaultCoroutine::class)($func);
}

/**
* 析构
*/
public function __destruct()
{
$this->_interface = null;
Expand Down
2 changes: 2 additions & 0 deletions src/Utils/Coroutine/Handlers/CoroutineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ interface CoroutineInterface
{
/**
* 创建协程
*
* @param \Closure $func = function (mixed $coroutineId) {}
*/
public function __construct(\Closure $func);

Expand Down
4 changes: 2 additions & 2 deletions src/Utils/Coroutine/Handlers/DefaultCoroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class DefaultCoroutine implements CoroutineInterface
/** @inheritdoc */
public function __construct(\Closure $func)
{
call_user_func($func);
$this->id = spl_object_hash($func);
call_user_func($func, $id = spl_object_hash($func));
$this->id = $id;
}

/** @inheritdoc */
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/Coroutine/Handlers/RippleCoroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class RippleCoroutine implements CoroutineInterface
*/
public function __construct(\Closure $func)
{
$this->_promise = $this->_async(function () use (&$promise, $func) {
$this->_promise = $this->_async(function () use ($func) {
try {
call_user_func($func);
call_user_func($func, $this->_id);
} finally {
// 移除协程id及promise
$this->_promise = null;
Expand Down
6 changes: 4 additions & 2 deletions src/Utils/Coroutine/Handlers/SwooleCoroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ class SwooleCoroutine implements CoroutineInterface
public function __construct(\Closure $func)
{
while (1) {
if ($res = Coroutine::create($func)) {
$this->_id = $res;
if ($id = Coroutine::create(function () use ($func) {
call_user_func($func, $this->_id);
})) {
$this->_id = $id;
break;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Utils/Coroutine/Handlers/SwowCoroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class SwowCoroutine implements CoroutineInterface
*/
protected int $_id;

/** @inheritdoc
* @param \Closure $func
*/
/** @inheritDoc */
public function __construct(\Closure $func)
{
$this->_coroutine = Coroutine::run($func);
$this->_coroutine = Coroutine::run(function () use ($func) {
call_user_func($func, $this->_id);
});
$this->_id = $this->_coroutine->getId();
}

Expand Down
5 changes: 4 additions & 1 deletion tests/UtilsCase/Coroutine/DefaultCoroutineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ class DefaultCoroutineTest extends TestCase
public function testConstruct()
{
$executed = false;
$func = function() use (&$executed) {
$id = null;
$func = function ($coroutineId) use (&$id, &$executed) {
$executed = true;
$id = $coroutineId;
};
$coroutine = new DefaultCoroutine($func);
$this->assertTrue($executed);
$this->assertEquals(spl_object_hash($func), $coroutine->id());
$this->assertEquals($coroutine->id(), $id);
}

public function testOrigin()
Expand Down
6 changes: 5 additions & 1 deletion tests/UtilsCase/Coroutine/RippleCoroutineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ protected function tearDown(): void
public function testConstruct()
{
$executed = false;
$func = function() use (&$executed) {
$id = null;
$func = function ($coroutineId) use (&$id, &$executed) {
$executed = true;
$id = $coroutineId;
};
$promiseMock = Mockery::mock('Psc\Core\Coroutine\Promise');

Expand All @@ -37,6 +39,8 @@ public function testConstruct()
$this->assertTrue($executed);
$this->assertInstanceOf('Psc\Core\Coroutine\Promise', $coroutine->origin());
$this->assertIsString($coroutine->id());
$this->assertEquals(spl_object_hash($promiseMock), $coroutine->id());
$this->assertEquals($coroutine->id(), $id);
}

public function testDestruct()
Expand Down
5 changes: 4 additions & 1 deletion tests/UtilsCase/Coroutine/SwooleCoroutineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ protected function tearDown(): void
public function testConstruct()
{
$executed = false;
$func = function() use (&$executed) {
$id = null;
$func = function ($coroutineId) use (&$id, &$executed) {
$executed = true;
$id = $coroutineId;
};

$coroutineMock = Mockery::mock('alias:Swoole\Coroutine');
Expand All @@ -34,6 +36,7 @@ public function testConstruct()
$this->assertTrue($executed);
$this->assertEquals(123, $coroutine->origin());
$this->assertEquals(123, $coroutine->id());
$this->assertEquals(123, $id());
}

public function testDestruct()
Expand Down
5 changes: 4 additions & 1 deletion tests/UtilsCase/Coroutine/SwowCoroutineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ protected function tearDown(): void
public function testConstruct()
{
$executed = false;
$func = function() use (&$executed) {
$id = null;
$func = function ($coroutineId) use (&$id, &$executed) {
$executed = true;
$id = $coroutineId;
};

$coroutineMock = Mockery::mock('alias:Swow\Coroutine');
Expand All @@ -35,6 +37,7 @@ public function testConstruct()
$this->assertTrue($executed);
$this->assertInstanceOf('Swow\Coroutine', $coroutine->origin());
$this->assertEquals(123, $coroutine->id());
$this->assertEquals(123, $id);
}

public function testDestruct()
Expand Down

0 comments on commit 277e2cf

Please sign in to comment.