Skip to content

Commit

Permalink
修复、更新文档、测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Nov 25, 2023
1 parent fe06b3e commit 0fb558b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
18 changes: 18 additions & 0 deletions doc/core/requestContext.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,21 @@ $result = \Imi\RequestContext::remember('myKey3', function () {
return 1 + 2;
});
```

### 推迟执行

当协程释放时触发,先进后出

```php
use function Yurun\Swoole\Coroutine\goWait;
$result = [];
goWait(static function () use (&$result): void {
RequestContext::defer(static function () use (&$result): void {
$result[] = 1;
});
RequestContext::defer(static function () use (&$result): void {
$result[] = 2;
});
}, -1, true);
var_dump($result); // [2, 1]
```
32 changes: 6 additions & 26 deletions src/Components/swoole/src/Context/CoroutineContextManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Imi\Core\Context\Exception\ContextExistsException;
use Imi\Core\Context\Exception\ContextNotFoundException;
use Imi\Event\Event;
use Imi\Log\Log;
use Imi\Swoole\Util\Coroutine;

/**
Expand All @@ -36,7 +35,7 @@ public function create(string|int $id, array $data = []): ContextData
if (!($swooleContext[static::class]['destroyBinded'] ?? false))
{
$swooleContext[static::class]['destroyBinded'] = true;
Coroutine::defer($this->__destroy(...));
Coroutine::defer(fn () => $this->destroy($id));
}
$context = $swooleContext[static::class]['context'] ?? null;
if ($context)
Expand Down Expand Up @@ -79,6 +78,8 @@ public function destroy(string|int $id): bool
{
return false;
}
// TODO: 实现新的连接管理器后移除
Event::trigger('IMI.REQUEST_CONTENT.DESTROY');
/** @var ContextData $context */
$context = $swooleContext[static::class]['context'];
$deferCallbacks = $context->getDeferCallbacks();
Expand All @@ -92,6 +93,8 @@ public function destroy(string|int $id): bool
}
elseif (isset($this->contexts[$id]))
{
// TODO: 实现新的连接管理器后移除
Event::trigger('IMI.REQUEST_CONTENT.DESTROY');
$deferCallbacks = $this->contexts[$id]->getDeferCallbacks();
while (!$deferCallbacks->isEmpty())
{
Expand Down Expand Up @@ -119,7 +122,7 @@ public function get(string|int $id, bool $autoCreate = false): ContextData
if (!($swooleContext[static::class]['destroyBinded'] ?? false))
{
$swooleContext[static::class]['destroyBinded'] = true;
Coroutine::defer($this->__destroy(...));
Coroutine::defer(fn () => $this->destroy($id));
}

if (!isset($swooleContext[static::class]['context']))
Expand Down Expand Up @@ -172,27 +175,4 @@ public function getCurrentId(): string|int
{
return (string) Coroutine::getCid();
}

/**
* 销毁当前请求的上下文.
*
* 不要手动调用!不要手动调用!不要手动调用!
*/
public function __destroy(): void
{
try
{
// TODO: 实现新的连接管理器后移除
Event::trigger('IMI.REQUEST_CONTENT.DESTROY');
$context = Coroutine::getContext();
if (!$context)
{
unset($this->contexts[Coroutine::getCid()]);
}
}
catch (\Throwable $th)
{
Log::error($th);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@
use Imi\RequestContext;
use Imi\Test\BaseTest;

use function Yurun\Swoole\Coroutine\goWait;

/**
* @testdox RequestContext
*/
class RequestContextTest extends BaseTest
{
public function testDefer(): void
{
$success = false;
RequestContext::defer(static function () use (&$success): void {
$success = true;
});
RequestContext::destroy();
$this->assertTrue($success);
$result = [];
goWait(static function () use (&$result): void {
RequestContext::defer(static function () use (&$result): void {
$result[] = 1;
});
RequestContext::defer(static function () use (&$result): void {
$result[] = 2;
});
}, -1, true);
$this->assertEquals([2, 1], $result);
}

public function testRemember(): void
Expand Down
11 changes: 7 additions & 4 deletions tests/unit/Component/Tests/RequestContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ class RequestContextTest extends BaseTest
{
public function testDefer(): void
{
$success = false;
RequestContext::defer(static function () use (&$success): void {
$success = true;
$result = [];
RequestContext::defer(static function () use (&$result): void {
$result[] = 1;
});
RequestContext::defer(static function () use (&$result): void {
$result[] = 2;
});
RequestContext::destroy();
$this->assertTrue($success);
$this->assertEquals([2, 1], $result);
}

public function testRemember(): void
Expand Down

0 comments on commit 0fb558b

Please sign in to comment.