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 8a65d99
Showing
5 changed files
with
240 additions
and
9 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,51 @@ | ||
<?php | ||
|
||
namespace React\Async; | ||
|
||
use Fiber; | ||
use React\Promise\PromiseInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class FiberMap | ||
{ | ||
private array $status = []; | ||
private array $map = []; | ||
|
||
public function register(Fiber $fiber): void | ||
{ | ||
$this->status[spl_object_hash($fiber)] = false; | ||
$this->map[spl_object_hash($fiber)] = []; | ||
} | ||
|
||
public function cancel(Fiber $fiber): void | ||
{ | ||
$this->status[spl_object_hash($fiber)] = true; | ||
} | ||
|
||
public function isCanceled(Fiber $fiber): bool | ||
{ | ||
return $this->status[spl_object_hash($fiber)]; | ||
} | ||
|
||
public function attachPromise(Fiber $fiber, PromiseInterface $promise): void | ||
{ | ||
$this->map[spl_object_hash($fiber)][spl_object_hash($promise)] = $promise; | ||
} | ||
|
||
public function has(Fiber $fiber): bool | ||
{ | ||
return array_key_exists(spl_object_hash($fiber), $this->map); | ||
} | ||
|
||
public function getPromises(Fiber $fiber): array | ||
{ | ||
return $this->map[spl_object_hash($fiber)]; | ||
} | ||
|
||
public function unregister(Fiber $fiber): void | ||
{ | ||
unset($this->status[spl_object_hash($fiber)], $this->map[spl_object_hash($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