1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Promise.php
См. документацию.
1<?php
2
4
5use Bitrix\Im\V2\Async\Promise\State;
7use Closure;
8use Throwable;
9
10abstract class Promise
11{
12 public readonly string $id;
13 private State $state = State::Pending;
14 private ?Closure $onFulfilled = null;
15 private ?Closure $onRejected = null;
16 protected mixed $result;
17
18 public function __construct(callable $job)
19 {
20 $this->id = UuidGenerator::generateV4();
21 try
22 {
23 $job([$this, 'fulfill'], [$this, 'reject']);
24 }
25 catch (Throwable $exception)
26 {
27 $this->reject($exception);
28 }
29 }
30
31 abstract public function wait(): mixed;
32
33 abstract public function onWait(): mixed;
34
35 public function getState(): State
36 {
37 return $this->state;
38 }
39
40 public function getResult(): mixed
41 {
42 return $this->result ?? null;
43 }
44
45 public function then(?callable $onFulfilled = null, ?callable $onRejected = null): static
46 {
47 return new static(function(callable $fulfill, callable $reject) use ($onFulfilled, $onRejected): void {
48 $wrappedOnFulfilled = $this->wrapCompletionHandler($onFulfilled, $fulfill, $reject);
49 $wrappedOnRejected = $this->wrapCompletionHandler($onRejected, $fulfill, $reject);
50
51 if ($this->state === State::Pending)
52 {
53 $this->onFulfilled = $wrappedOnFulfilled;
54 $this->onRejected = $wrappedOnRejected;
55 }
56 elseif ($this->state === State::Fulfilled)
57 {
58 $wrappedOnFulfilled();
59 }
60 else
61 {
62 $wrappedOnRejected();
63 }
64 });
65 }
66
67 public function catch(callable $onRejected): static
68 {
69 return $this->then(null, $onRejected);
70 }
71
72 protected function fulfill(mixed $result): void
73 {
74 $this->complete(State::Fulfilled, $result);
75 }
76
77 protected function reject(mixed $result): void
78 {
80 }
81
82 protected function complete(State $newState, mixed $result): void
83 {
84 if ($this->state !== State::Pending)
85 {
86 return;
87 }
88
89 $callback = $newState === State::Fulfilled ? $this->onFulfilled : $this->onRejected;
90 $this->state = $newState;
91 $this->result = $result;
92 if ($callback)
93 {
94 $callback();
95 }
96 elseif ($newState === State::Rejected && $result instanceof Throwable)
97 {
98 throw $result;
99 }
100 }
101
102 private function wrapCompletionHandler(?callable $completionHandler, callable $fulfill, callable $reject): callable
103 {
104 return function() use ($completionHandler, $fulfill, $reject): void {
105 try
106 {
107 $this->processCompletion($completionHandler, $fulfill, $reject);
108 }
109 catch (Throwable $e)
110 {
111 $reject($e);
112 }
113 };
114 }
115
116 private function processCompletion(?callable $completionHandler, callable $fulfill, callable $reject): void
117 {
118 if ($completionHandler)
119 {
120 $this->executeCompletionHandler($completionHandler, $fulfill, $reject);
121 }
122 else
123 {
124 $this->propagateState($fulfill, $reject);
125 }
126 }
127
128 private function executeCompletionHandler(callable $completionHandler, callable $fulfill, callable $reject): void
129 {
130 $result = $completionHandler($this->result);
131
132 if ($result instanceof self)
133 {
134 $result->then($fulfill, $reject);
135 }
136 else
137 {
138 $fulfill($result);
139 }
140 }
141
142 private function propagateState(callable $fulfill, callable $reject): void
143 {
144 if ($this->state === State::Fulfilled)
145 {
146 $fulfill($this->getResult());
147 }
148 else
149 {
150 $reject($this->getResult());
151 }
152 }
153}
reject(mixed $result)
Определения Promise.php:77
mixed $result
Определения Promise.php:16
readonly string $id
Определения Promise.php:12
complete(State $newState, mixed $result)
Определения Promise.php:82
then(?callable $onFulfilled=null, ?callable $onRejected=null)
Определения Promise.php:45
__construct(callable $job)
Определения Promise.php:18
@ Rejected
Определения State.php:9
fulfill(mixed $result)
Определения Promise.php:72
getResult()
Определения Promise.php:40
getState()
Определения Promise.php:35
static generateV4()
Определения UuidGenerator.php:11
$result
Определения get_property_values.php:14
Определения Promise.php:3
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393