From f040e8801e319177532ec7df415e80ccadc83ea4 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sun, 12 Mar 2017 12:03:13 -0500 Subject: [PATCH] Fast return on void watcher callback --- lib/Loop/Driver.php | 4 ++++ lib/Loop/EvDriver.php | 12 ++++++++++++ lib/Loop/EventDriver.php | 14 +++++++++++++- lib/Loop/NativeDriver.php | 14 ++++++++++++++ lib/Loop/UvDriver.php | 12 ++++++++++++ 5 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lib/Loop/Driver.php b/lib/Loop/Driver.php index b3bbc50..9d174ad 100644 --- a/lib/Loop/Driver.php +++ b/lib/Loop/Driver.php @@ -111,6 +111,10 @@ abstract class Driver { $callback = $watcher->callback; $result = $callback($watcher->id, $watcher->data); + if ($result === null) { + continue; + } + if ($result instanceof \Generator) { $result = new Coroutine($result); } elseif ($result instanceof ReactPromise) { diff --git a/lib/Loop/EvDriver.php b/lib/Loop/EvDriver.php index 5a8ce54..9e97226 100644 --- a/lib/Loop/EvDriver.php +++ b/lib/Loop/EvDriver.php @@ -39,6 +39,10 @@ class EvDriver extends Driver { $callback = $watcher->callback; $result = $callback($watcher->id, $watcher->value, $watcher->data); + if ($result === null) { + return; + } + if ($result instanceof \Generator) { $result = new Coroutine($result); } elseif ($result instanceof ReactPromise) { @@ -61,6 +65,10 @@ class EvDriver extends Driver { $callback = $watcher->callback; $result = $callback($watcher->id, $watcher->data); + if ($result === null) { + return; + } + if ($result instanceof \Generator) { $result = new Coroutine($result); } elseif ($result instanceof ReactPromise) { @@ -79,6 +87,10 @@ class EvDriver extends Driver { $callback = $watcher->callback; $result = $callback($watcher->id, $watcher->value, $watcher->data); + if ($result === null) { + return; + } + if ($result instanceof \Generator) { $result = new Coroutine($result); } elseif ($result instanceof ReactPromise) { diff --git a/lib/Loop/EventDriver.php b/lib/Loop/EventDriver.php index c4852d3..3732898 100644 --- a/lib/Loop/EventDriver.php +++ b/lib/Loop/EventDriver.php @@ -36,6 +36,10 @@ class EventDriver extends Driver { $callback = $watcher->callback; $result = $callback($watcher->id, $watcher->value, $watcher->data); + if ($result === null) { + return; + } + if ($result instanceof \Generator) { $result = new Coroutine($result); } elseif ($result instanceof ReactPromise) { @@ -51,10 +55,14 @@ class EventDriver extends Driver { if ($watcher->type & Watcher::DELAY) { $this->cancel($watcher->id); } - + $callback = $watcher->callback; $result = $callback($watcher->id, $watcher->data); + if ($result === null) { + return; + } + if ($result instanceof \Generator) { $result = new Coroutine($result); } elseif ($result instanceof ReactPromise) { @@ -70,6 +78,10 @@ class EventDriver extends Driver { $callback = $watcher->callback; $result = $callback($watcher->id, $watcher->value, $watcher->data); + if ($result === null) { + return; + } + if ($result instanceof \Generator) { $result = new Coroutine($result); } elseif ($result instanceof ReactPromise) { diff --git a/lib/Loop/NativeDriver.php b/lib/Loop/NativeDriver.php index 67ecd5b..b873e0d 100644 --- a/lib/Loop/NativeDriver.php +++ b/lib/Loop/NativeDriver.php @@ -147,6 +147,10 @@ class NativeDriver extends Driver { $callback = $watcher->callback; $result = $callback($watcher->id, $stream, $watcher->data); + if ($result === null) { + continue; + } + if ($result instanceof \Generator) { $result = new Coroutine($result); } elseif ($result instanceof ReactPromise) { @@ -171,6 +175,10 @@ class NativeDriver extends Driver { $callback = $watcher->callback; $result = $callback($watcher->id, $stream, $watcher->data); + if ($result === null) { + continue; + } + if ($result instanceof \Generator) { $result = new Coroutine($result); } elseif ($result instanceof ReactPromise) { @@ -314,8 +322,14 @@ class NativeDriver extends Driver { $callback = $watcher->callback; $result = $callback($watcher->id, $signo, $watcher->data); + if ($result === null) { + continue; + } + if ($result instanceof \Generator) { $result = new Coroutine($result); + } elseif ($result instanceof ReactPromise) { + $result = adapt($result); } if ($result instanceof Promise) { diff --git a/lib/Loop/UvDriver.php b/lib/Loop/UvDriver.php index d65b59a..8a63f88 100644 --- a/lib/Loop/UvDriver.php +++ b/lib/Loop/UvDriver.php @@ -61,6 +61,10 @@ class UvDriver extends Driver { $callback = $watcher->callback; $result = $callback($watcher->id, $resource, $watcher->data); + if ($result === null) { + return; + } + if ($result instanceof \Generator) { $result = new Coroutine($result); } elseif ($result instanceof ReactPromise) { @@ -83,6 +87,10 @@ class UvDriver extends Driver { $callback = $watcher->callback; $result = $callback($watcher->id, $watcher->data); + if ($result === null) { + return; + } + if ($result instanceof \Generator) { $result = new Coroutine($result); } elseif ($result instanceof ReactPromise) { @@ -100,6 +108,10 @@ class UvDriver extends Driver { $callback = $watcher->callback; $result = $callback($watcher->id, $signo, $watcher->data); + if ($result === null) { + return; + } + if ($result instanceof \Generator) { $result = new Coroutine($result); } elseif ($result instanceof ReactPromise) {