Skip to content

Commit c05ba0d

Browse files
committed
Merge pull request #72 from async-interop/invalid-watcher-handling
(Re-)Introduce InvalidWatcherException
2 parents d29e175 + f6c6f41 commit c05ba0d

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

src/Loop.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Interop\Async\Loop\Driver;
66
use Interop\Async\Loop\DriverFactory;
7+
use Interop\Async\Loop\InvalidWatcherException;
8+
use Interop\Async\Loop\UnsupportedFeatureException;
79

810
final class Loop
911
{
@@ -199,6 +201,8 @@ public static function onWritable($stream, callable $callback, $data = null)
199201
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
200202
*
201203
* @return string An identifier that can be used to cancel, enable or disable the watcher.
204+
*
205+
* @throws UnsupportedFeatureException Thrown if signal handling is not supported.
202206
*/
203207
public static function onSignal($signo, callable $callback, $data = null)
204208
{
@@ -212,6 +216,8 @@ public static function onSignal($signo, callable $callback, $data = null)
212216
* @param string $watcherId The watcher identifier.
213217
*
214218
* @return void
219+
*
220+
* @throws InvalidWatcherException Thrown if the watcher identifier is invalid.
215221
*/
216222
public static function enable($watcherId)
217223
{
@@ -225,6 +231,8 @@ public static function enable($watcherId)
225231
* @param string $watcherId The watcher identifier.
226232
*
227233
* @return void
234+
*
235+
* @throws InvalidWatcherException Thrown if the watcher identifier is invalid.
228236
*/
229237
public static function disable($watcherId)
230238
{
@@ -233,7 +241,7 @@ public static function disable($watcherId)
233241
}
234242

235243
/**
236-
* Cancel a watcher.
244+
* Cancel a watcher. This marks the watcher identifier as invalid.
237245
*
238246
* @param string $watcherId The watcher identifier.
239247
*
@@ -254,6 +262,8 @@ public static function cancel($watcherId)
254262
* @param string $watcherId The watcher identifier.
255263
*
256264
* @return void
265+
*
266+
* @throws InvalidWatcherException Thrown if the watcher identifier is invalid.
257267
*/
258268
public static function reference($watcherId)
259269
{
@@ -270,6 +280,8 @@ public static function reference($watcherId)
270280
* @param string $watcherId The watcher identifier.
271281
*
272282
* @return void
283+
*
284+
* @throws InvalidWatcherException Thrown if the watcher identifier is invalid.
273285
*/
274286
public static function unreference($watcherId)
275287
{

src/Loop/Driver.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function stop();
3333
* @param callable(string $watcherId, mixed $data) $callback The callback to defer. The $watcherId will be invalidated before the callback call.
3434
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
3535
*
36-
* @return string An identifier that can be used to cancel, enable or disable the watcher.
36+
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
3737
*/
3838
public function defer(callable $callback, $data = null);
3939

@@ -46,7 +46,7 @@ public function defer(callable $callback, $data = null);
4646
* @param callable(string $watcherId, mixed $data) $callback The callback to delay. The $watcherId will be invalidated before the callback call.
4747
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
4848
*
49-
* @return string An identifier that can be used to cancel, enable or disable the watcher.
49+
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
5050
*/
5151
public function delay($delay, callable $callback, $data = null);
5252

@@ -60,7 +60,7 @@ public function delay($delay, callable $callback, $data = null);
6060
* @param callable(string $watcherId, mixed $data) $callback The callback to repeat.
6161
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
6262
*
63-
* @return string An identifier that can be used to cancel, enable or disable the watcher.
63+
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
6464
*/
6565
public function repeat($interval, callable $callback, $data = null);
6666

@@ -71,7 +71,7 @@ public function repeat($interval, callable $callback, $data = null);
7171
* @param callable(string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
7272
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
7373
*
74-
* @return string An identifier that can be used to cancel, enable or disable the watcher.
74+
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
7575
*/
7676
public function onReadable($stream, callable $callback, $data = null);
7777

@@ -82,7 +82,7 @@ public function onReadable($stream, callable $callback, $data = null);
8282
* @param callable(string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
8383
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
8484
*
85-
* @return string An identifier that can be used to cancel, enable or disable the watcher.
85+
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
8686
*/
8787
public function onWritable($stream, callable $callback, $data = null);
8888

@@ -93,7 +93,7 @@ public function onWritable($stream, callable $callback, $data = null);
9393
* @param callable(string $watcherId, int $signo, mixed $data) $callback The callback to execute.
9494
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
9595
*
96-
* @return string An identifier that can be used to cancel, enable or disable the watcher.
96+
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
9797
*
9898
* @throws UnsupportedFeatureException Thrown if signal handling is not supported.
9999
*/
@@ -105,6 +105,8 @@ public function onSignal($signo, callable $callback, $data = null);
105105
* @param string $watcherId The watcher identifier.
106106
*
107107
* @return void
108+
*
109+
* @throws InvalidWatcherException Thrown if the watcher identifier is invalid.
108110
*/
109111
public function enable($watcherId);
110112

@@ -114,11 +116,13 @@ public function enable($watcherId);
114116
* @param string $watcherId The watcher identifier.
115117
*
116118
* @return void
119+
*
120+
* @throws InvalidWatcherException Thrown if the watcher identifier is invalid.
117121
*/
118122
public function disable($watcherId);
119123

120124
/**
121-
* Cancel a watcher.
125+
* Cancel a watcher. This marks the watcher as invalid. Calling this function MUST never fail, even when passed an invalid watcher.
122126
*
123127
* @param string $watcherId The watcher identifier.
124128
*
@@ -135,6 +139,8 @@ public function cancel($watcherId);
135139
* @param string $watcherId The watcher identifier.
136140
*
137141
* @return void
142+
*
143+
* @throws InvalidWatcherException Thrown if the watcher identifier is invalid.
138144
*/
139145
public function reference($watcherId);
140146

@@ -147,6 +153,8 @@ public function reference($watcherId);
147153
* @param string $watcherId The watcher identifier.
148154
*
149155
* @return void
156+
*
157+
* @throws InvalidWatcherException Thrown if the watcher identifier is invalid.
150158
*/
151159
public function unreference($watcherId);
152160

src/Loop/InvalidWatcherException.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Interop\Async\Loop;
4+
5+
/**
6+
* MUST be thrown if any operation (except cancel()) is attempted with an invalid watcher identifier. [Invalid watcher identifier: any identifier not yet emitted by the driver or cancelled by the user]
7+
*/
8+
class InvalidWatcherException extends \LogicException
9+
{
10+
11+
}

0 commit comments

Comments
 (0)