-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflow.php
More file actions
175 lines (135 loc) · 4.16 KB
/
flow.php
File metadata and controls
175 lines (135 loc) · 4.16 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
168
169
170
171
172
173
174
175
<?php
namespace Atrox;
use React\Promise;
use React\Promise\PromiseInterface;
use React\Promise\Deferred;
class Async {
/**
* @param Closure|Generator
*/
static function getGenerator($f) {
if ($f instanceof \Generator) {
return $f;
} elseif ($f instanceof \Closure) {
return $f();
} else {
throw new \InvalidArgumentException('Generator of Closure expected');
}
}
static function flow($f) {
$gen = self::getGenerator($f);
$throwExc = function ($ex) use ($gen) { $gen->throw($ex); };
$first = true;
$recur = function($pureValue) use($gen, &$recur, &$first, $throwExc) {
try {
$x = $first ? $gen->current() : $gen->send($pureValue);
} catch (\Exception $e) {
return Promise\reject($e);
}
$first = false;
if (!$gen->valid()) return $pureValue;
if ($x instanceof PromiseInterface) return $x->then($recur, $throwExc);
else return $recur($x);
};
return Promise\resolve($recur(null));
}
static function runCallbacks($f) {
$gen = self::getGenerator($f);
$recur = function ($success, $exception) use ($gen, &$recur) {
$x = ($exception !== null) ? $gen->throw($exception) : $gen->send($success);
if ($gen->valid()) $x($recur);
};
try {
$x = $gen->current();
$x($recur);
} catch (Exception $e) {
$recur(null, $e);
}
}
function concurrently($n, $f) {
$gen = self::getGenerator($f);
$runNext = function () use ($gen, &$runNext) {
if ($gen->valid()) {
$p = $gen->current();
$gen->next();
$p->then($runNext, $runNext);
}
};
for ($i = 0; $i < $n; $i++) $runNext();
}
}
class Chain {
/**
* G[P] => P[(V, P[Next])]
* @param Closure|Generator
*/
static function promiseChainSeq($f) {
$gen = Async::getGenerator($f); // generator of promises
$throwExc = function ($ex) use ($gen) { $gen->throw($ex); };
$first = true;
$recur = function($pureValue) use($gen, &$recur, &$first, $throwExc) {
try {
$x = $first ? $gen->current() : $gen->send($pureValue);
} catch (\Exception $e) {
return Promise\reject($e);
}
$first = false;
if (!$gen->valid())
return Promise\resolve([$pureValue, null]);
return Promise\resolve($x)->then(
function ($val) use ($recur) { return [$val, $recur($val)]; },
function ($err) use ($gen) { $gen->throw($err); }
);
};
return $recur(null);
}
static function promiseChainPar($f) {
return self::concurrently(PHP_INT_MAX, $f);
}
static function flattenChains($ch) {
$firstDeferred = $deferred = new Deferred;
$makeChain = function ($v) use (&$deferred) {
$newDeferred = new Deferred;
$deferred->resolve([$v, $newDeferred->promise()]);
$deferred = $newDeferred;
};
$unwrapSub = function (array $pair) use ($makeChain, &$unwrapSub) {
list($v, $next) = $pair;
$makeChain($v);
if ($next !== null) {
$next->then($unwrapSub);
}
};
$unwrap = function (array $pair) use (&$unwrap, $unwrapSub, $makeChain) {
list($subChain, $next) = $pair;
// ??? empty subchain
$subChain = Promise\resolve($subChain); // needed because `then` acts as both map and flatMap and automatically flattens nested promises
$subChain->then($unwrapSub);
if ($next !== null)
$next->then($unwrap);
};
$ch->then($unwrap);
return $firstDeferred->promise();
}
static function concurrently($n, $f) {
$gen = Async::getGenerator($f);
$firstDeferred = $deferred = new Deferred;
$makeChain = function ($v) use (&$deferred) {
$newDeferred = new Deferred;
$deferred->resolve([$v, $newDeferred->promise()]);
$deferred = $newDeferred;
};
$runNext = function () use ($gen, &$runNext, $makeChain) {
if ($gen->valid()) {
$p = $gen->current();
$gen->next();
$p->then($makeChain); // throw away errors?
$p->then($runNext, $runNext);
}
};
for ($i = 0; $i < $n; $i++) {
$runNext();
}
return $firstDeferred->promise();
}
}