22
33namespace React \Filesystem \Stream ;
44
5+ use React \Promise \FulfilledPromise ;
6+ use React \Promise \RejectedPromise ;
57use React \Stream \Util ;
68use React \Stream \WritableStreamInterface ;
79
@@ -11,22 +13,31 @@ trait ReadableStreamTrait
1113 protected $ readCursor ;
1214 protected $ chunkSize = 8192 ;
1315 protected $ pause = true ;
16+ protected $ isReading = false ;
17+ private $ sizeLookupPromise ;
1418
1519 public function resume ()
1620 {
21+ if (!$ this ->pause || $ this ->isReading ) {
22+ return ;
23+ }
24+
1725 $ this ->pause = false ;
1826
19- if ($ this ->size === null ) {
20- $ this ->getFilesystem ()->stat ($ this ->getPath ())->then (function ($ info ) {
27+ if ($ this ->size === null && $ this ->sizeLookupPromise === null ) {
28+ $ this ->sizeLookupPromise = $ this ->getFilesystem ()->stat ($ this ->getPath ())->then (function ($ info ) {
29+ if ($ this ->size !== null ) {
30+ return new RejectedPromise ();
31+ }
2132 $ this ->size = $ info ['size ' ];
2233 $ this ->readCursor = 0 ;
23-
24- $ this ->readChunk ();
34+ return new FulfilledPromise ();
2535 });
26- return ;
2736 }
2837
29- $ this ->readChunk ();
38+ $ this ->sizeLookupPromise ->then (function () {
39+ $ this ->readChunk ();
40+ });
3041 }
3142
3243 public function pause ()
@@ -52,7 +63,7 @@ public function isReadable()
5263
5364 protected function readChunk ()
5465 {
55- if ($ this ->pause ) {
66+ if ($ this ->pause || $ this -> isReading ) {
5667 return ;
5768 }
5869
@@ -70,7 +81,13 @@ protected function calculateChunkSize()
7081
7182 protected function performRead ($ chunkSize )
7283 {
84+ $ this ->isReading = true ;
7385 $ this ->getFilesystem ()->read ($ this ->getFileDescriptor (), $ chunkSize , $ this ->readCursor )->then (function ($ data ) use ($ chunkSize ) {
86+ $ this ->isReading = false ;
87+ if ($ this ->pause ) {
88+ return ;
89+ }
90+
7491 // If chunk size can be set make sure to copy it before running this operation so
7592 // that used can't change it mid operation and cause funkyness.
7693 $ this ->readCursor += $ chunkSize ;
0 commit comments