forked from reactphp/async
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve
async()
by making its promises cancelable
Since `async()` returns a promise and those are normally cancelable, implementing this puts them in line with the rest of our ecosystem. As such the following example will throw a timeout exception from the canceled `sleep()` call. ```php $promise = async(static function (): int { echo 'a'; await(sleep(2)); echo 'b'; return time(); })(); $promise->cancel(); await($promise); ```` This builds on top of reactphp#15, reactphp#18, reactphp#19, reactphp#26, reactphp#28, reactphp#30, and reactphp#32.
- Loading branch information
1 parent
4cadacc
commit 9df8ce5
Showing
6 changed files
with
312 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace React\Async; | ||
|
||
use React\Promise\PromiseInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class FiberMap | ||
{ | ||
private static array $status = []; | ||
private static array $map = []; | ||
|
||
public static function register(\Fiber $fiber): void | ||
{ | ||
self::$status[\spl_object_id($fiber)] = false; | ||
self::$map[\spl_object_id($fiber)] = []; | ||
} | ||
|
||
public static function cancel(\Fiber $fiber): void | ||
{ | ||
self::$status[\spl_object_id($fiber)] = true; | ||
} | ||
|
||
public static function isCancelled(\Fiber $fiber): bool | ||
{ | ||
return self::$status[\spl_object_id($fiber)]; | ||
} | ||
|
||
public static function attachPromise(\Fiber $fiber, PromiseInterface $promise): void | ||
{ | ||
self::$map[\spl_object_id($fiber)][\spl_object_id($promise)] = $promise; | ||
} | ||
|
||
public static function detachPromise(\Fiber $fiber, PromiseInterface $promise): void | ||
{ | ||
unset(self::$map[\spl_object_id($fiber)][\spl_object_id($promise)]); | ||
} | ||
|
||
public static function has(\Fiber $fiber): bool | ||
{ | ||
return array_key_exists(\spl_object_id($fiber), self::$map); | ||
} | ||
|
||
public static function getPromises(\Fiber $fiber): array | ||
{ | ||
return self::$map[\spl_object_id($fiber)] ?? []; | ||
} | ||
|
||
public static function unregister(\Fiber $fiber): void | ||
{ | ||
unset(self::$status[\spl_object_id($fiber)], self::$map[\spl_object_id($fiber)]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters