Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Commit fd75cd3

Browse files
committed
Refactor
1 parent fffaf16 commit fd75cd3

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/Coroutine/Context.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Context
2828
*/
2929
public static function getApp()
3030
{
31-
return static::$apps[static::getCoroutineId()] ?? null;
31+
return static::$apps[static::getRequestedCoroutineId()] ?? null;
3232
}
3333

3434
/**
@@ -38,7 +38,7 @@ public static function getApp()
3838
*/
3939
public static function setApp(Container $app)
4040
{
41-
static::$apps[static::getCoroutineId()] = $app;
41+
static::$apps[static::getRequestedCoroutineId()] = $app;
4242
}
4343

4444
/**
@@ -50,7 +50,7 @@ public static function setApp(Container $app)
5050
*/
5151
public static function getData(string $key)
5252
{
53-
return static::$data[static::getCoroutineId()][$key] ?? null;
53+
return static::$data[static::getRequestedCoroutineId()][$key] ?? null;
5454
}
5555

5656
/**
@@ -61,7 +61,7 @@ public static function getData(string $key)
6161
*/
6262
public static function setData(string $key, $value)
6363
{
64-
static::$data[static::getCoroutineId()][$key] = $value;
64+
static::$data[static::getRequestedCoroutineId()][$key] = $value;
6565
}
6666

6767
/**
@@ -71,38 +71,43 @@ public static function setData(string $key, $value)
7171
*/
7272
public static function removeData(string $key)
7373
{
74-
unset(static::$data[static::getCoroutineId()][$key]);
74+
unset(static::$data[static::getRequestedCoroutineId()][$key]);
7575
}
7676

7777
/**
7878
* Get data keys by current coroutine id.
7979
*/
8080
public static function getDataKeys()
8181
{
82-
return array_keys(static::$data[static::getCoroutineId()] ?? []);
82+
return array_keys(static::$data[static::getRequestedCoroutineId()] ?? []);
8383
}
8484

8585
/**
8686
* Clear data by current coroutine id.
8787
*/
8888
public static function clear()
8989
{
90-
unset(static::$apps[static::getCoroutineId()]);
91-
unset(static::$data[static::getCoroutineId()]);
90+
unset(static::$apps[static::getRequestedCoroutineId()]);
91+
unset(static::$data[static::getRequestedCoroutineId()]);
92+
}
93+
94+
public static function getCoroutineId(): int
95+
{
96+
return Coroutine::getuid();
9297
}
9398

9499
/**
95-
* Get current requesting coroutine id.
100+
* Get current coroutine id.
96101
*/
97-
public static function getCoroutineId(): int
102+
public static function getRequestedCoroutineId(): int
98103
{
99-
$currentId = Coroutine::getuid();
104+
$currentId = static::getCoroutineId();
100105
if ($currentId === -1) {
101106
return -1;
102107
}
103108

104109
$counter = 0;
105-
while (($topCoroutineId = Coroutine::getPcid($currentId)) !== -1 && $counter <= self::MAX_RECURSE_COROUTINE_ID) {
110+
while (($topCoroutineId = Coroutine::getPcid($currentId)) !== -1 && $counter <= static::MAX_RECURSE_COROUTINE_ID) {
106111
$currentId = $topCoroutineId;
107112
$counter++;
108113
}

tests/Coroutine/ContextTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ public function testGetDataKeyInCoroutine()
6363

6464
$data1 = Context::getData('foo');
6565
$data2 = 'baz';
66-
$coroutineId1 = Context::getCoroutineId();
66+
$coroutineId1 = Context::getRequestedCoroutineId();
6767
$coroutineId2 = -1;
6868

6969
go(function () use (&$data2, &$coroutineId2) {
7070
$data2 = Context::getData('foo');
71-
$coroutineId2 = Context::getCoroutineId();
71+
$coroutineId2 = Context::getRequestedCoroutineId();
7272
});
7373
});
7474

0 commit comments

Comments
 (0)