3 namespace seekat\API\Future
;
6 use seekat\Exception\UnexpectedValueException
;
8 abstract class Common
implements Future
{
9 protected \WeakMap
$cancellations;
10 protected string $promiseType;
12 public function __construct() {
13 if (empty($this->promiseType
)) {
14 throw new UnexpectedValueException("Promise type must be set in Future implementation");
16 $this->cancellations
= new \WeakMap
;
19 function isPromise(object $promise): bool {
20 return $promise instanceof $this->promiseType
;
23 function getPromise(object $context) : object {
24 return $context->promise();
27 function cancelPromise(object $promise) : void
{
28 if (isset($this->cancellations
[$promise])) {
29 $this->cancellations
[$promise]();
33 function resolve(mixed $value) : object {
34 $context = $this->createContext();
35 $promise = $context->promise();
36 $context->resolve($value);
40 function resolver(object $context) : \Closure
{
41 return function($value) use($context) {
42 $context->resolve($value);
46 function reducer() : \Closure
{
47 return function(array $promises) {
48 return $this->all($promises);