From d02fb30bf5e7c361f039df09a1018507b897fa6b Mon Sep 17 00:00:00 2001 From: Dennis Povshedny Date: Thu, 7 May 2015 18:01:01 +0300 Subject: [PATCH 1/2] Small fix to emitter of 'data' event I believe the $response parameter should be added to not miss very first chunk of data. Proposed change makes code to work in the same way as the following line $response->emit('data', array($bodyChunk, $response)); in the Response::handleData function. --- src/Request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Request.php b/src/Request.php index 8dbd150..67ae3a1 100644 --- a/src/Request.php +++ b/src/Request.php @@ -147,7 +147,7 @@ public function handleData($data) $this->emit('response', array($response, $this)); - $response->emit('data', array($bodyChunk)); + $response->emit('data', array($bodyChunk, $response)); } } From 678188742106049ae4435d088ba33bc64033c706 Mon Sep 17 00:00:00 2001 From: Dennis Povshedny Date: Sun, 10 May 2015 00:55:31 +0300 Subject: [PATCH 2/2] The 'data' event emitted by Response always passing itself as a second argument. --- README.md | 13 +++++++------ src/Response.php | 2 +- tests/RequestTest.php | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e4f0827..b415670 100644 --- a/README.md +++ b/README.md @@ -17,16 +17,17 @@ Interesting events emitted by Request: * `response`: The response headers were received from the server and successfully parsed. The first argument is a Response instance. -* `error`: An error occured. -* `end`: The request is finished. If an error occured, it is passed as first +* `error`: An error occurred. +* `end`: The request is finished. If an error occurred, it is passed as first argument. Second and third arguments are the Response and the Request. Interesting events emitted by Response: -* `data`: Passes a chunk of the response body as first argument -* `error`: An error occured. +* `data`: Passes a chunk of the response body as first argument and a Response + object itself as second argument. +* `error`: An error occurred. * `end`: The response has been fully received. If an error - occured, it is passed as first argument + occurred, it is passed as first argument. ### Example @@ -43,7 +44,7 @@ $client = $factory->create($loop, $dnsResolver); $request = $client->request('GET', 'https://github.com/'); $request->on('response', function ($response) { - $response->on('data', function ($data) { + $response->on('data', function ($data, $response) { // ... }); }); diff --git a/src/Response.php b/src/Response.php index 318c0c5..880d412 100644 --- a/src/Response.php +++ b/src/Response.php @@ -9,7 +9,7 @@ use React\Stream\WritableStreamInterface; /** - * @event data + * @event data ($bodyChunk, Response $thisResponse) * @event error * @event end */ diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 5d2d595..eb17727 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -71,7 +71,7 @@ public function requestShouldBindToStreamEventsAndUseconnector() $response->expects($this->once()) ->method('emit') - ->with('data', array('body')); + ->with('data', array('body', $response)); $response->expects($this->at(0)) ->method('on')