From e7fcd487226ba663678858436c984fa7c1fd10bc Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Tue, 25 Oct 2016 23:43:15 +0200 Subject: [PATCH] Improve implementation --- src/ProtocolStreamWriter.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ProtocolStreamWriter.php b/src/ProtocolStreamWriter.php index 98b9275..41d7afc 100644 --- a/src/ProtocolStreamWriter.php +++ b/src/ProtocolStreamWriter.php @@ -34,13 +34,15 @@ class ProtocolStreamWriter implements ProtocolWriter * Sends a Message to the client * * @param Message $msg - * @return void + * @return Promise Resolved when the message has been fully written out to the output stream */ public function write(Message $msg) { // if the message queue is currently empty, register a write handler. if (empty($this->messages)) { - Loop\addWriteStream($this->output, [$this, 'writeData']); + Loop\addWriteStream($this->output, function () { + $this->flush(); + }); } $promise = new Promise(); @@ -53,11 +55,10 @@ class ProtocolStreamWriter implements ProtocolWriter /** * Writes pending messages to the output stream. - * Must be public to be able to be used as a callback. * * @return void */ - public function writeData() + private function flush() { $keepWriting = true; while ($keepWriting) {