forked from danog/MadelineProto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncTools.php
174 lines (166 loc) · 5.75 KB
/
AsyncTools.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types=1);
/**
* Tools module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2023 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto;
use Amp\Cancellation;
use Amp\CancelledException;
use Amp\DeferredCancellation;
use Amp\Future;
use Amp\TimeoutException;
use Closure;
use Generator;
use Revolt\EventLoop;
use Throwable;
use const LOCK_NB;
use const LOCK_UN;
use function Amp\async;
use function Amp\ByteStream\getOutputBufferStream;
use function Amp\ByteStream\getStdin;
use function Amp\ByteStream\getStdout;
use function Amp\delay;
/**
* Async tools.
*/
abstract class AsyncTools extends StrTools
{
/**
* Rethrow exception into event loop.
*/
public static function rethrow(Throwable $e): void
{
EventLoop::queue(fn () => throw $e);
}
/**
* Fork a new green thread and execute the passed function in the background.
*
* @template T
*
* @param \Closure(...):T $callable Function to execute
* @param mixed ...$args Arguments forwarded to the function when forking the thread.
*
* @return Future<T>
*
* @psalm-suppress InvalidScope
*/
public static function callFork(callable|Generator|Future $callable, ...$args): Future
{
if (\is_callable($callable)) {
$callable = async($callable, ...$args);
}
return $callable;
}
/**
* Asynchronously lock a file
* Resolves with a callbable that MUST eventually be called in order to release the lock.
*
* @param string $file File to lock
* @param integer $operation Locking mode
* @param float $polling Polling interval
* @param ?Cancellation $token Cancellation token
* @param ?Closure $failureCb Failure callback, called only once if the first locking attempt fails.
* @return ($token is null ? (Closure(): void) : ((Closure(): void)|null))
*/
public static function flock(string $file, int $operation, float $polling = 0.1, ?Cancellation $token = null, ?Closure $failureCb = null): ?Closure
{
if (!file_exists($file)) {
touch($file);
}
$operation |= LOCK_NB;
$res = fopen($file, 'c');
do {
$result = flock($res, $operation);
if (!$result) {
if ($failureCb) {
EventLoop::queue($failureCb);
$failureCb = null;
}
if ($token) {
if ($token->isRequested()) {
return null;
}
try {
delay($polling, true, $token);
} catch (CancelledException) {
return null;
}
} else {
delay($polling);
}
}
} while (!$result);
return static function () use (&$res): void {
if ($res) {
flock($res, LOCK_UN);
fclose($res);
$res = null;
}
};
}
/**
* Asynchronously sleep.
*
* @param float $time Number of seconds to sleep for
*/
public static function sleep(float $time): void
{
delay($time);
}
/**
* @internal
*/
public static function getTimeoutCancellation(float $timeout, string $message = "Operation timed out"): Cancellation
{
$e = new TimeoutException($message);
$deferred = new DeferredCancellation;
EventLoop::delay($timeout, fn () => $deferred->cancel($e));
return $deferred->getCancellation();
}
/**
* Asynchronously read line.
*
* @param string $prompt Prompt
*/
public static function readLine(string $prompt = '', ?Cancellation $cancel = null): string
{
try {
Magic::togglePeriodicLogging();
$stdin = getStdin();
$stdout = getStdout();
if ($prompt) {
$stdout->write($prompt);
}
static $lines = [''];
while (\count($lines) < 2 && ($chunk = $stdin->read($cancel)) !== null) {
$chunk = explode("\n", str_replace(["\r", "\n\n"], "\n", $chunk));
$lines[\count($lines) - 1] .= array_shift($chunk);
$lines = array_merge($lines, $chunk);
}
} finally {
Magic::togglePeriodicLogging();
}
return array_shift($lines) ?? '';
}
/**
* Asynchronously write to stdout/browser.
*
* @param string $string Message to echo
*/
public static function echo(string $string): void
{
getOutputBufferStream()->write($string);
}
}