diff --git a/src/Utils/Coroutine/Handlers/RippleCoroutine.php b/src/Utils/Coroutine/Handlers/RippleCoroutine.php index 454112b..a835682 100644 --- a/src/Utils/Coroutine/Handlers/RippleCoroutine.php +++ b/src/Utils/Coroutine/Handlers/RippleCoroutine.php @@ -7,22 +7,25 @@ namespace Workbunny\WebmanCoroutine\Utils\Coroutine\Handlers; +use Closure; use Psc\Core\Coroutine\Promise; use Revolt\EventLoop\Suspension; -use function Co\async; -use function Co\getSuspension; - class RippleCoroutine implements CoroutineInterface { - protected Promise|null $promise = null; + /** + * @var \Revolt\EventLoop\Suspension + */ + protected Suspension $suspension; /** @inheritdoc * @param \Closure $func */ - public function __construct(\Closure $func) + public function __construct(Closure $func) { - $this->promise = $this->_async(function (\Closure $resolve, \Closure $reject) use ($func) { + $this->_async(function (Closure $resolve, Closure $reject) use ($func) { + $this->suspension = \Co\getSuspension(); + try { $result = call_user_func( $func, @@ -46,7 +49,7 @@ public function __destruct() /** @inheritdoc */ public function origin(): Suspension { - return getSuspension(); + return $this->suspension; } /** @inheritdoc */ @@ -60,10 +63,10 @@ public function id(): ?string * * @param \Closure $closure * - * @return mixed + * @return \Psc\Core\Coroutine\Promise */ - protected function _async(\Closure $closure) + protected function _async(Closure $closure): Promise { - return async($closure); + return \Co\async($closure); } } diff --git a/tests/UtilsCase/Coroutine/RippleCoroutineTest.php b/tests/UtilsCase/Coroutine/RippleCoroutineTest.php index d6d98f4..406ec2c 100644 --- a/tests/UtilsCase/Coroutine/RippleCoroutineTest.php +++ b/tests/UtilsCase/Coroutine/RippleCoroutineTest.php @@ -26,7 +26,7 @@ public function testConstruct() // mock async $callback = null; - $promiseMock = Mockery::mock('Psc\Core\Coroutine\Promise'); + $promiseMock = Mockery::mock('Revolt\EventLoop\Suspension'); $coroutine = Mockery::mock(RippleCoroutine::class)->makePartial(); $coroutine->shouldAllowMockingProtectedMethods()->shouldReceive('_async') ->andReturnUsing(function ($closure) use (&$callback, $promiseMock) { @@ -39,7 +39,7 @@ public function testConstruct() $constructor->invoke($coroutine, $func); $this->assertFalse($executed); - $this->assertInstanceOf('Psc\Core\Coroutine\Promise', $coroutine->origin()); + $this->assertInstanceOf('Revolt\EventLoop\Suspension', $coroutine->origin()); $this->assertIsString($getId = $coroutine->id()); $this->assertEquals(spl_object_hash($promiseMock), $coroutine->id()); $this->assertNull($id); @@ -62,7 +62,7 @@ public function testDestruct() // mock async $callback = null; - $promiseMock = Mockery::mock('Psc\Core\Coroutine\Promise'); + $promiseMock = Mockery::mock('Revolt\EventLoop\Suspension'); $coroutine = Mockery::mock(RippleCoroutine::class)->makePartial(); $coroutine->shouldAllowMockingProtectedMethods()->shouldReceive('_async') ->andReturnUsing(function ($closure) use (&$callback, $promiseMock) { @@ -90,7 +90,7 @@ public function testOrigin() }; // mock async $callback = null; - $promiseMock = Mockery::mock('Psc\Core\Coroutine\Promise'); + $promiseMock = Mockery::mock('Revolt\EventLoop\Suspension'); $coroutine = Mockery::mock(RippleCoroutine::class)->makePartial(); $coroutine->shouldAllowMockingProtectedMethods()->shouldReceive('_async') ->andReturnUsing(function ($closure) use (&$callback, $promiseMock) { @@ -102,7 +102,7 @@ public function testOrigin() $constructor = new \ReflectionMethod(RippleCoroutine::class, '__construct'); $constructor->invoke($coroutine, $func); - $this->assertInstanceOf('Psc\Core\Coroutine\Promise', $coroutine->origin()); + $this->assertInstanceOf('Revolt\EventLoop\Suspension', $coroutine->origin()); // 模拟构造后协程执行callback call_user_func($callback); @@ -116,7 +116,7 @@ public function testId() }; // mock async $callback = null; - $promiseMock = Mockery::mock('Psc\Core\Coroutine\Promise'); + $promiseMock = Mockery::mock('Revolt\EventLoop\Suspension'); $coroutine = Mockery::mock(RippleCoroutine::class)->makePartial(); $coroutine->shouldAllowMockingProtectedMethods()->shouldReceive('_async') ->andReturnUsing(function ($closure) use (&$callback, $promiseMock) {