1
0
mirror of https://github.com/danog/amp.git synced 2024-11-26 20:15:00 +01:00

Standardize all watcher invocation params to: func($reactor, $watcherId, $data)

This commit is contained in:
Daniel Lowrey 2014-08-09 10:25:07 -04:00
parent 92f1ae05f5
commit c7c5b0ba8f
6 changed files with 65 additions and 24 deletions

View File

@ -1,9 +1,7 @@
##### master
- none
### v0.11.0
- `Reactor::immediately()` watchers are now always enacted in a fresh call stack in the next
iteration of the event loop. They may still be disabled/enabled/cancelled like any other watcher.
- `Reactor::at()` implementations now accept unix timestamps in addition to strtotime() parsable
strings at parameter 2.
- Implement `Alert\SignalReactor` interface in `Alert\UvReactor` for signal handling support
@ -17,6 +15,10 @@
* Reactor::POLL_WRITE -> Reactor::WATCH_WRITE
* Reactor::ENABLE_NOW -> Reactor::WATCH_NOW
- The `Reactor::POLL_SOCK` constant has been removed
- Callback parameter order has changed and is now standardized for all watcher types:
- timers = func($reactor, $watcherId)
- stream = func($reactor, $watcherId, $stream)
- signal = func($reactor, $watcherId, $signo)
- The parameter order in `Reactor::watchStream()` and `watchStream()` has changed.
#### v0.10.2

View File

@ -137,12 +137,11 @@ class LibeventReactor implements SignalReactor {
}
private function wrapOnceCallback(LibeventWatcher $watcher) {
$callback = $watcher->callback;
$watcherId = $watcher->id;
return function() use ($callback, $watcherId) {
return function() use ($watcher) {
try {
$callback($watcherId, $this);
$callback = $watcher->callback;
$watcherId = $watcher->id;
$callback($this, $watcherId);
$this->cancel($watcherId);
} catch (\Exception $e) {
$this->stopException = $e;
@ -168,7 +167,6 @@ class LibeventReactor implements SignalReactor {
$watcher->eventResource = $eventResource;
$watcher->msDelay = $msDelay;
$watcher->callback = $callback;
$watcher->wrapper = $this->wrapRepeatingCallback($watcher);
$this->watchers[$watcherId] = $watcher;
@ -188,7 +186,7 @@ class LibeventReactor implements SignalReactor {
return function() use ($callback, $eventResource, $msDelay, $watcherId) {
try {
$callback($watcherId, $this);
$callback($this, $watcherId);
event_add($eventResource, $msDelay);
} catch (\Exception $e) {
$this->stopException = $e;
@ -248,10 +246,11 @@ class LibeventReactor implements SignalReactor {
private function wrapStreamCallback(LibeventWatcher $watcher) {
$callback = $watcher->callback;
$watcherId = $watcher->id;
$stream = $watcher->stream;
return function($stream) use ($callback, $watcherId) {
return function() use ($callback, $watcherId, $stream) {
try {
$callback($watcherId, $stream, $this);
$callback($this, $watcherId, $stream);
} catch (\Exception $e) {
$this->stopException = $e;
$this->stop();
@ -296,6 +295,7 @@ class LibeventReactor implements SignalReactor {
$eventResource = event_new();
$watcher = new LibeventWatcher;
$watcher->id = $watcherId;
$watcher->signo = $signo;
$watcher->eventResource = $eventResource;
$watcher->callback = $onSignal;
@ -313,10 +313,11 @@ class LibeventReactor implements SignalReactor {
private function wrapSignalCallback(LibeventWatcher $watcher) {
$callback = $watcher->callback;
$watcherId = $watcher->id;
$signo = $watcher->signo;
return function() use ($callback, $watcherId) {
return function() use ($callback, $watcherId, $signo) {
try {
$callback($watcherId, $this);
$callback($this, $watcherId, $signo);
} catch (\Exception $e) {
$this->stopException = $e;
$this->stop();
@ -391,5 +392,4 @@ class LibeventReactor implements SignalReactor {
$this->isGCScheduled = false;
event_del($this->gcEvent);
}
}

1
lib/LibeventWatcher.php Executable file → Normal file
View File

@ -6,6 +6,7 @@ class LibeventWatcher {
public $id;
public $eventResource;
public $stream;
public $signo;
public $callback;
public $wrapper;
public $msDelay = -1;

View File

@ -84,7 +84,7 @@ class NativeReactor implements Reactor {
if ($immediates = $this->immediates) {
$this->immediates = [];
foreach ($immediates as $watcherId => $callback) {
$callback($watcherId, $this);
$callback($this, $watcherId);
}
}
@ -122,13 +122,13 @@ class NativeReactor implements Reactor {
foreach ($r as $readableStream) {
$streamId = (int) $readableStream;
foreach ($this->readCallbacks[$streamId] as $watcherId => $callback) {
$callback($watcherId, $readableStream, $this);
$callback($this, $watcherId, $readableStream);
}
}
foreach ($w as $writableStream) {
$streamId = (int) $writableStream;
foreach ($this->writeCallbacks[$streamId] as $watcherId => $callback) {
$callback($watcherId, $writableStream, $this);
$callback($this, $watcherId, $writableStream);
}
}
}
@ -162,7 +162,7 @@ class NativeReactor implements Reactor {
);
}
$callback($watcherId, $this);
$callback($this, $watcherId);
}
/**

View File

@ -68,7 +68,7 @@ class UvReactor implements SignalReactor {
private function doImmediates() {
$immediates = $this->immediates;
foreach ($immediates as $watcherId => $callback) {
$callback($this);
$callback($this, $watcherId);
unset(
$this->immediates[$watcherId],
$this->watchers[$watcherId]
@ -188,7 +188,7 @@ class UvReactor implements SignalReactor {
private function wrapTimerCallback($watcher, $callback) {
return function() use ($watcher, $callback) {
try {
$callback($watcher->id, $this);
$callback($this, $watcher->id);
if ($watcher->mode === self::$MODE_ONCE) {
$this->clearWatcher($watcher->id);
}
@ -309,7 +309,7 @@ class UvReactor implements SignalReactor {
private function wrapStreamCallback($watcher, $callback) {
return function() use ($watcher, $callback) {
try {
$callback($watcher->id, $watcher->stream, $this);
$callback($this, $watcher->id, $watcher->stream);
} catch (\Exception $e) {
$this->stopException = $e;
$this->stop();
@ -343,7 +343,7 @@ class UvReactor implements SignalReactor {
private function wrapSignalCallback($watcher, $callback) {
return function() use ($watcher, $callback) {
try {
$callback($watcher->id, $watcher->signo, $this);
$callback($this, $watcher->id, $watcher->signo);
} catch (\Exception $e) {
$this->stopException = $e;
$this->stop();

View File

@ -25,6 +25,44 @@ abstract class ReactorTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals(1, $increment);
}
public function testTimerWatcherParameterOrder() {
$reactor = $this->getReactor();
$counter = 0;
$reactor->immediately(function($reactorArg, $watcherId) use ($reactor, &$counter) {
$this->assertSame($reactor, $reactorArg);
$this->assertTrue(is_int($watcherId));
if (++$counter === 3) {
$reactor->stop();
}
});
$reactor->once(function($reactorArg, $watcherId) use ($reactor, &$counter) {
$this->assertSame($reactor, $reactorArg);
$this->assertTrue(is_int($watcherId));
if (++$counter === 3) {
$reactor->stop();
}
}, $msDelay = 1);
$reactor->repeat(function($reactorArg, $watcherId) use ($reactor, &$counter) {
$this->assertSame($reactor, $reactorArg);
$this->assertTrue(is_int($watcherId));
$reactor->cancel($watcherId);
if (++$counter === 3) {
$reactor->stop();
}
}, $msDelay = 1);
$reactor->run();
}
public function testStreamWatcherParameterOrder() {
$reactor = $this->getReactor();
$reactor->onWritable(STDOUT, function($reactorArg, $watcherId) use ($reactor) {
$this->assertSame($reactor, $reactorArg);
$this->assertTrue(is_int($watcherId));
$reactor->stop();
});
}
public function testDisablingWatcherPreventsSubsequentInvocation() {
$reactor = $this->getReactor();
$increment = 0;