Skip to content

Commit

Permalink
Implement BufferedSink which resembles a promise-based async stream_g…
Browse files Browse the repository at this point in the history
…et_contents()
  • Loading branch information
igorw committed Nov 18, 2012
1 parent 2972666 commit 833fe52
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions BufferedSink.php
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();
}
}

0 comments on commit 833fe52

Please sign in to comment.