Skip to content

Commit 6a13263

Browse files
committed
Merge pull request #35 from trowski/watcher-data
Add watcher data parameter
2 parents 78de0c9 + ffde57f commit 6a13263

File tree

2 files changed

+97
-84
lines changed

2 files changed

+97
-84
lines changed

src/Loop.php

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -64,155 +64,162 @@ public static function stop()
6464
/**
6565
* Defer the execution of a callback.
6666
*
67-
* @param callable $callback The callback to defer.
67+
* @param callable(string $watcherId, mixed $data) $callback The callback to defer.
68+
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
6869
*
69-
* @return string An identifier that can be used to cancel, enable or disable the event.
70+
* @return string An identifier that can be used to cancel, enable or disable the watcher.
7071
*/
71-
public static function defer(callable $callback)
72+
public static function defer(callable $callback, $data = null)
7273
{
73-
return self::get()->defer($callback);
74+
return self::get()->defer($callback, $data);
7475
}
7576

7677
/**
7778
* Delay the execution of a callback. The time delay is approximate and accuracy is not guaranteed.
7879
*
79-
* @param callable $callback The callback to delay.
80+
* @param callable(string $watcherId, mixed $data) $callback The callback to delay.
8081
* @param int $time The amount of time, in milliseconds, to delay the execution for.
82+
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
8183
*
82-
* @return string An identifier that can be used to cancel, enable or disable the event.
84+
* @return string An identifier that can be used to cancel, enable or disable the watcher.
8385
*/
84-
public static function delay(callable $callback, $time)
86+
public static function delay(callable $callback, $time, $data = null)
8587
{
86-
return self::get()->delay($callback, $time);
88+
return self::get()->delay($callback, $time, $data);
8789
}
8890

8991
/**
9092
* Repeatedly execute a callback. The interval between executions is approximate and accuracy is not guaranteed.
9193
*
92-
* @param callable $callback The callback to repeat.
94+
* @param callable(string $watcherId, mixed $data) $callback The callback to repeat.
9395
* @param int $interval The time interval, in milliseconds, to wait between executions.
96+
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
9497
*
95-
* @return string An identifier that can be used to cancel, enable or disable the event.
98+
* @return string An identifier that can be used to cancel, enable or disable the watcher.
9699
*/
97-
public static function repeat(callable $callback, $interval)
100+
public static function repeat(callable $callback, $interval, $data = null)
98101
{
99-
return self::get()->repeat($callback, $interval);
102+
return self::get()->repeat($callback, $interval, $data);
100103
}
101104

102105
/**
103106
* Execute a callback when a stream resource becomes readable.
104107
*
105108
* @param resource $stream The stream to monitor.
106-
* @param callable $callback The callback to execute.
109+
* @param callable(string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
110+
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
107111
*
108-
* @return string An identifier that can be used to cancel, enable or disable the event.
112+
* @return string An identifier that can be used to cancel, enable or disable the watcher.
109113
*/
110-
public static function onReadable($stream, callable $callback)
114+
public static function onReadable($stream, callable $callback, $data = null)
111115
{
112-
return self::get()->onReadable($stream, $callback);
116+
return self::get()->onReadable($stream, $callback, $data);
113117
}
114118

115119
/**
116120
* Execute a callback when a stream resource becomes writable.
117121
*
118122
* @param resource $stream The stream to monitor.
119-
* @param callable $callback The callback to execute.
123+
* @param callable(string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
124+
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
120125
*
121-
* @return string An identifier that can be used to cancel, enable or disable the event.
126+
* @return string An identifier that can be used to cancel, enable or disable the watcher.
122127
*/
123-
public static function onWritable($stream, callable $callback)
128+
public static function onWritable($stream, callable $callback, $data = null)
124129
{
125-
return self::get()->onWritable($stream, $callback);
130+
return self::get()->onWritable($stream, $callback, $data);
126131
}
127132

128133
/**
129134
* Execute a callback when a signal is received.
130135
*
131136
* @param int $signo The signal number to monitor.
132-
* @param callable $callback The callback to execute.
137+
* @param callable(string $watcherId, int $signo, mixed $data) $callback The callback to execute.
138+
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
133139
*
134-
* @return string An identifier that can be used to cancel, enable or disable the event.
140+
* @return string An identifier that can be used to cancel, enable or disable the watcher.
135141
*/
136-
public static function onSignal($signo, callable $callback)
142+
public static function onSignal($signo, callable $callback, $data = null)
137143
{
138-
return self::get()->onSignal($signo, $callback);
144+
return self::get()->onSignal($signo, $callback, $data);
139145
}
140146

141147
/**
142148
* Execute a callback when an error occurs.
143149
*
144-
* @param callable $callback The callback to execute.
150+
* @param callable(\Throwable|\Exception $exception) $callback The callback to execute.
145151
*
146-
* @return string An identifier that can be used to cancel, enable or disable the event.
152+
* @return string An identifier that can be used to cancel, enable or disable the watcher.
147153
*/
148154
public static function onError(callable $callback)
149155
{
150156
return self::get()->onError($callback);
151157
}
152158

153159
/**
154-
* Enable an event.
160+
* Enable a watcher.
155161
*
156-
* @param string $eventIdentifier The event identifier.
162+
* @param string $watcherId The watcher identifier.
157163
*
158164
* @return void
159165
*/
160-
public static function enable($eventIdentifier)
166+
public static function enable($watcherId)
161167
{
162-
self::get()->enable($eventIdentifier);
168+
self::get()->enable($watcherId);
163169
}
164170

165171
/**
166-
* Disable an event.
172+
* Disable a watcher.
167173
*
168-
* @param string $eventIdentifier The event identifier.
174+
* @param string $watcherId The watcher identifier.
169175
*
170176
* @return void
171177
*/
172-
public static function disable($eventIdentifier)
178+
public static function disable($watcherId)
173179
{
174-
self::get()->disable($eventIdentifier);
180+
self::get()->disable($watcherId);
175181
}
176182

177183
/**
178-
* Cancel an event.
184+
* Cancel a watcher.
179185
*
180-
* @param string $eventIdentifier The event identifier.
186+
* @param string $watcherId The watcher identifier.
181187
*
182188
* @return void
183189
*/
184-
public static function cancel($eventIdentifier)
190+
public static function cancel($watcherId)
185191
{
186-
self::get()->cancel($eventIdentifier);
192+
self::get()->cancel($watcherId);
187193
}
188194

189195
/**
190-
* Reference an event.
196+
* Reference a watcher.
191197
*
192-
* This will keep the event loop alive whilst the event is still being monitored. Events have this state by default.
198+
* This will keep the event loop alive whilst the event is still being monitored. Watchers have this state by
199+
* default.
193200
*
194-
* @param string $eventIdentifier The event identifier.
201+
* @param string $watcherId The watcher identifier.
195202
*
196203
* @return void
197204
*/
198-
public static function reference($eventIdentifier)
205+
public static function reference($watcherId)
199206
{
200-
self::get()->reference($eventIdentifier);
207+
self::get()->reference($watcherId);
201208
}
202209

203210
/**
204-
* Unreference an event.
211+
* Unreference a watcher.
205212
*
206-
* The event loop should exit the run method when only unreferenced events are still being monitored. Events are all
207-
* referenced by default.
213+
* The event loop should exit the run method when only unreferenced watchers are still being monitored. Events are
214+
* all referenced by default.
208215
*
209-
* @param string $eventIdentifier The event identifier.
216+
* @param string $watcherId The watcher identifier.
210217
*
211218
* @return void
212219
*/
213-
public static function unreference($eventIdentifier)
220+
public static function unreference($watcherId)
214221
{
215-
self::get()->unreference($eventIdentifier);
222+
self::get()->unreference($watcherId);
216223
}
217224

218225
/**

src/LoopDriver.php

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,120 +23,126 @@ public function stop();
2323
/**
2424
* Defer the execution of a callback.
2525
*
26-
* @param callable $callback The callback to defer.
26+
* @param callable(string $watcherId, mixed $data) $callback The callback to defer.
27+
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
2728
*
28-
* @return string An identifier that can be used to cancel, enable or disable the event.
29+
* @return string An identifier that can be used to cancel, enable or disable the watcher.
2930
*/
30-
public function defer(callable $callback);
31+
public function defer(callable $callback, $data = null);
3132

3233
/**
3334
* Delay the execution of a callback. The time delay is approximate and accuracy is not guaranteed.
3435
*
35-
* @param callable $callback The callback to delay.
36+
* @param callable(string $watcherId, mixed $data) $callback The callback to delay.
3637
* @param int $delay The amount of time, in milliseconds, to delay the execution for.
38+
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
3739
*
38-
* @return string An identifier that can be used to cancel, enable or disable the event.
40+
* @return string An identifier that can be used to cancel, enable or disable the watcher.
3941
*/
40-
public function delay(callable $callback, $delay);
42+
public function delay(callable $callback, $delay, $data = null);
4143

4244
/**
4345
* Repeatedly execute a callback. The interval between executions is approximate and accuracy is not guaranteed.
4446
*
45-
* @param callable $callback The callback to repeat.
47+
* @param callable(string $watcherId, mixed $data) $callback The callback to repeat.
4648
* @param int $interval The time interval, in milliseconds, to wait between executions.
49+
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
4750
*
48-
* @return string An identifier that can be used to cancel, enable or disable the event.
51+
* @return string An identifier that can be used to cancel, enable or disable the watcher.
4952
*/
50-
public function repeat(callable $callback, $interval);
53+
public function repeat(callable $callback, $interval, $data = null);
5154

5255
/**
5356
* Execute a callback when a stream resource becomes readable.
5457
*
5558
* @param resource $stream The stream to monitor.
56-
* @param callable $callback The callback to execute.
59+
* @param callable(string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
60+
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
5761
*
58-
* @return string An identifier that can be used to cancel, enable or disable the event.
62+
* @return string An identifier that can be used to cancel, enable or disable the watcher.
5963
*/
60-
public function onReadable($stream, callable $callback);
64+
public function onReadable($stream, callable $callback, $data = null);
6165

6266
/**
6367
* Execute a callback when a stream resource becomes writable.
6468
*
6569
* @param resource $stream The stream to monitor.
66-
* @param callable $callback The callback to execute.
70+
* @param callable(string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
71+
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
6772
*
68-
* @return string An identifier that can be used to cancel, enable or disable the event.
73+
* @return string An identifier that can be used to cancel, enable or disable the watcher.
6974
*/
70-
public function onWritable($stream, callable $callback);
75+
public function onWritable($stream, callable $callback, $data = null);
7176

7277
/**
7378
* Execute a callback when a signal is received.
7479
*
7580
* @param int $signo The signal number to monitor.
76-
* @param callable $callback The callback to execute.
81+
* @param callable(string $watcherId, int $signo, mixed $data) $callback The callback to execute.
82+
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
7783
*
78-
* @return string An identifier that can be used to cancel, enable or disable the event.
84+
* @return string An identifier that can be used to cancel, enable or disable the watcher.
7985
*/
80-
public function onSignal($signo, callable $callback);
86+
public function onSignal($signo, callable $callback, $data = null);
8187

8288
/**
8389
* Execute a callback when an error occurs.
8490
*
85-
* @param callable $callback The callback to execute.
91+
* @param callable(\Throwable|\Exception $exception) $callback The callback to execute.
8692
*
87-
* @return string An identifier that can be used to cancel, enable or disable the event.
93+
* @return string An identifier that can be used to cancel, enable or disable the watcher.
8894
*/
8995
public function onError(callable $callback);
9096

9197
/**
92-
* Enable an event.
98+
* Enable a watcher.
9399
*
94-
* @param string $eventIdentifier The event identifier.
100+
* @param string $watcherId The watcher identifier.
95101
*
96102
* @return void
97103
*/
98-
public function enable($eventIdentifier);
104+
public function enable($watcherId);
99105

100106
/**
101-
* Disable an event.
107+
* Disable a watcher.
102108
*
103-
* @param string $eventIdentifier The event identifier.
109+
* @param string $watcherId The watcher identifier.
104110
*
105111
* @return void
106112
*/
107-
public function disable($eventIdentifier);
113+
public function disable($watcherId);
108114

109115
/**
110-
* Cancel an event.
116+
* Cancel a watcher.
111117
*
112-
* @param string $eventIdentifier The event identifier.
118+
* @param string $watcherId The watcher identifier.
113119
*
114120
* @return void
115121
*/
116-
public function cancel($eventIdentifier);
122+
public function cancel($watcherId);
117123

118124
/**
119-
* Reference an event.
125+
* Reference a watcher.
120126
*
121127
* This will keep the event loop alive whilst the event is still being monitored. Events have this state by default.
122128
*
123-
* @param string $eventIdentifier The event identifier.
129+
* @param string $watcherId The watcher identifier.
124130
*
125131
* @return void
126132
*/
127-
public function reference($eventIdentifier);
133+
public function reference($watcherId);
128134

129135
/**
130-
* Unreference an event.
136+
* Unreference a watcher.
131137
*
132138
* The event loop should exit the run method when only unreferenced events are still being monitored. Events are all
133139
* referenced by default.
134140
*
135-
* @param string $eventIdentifier The event identifier.
141+
* @param string $watcherId The watcher identifier.
136142
*
137143
* @return void
138144
*/
139-
public function unreference($eventIdentifier);
145+
public function unreference($watcherId);
140146

141147
/**
142148
* Check whether an optional features is supported by this implementation

0 commit comments

Comments
 (0)