@@ -23,11 +23,11 @@ var PromiseStream = require('./PromiseStream');
2323var maxConcurrency = 50 ;
2424
2525function getArticles ( options , res ) {
26- var next = res . next || '' ;
27- if ( next === 'finished' ) {
26+ if ( ! res || res . next === 'finished' ) {
2827 // nothing more to do.
29- return P . reject ( 'Articles done' ) ;
28+ return P . resolve ( null ) ;
3029 }
30+ var next = res . next || '' ;
3131
3232 var url = options . apiURL + '?action=query&generator=allpages&gapfilterredir=nonredirects'
3333 + '&gaplimit=500&prop=revisions&gapnamespace='
@@ -51,7 +51,7 @@ function getArticles (options, res) {
5151 }
5252 } ) ;
5353 var next2 = res2 [ 'query-continue' ]
54- && res2 [ 'query-continue' ] . allpages . gapcontinue ;
54+ && res2 [ 'query-continue' ] . allpages . gapcontinue || 'finished' ;
5555 // XXX
5656 //next = 'finished';
5757 return {
@@ -113,9 +113,17 @@ function Dumper (articleChunkStream, options) {
113113 this . options = options ;
114114 this . articles = [ ] ;
115115 this . waiters = [ ] ;
116+ this . done = false ;
116117}
117118
118119Dumper . prototype . processArticles = function ( newArticles ) {
120+ if ( newArticles === null ) {
121+ this . done = true ;
122+ while ( this . waiters . length ) {
123+ this . waiters . pop ( ) . resolve ( null ) ;
124+ }
125+ return ;
126+ }
119127 this . articles = newArticles . articles ;
120128 while ( this . waiters . length && this . articles . length ) {
121129 this . waiters . pop ( ) . resolve ( this . articles . shift ( ) ) ;
@@ -143,6 +151,9 @@ Dumper.prototype.next = function () {
143151 var self = this ;
144152 return this . getArticle ( )
145153 . then ( function ( article ) {
154+ if ( article === null ) {
155+ return null ;
156+ }
146157 var title = article [ 0 ] ;
147158 var oldid = article [ 1 ] ;
148159 return dumpArticle ( self . options , title , oldid )
@@ -164,7 +175,10 @@ function dumpLoop (options) {
164175 return new P ( function ( resolve , reject ) {
165176 function loop ( ) {
166177 return dumpStream . next ( )
167- . then ( function ( ) {
178+ . then ( function ( res ) {
179+ if ( res === null ) {
180+ return resolve ( ) ;
181+ }
168182 if ( i ++ === 10000 ) {
169183 i = 0 ;
170184 process . nextTick ( loop ) ;
@@ -173,7 +187,11 @@ function dumpLoop (options) {
173187 }
174188 } )
175189 . catch ( function ( e ) {
176- resolve ( e ) ;
190+ if ( e instanceof String ) {
191+ resolve ( e ) ;
192+ } else {
193+ reject ( e ) ;
194+ }
177195 } ) ;
178196 }
179197
0 commit comments