Skip to content

Commit d1bd93a

Browse files
authored
Merge pull request #649: Promises v3
2 parents 40272bf + 4560e27 commit d1bd93a

File tree

13 files changed

+198
-73
lines changed

13 files changed

+198
-73
lines changed

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
"google/common-protos": "^4.9",
2828
"google/protobuf": "^4.31.1",
2929
"grpc/grpc": "^1.57",
30+
"internal/promise": "^2.12 || ^3.4",
3031
"internal/destroy": "^1.0",
31-
"internal/promise": "^2.12",
3232
"nesbot/carbon": "^2.72.6 || ^3.8.4",
3333
"psr/log": "^2.0 || ^3.0.2",
3434
"ramsey/uuid": "^4.7.6",
@@ -50,7 +50,10 @@
5050
"GPBMetadata\\Temporal\\Api\\Testservice\\": "testing/api/testservice/GPBMetadata/Temporal/Api/Testservice",
5151
"Temporal\\Testing\\": "testing/src",
5252
"Temporal\\": "src"
53-
}
53+
},
54+
"files": [
55+
"src/include.php"
56+
]
5457
},
5558
"require-dev": {
5659
"buggregator/trap": "^1.13.0",
@@ -61,7 +64,7 @@
6164
"jetbrains/phpstorm-attributes": "dev-master",
6265
"laminas/laminas-code": "^4.16",
6366
"phpunit/phpunit": "10.5.45",
64-
"spiral/code-style": "~2.2.2",
67+
"spiral/code-style": "~2.3.0",
6568
"spiral/core": "^3.14.9",
6669
"ta-tikoma/phpunit-architecture-test": "^0.8.5",
6770
"vimeo/psalm": "^5.26.1 || ^6.2"

psalm-baseline.xml

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,15 +1024,9 @@
10241024
</UndefinedInterfaceMethod>
10251025
</file>
10261026
<file src="src/Internal/Transport/CompletableResult.php">
1027-
<PossiblyNullArgument>
1027+
<InvalidArgument>
10281028
<code><![CDATA[$this->wrapContext($onFulfilledOrRejected)]]></code>
1029-
<code><![CDATA[$this->wrapContext($onRejected)]]></code>
1030-
</PossiblyNullArgument>
1031-
<UndefinedInterfaceMethod>
1032-
<code><![CDATA[cancel]]></code>
1033-
<code><![CDATA[catch]]></code>
1034-
<code><![CDATA[finally]]></code>
1035-
</UndefinedInterfaceMethod>
1029+
</InvalidArgument>
10361030
</file>
10371031
<file src="src/Internal/Transport/Router/DestroyWorkflow.php">
10381032
<UndefinedInterfaceMethod>
@@ -1061,9 +1055,6 @@
10611055
<code><![CDATA[$request->getOptions()['name']]]></code>
10621056
<code><![CDATA[$request->getOptions()['updateId']]]></code>
10631057
</PossiblyUndefinedStringArrayOffset>
1064-
<UndefinedInterfaceMethod>
1065-
<code><![CDATA[cancel]]></code>
1066-
</UndefinedInterfaceMethod>
10671058
</file>
10681059
<file src="src/Internal/Transport/Router/StackTrace.php">
10691060
<ArgumentTypeCoercion>
@@ -1172,6 +1163,7 @@
11721163
<InvalidArgument>
11731164
<code><![CDATA[$onFulfilled]]></code>
11741165
<code><![CDATA[$onRejected]]></code>
1166+
<code><![CDATA[$resolver->resolve(...)]]></code>
11751167
</InvalidArgument>
11761168
<PropertyNotSetInConstructor>
11771169
<code><![CDATA[$coroutine]]></code>
@@ -1184,10 +1176,6 @@
11841176
<code><![CDATA[$this->context]]></code>
11851177
<code><![CDATA[$this->scopeContext]]></code>
11861178
</TypeDoesNotContainNull>
1187-
<UndefinedInterfaceMethod>
1188-
<code><![CDATA[catch]]></code>
1189-
<code><![CDATA[finally]]></code>
1190-
</UndefinedInterfaceMethod>
11911179
<UnusedClosureParam>
11921180
<code><![CDATA[$e]]></code>
11931181
</UnusedClosureParam>
@@ -1262,6 +1250,11 @@
12621250
<file src="src/Promise.php">
12631251
<InvalidArgument>
12641252
<code><![CDATA[$reasons]]></code>
1253+
<code><![CDATA[$resolve]]></code>
1254+
<code><![CDATA[$values]]></code>
1255+
<code><![CDATA[$values]]></code>
1256+
<code><![CDATA[[]]]></code>
1257+
<code><![CDATA[[]]]></code>
12651258
</InvalidArgument>
12661259
<InvalidOperand>
12671260
<code><![CDATA[$promisesOrValues]]></code>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Internal\Promise;
6+
7+
/**
8+
* An exception that wraps a rejected value.
9+
*
10+
* @see \Temporal\Promise::reject()
11+
* @internal
12+
*/
13+
final class RejectedValue extends \Exception
14+
{
15+
public function __construct(
16+
public mixed $value,
17+
) {
18+
parent::__construct();
19+
}
20+
}

src/Internal/Transport/CompletableResult.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ public function cancel(): void
114114
*/
115115
public function otherwise(callable $onRejected): PromiseInterface
116116
{
117-
return $this->catch($this->wrapContext($onRejected));
117+
return $this->catch($onRejected);
118118
}
119119

120120
/**
121121
* @deprecated
122122
*/
123123
public function always(callable $onFulfilledOrRejected): PromiseInterface
124124
{
125-
return $this->finally($this->wrapContext($onFulfilledOrRejected));
125+
return $this->finally($onFulfilledOrRejected);
126126
}
127127

128128
private function onFulfilled(mixed $result): void

src/Internal/Workflow/Process/Process.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ public function initAndStart(
188188
$handler = $instance->getHandler();
189189
$instance = $context->getWorkflowInstance();
190190
$arguments = null;
191+
$values = [];
191192

192193
try {
193194
// Initialize workflow instance
@@ -205,14 +206,13 @@ public function initAndStart(
205206
$context->getHeader(),
206207
),
207208
);
208-
Workflow::setCurrentContext($context);
209-
210-
$instance->init($values);
211-
} else {
212-
Workflow::setCurrentContext($context);
213-
$instance->init();
214209
}
215210

211+
// Workflow::setCurrentContext($context);
212+
$this->setContext($context);
213+
$this->makeCurrent();
214+
$instance->init($values);
215+
216216
$context->setReadonly(false);
217217

218218
// Execute
@@ -240,8 +240,8 @@ function (WorkflowInput $input) use ($context, $arguments, $handler, $deferred):
240240
$context->isReplaying(),
241241
));
242242
} catch (\Throwable $e) {
243-
/** @psalm-suppress RedundantPropertyInitializationCheck */
244-
isset($this->context) or $this->setContext($context->setReadonly(false));
243+
isset($this->context) or $this->setContext($context);
244+
$context->setReadonly(false);
245245
$this->complete($e);
246246
} finally {
247247
Workflow::setCurrentContext(null);

src/Internal/Workflow/WorkflowContext.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ public function upsertMemo(array $values): void
517517
$this->callsInterceptor->with(
518518
function (UpsertMemoInput $input): PromiseInterface {
519519
if ($input->memo === []) {
520-
return resolve();
520+
return resolve(null);
521521
}
522522

523523
$result = $this->request(new UpsertMemo($input->memo), false, false);
@@ -546,7 +546,7 @@ public function upsertSearchAttributes(array $searchAttributes): void
546546
$this->callsInterceptor->with(
547547
function (UpsertSearchAttributesInput $input): PromiseInterface {
548548
if ($input->searchAttributes === []) {
549-
return resolve();
549+
return resolve(null);
550550
}
551551

552552
$result = $this->request(new UpsertSearchAttributes($input->searchAttributes), false, false);
@@ -574,7 +574,7 @@ public function upsertTypedSearchAttributes(SearchAttributeUpdate ...$updates):
574574
$this->callsInterceptor->with(
575575
function (UpsertTypedSearchAttributesInput $input): PromiseInterface {
576576
if ($input->updates === []) {
577-
return resolve();
577+
return resolve(null);
578578
}
579579

580580
$result = $this->request(new UpsertTypedSearchAttributes($input->updates), false, false);
@@ -648,7 +648,7 @@ public function resolveConditions(): void
648648
foreach ($awaitsGroup as $i => [$condition, $deferred]) {
649649
if ($condition()) {
650650
unset($this->awaits[$awaitsGroupId][$i]);
651-
$deferred->resolve();
651+
$deferred->resolve(null);
652652
$this->resolveConditionGroup($awaitsGroupId);
653653
}
654654
}

src/Promise.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use React\Promise\PromiseInterface;
1616
use Temporal\Internal\Promise\CancellationQueue;
1717
use Temporal\Internal\Promise\Reasons;
18+
use Temporal\Internal\Promise\RejectedValue;
1819

1920
use function React\Promise\race;
2021
use function React\Promise\reject;
@@ -36,7 +37,7 @@ final class Promise
3637
*/
3738
public static function all(iterable $promises): PromiseInterface
3839
{
39-
return self::map($promises, static fn($val): mixed => $val);
40+
return self::map($promises, static fn(mixed $val): mixed => $val);
4041
}
4142

4243
/**
@@ -205,10 +206,9 @@ static function (mixed $mapped) use ($i, &$values, &$toResolve, $resolve): void
205206
*
206207
* @param iterable<int, PromiseInterface|mixed> $promises
207208
* @param callable(mixed $current, mixed $carry, int $current, positive-int $items): mixed $reduce
208-
* @param mixed $initial
209209
* @psalm-param PromiseReduceCallback $reduce
210210
*/
211-
public static function reduce(iterable $promises, callable $reduce, $initial = null): PromiseInterface
211+
public static function reduce(iterable $promises, callable $reduce, mixed $initial = null): PromiseInterface
212212
{
213213
$cancellationQueue = new CancellationQueue();
214214
$cancellationQueue->enqueue($promises);
@@ -273,32 +273,27 @@ static function (iterable $array) use (
273273
*
274274
* If `$promiseOrValue` is a promise, it will be returned as is.
275275
*
276-
* @param null|mixed $promiseOrValue
276+
* @template T
277+
* @param PromiseInterface<T>|T $promiseOrValue
278+
* @return PromiseInterface<T>
277279
*/
278-
public static function resolve($promiseOrValue = null): PromiseInterface
280+
public static function resolve(mixed $promiseOrValue = null): PromiseInterface
279281
{
280282
return resolve($promiseOrValue);
281283
}
282284

283285
/**
284286
* Creates a rejected promise for the supplied `$promiseOrValue`.
285287
*
286-
* If `$promiseOrValue` is a value, it will be the rejection value of the
287-
* returned promise.
288-
*
289-
* If `$promiseOrValue` is a promise, its completion value will be the rejected
290-
* value of the returned promise.
291-
*
292-
* This can be useful in situations where you need to reject a promise without
293-
* throwing an exception. For example, it allows you to propagate a rejection with
294-
* the value of another promise.
288+
* If `$reason` is not a `\Throwable`, the returned promise will be
289+
* fulfilled with that value instead.
295290
*
296-
* @param null|mixed $promiseOrValue
291+
* @param \Throwable $reason
297292
* @return PromiseInterface<never>
298293
*/
299-
public static function reject($promiseOrValue = null): PromiseInterface
294+
public static function reject(mixed $reason): PromiseInterface
300295
{
301-
return reject($promiseOrValue);
296+
return reject($reason instanceof \Throwable ? $reason : new RejectedValue($reason));
302297
}
303298

304299
/**

src/include.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal;
6+
7+
use function React\Promise\set_rejection_handler;
8+
9+
if (\function_exists('\React\Promise\set_rejection_handler')) {
10+
set_rejection_handler(null);
11+
}

0 commit comments

Comments
 (0)