Skip to content

Commit c113826

Browse files
committed
[EventLoop] Associate only one listener per each stream event type.
Allowing multiple listeners makes code more complex and it is beyond the scope of the whole event loop abstraction anyway. Such a requirement could be easily satisfied on a higher level if a class wrapping the event loop needs to notify more than one listener about changes in the status of a stream resource.
1 parent 0a2b17a commit c113826

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

StreamSelectLoop.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ public function addReadStream($stream, $listener)
2626

2727
if (!isset($this->readStreams[$id])) {
2828
$this->readStreams[$id] = $stream;
29-
$this->readListeners[$id] = array();
29+
$this->readListeners[$id] = $listener;
3030
}
31-
32-
$this->readListeners[$id][] = $listener;
3331
}
3432

3533
public function addWriteStream($stream, $listener)
@@ -38,26 +36,28 @@ public function addWriteStream($stream, $listener)
3836

3937
if (!isset($this->writeStreams[$id])) {
4038
$this->writeStreams[$id] = $stream;
41-
$this->writeListeners[$id] = array();
39+
$this->writeListeners[$id] = $listener;
4240
}
43-
44-
$this->writeListeners[$id][] = $listener;
4541
}
4642

4743
public function removeReadStream($stream)
4844
{
4945
$id = (int) $stream;
5046

51-
unset($this->readStreams[$id]);
52-
unset($this->readListeners[$id]);
47+
unset(
48+
$this->readStreams[$id],
49+
$this->readListeners[$id]
50+
);
5351
}
5452

5553
public function removeWriteStream($stream)
5654
{
5755
$id = (int) $stream;
5856

59-
unset($this->writeStreams[$id]);
60-
unset($this->writeListeners[$id]);
57+
unset(
58+
$this->writeStreams[$id],
59+
$this->writeListeners[$id]
60+
);
6161
}
6262

6363
public function removeStream($stream)
@@ -79,11 +79,10 @@ public function tick()
7979
if (stream_select($read, $write, $except, 0, $this->timeout) > 0) {
8080
if ($read) {
8181
foreach ($read as $stream) {
82-
foreach ($this->readListeners[(int) $stream] as $listener) {
83-
if (call_user_func($listener, $stream, $this) === false) {
84-
$this->removeReadStream($stream);
85-
break;
86-
}
82+
$listener = $this->readListeners[(int) $stream];
83+
if (call_user_func($listener, $stream, $this) === false) {
84+
$this->removeReadStream($stream);
85+
break;
8786
}
8887
}
8988
}
@@ -93,11 +92,11 @@ public function tick()
9392
if (!isset($this->writeListeners[(int) $stream])) {
9493
continue;
9594
}
96-
foreach ($this->writeListeners[(int) $stream] as $listener) {
97-
if (call_user_func($listener, $stream, $this) === false) {
98-
$this->removeWriteStream($stream);
99-
break;
100-
}
95+
96+
$listener = $this->writeListeners[(int) $stream];
97+
if (call_user_func($listener, $stream, $this) === false) {
98+
$this->removeWriteStream($stream);
99+
break;
101100
}
102101
}
103102
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy