Skip to content

Commit 9df5962

Browse files
Atryfacebook-github-bot
authored andcommitted
Add Condition->trySucceed and Condition->tryFail (facebook#185)
Summary: It would allow for use cases like this: ``` async function outer(): Awaitable<void> { await wait_for_notification_async( async $notifyee ==> { concurrent { await async { await gen_usleep(100); $notifyee->trySucceed("fast condition"); } await async { await gen_usleep(10000000000); $notifyee->trySucceed("slow condition"); } } } ); } ``` X-link: hhvm/hsl#185 Reviewed By: fredemmott Differential Revision: D34903436 Pulled By: Atry fbshipit-source-id: 859d72707fa338df22522c778c35f47c23497746
1 parent a0db154 commit 9df5962

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

hphp/hsl/src/async/Condition.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,58 @@ class Condition<T> {
2121
* Notify the condition variable of success and set the result.
2222
*/
2323
final public function succeed(T $result): void {
24+
invariant($this->trySucceed($result), 'Unable to notify Condition twice');
25+
}
26+
27+
/**
28+
* Notify the condition variable of failure and set the exception.
29+
*/
30+
final public function fail(\Exception $exception): void {
31+
invariant($this->tryFail($exception), 'Unable to notify Condition twice');
32+
}
33+
34+
/**
35+
* Notify the condition variable of success and set the $result.
36+
*
37+
* @return
38+
* true if the condition is set to $result successfully, false if the
39+
* condition was previously set to another result or exception.
40+
*/
41+
final public function trySucceed(T $result): bool {
2442
if ($this->condition === null) {
2543
$this->condition = async {
2644
return $result;
2745
};
46+
return true;
2847
} else {
29-
invariant(
30-
$this->condition is ConditionWaitHandle<_>,
31-
'Unable to notify AsyncCondition twice',
32-
);
48+
if (!($this->condition is ConditionWaitHandle<_>)) {
49+
return false;
50+
}
3351
/* HH_FIXME[4110]: Type error revealed by type-safe instanceof feature. See https://fburl.com/instanceof */
3452
$this->condition->succeed($result);
53+
return true;
3554
}
3655
}
3756

3857
/**
39-
* Notify the condition variable of failure and set the exception.
58+
* Notify the condition variable of failure and set the $exception.
59+
*
60+
* @return
61+
* true if the condition is set to $exception successfully, false if the
62+
* condition was previously set to another result or exception.
4063
*/
41-
final public function fail(\Exception $exception): void {
64+
final public function tryFail(\Exception $exception): bool {
4265
if ($this->condition === null) {
4366
$this->condition = async {
4467
throw $exception;
4568
};
69+
return true;
4670
} else {
47-
invariant(
48-
$this->condition is ConditionWaitHandle<_>,
49-
'Unable to notify AsyncCondition twice',
50-
);
71+
if (!($this->condition is ConditionWaitHandle<_>)) {
72+
return false;
73+
}
5174
$this->condition->fail($exception);
75+
return true;
5276
}
5377
}
5478

0 commit comments

Comments
 (0)