@@ -7,6 +7,7 @@ var Stream = require('stream').Stream
77 , bufferStream = require ( 'simple-bufferstream' )
88 , inherits = require ( 'util' ) . inherits
99 , extend = require ( 'util' ) . _extend
10+ , State = require ( './read-stream-state' )
1011
1112 , toEncoding = require ( './util' ) . toEncoding
1213 , toSlice = require ( './util' ) . toSlice
@@ -28,11 +29,11 @@ var Stream = require('stream').Stream
2829 }
2930 , makeNoData = function ( ) { return null }
3031
31-
3232function ReadStream ( options , db , iteratorFactory ) {
3333 Stream . call ( this )
3434
35- this . _status = 'ready'
35+ this . _state = State ( )
36+
3637 this . _dataEvent = 'data'
3738 this . readable = true
3839 this . writable = false
@@ -59,9 +60,10 @@ function ReadStream (options, db, iteratorFactory) {
5960
6061
6162 var ready = function ( ) {
62- if ( this . _status == 'ended' )
63+ if ( ! this . _state . canEmitData ( ) )
6364 return
6465
66+ this . _state . ready ( )
6567 this . _iterator = iteratorFactory ( this . _options )
6668 this . emit ( 'ready' )
6769 this . _read ( )
@@ -76,21 +78,22 @@ function ReadStream (options, db, iteratorFactory) {
7678inherits ( ReadStream , Stream )
7779
7880ReadStream . prototype . destroy = function ( ) {
79- this . _status = 'destroyed'
80- this . _cleanup ( )
81+ this . _state . destroy ( )
82+ if ( this . _state . canCleanup ( ) )
83+ this . _cleanup ( )
8184}
8285
8386ReadStream . prototype . pause = function ( ) {
84- if ( this . _status != 'ended' && ! / \+ p a u s e d $ / . test ( this . _status ) ) {
87+ if ( this . _state . canPause ( ) ) {
88+ this . _state . pause ( )
8589 this . emit ( 'pause' )
86- this . _status += '+paused' // preserve existing status
8790 }
8891}
8992
9093ReadStream . prototype . resume = function ( ) {
91- if ( this . _status != 'ended' ) {
94+ if ( this . _state . canResume ( ) ) {
9295 this . emit ( 'resume' )
93- this . _status = this . _status . replace ( / \+ p a u s e d $ / , '' )
96+ this . _state . resume ( )
9497 this . _read ( )
9598 }
9699}
@@ -115,44 +118,39 @@ ReadStream.prototype.pipe = function (dest) {
115118}
116119
117120ReadStream . prototype . _read = function ( ) {
118- if ( this . _status == 'ready' ) {
119- this . _status = 'reading'
121+ if ( this . _state . canRead ( ) ) {
122+ this . _state . read ( )
120123 this . _iterator . next ( this . _onData . bind ( this ) )
121124 }
122125}
123126
124127ReadStream . prototype . _onData = function ( err , key , value ) {
125- if ( err )
128+ this . _state . endRead ( )
129+ if ( err || ! arguments . length /* end */ || ! this . _state . canEmitData ( ) )
126130 return this . _cleanup ( err )
127- if ( ! arguments . length ) // end
128- return this . _cleanup ( )
129- if ( this . _status == 'ended' )
130- return
131- if ( / ^ r e a d i n g / . test ( this . _status ) )
132- this . _status = this . _status . replace ( / ^ r e a d i n g / , 'ready' )
133- this . _read ( )
131+ this . _read ( ) // queue another read even tho we may not need it
134132 this . emit ( this . _dataEvent , this . _makeData ( key , value ) )
135133}
136134
137135ReadStream . prototype . _cleanup = function ( err ) {
138- if ( this . _status == 'ended' )
139- return err && this . emit ( 'error' , err )
136+ if ( err )
137+ this . emit ( 'error' , err )
138+
139+ if ( ! this . _state . canEnd ( ) )
140+ return
140141
141- var s = this . _status
142- this . _status = 'ended'
142+ this . _state . end ( )
143143 this . readable = false
144144
145145 if ( this . _iterator ) {
146146 this . _iterator . end ( function ( ) {
147+ this . _iterator = null
147148 this . emit ( 'close' )
148149 } . bind ( this ) )
149150 } else
150151 this . emit ( 'close' )
151152
152- if ( err )
153- this . emit ( 'error' , err )
154- else ( s != 'destroyed' )
155- this . emit ( 'end' )
153+ this . emit ( 'end' )
156154}
157155
158156ReadStream . prototype . toString = function ( ) {
0 commit comments