-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement BufferedSink which resembles a promise-based async stream_g…
…et_contents()
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace React\Stream; | ||
|
||
use React\Promise\Deferred; | ||
use React\Promise\PromisorInterface; | ||
use React\Stream\WritableStream; | ||
|
||
class BufferedSink extends WritableStream implements PromisorInterface | ||
{ | ||
private $buffer = ''; | ||
private $deferred; | ||
|
||
public function __construct() | ||
{ | ||
$this->deferred = new Deferred(); | ||
|
||
$this->on('pipe', array($this, 'handlePipeEvent')); | ||
$this->on('error', array($this, 'handleErrorEvent')); | ||
} | ||
|
||
public function handlePipeEvent($source) | ||
{ | ||
Util::forwardEvents($source, $this, array('error')); | ||
} | ||
|
||
public function handleErrorEvent($e) | ||
{ | ||
$this->deferred->reject($e); | ||
} | ||
|
||
public function write($data) | ||
{ | ||
$this->buffer .= $data; | ||
} | ||
|
||
public function close() | ||
{ | ||
if ($this->closed) { | ||
return; | ||
} | ||
|
||
parent::close(); | ||
$this->deferred->resolve($this->buffer); | ||
} | ||
|
||
public function promise() | ||
{ | ||
return $this->deferred->promise(); | ||
} | ||
} |