22
33namespace React \Stream ;
44
5- class ThroughStream extends CompositeStream
5+ use Evenement \EventEmitter ;
6+
7+ class ThroughStream extends EventEmitter implements DuplexStreamInterface
68{
9+ private $ readable = true ;
10+ private $ writable = true ;
11+ private $ closed = false ;
712 private $ paused = false ;
8-
9- public function __construct ()
10- {
11- $ readable = new ReadableStream ();
12- $ writable = new WritableStream ();
13-
14- parent ::__construct ($ readable , $ writable );
15- }
13+ private $ drain = false ;
1614
1715 public function filter ($ data )
1816 {
@@ -21,39 +19,80 @@ public function filter($data)
2119
2220 public function pause ()
2321 {
24- parent ::pause ();
2522 $ this ->paused = true ;
2623 }
2724
2825 public function resume ()
2926 {
30- parent ::resume ();
27+ if ($ this ->drain ) {
28+ $ this ->drain = false ;
29+ $ this ->emit ('drain ' );
30+ }
3131 $ this ->paused = false ;
3232 }
3333
34+ public function pipe (WritableStreamInterface $ dest , array $ options = array ())
35+ {
36+ return Util::pipe ($ this , $ dest , $ options );
37+ }
38+
39+ public function isReadable ()
40+ {
41+ return $ this ->readable ;
42+ }
43+
44+ public function isWritable ()
45+ {
46+ return $ this ->writable ;
47+ }
48+
3449 public function write ($ data )
3550 {
36- if (!$ this ->writable -> isWritable () ) {
51+ if (!$ this ->writable ) {
3752 return false ;
3853 }
3954
40- $ this ->readable ->emit ('data ' , array ($ this ->filter ($ data )));
55+ $ this ->emit ('data ' , array ($ this ->filter ($ data )));
56+
57+ if ($ this ->paused ) {
58+ $ this ->drain = true ;
59+ return false ;
60+ }
4161
42- return $ this -> writable -> isWritable () && ! $ this -> paused ;
62+ return true ;
4363 }
4464
4565 public function end ($ data = null )
4666 {
47- if (!$ this ->writable -> isWritable () ) {
67+ if (!$ this ->writable ) {
4868 return ;
4969 }
5070
5171 if (null !== $ data ) {
52- $ this ->readable -> emit ( ' data ' , array ( $ this -> filter ( $ data)) );
72+ $ this ->write ( $ data );
5373 }
5474
55- $ this ->readable ->emit ('end ' );
75+ $ this ->readable = false ;
76+ $ this ->writable = false ;
77+ $ this ->paused = true ;
78+ $ this ->drain = false ;
79+
80+ $ this ->emit ('end ' );
81+ $ this ->close ();
82+ }
83+
84+ public function close ()
85+ {
86+ if ($ this ->closed ) {
87+ return ;
88+ }
89+
90+ $ this ->readable = false ;
91+ $ this ->writable = false ;
92+ $ this ->closed = true ;
93+ $ this ->paused = true ;
94+ $ this ->drain = false ;
5695
57- $ this ->writable -> end ( );
96+ $ this ->emit ( ' close ' );
5897 }
5998}
0 commit comments