Skip to content

Commit 9d74cf3

Browse files
authored
Merge pull request #36 from clue-labs/refactor-retry
Refactor `retry` field handling, avoid internal `MessageEvent` property
2 parents 3b76fae + 74daad4 commit 9d74cf3

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed

src/EventSource.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,9 @@ private function request()
211211
$buffer = array_pop($messageEvents);
212212

213213
foreach ($messageEvents as $data) {
214-
$message = MessageEvent::parse($data, $this->lastEventId);
214+
$message = MessageEvent::parse($data, $this->lastEventId, $this->reconnectTime);
215215
$this->lastEventId = $message->lastEventId;
216216

217-
if ($message->retry !== null) {
218-
$this->reconnectTime = $message->retry / 1000;
219-
}
220-
221217
if ($message->data !== '') {
222218
$this->emit($message->type, array($message));
223219
if ($this->readyState === self::CLOSED) {

src/MessageEvent.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ class MessageEvent
77
/**
88
* @param string $data
99
* @param string $lastEventId
10+
* @param float $retryTime passed by reference, will be updated with `retry` field in seconds if valid
1011
* @return self
1112
* @internal
1213
*/
13-
public static function parse($data, $lastEventId)
14+
public static function parse($data, $lastEventId, &$retryTime = 0.0)
1415
{
1516
$lines = preg_split(
1617
'/\r\n|\r(?!\n)|\n/S',
@@ -20,7 +21,6 @@ public static function parse($data, $lastEventId)
2021
$data = '';
2122
$id = $lastEventId;
2223
$type = 'message';
23-
$retry = null;
2424

2525
foreach ($lines as $line) {
2626
$name = strstr($line, ':', true);
@@ -35,30 +35,28 @@ public static function parse($data, $lastEventId)
3535
} elseif ($name === 'event' && $value !== '') {
3636
$type = $value;
3737
} elseif ($name === 'retry' && $value === (string)(int)$value && $value >= 0) {
38-
$retry = (int)$value;
38+
$retryTime = $value * 0.001;
3939
}
4040
}
4141

4242
if (substr($data, -1) === "\n") {
4343
$data = substr($data, 0, -1);
4444
}
4545

46-
return new self($data, $id, $type, $retry);
46+
return new self($data, $id, $type);
4747
}
4848

4949
/**
5050
* @internal
5151
* @param string $data
5252
* @param string $lastEventId
5353
* @param string $type
54-
* @param ?int $retry
5554
*/
56-
private function __construct($data, $lastEventId, $type, $retry)
55+
private function __construct($data, $lastEventId, $type)
5756
{
5857
$this->data = $data;
5958
$this->lastEventId = $lastEventId;
6059
$this->type = $type;
61-
$this->retry = $retry;
6260
}
6361

6462
/**
@@ -78,11 +76,4 @@ private function __construct($data, $lastEventId, $type, $retry)
7876
* @readonly
7977
*/
8078
public $type = 'message';
81-
82-
/**
83-
* @internal
84-
* @var ?int
85-
* @readonly
86-
*/
87-
public $retry;
8879
}

tests/MessageEventTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ public function testParseWithEmptyEventReturnsMessageWithDefaultMessageType()
123123
public function retryTimeDataProvider()
124124
{
125125
return [
126-
['retry: 1234', 1234,],
127-
['retry: 0', 0,],
128-
['retry: ' . PHP_INT_MAX, PHP_INT_MAX,],
129-
['retry: ' . PHP_INT_MAX . '9', null,],
130-
['retry: 1.234', null,],
131-
['retry: now', null,],
132-
['retry: -1', null,],
133-
['retry: -1.234', null,],
126+
['retry: 1234', 1.234],
127+
['retry: 0', 0.0],
128+
['retry: ' . PHP_INT_MAX, PHP_INT_MAX * 0.001],
129+
['retry: ' . PHP_INT_MAX . '9', null],
130+
['retry: 1.234', null],
131+
['retry: now', null],
132+
['retry: -1', null],
133+
['retry: -1.234', null]
134134
];
135135
}
136136

@@ -139,8 +139,9 @@ public function retryTimeDataProvider()
139139
*/
140140
public function testParseRetryTime($input, $expected)
141141
{
142-
$message = MessageEvent::parse($input, '');
142+
$retryTime = null;
143+
MessageEvent::parse($input, '', $retryTime);
143144

144-
$this->assertSame($expected, $message->retry);
145+
$this->assertSame($expected, $retryTime);
145146
}
146147
}

0 commit comments

Comments
 (0)