@@ -34,7 +34,8 @@ var extend = require('extend');
3434var nextTick = require ( 'process-nextick-args' ) ;
3535var util = require ( 'util' ) ;
3636var NormalApiCaller = require ( './api_callable' ) . NormalApiCaller ;
37- var ReadableStream = require ( 'readable-stream' ) ;
37+ var through2 = require ( 'through2' ) ;
38+ var ended = require ( 'is-stream-ended' ) ;
3839
3940/**
4041 * Creates an API caller that returns a stream to performs page-streaming.
@@ -97,49 +98,31 @@ PagedIteration.prototype.call = function(
9798 return ;
9899 }
99100
101+ var maxResults = settings . maxResults || - 1 ;
100102 var allResources = [ ] ;
101103 function pushResources ( err , resources , next ) {
102104 if ( err ) {
103105 canceller . callback ( err ) ;
104106 return ;
105107 }
106108
107- allResources . push . apply ( allResources , resources ) ;
109+ for ( var i = 0 ; i < resources . length ; ++ i ) {
110+ allResources . push ( resources [ i ] ) ;
111+ if ( allResources . length === maxResults ) {
112+ next = null ;
113+ break ;
114+ }
115+ }
108116 if ( ! next ) {
109117 canceller . callback ( null , allResources ) ;
110118 return ;
111119 }
112- nextTick ( apiCall , argument , pushResources ) ;
120+ nextTick ( apiCall , next , pushResources ) ;
113121 }
114122
115123 nextTick ( apiCall , argument , pushResources ) ;
116124} ;
117125
118- /**
119- * An implementation of readalbe stream which fits for the usage of paged iteration.
120- * @private
121- * @constructor
122- */
123- function PagedStream ( ) {
124- ReadableStream . call ( this , { objectMode : true } ) ;
125- }
126-
127- util . inherits ( PagedStream , ReadableStream ) ;
128-
129- PagedStream . prototype . _read = function ( n ) {
130- } ;
131-
132- /**
133- */
134- PagedStream . prototype . end = function ( ) {
135- // pushing a null will cause ending the stream.
136- this . push ( null ) ;
137-
138- // onEof callback of ReadableStream does not update 'readable' field immediately,
139- // thus settings here explicitly.
140- this . readable = false ;
141- } ;
142-
143126/**
144127 * Describes the structure of a page-streaming call.
145128 *
@@ -174,24 +157,33 @@ exports.PageDescriptor = PageDescriptor;
174157 */
175158PageDescriptor . prototype . createStream = function (
176159 apiCall , request , options ) {
177- var stream = new PagedStream ( ) ;
178- options = extend ( { } , options ) ;
179- options . autoPaginate = false ;
180- function callback ( response ) {
181- var resources = response [ 0 ] ;
160+ var stream = through2 . obj ( ) ;
161+ options = extend ( { } , options , { autoPaginate : false } ) ;
162+ var maxResults = ( 'maxResults' in options ) ? options . maxResults : - 1 ;
163+ var pushCount = 0 ;
164+ var started = false ;
165+ function callback ( err , resources , next ) {
166+ if ( err ) {
167+ stream . emit ( 'error' , err ) ;
168+ return ;
169+ }
182170 for ( var i = 0 ; i < resources . length ; ++ i ) {
183- if ( ! stream . readable ) {
171+ if ( ended ( stream ) ) {
184172 return ;
185173 }
186174 if ( resources [ i ] === null ) {
187175 continue ;
188176 }
189177 stream . push ( resources [ i ] ) ;
178+ pushCount ++ ;
179+ if ( pushCount === maxResults ) {
180+ stream . end ( ) ;
181+ }
190182 }
191- if ( ! stream . readable ) {
183+ if ( ended ( stream ) ) {
192184 return ;
193185 }
194- if ( ! response [ 1 ] ) {
186+ if ( ! next ) {
195187 stream . end ( ) ;
196188 return ;
197189 }
@@ -200,17 +192,19 @@ PageDescriptor.prototype.createStream = function(
200192 if ( 'pageToken' in options ) {
201193 delete options . pageToken ;
202194 }
203- return apiCall ( response [ 1 ] , options )
204- . then ( callback )
205- . catch ( function ( err ) {
206- stream . emit ( 'error' , err ) ;
207- } ) ;
195+ if ( stream . isPaused ( ) ) {
196+ request = next ;
197+ started = false ;
198+ } else {
199+ nextTick ( apiCall , next , options , callback ) ;
200+ }
208201 }
209- apiCall ( request , options )
210- . then ( callback )
211- . catch ( function ( err ) {
212- stream . emit ( 'error' , err ) ;
213- } ) ;
202+ stream . on ( 'resume' , function ( ) {
203+ if ( ! started ) {
204+ started = true ;
205+ apiCall ( request , options , callback ) ;
206+ }
207+ } ) ;
214208 return stream ;
215209} ;
216210
0 commit comments