This commit is contained in:
xl
2024-11-06 21:41:43 +08:00
parent a21b949512
commit 03bffb5e5b
+12 -34
View File
@@ -14,7 +14,7 @@ class CoroutineContext implements ContextInterface
*/ */
public static function inCoroutine(): bool public static function inCoroutine(): bool
{ {
return true; return Coroutine::getCid() > -1;
} }
@@ -26,11 +26,7 @@ class CoroutineContext implements ContextInterface
*/ */
public static function set(string $key, mixed $value, ?int $coroutineId = null): mixed public static function set(string $key, mixed $value, ?int $coroutineId = null): mixed
{ {
// TODO: Implement set() method. return Coroutine::getContext()[$key] = $value;
if (is_null($coroutineId)) {
$coroutineId = Coroutine::getCid();
}
return Coroutine::getContext($coroutineId)[$key] = $value;
} }
/** /**
@@ -41,11 +37,7 @@ class CoroutineContext implements ContextInterface
*/ */
public static function get(string $key, mixed $defaultValue = null, ?int $coroutineId = null): mixed public static function get(string $key, mixed $defaultValue = null, ?int $coroutineId = null): mixed
{ {
// TODO: Implement get() method. return Coroutine::getContext()[$key] ?? $defaultValue;
if (is_null($coroutineId)) {
$coroutineId = Coroutine::getCid();
}
return Coroutine::getContext($coroutineId)[$key] ?? $defaultValue;
} }
/** /**
@@ -55,11 +47,7 @@ class CoroutineContext implements ContextInterface
*/ */
public static function exists(string $key, ?int $coroutineId = null): bool public static function exists(string $key, ?int $coroutineId = null): bool
{ {
// TODO: Implement exists() method. return isset(Coroutine::getContext()[$key]);
if (is_null($coroutineId)) {
$coroutineId = Coroutine::getCid();
}
return isset(Coroutine::getContext($coroutineId)[$key]);
} }
/** /**
@@ -69,12 +57,8 @@ class CoroutineContext implements ContextInterface
*/ */
public static function remove(string $key, ?int $coroutineId = null): void public static function remove(string $key, ?int $coroutineId = null): void
{ {
// TODO: Implement remove() method. Coroutine::getContext()[$key] = null;
if (is_null($coroutineId)) { unset(Coroutine::getContext()[$key]);
$coroutineId = Coroutine::getCid();
}
Coroutine::getContext($coroutineId)[$key] = null;
unset(Coroutine::getContext($coroutineId)[$key]);
} }
@@ -86,13 +70,10 @@ class CoroutineContext implements ContextInterface
*/ */
public static function increment(string $id, int $value = 1, ?int $coroutineId = null): int public static function increment(string $id, int $value = 1, ?int $coroutineId = null): int
{ {
if (is_null($coroutineId)) { if (!isset(Coroutine::getContext()[$id])) {
$coroutineId = Coroutine::getCid(); Coroutine::getContext()[$id] = 0;
} }
if (!isset(Coroutine::getContext($coroutineId)[$id])) { return Coroutine::getContext()[$id] += $value;
Coroutine::getContext($coroutineId)[$id] = 0;
}
return Coroutine::getContext($coroutineId)[$id] += $value;
} }
/** /**
@@ -103,12 +84,9 @@ class CoroutineContext implements ContextInterface
*/ */
public static function decrement(string $id, int $value = 1, ?int $coroutineId = null): int public static function decrement(string $id, int $value = 1, ?int $coroutineId = null): int
{ {
if (is_null($coroutineId)) { if (!isset(Coroutine::getContext()[$id])) {
$coroutineId = Coroutine::getCid(); Coroutine::getContext()[$id] = 0;
} }
if (!isset(Coroutine::getContext($coroutineId)[$id])) { return Coroutine::getContext()[$id] -= $value;
Coroutine::getContext($coroutineId)[$id] = 0;
}
return Coroutine::getContext($coroutineId)[$id] -= $value;
} }
} }