Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
minor code style adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Jan 9, 2013
1 parent 3c2c360 commit 6e52dc6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ LevelUP emits events when the callbacks to the corresponding methods are called.
* `db.emit('put', key, value)` emitted when a new value is `'put'`
* `db.emit('del', key)` emitted when a value is deleted
* `db.emit('batch', ary)` emitted when a batch operation has executed
* `db.emit('ready')` emitted when the database has opened `'open'` is synonym.
* `db.emit('ready')` emitted when the database has opened (`'open'` is synonym)
* `db.emit('closed')` emitted when the database has closed
* `db.emit('opening')` emitted when the database is opening
* `db.emit('closing')` emitted when the database is closing
Expand Down
44 changes: 23 additions & 21 deletions lib/levelup.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,14 @@ inherits(LevelUP, EventEmitter)

LevelUP.prototype.open = function (callback) {

if(this.isOpen()) {
var self = this
process.nextTick(function () {
callback(null, self)
})
if (this.isOpen()) {
if (callback)
process.nextTick(callback.bind(null, null, this))
return this
}

if(this._status === 'opening')
return this.once('open', function () {
if(callback)
callback(null, this)
})
if (this._status == 'opening')
return callback && this.once('open', callback.bind(null, null, this))

this._status = 'opening'
var execute = function () {
Expand All @@ -84,7 +79,8 @@ LevelUP.prototype.open = function (callback) {
} else {
this._db = db
this._status = 'open'
callback && callback(null, this)
if (callback)
callback(null, this)
this.emit('open')
this.emit('ready')
}
Expand All @@ -102,14 +98,17 @@ LevelUP.prototype.close = function (callback) {
this._db.close(function () {
this._status = 'closed'
this.emit('closed')
callback && callback.apply(null, arguments)
if (callback)
callback.apply(null, arguments)
}.bind(this))
this.emit('closing')
this._db = null
} else if (this._status == 'closed') {
callback && callback()
if (callback)
callback()
} else if (this._status == 'closing') {
callback && this.once('closed', callback)
if (callback)
this.once('closed', callback)
} else if (this._status == 'opening') {
this.once('open', function () {
this.close(callback)
Expand Down Expand Up @@ -159,7 +158,8 @@ LevelUP.prototype.get = function (key_, options_, callback_) {
return callback(err)
throw err
}
callback && callback(null, toEncoding[valueEnc](value), key_)
if (callback)
callback(null, toEncoding[valueEnc](value), key_)
})
} else {
err = new errors.ReadError('Database is not open')
Expand Down Expand Up @@ -194,7 +194,8 @@ LevelUP.prototype.put = function (key_, value_, options_, callback_) {
this.emit('error', err)
} else {
this.emit('put', key_, value_)
callback && callback(null, key, value)
if (callback)
callback(null, key, value)
}
}.bind(this))
} else {
Expand Down Expand Up @@ -228,7 +229,8 @@ LevelUP.prototype.del = function (key_, options_, callback_) {
this.emit('error', err)
} else {
this.emit('del', key_)
callback && callback(null, key)
if (callback)
callback(null, key)
}
}.bind(this))
} else {
Expand Down Expand Up @@ -288,7 +290,8 @@ LevelUP.prototype.batch = function (arr_, options_, callback_) {
this.emit('error', err)
} else {
this.emit('batch', arr_)
callback && callback(null, arr)
if (callback)
callback(null, arr)
}
}.bind(this))
}
Expand All @@ -315,9 +318,8 @@ LevelUP.prototype.approximateSize = function(start, end, callback) {
if (callback)
return callback(err)
this.emit('error', err)
} else {
callback && callback(null, size)
}
} else if (callback)
callback(null, size)
})
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
, "storage"
, "json"
]
, "version": "0.4.4"
, "version": "0.5.0"
, "main": "lib/levelup.js"
, "dependencies": {
"errno": "~0.0.3"
Expand Down
64 changes: 31 additions & 33 deletions test/idempotent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,44 @@
var buster = require('buster')
, assert = buster.assert
, levelup = require('../lib/levelup.js')
, errors = require('../lib/errors.js')
, async = require('async')
, fs = require('fs')
, common = require('./common')

buster.testCase('idempotent open & close', {
'call open twice, should emit "open" once': function (done) {
buster.testCase('Idempotent open & close', {
'call open twice, should emit "open" once': function (done) {
var location = common.nextLocation()
var n = 0, m = 0
var db = levelup(location, { createIfMissing: true }, function (err, db) {
//callback should fire only once.
assert.equals(n++, 0)
if(n && m) close()
})
, n = 0
, m = 0
, db
, close = function () {
var closing = this.spy()
db.on('closing', closing)
db.on('closed', function () {
assert.equals(closing.callCount, 1)
assert.equals(closing.getCall(0).args, [])
done()
})

//close needs to be idempotent too.
db.close()
process.nextTick(db.close.bind(db))
}.bind(this)

db = levelup(
location
, { createIfMissing: true }
, function () {
assert.equals(n++, 0, 'callback should fire only once')
if (n && m)
close()
}
)

db.on('open', function () {
console.log('emit open')
assert.equals(m++, 0)
if(n && m) close()
assert.equals(m++, 0, 'callback should fire only once')
if (n && m)
close()
})

db.open()

//this will only be called once.
function close () {
var closing = false
db.on('closing', function () {
closing = true
})
db.on('closed', function () {
assert.equals(closing, true)
done()
})

//close needs to be idempotent too.
db.close()

process.nextTick(function () {
db.close()
})
}
}
})

0 comments on commit 6e52dc6

Please sign in to comment.