From 6e52dc631eaaeed8d84b93f5aa8f7c346af1a13d Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Wed, 9 Jan 2013 11:37:54 +1100 Subject: [PATCH] minor code style adjustments --- README.md | 2 +- lib/levelup.js | 44 ++++++++++++++-------------- package.json | 2 +- test/idempotent-test.js | 64 ++++++++++++++++++++--------------------- 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index d32f967e..dc59a9f4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/levelup.js b/lib/levelup.js index 130c187b..0992d57b 100644 --- a/lib/levelup.js +++ b/lib/levelup.js @@ -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 () { @@ -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') } @@ -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) @@ -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') @@ -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 { @@ -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 { @@ -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)) } @@ -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) }) } diff --git a/package.json b/package.json index 143a2a61..e30980b9 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ , "storage" , "json" ] - , "version": "0.4.4" + , "version": "0.5.0" , "main": "lib/levelup.js" , "dependencies": { "errno": "~0.0.3" diff --git a/test/idempotent-test.js b/test/idempotent-test.js index 39d73917..90a41fd7 100644 --- a/test/idempotent-test.js +++ b/test/idempotent-test.js @@ -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() - }) - } } })