Skip to content

Commit 97ea247

Browse files
committed
dont call notify callback if we dont have a proper change
1 parent 95a5ecb commit 97ea247

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/NotifyHandler.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ public function getChanges() {
5959
public function listen($callback) {
6060
if ($this->listening) {
6161
$this->connection->read(function ($line) use ($callback) {
62-
return $callback($this->parseChangeLine($line));
62+
$change = $this->parseChangeLine($line);
63+
if ($change) {
64+
return $callback($change);
65+
}
6366
});
6467
}
6568
}

tests/NotifyHandlerTest.php

+38-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public function setUp() {
3131
*
3232
* filter them out so we can compare changes properly
3333
*
34-
* @param array $changes
35-
* @return array
34+
* @param Change[] $changes
35+
* @return Change[]
3636
*/
3737
private function filterModifiedChanges(array $changes) {
3838
return array_values(array_filter($changes, function (Change $change) {
@@ -114,4 +114,40 @@ public function testStopped() {
114114
$process->stop();
115115
$this->assertEquals([], $process->getChanges());
116116
}
117+
118+
public function testListenAfterGetChanges() {
119+
$share = $this->server->getShare($this->config->share);
120+
$process = $share->notify('');
121+
122+
usleep(1000 * 100);// give it some time to start listening
123+
124+
$share->put(__FILE__, 'source.txt');
125+
$share->rename('source.txt', 'target.txt');
126+
$share->del('target.txt');
127+
usleep(1000 * 100);// give it some time
128+
129+
$changes = $process->getChanges();
130+
$expected = [
131+
new Change(INotifyHandler::NOTIFY_ADDED, 'source.txt'),
132+
new Change(INotifyHandler::NOTIFY_RENAMED_OLD, 'source.txt'),
133+
new Change(INotifyHandler::NOTIFY_RENAMED_NEW, 'target.txt'),
134+
new Change(INotifyHandler::NOTIFY_REMOVED, 'target.txt'),
135+
];
136+
137+
$this->assertEquals($expected, $this->filterModifiedChanges($changes));
138+
139+
usleep(1000 * 200);
140+
141+
$share->put(__FILE__, 'source2.txt');
142+
$share->del('source2.txt');
143+
144+
$results = null;
145+
146+
// the notify process buffers incoming messages so callback will be triggered for the above changes
147+
$process->listen(function ($change) use (&$results) {
148+
$results = $change;
149+
return false; // stop listening
150+
});
151+
$this->assertNotNull($results);
152+
}
117153
}

0 commit comments

Comments
 (0)