Skip to content

Commit

Permalink
Fixed Hprose\Async\Swoole
Browse files Browse the repository at this point in the history
  • Loading branch information
andot committed Mar 29, 2016
1 parent 9d67249 commit af73529
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
24 changes: 19 additions & 5 deletions src/Hprose/Async/Swoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,39 @@

class Swoole extends Base {
const MILLISECONDS_PER_SECOND = 1000;
public function __construct() {
private $n = 0;
private function stopEvent() {
$this->n--;
if ($this->n === 0) {
swoole_event_exit();
}
}
protected function setEvent($func, $delay, $loop, $args) {
$delay = $delay * self::MILLISECONDS_PER_SECOND;
if ($delay === 0) $delay = 1;
if ($delay === 0) {
$delay = 1;
}
$this->n++;
if ($loop) {
$timer = swoole_timer_tick($delay, function() use($func, $args) {
call_user_func_array($func, $args);
});
}
else {
$timer = swoole_timer_after($delay, function() use($func, $args) {
$self = $this;
$timer = swoole_timer_after($delay, function() use($self, $func, $args) {
$self->stopEvent();
call_user_func_array($func, $args);
});
}
return $timer;
}
protected function clearEvent($timer) {
swoole_timer_clear($timer);
if (@swoole_timer_clear($timer)) {
$this->stopEvent();
}
}
function loop() {
swoole_event_wait();
}
function loop() {}
}
4 changes: 2 additions & 2 deletions src/Hprose/Future.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ function($array) use ($onfulfilledArray) {
public function get($key) {
return $this->then(
function($result) use ($key) {
return $result[$key];
return $result->$key;
}
);
}
Expand All @@ -329,7 +329,7 @@ public function __get($key) {
public function set($key, $value) {
return $this->then(
function($result) use ($key, $value) {
$result[$key] = $value;
$result->$key = $value;
return $result;
}
);
Expand Down
18 changes: 18 additions & 0 deletions tests/PromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,22 @@ public function testFutureSpread() {
$self->assertEquals($result, 300);
});
}
public function testFutureGet() {
$self = $this;
$o = new \stdClass();
$o->name = "Tom";
$p = \Hprose\Future\value($o);
$p->name->done(function($result) use ($self) {
$self->assertEquals($result, "Tom");
});
}
public function testFutureSet() {
$self = $this;
$o = new \stdClass();
$p = \Hprose\Future\value($o);
$p = $p->set('name', "Tom");
$p->get('name')->done(function($result) use ($self) {
$self->assertEquals($result, "Tom");
});
}
}

0 comments on commit af73529

Please sign in to comment.