Skip to content

Commit 11b3492

Browse files
committed
Implement basic nested loops with Amp
1 parent 91f61ee commit 11b3492

File tree

4 files changed

+283
-12
lines changed

4 files changed

+283
-12
lines changed

composer.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@
99
"email": "[email protected]"
1010
}
1111
],
12+
"autoload": {
13+
"psr-4": {
14+
"Playground\\": "src"
15+
}
16+
},
1217
"require": {
13-
"async-interop/event-loop": "dev-static-class-protot as 1.0.0"
18+
"async-interop/event-loop": "dev-master as 1.0.0",
19+
"amphp/amp": "^1.2"
1420
},
1521
"repositories": [
1622
{
1723
"type": "vcs",
18-
"url": "https://github.com/AndrewCarterUK/event-loop"
24+
"url": "https://github.com/async-interop/event-loop"
1925
}
2026
]
2127
}

composer.lock

+68-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

run.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Playground;
4+
5+
use Amp\Deferred;
6+
use Interop\Async\EventLoop\EventLoop;
7+
8+
require __DIR__ . "/vendor/autoload.php";
9+
10+
EventLoop::execute(function () {
11+
$deferred = new Deferred;
12+
13+
$deferred->promise()->when(function($error, $value) {
14+
if ($error) {
15+
var_dump($error);
16+
} else {
17+
var_dump($value);
18+
}
19+
});
20+
21+
EventLoop::execute(function () {
22+
var_dump(__LINE__);
23+
24+
EventLoop::execute(function () {
25+
var_dump(__LINE__);
26+
}, new AmpDriver(\Amp\driver()));
27+
}, new AmpDriver(\Amp\driver()));
28+
29+
EventLoop::delay(function() use ($deferred) {
30+
$deferred->succeed(__LINE__);
31+
}, 1000);
32+
}, new AmpDriver(\Amp\driver()));

src/AmpDriver.php

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

0 commit comments

Comments
 (0)