-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #351: Add supporting for React/Promises v3
- Loading branch information
Showing
21 changed files
with
1,487 additions
and
160 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,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\Internal\Promise; | ||
|
||
/** | ||
* @internal | ||
* @psalm-internal Temporal | ||
*/ | ||
class CancellationQueue | ||
{ | ||
private bool $started = false; | ||
private array $queue = []; | ||
|
||
public function __invoke() | ||
{ | ||
if ($this->started) { | ||
return; | ||
} | ||
|
||
$this->started = true; | ||
$this->drain(); | ||
} | ||
|
||
public function enqueue(mixed $cancellable): void | ||
{ | ||
if (!\is_object($cancellable) | ||
|| !\method_exists($cancellable, 'then') | ||
|| !\method_exists($cancellable, 'cancel') | ||
) { | ||
return; | ||
} | ||
|
||
$length = \array_push($this->queue, $cancellable); | ||
|
||
if ($this->started && 1 === $length) { | ||
$this->drain(); | ||
} | ||
} | ||
|
||
private function drain(): void | ||
{ | ||
for ($i = \key($this->queue); isset($this->queue[$i]); $i++) { | ||
$cancellable = $this->queue[$i]; | ||
|
||
$exception = null; | ||
|
||
try { | ||
$cancellable->cancel(); | ||
} catch (\Throwable $exception) { | ||
} | ||
|
||
unset($this->queue[$i]); | ||
|
||
if ($exception) { | ||
throw $exception; | ||
} | ||
} | ||
|
||
$this->queue = []; | ||
} | ||
} |
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,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\Internal\Promise; | ||
|
||
use ArrayAccess; | ||
use Countable; | ||
use Iterator; | ||
use RuntimeException; | ||
use Traversable; | ||
|
||
/** | ||
* @internal | ||
* @psalm-internal Temporal | ||
* @template TKey of array-key | ||
* @template TValue of Traversable | ||
* @implements Iterator<TKey, TValue> | ||
* @implements ArrayAccess<TKey, TValue> | ||
*/ | ||
final class Reasons extends RuntimeException implements Iterator, ArrayAccess, Countable | ||
{ | ||
/** | ||
* @param array<TKey, TValue> $collection | ||
*/ | ||
public function __construct( | ||
public array $collection, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
public function current(): mixed | ||
{ | ||
return \current($this->collection); | ||
} | ||
|
||
public function next(): void | ||
{ | ||
\next($this->collection); | ||
} | ||
|
||
public function key(): string|int|null | ||
{ | ||
return \key($this->collection); | ||
} | ||
|
||
public function valid(): bool | ||
{ | ||
return null !== \key($this->collection); | ||
} | ||
|
||
public function rewind(): void | ||
{ | ||
\reset($this->collection); | ||
} | ||
|
||
public function offsetExists(mixed $offset): bool | ||
{ | ||
return isset($this->collection[$offset]); | ||
} | ||
|
||
/** | ||
* @param TKey $offset | ||
* @return TValue | ||
*/ | ||
public function offsetGet(mixed $offset): Traversable | ||
{ | ||
return $this->collection[$offset]; | ||
} | ||
|
||
/** | ||
* @param TKey $offset | ||
* @param TValue $value | ||
*/ | ||
public function offsetSet(mixed $offset, mixed $value): void | ||
{ | ||
$this->collection[$offset] = $value; | ||
} | ||
|
||
public function offsetUnset(mixed $offset): void | ||
{ | ||
unset($this->collection[$offset]); | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return \count($this->collection); | ||
} | ||
} |
Oops, something went wrong.