Skip to content

Commit

Permalink
Breaking: modernize syntax (Level/community#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Apr 25, 2021
1 parent 92271b2 commit 2319634
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 91 deletions.
1 change: 0 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ updates:
ignore:
- dependency-name: dependency-check
- dependency-name: nyc
- dependency-name: standard
- dependency-name: tempy
60 changes: 27 additions & 33 deletions level-ws.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var Writable = require('readable-stream').Writable
var inherits = require('inherits')
var extend = require('xtend')
'use strict'

var defaultOptions = { type: 'put' }
const Writable = require('readable-stream').Writable
const inherits = require('inherits')
const extend = require('xtend')

const defaultOptions = { type: 'put' }

function WriteStream (db, options) {
if (!(this instanceof WriteStream)) {
Expand All @@ -22,64 +24,56 @@ function WriteStream (db, options) {
this._flushing = false
this._maxBufferLength = options.maxBufferLength || Infinity

var self = this

this.on('finish', function () {
self.emit('close')
this.on('finish', () => {
this.emit('close')
})
}

inherits(WriteStream, Writable)

WriteStream.prototype._write = function (data, enc, next) {
var self = this
if (self.destroyed) return
if (this.destroyed) return

if (!self._flushing) {
self._flushing = true
process.nextTick(function () { self._flush() })
if (!this._flushing) {
this._flushing = true
process.nextTick(() => { this._flush() })
}

if (self._buffer.length >= self._maxBufferLength) {
self.once('_flush', function (err) {
if (err) return self.destroy(err)
self._write(data, enc, next)
if (this._buffer.length >= this._maxBufferLength) {
this.once('_flush', (err) => {
if (err) return this.destroy(err)
this._write(data, enc, next)
})
} else {
self._buffer.push(extend({ type: self._options.type }, data))
this._buffer.push(extend({ type: this._options.type }, data))
next()
}
}

WriteStream.prototype._flush = function () {
var self = this
var buffer = self._buffer
const buffer = this._buffer

if (self.destroyed) return
if (this.destroyed) return

self._buffer = []
self._db.batch(buffer, cb)

function cb (err) {
self._flushing = false
this._buffer = []
this._db.batch(buffer, (err) => {
this._flushing = false

if (!self.emit('_flush', err) && err) {
if (!this.emit('_flush', err) && err) {
// There was no _flush listener.
self.destroy(err)
this.destroy(err)
}
}
})
}

WriteStream.prototype._final = function (cb) {
var self = this

if (this._flushing) {
// Wait for scheduled or in-progress _flush()
this.once('_flush', function (err) {
this.once('_flush', (err) => {
if (err) return cb(err)

// There could be additional buffered writes
self._final(cb)
this._final(cb)
})
} else if (this._buffer && this._buffer.length) {
this.once('_flush', cb)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"level-concat-iterator": "^2.0.0",
"nyc": "^14.0.0",
"secret-event-listener": "^1.0.0",
"standard": "^14.1.0",
"standard": "^16.0.3",
"tape": "^5.0.1",
"tempy": "^0.2.1"
},
Expand Down
Loading

0 comments on commit 2319634

Please sign in to comment.