update to PHP-8.1
[m6w6/seekat] / lib / API / Future / Common.php
1 <?php
2
3 namespace seekat\API\Future;
4
5 use seekat\API\Future;
6 use seekat\Exception\UnexpectedValueException;
7
8 abstract class Common implements Future {
9 protected \WeakMap $cancellations;
10 protected string $promiseType;
11
12 public function __construct() {
13 if (empty($this->promiseType)) {
14 throw new UnexpectedValueException("Promise type must be set in Future implementation");
15 }
16 $this->cancellations = new \WeakMap;
17 }
18
19 function isPromise(object $promise): bool {
20 return $promise instanceof $this->promiseType;
21 }
22
23 function getPromise(object $context) : object {
24 return $context->promise();
25 }
26
27 function cancelPromise(object $promise) : void {
28 if (isset($this->cancellations[$promise])) {
29 $this->cancellations[$promise]();
30 }
31 }
32
33 function resolve(mixed $value) : object {
34 $context = $this->createContext();
35 $promise = $context->promise();
36 $context->resolve($value);
37 return $promise;
38 }
39
40 function resolver(object $context) : \Closure {
41 return function($value) use($context) {
42 $context->resolve($value);
43 };
44 }
45
46 function reducer() : \Closure {
47 return function(array $promises) {
48 return $this->all($promises);
49 };
50 }
51
52 }