File tree Expand file tree Collapse file tree 2 files changed +48
-1
lines changed Expand file tree Collapse file tree 2 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ class UnwrapReadableStream extends EventEmitter implements ReadableStreamInterfa
27
27
public function __construct (PromiseInterface $ promise )
28
28
{
29
29
$ out = $ this ;
30
+ $ closed =& $ this ->closed ;
30
31
31
32
$ this ->promise = $ promise ->then (
32
33
function ($ stream ) {
@@ -36,12 +37,19 @@ function ($stream) {
36
37
return $ stream ;
37
38
}
38
39
)->then (
39
- function (ReadableStreamInterface $ stream ) use ($ out ) {
40
+ function (ReadableStreamInterface $ stream ) use ($ out , &$ closed ) {
41
+ // stream is already closed, make sure to close output stream
40
42
if (!$ stream ->isReadable ()) {
41
43
$ out ->close ();
42
44
return $ stream ;
43
45
}
44
46
47
+ // resolves but output is already closed, make sure to close stream silently
48
+ if ($ closed ) {
49
+ $ stream ->close ();
50
+ return $ stream ;
51
+ }
52
+
45
53
// stream any writes into output stream
46
54
$ stream ->on ('data ' , function ($ data ) use ($ out ) {
47
55
$ out ->emit ('data ' , array ($ data , $ out ));
Original file line number Diff line number Diff line change @@ -210,4 +210,43 @@ public function testPipingStreamWillForwardDataEvents()
210
210
211
211
$ output ->promise ()->then ($ this ->expectCallableOnceWith ('helloworld ' ));
212
212
}
213
+
214
+ public function testClosingStreamWillCloseStreamIfItIgnoredCancellationAndResolvesLater ()
215
+ {
216
+ $ input = new ReadableStream ();
217
+
218
+ $ loop = $ this ->loop ;
219
+ $ promise = new Promise \Promise (function ($ resolve ) use ($ loop , $ input ) {
220
+ $ loop ->addTimer (0.001 , function () use ($ resolve , $ input ) {
221
+ $ resolve ($ input );
222
+ });
223
+ });
224
+
225
+ $ stream = Stream \unwrapReadable ($ promise );
226
+
227
+ $ stream ->on ('close ' , $ this ->expectCallableOnce ());
228
+
229
+ $ stream ->close ();
230
+
231
+ Block \await ($ promise , $ this ->loop );
232
+
233
+ $ this ->assertFalse ($ input ->isReadable ());
234
+ }
235
+
236
+ public function testClosingStreamWillCloseStreamFromCancellationHandler ()
237
+ {
238
+ $ input = new ReadableStream ();
239
+
240
+ $ promise = new \React \Promise \Promise (function () { }, function ($ resolve ) use ($ input ) {
241
+ $ resolve ($ input );
242
+ });
243
+
244
+ $ stream = Stream \unwrapReadable ($ promise );
245
+
246
+ $ stream ->on ('close ' , $ this ->expectCallableOnce ());
247
+
248
+ $ stream ->close ();
249
+
250
+ $ this ->assertFalse ($ input ->isReadable ());
251
+ }
213
252
}
You can’t perform that action at this time.
0 commit comments