Skip to content

Commit 7e4ce36

Browse files
Prototype static event loop class
1 parent 09a4083 commit 7e4ce36

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed

src/EventLoop.php

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
<?php
2+
3+
namespace Interop\Async\EventLoop;
4+
5+
final class EventLoop
6+
{
7+
/**
8+
* @var EventLoopDriver
9+
*/
10+
private static $driver = null;
11+
12+
/**
13+
* Execute a callback within the scope of an event loop driver.
14+
*
15+
* @param callable $callback The callback to execute
16+
* @param EventLoopDriver $driver The event loop driver
17+
*
18+
* @return void
19+
*/
20+
public static function execute(callable $callback, EventLoopDriver $driver)
21+
{
22+
$previousDriver = self::$driver;
23+
24+
self::$driver = $driver;
25+
26+
try {
27+
$callback();
28+
29+
self::$driver->run();
30+
} finally {
31+
self::$driver = $previousDriver;
32+
}
33+
}
34+
35+
/**
36+
* Stop the event loop.
37+
*
38+
* @return void
39+
*/
40+
public static function stop()
41+
{
42+
self::$driver->stop();
43+
}
44+
45+
/**
46+
* Defer the execution of a callback.
47+
*
48+
* @param callable $callback The callback to defer.
49+
*
50+
* @return string An identifier that can be used to cancel, enable or disable the event.
51+
*/
52+
public static function defer(callable $callback)
53+
{
54+
return self::$driver->defer($callback);
55+
}
56+
57+
/**
58+
* Delay the execution of a callback. The time delay is approximate and accuracy is not guaranteed.
59+
*
60+
* @param callable $callback The callback to delay.
61+
* @param float $time The amount of time, in seconds, to delay the execution for.
62+
*
63+
* @return string An identifier that can be used to cancel, enable or disable the event.
64+
*/
65+
public static function delay(callable $callback, float $time)
66+
{
67+
return self::$driver->delay($callback, $time);
68+
}
69+
70+
/**
71+
* Repeatedly execute a callback. The interval between executions is approximate and accuracy is not guaranteed.
72+
*
73+
* @param callable $callback The callback to repeat.
74+
* @param float $interval The time interval, in seconds, to wait between executions.
75+
*
76+
* @return string An identifier that can be used to cancel, enable or disable the event.
77+
*/
78+
public static function repeat(callable $callback, float $interval)
79+
{
80+
return self::$driver->repeat($callback, $interval);
81+
}
82+
83+
/**
84+
* Execute a callback when a stream resource becomes readable.
85+
*
86+
* @param resource $stream The stream to monitor.
87+
* @param callable $callback The callback to execute.
88+
*
89+
* @return string An identifier that can be used to cancel, enable or disable the event.
90+
*/
91+
public static function onReadable($stream, callable $callback)
92+
{
93+
return self::$driver->onReadable($stream, $callback);
94+
}
95+
96+
/**
97+
* Execute a callback when a stream resource becomes writable.
98+
*
99+
* @param resource $stream The stream to monitor.
100+
* @param callable $callback The callback to execute.
101+
*
102+
* @return string An identifier that can be used to cancel, enable or disable the event.
103+
*/
104+
public function onWritable($stream, callable $callback)
105+
{
106+
return self::$driver->onWritable($stream, $callback);
107+
}
108+
109+
/**
110+
* Execute a callback when a signal is received.
111+
*
112+
* @param int $signo The signal number to monitor.
113+
* @param callable $callback The callback to execute.
114+
*
115+
* @return string An identifier that can be used to cancel, enable or disable the event.
116+
*/
117+
public function onSignal(int $signo, callable $callback)
118+
{
119+
return self::$driver->onSignal($signo, $callback);
120+
}
121+
122+
/**
123+
* Execute a callback when an error occurs.
124+
*
125+
* @param callable $callback The callback to execute.
126+
*
127+
* @return string An identifier that can be used to cancel, enable or disable the event.
128+
*/
129+
public function onError(callable $callback)
130+
{
131+
return self::$driver->onError($callback);
132+
}
133+
134+
/**
135+
* Enable an event.
136+
*
137+
* @param string $eventIdentifier The event identifier.
138+
*
139+
* @return void
140+
*/
141+
public function enable(string $eventIdentifier)
142+
{
143+
self::$driver->enable($eventIdentifier);
144+
}
145+
146+
/**
147+
* Disable an event.
148+
*
149+
* @param string $eventIdentifier The event identifier.
150+
*
151+
* @return void
152+
*/
153+
public function disable(string $eventIdentifier)
154+
{
155+
self::$driver->disable($eventIdentifier);
156+
}
157+
158+
/**
159+
* Cancel an event.
160+
*
161+
* @param string $eventIdentifier The event identifier.
162+
*
163+
* @return void
164+
*/
165+
public function cancel(string $eventIdentifier)
166+
{
167+
self::$driver->cancel($eventIdentifier);
168+
}
169+
170+
/**
171+
* Reference an event.
172+
*
173+
* This will keep the event loop alive whilst the event is still being monitored. Events have this state by default.
174+
*
175+
* @param string $eventIdentifier The event identifier.
176+
*
177+
* @return void
178+
*/
179+
public function reference(string $eventIdentifier)
180+
{
181+
self::$driver->reference($eventIdentifier);
182+
}
183+
184+
/**
185+
* Unreference an event.
186+
*
187+
* The event loop should exit the run method when only unreferenced events are still being monitored. Events are all
188+
* referenced by default.
189+
*
190+
* @param string $eventIdentifier The event identifier.
191+
*
192+
* @return void
193+
*/
194+
public function unreference(string $eventIdentifier)
195+
{
196+
self::$driver->unreference($eventIdentifier);
197+
}
198+
199+
/**
200+
* Disable construction as this is a static class.
201+
*/
202+
public function __construct()
203+
{
204+
throw new \LogicException('This class is a static class and should not be initialized');
205+
}
206+
}

0 commit comments

Comments
 (0)