Skip to content

Commit e356b73

Browse files
authored
Merge pull request #15 from clue-labs/close-during-data
Stop processing incoming data when stream is closed from data handler
2 parents 92bec1b + 078d7b5 commit e356b73

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/EventSource.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ private function request()
151151
}
152152
});
153153

154-
$stream->on('close', function () {
154+
$stream->on('close', function () use (&$buffer) {
155+
$buffer = '';
155156
$this->request = null;
156157
if ($this->readyState === self::OPEN) {
157158
$this->readyState = self::CONNECTING;

tests/EventSourceTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,34 @@ public function testEmitMessageWithParsedDataAndPreviousIdWhenNotGivenAgainFromE
557557
$this->assertEquals('123', $message->lastEventId);
558558
}
559559

560+
public function testEmitMessageOnceWhenCallingCloseFromMessageHandlerFromEventStreamWithMultipleMessages()
561+
{
562+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
563+
564+
$deferred = new Deferred();
565+
$browser = $this->getMockBuilder('React\Http\Browser')->disableOriginalConstructor()->getMock();
566+
$browser->expects($this->once())->method('withRejectErrorResponse')->willReturnSelf();
567+
$browser->expects($this->once())->method('requestStreaming')->willReturn($deferred->promise());
568+
569+
$es = new EventSource('http://example.com', $loop, $browser);
570+
571+
$stream = new ThroughStream();
572+
$response = new Response(200, array('Content-Type' => 'text/event-stream'), new ReadableBodyStream($stream));
573+
$deferred->resolve($response);
574+
575+
$message = null;
576+
$es->on('message', function ($m) use (&$message, $es) {
577+
$message = $m;
578+
$es->close();
579+
});
580+
581+
$stream->write("id:1\ndata:hello\n\nid:2\ndata:world\n\n");
582+
583+
$this->assertInstanceOf('Clue\React\EventSource\MessageEvent', $message);
584+
$this->assertEquals('1', $message->lastEventId);
585+
$this->assertEquals('1', $es->lastEventId);
586+
}
587+
560588
public function testReconnectAfterStreamClosesUsesLastEventIdFromParsedEventStreamForNextRequest()
561589
{
562590
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();

0 commit comments

Comments
 (0)