Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Condition::succeed and Condition::fail to xsucceed and xfail respectively #186

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 50 additions & 10 deletions src/async/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,75 @@ class Condition<T> {
/**
* Notify the condition variable of success and set the result.
*/
final public function xsucceed(T $result): void {
invariant($this->trySucceed($result), 'Unable to notify Condition twice');
}

/**
* Notify the condition variable of failure and set the exception.
*/
final public function xfail(\Exception $exception): void {
invariant($this->tryFail($exception), 'Unable to notify Condition twice');
}

/**
* Notify the condition variable of success and set the result.
*/
<<__Deprecated('use xsucceed() instead')>>
final public function succeed(T $result): void {
$this->xsucceed($result);
}

/**
* Notify the condition variable of failure and set the exception.
*/
<<__Deprecated('use xfail() instead')>>
final public function fail(\Exception $exception): void {
$this->xfail($exception);
}

/**
* Notify the condition variable of success and set the $result.
*
* @return
* true if the condition is set to $result successfully, false if the
* condition was previously set to another result or exception.
*/
final public function trySucceed(T $result): bool {
if ($this->condition === null) {
$this->condition = async {
return $result;
};
return true;
} else {
invariant(
$this->condition is ConditionWaitHandle<_>,
'Unable to notify AsyncCondition twice',
);
if (!($this->condition is ConditionWaitHandle<_>)) {
return false;
}
/* HH_FIXME[4110]: Type error revealed by type-safe instanceof feature. See https://fburl.com/instanceof */
$this->condition->succeed($result);
return true;
}
}

/**
* Notify the condition variable of failure and set the exception.
* Notify the condition variable of failure and set the $exception.
*
* @return
* true if the condition is set to $exception successfully, false if the
* condition was previously set to another result or exception.
*/
final public function fail(\Exception $exception): void {
final public function tryFail(\Exception $exception): bool {
if ($this->condition === null) {
$this->condition = async {
throw $exception;
};
return true;
} else {
invariant(
$this->condition is ConditionWaitHandle<_>,
'Unable to notify AsyncCondition twice',
);
if (!($this->condition is ConditionWaitHandle<_>)) {
return false;
}
$this->condition->fail($exception);
return true;
}
}

Expand Down