1- var Writable = require ( 'readable-stream' ) . Writable
2- var inherits = require ( 'inherits' )
3- var extend = require ( 'xtend' )
1+ 'use strict'
42
5- var defaultOptions = { type : 'put' }
3+ const Writable = require ( 'readable-stream' ) . Writable
4+ const inherits = require ( 'inherits' )
5+ const extend = require ( 'xtend' )
6+
7+ const defaultOptions = { type : 'put' }
68
79function WriteStream ( db , options ) {
810 if ( ! ( this instanceof WriteStream ) ) {
@@ -22,64 +24,56 @@ function WriteStream (db, options) {
2224 this . _flushing = false
2325 this . _maxBufferLength = options . maxBufferLength || Infinity
2426
25- var self = this
26-
27- this . on ( 'finish' , function ( ) {
28- self . emit ( 'close' )
27+ this . on ( 'finish' , ( ) => {
28+ this . emit ( 'close' )
2929 } )
3030}
3131
3232inherits ( WriteStream , Writable )
3333
3434WriteStream . prototype . _write = function ( data , enc , next ) {
35- var self = this
36- if ( self . destroyed ) return
35+ if ( this . destroyed ) return
3736
38- if ( ! self . _flushing ) {
39- self . _flushing = true
40- process . nextTick ( function ( ) { self . _flush ( ) } )
37+ if ( ! this . _flushing ) {
38+ this . _flushing = true
39+ process . nextTick ( ( ) => { this . _flush ( ) } )
4140 }
4241
43- if ( self . _buffer . length >= self . _maxBufferLength ) {
44- self . once ( '_flush' , function ( err ) {
45- if ( err ) return self . destroy ( err )
46- self . _write ( data , enc , next )
42+ if ( this . _buffer . length >= this . _maxBufferLength ) {
43+ this . once ( '_flush' , ( err ) => {
44+ if ( err ) return this . destroy ( err )
45+ this . _write ( data , enc , next )
4746 } )
4847 } else {
49- self . _buffer . push ( extend ( { type : self . _options . type } , data ) )
48+ this . _buffer . push ( extend ( { type : this . _options . type } , data ) )
5049 next ( )
5150 }
5251}
5352
5453WriteStream . prototype . _flush = function ( ) {
55- var self = this
56- var buffer = self . _buffer
54+ const buffer = this . _buffer
5755
58- if ( self . destroyed ) return
56+ if ( this . destroyed ) return
5957
60- self . _buffer = [ ]
61- self . _db . batch ( buffer , cb )
62-
63- function cb ( err ) {
64- self . _flushing = false
58+ this . _buffer = [ ]
59+ this . _db . batch ( buffer , ( err ) => {
60+ this . _flushing = false
6561
66- if ( ! self . emit ( '_flush' , err ) && err ) {
62+ if ( ! this . emit ( '_flush' , err ) && err ) {
6763 // There was no _flush listener.
68- self . destroy ( err )
64+ this . destroy ( err )
6965 }
70- }
66+ } )
7167}
7268
7369WriteStream . prototype . _final = function ( cb ) {
74- var self = this
75-
7670 if ( this . _flushing ) {
7771 // Wait for scheduled or in-progress _flush()
78- this . once ( '_flush' , function ( err ) {
72+ this . once ( '_flush' , ( err ) => {
7973 if ( err ) return cb ( err )
8074
8175 // There could be additional buffered writes
82- self . _final ( cb )
76+ this . _final ( cb )
8377 } )
8478 } else if ( this . _buffer && this . _buffer . length ) {
8579 this . once ( '_flush' , cb )
0 commit comments