Skip to content

Commit 20ec43d

Browse files
committed
Removed Future::$nextTick
Improved complete Improved Promise
1 parent e6d536d commit 20ec43d

File tree

4 files changed

+28
-42
lines changed

4 files changed

+28
-42
lines changed

examples/src/testco.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
function wait() {
77
return new Promise(function($reslove, $reject) {
88
swoole_timer_after(1, function() use (&$reslove) {
9-
$reslove(null);
9+
$reslove();
1010
});
1111
});
1212
}
@@ -18,7 +18,7 @@ function wait() {
1818
yield wait();
1919
if ($i % 10000 === 0) {
2020
gc_collect_cycles();
21-
var_dump(memory_get_usage(true));
21+
var_dump(memory_get_usage(true), time());
2222
}
2323
}
2424
})->then(function() {

src/Hprose/Future.php

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,46 +36,38 @@ class Future {
3636
public $value;
3737
public $reason;
3838
private $subscribers = array();
39-
public static $nextTick = null;
4039

4140
public function __construct($computation = NULL) {
4241
if (is_callable($computation)) {
43-
$self = $this;
44-
$nextTick = self::$nextTick;
45-
$nextTick(function() use ($self, $computation) {
46-
try {
47-
$self->resolve(call_user_func($computation));
48-
}
49-
catch (UncatchableException $e) {
50-
throw $e->getPrevious();
51-
}
52-
catch (Exception $e) {
53-
$self->reject($e);
54-
}
55-
catch (Throwable $e) {
56-
$self->reject($e);
57-
}
58-
});
59-
}
60-
}
61-
62-
private function privateCall($callback, $next, $x) {
63-
$nextTick = self::$nextTick;
64-
$nextTick(function() use ($callback, $next, $x) {
6542
try {
66-
$r = call_user_func($callback, $x);
67-
$next->resolve($r);
43+
$this->resolve(call_user_func($computation));
6844
}
6945
catch (UncatchableException $e) {
7046
throw $e->getPrevious();
7147
}
7248
catch (Exception $e) {
73-
$next->reject($e);
49+
$this->reject($e);
7450
}
7551
catch (Throwable $e) {
76-
$next->reject($e);
52+
$this->reject($e);
7753
}
78-
});
54+
}
55+
}
56+
57+
private function privateCall($callback, $next, $x) {
58+
try {
59+
$r = call_user_func($callback, $x);
60+
$next->resolve($r);
61+
}
62+
catch (UncatchableException $e) {
63+
throw $e->getPrevious();
64+
}
65+
catch (Exception $e) {
66+
$next->reject($e);
67+
}
68+
catch (Throwable $e) {
69+
$next->reject($e);
70+
}
7971
}
8072

8173
private function privateResolve($onfulfill, $next, $x) {
@@ -96,7 +88,7 @@ private function privateReject($onreject, $next, $e) {
9688
}
9789
}
9890

99-
public function resolve($value) {
91+
public function resolve($value = NULL) {
10092
if ($value === $this) {
10193
$this->reject(new TypeError('Self resolution'));
10294
return;
@@ -193,10 +185,7 @@ public function then($onfulfill, $onreject = NULL) {
193185

194186
public function done($onfulfill, $onreject = NULL) {
195187
$this->then($onfulfill, $onreject)->then(NULL, function($error) {
196-
$nextTick = self::$nextTick;
197-
$nextTick(function() use ($error) {
198-
throw new UncatchableException("", 0, $error);
199-
});
188+
throw new UncatchableException("", 0, $error);
200189
});
201190
}
202191

@@ -242,7 +231,8 @@ function($e) use ($action) {
242231
);
243232
}
244233

245-
public function complete($oncomplete) {
234+
public function complete($oncomplete = false) {
235+
$oncomplete = $oncomplete ?: function($v) { return $v; };
246236
return $this->then($oncomplete, $oncomplete);
247237
}
248238

src/Hprose/Future/functions.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
use ReflectionMethod;
3030
use ReflectionObject;
3131

32-
if (Future::$nextTick === null) {
33-
Future::$nextTick = function(\Closure $fn) { $fn(); };
34-
}
35-
3632
function isFuture($obj) {
3733
return $obj instanceof Future;
3834
}

src/Hprose/Promise.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* *
1515
* Promise for php 5.3+ *
1616
* *
17-
* LastModified: Jul 23, 2016 *
17+
* LastModified: Dec 5, 2016 *
1818
* Author: Ma Bingyao <[email protected]> *
1919
* *
2020
\**********************************************************/
@@ -27,7 +27,7 @@ public function __construct($executor = null) {
2727
if (is_callable($executor)) {
2828
$self = $this;
2929
call_user_func($executor,
30-
function($value) use ($self) {
30+
function($value = NULL) use ($self) {
3131
$self->resolve($value);
3232
},
3333
function($reason) use ($self) {

0 commit comments

Comments
 (0)