-
Notifications
You must be signed in to change notification settings - Fork 31
/
ManualResetEventSlim.php
66 lines (53 loc) · 1.49 KB
/
ManualResetEventSlim.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* This file is part of `prooph/event-store-client`.
* (c) 2018-2018 prooph software GmbH <contact@prooph.de>
* (c) 2018-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Prooph\EventStoreClient\Internal;
use Amp\Deferred;
use Amp\Promise;
/** @internal */
class ManualResetEventSlim
{
/** @var bool */
private $isSet;
/** @var Deferred */
private $deferred;
public function __construct(bool $isSet = false)
{
$this->isSet = $isSet;
$this->deferred = new Deferred();
if ($isSet) {
$this->deferred->resolve(true);
}
}
public function set(): void
{
$this->isSet = true;
$this->deferred->resolve(true);
}
public function reset(): void
{
$this->isSet = false;
$this->deferred = new Deferred();
}
public function wait(int $timeout): Promise
{
$promise = Promise\timeout($this->deferred->promise(), $timeout);
$deferred = new Deferred();
$newPromise = $deferred->promise();
$promise->onResolve(function (?\Throwable $exception = null, $result) use ($deferred) {
if ($exception) {
$deferred->resolve(false);
} else {
$deferred->resolve(true);
}
});
return $newPromise;
}
}