-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcmds.php
More file actions
167 lines (155 loc) · 3.7 KB
/
cmds.php
File metadata and controls
167 lines (155 loc) · 3.7 KB
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
<?php
/**
* Commands kit
* User: moyo
* Date: 07/08/2017
* Time: 6:18 PM
*/
namespace Carno\Coroutine;
use Carno\Coroutine\Exception\TimeoutException;
use Carno\Coroutine\Job\Creator;
use Carno\Promise\Promise;
use Carno\Promise\Promised;
use Carno\Timer\Timer;
use Closure;
use Throwable;
/**
* @param $ms
* @param string $ec
* @param string $em
* @return Promised
*/
function timeout(
int $ms,
string $ec = TimeoutException::class,
string $em = ''
) : Promised {
$racer = Promise::deferred();
if ($ms > 0) {
$timer = Timer::after($ms, static function () use ($racer, $ec, $em) {
$racer->throw(new $ec($em));
});
$racer->catch(static function () use ($timer) {
Timer::clear($timer);
});
}
return $racer;
}
/**
* sleep [and do something] (optional)
* @param int $ms
* @param Closure $do
* @return Promised
*/
function msleep(
int $ms,
Closure $do = null
) : Promised {
return new Promise(static function (Promised $promised) use ($ms, $do) {
$tick = Timer::after($ms, static function () use ($promised, $do) {
$promised->resolve($do ? $do() : null);
});
$promised->catch(static function () use ($tick) {
Timer::clear($tick);
});
});
}
/**
* "GO" new program without results but throws exception if error
* @param mixed $program
* @param Context $ctx
*/
function go($program, Context $ctx = null) : void
{
async($program, $ctx)->fusion();
}
/**
* similar with "GO" but returns "CLOSURE"
* @param mixed $program
* @param Context $ctx
* @return Closure
*/
function co($program, Context $ctx = null) : Closure
{
return static function (...$args) use ($program, $ctx) {
return async($program, $ctx, ...$args)->fusion();
};
}
/**
* similar with "GO" but returns "PROMISED" and no "THROWS"
* @param mixed $program
* @param Context $ctx
* @param mixed $args
* @return Promised
*/
function async($program, Context $ctx = null, ...$args) : Promised
{
return Creator::promised($program, $args, $ctx);
}
/**
* wait and wake programme
* @param Closure $dial
* @param Closure $awake
* @param int $timeout
* @param string $error
* @param string $message
* @return Promised
*/
function await(
Closure $dial,
Closure $awake,
int $timeout = 60000,
string $error = TimeoutException::class,
string $message = ''
) : Promised {
return race(
new Promise(static function (Promised $await) use ($dial, $awake) {
$dial(static function (...$args) use ($await, $awake) {
try {
$await->pended() && $out = $awake(...$args);
$await->pended() && $await->resolve($out ?? null);
} catch (Throwable $e) {
$await->pended() && $await->throw($e);
}
});
}),
timeout($timeout, $error, $message)
);
}
/**
* @return Syscall
*/
function ctx() : Syscall
{
return new Syscall(static function (Job $job) {
return $job->ctx();
});
}
/**
* @param Closure $program
* @return Syscall
*/
function defer(Closure $program) : Syscall
{
return new Syscall(static function (Job $job) use ($program) {
$job->roll()->then(static function ($stage) use ($job, $program) {
co($program, $job->ctx())($stage);
});
});
}
/**
* @param mixed ...$programs
* @return Promised
*/
function race(...$programs) : Promised
{
return Promise::race(...Creator::promises(...Creator::context($programs)));
}
/**
* @param mixed ...$programs
* @return Promised
*/
function all(...$programs) : Promised
{
return Promise::all(...Creator::promises(...Creator::context($programs)));
}