Permalink
Browse files

do not call public api again when defering open

  • Loading branch information...
1 parent 8880d98 commit 13cfb43fa5eedf9775ebd35244243f161ae62fda @dominictarr dominictarr committed Mar 3, 2013
Showing with 22 additions and 40 deletions.
  1. +22 −40 lib/levelup.js
View
@@ -148,20 +148,26 @@ var leveldown = require('leveldown')
LevelUP.prototype.isClosed = function () { return (/^clos/).test(status) }
+ function defer(db, fun) {
+ return function () {
+ if(isOpening()) {
+ var args = [].slice.call(arguments)
+ return db.once('ready', function () {
+ db._db[fun].apply(db._db, args)
+ })
+ }
+ return db._db[fun].apply(db._db, arguments)
+ }
+ }
+
LevelUP.prototype.get = function (key_, options, callback) {
var key
, valueEnc
, err
- if (isOpening()) {
- return this.once('ready', function () {
- this.get(key_, options, callback)
- })
- }
-
callback = getCallback(options, callback)
- if (!isOpen()) {
+ if (!isOpening() && !isOpen()) {
err = new errors.ReadError('Database is not open')
return dispatchError(err, callback)
}
@@ -171,7 +177,7 @@ var leveldown = require('leveldown')
valueEnc = valueEncoding(options)
options.asBuffer = valueEnc != 'utf8' && valueEnc != 'json'
- this._db.get(key, options, function (err, value) {
+ defer(this, 'get')(key, options, function (err, value) {
if (err) {
err = new errors.NotFoundError(
'Key not found in database [' + key_ + ']')
@@ -187,15 +193,9 @@ var leveldown = require('leveldown')
, key
, value
- if (isOpening()) {
- return this.once('ready', function () {
- this.put(key_, value_, options, callback)
- })
- }
-
callback = getCallback(options, callback)
- if (!isOpen()) {
+ if (!isOpening() && !isOpen()) {
err = new errors.WriteError('Database is not open')
return dispatchError(err, callback)
}
@@ -204,7 +204,7 @@ var leveldown = require('leveldown')
key = toSlice[keyEncoding(options)](key_)
value = toSlice[valueEncoding(options)](value_)
- this._db.put(key, value, options, function (err) {
+ defer(this, 'put')(key, value, options, function (err) {
if (err) {
err = new errors.WriteError(err)
return dispatchError(err, callback)
@@ -220,23 +220,17 @@ var leveldown = require('leveldown')
var err
, key
- if (isOpening()) {
- return this.once('ready', function () {
- this.del(key_, options, callback)
- })
- }
-
callback = getCallback(options, callback)
- if (!isOpen()) {
+ if (!isOpening() && !isOpen()) {
err = new errors.WriteError('Database is not open')
return dispatchError(err, callback)
}
options = getOptions(options)
key = toSlice[keyEncoding(options)](key_)
- this._db.del(key, options, function (err) {
+ defer(this, 'del')(key, options, function (err) {
if (err) {
err = new errors.WriteError(err)
return dispatchError(err, callback)
@@ -254,15 +248,9 @@ var leveldown = require('leveldown')
, err
, arr
- if (isOpening()) {
- return this.once('ready', function () {
- this.batch(arr_, options, callback)
- })
- }
-
callback = getCallback(options, callback)
- if (!isOpen()) {
+ if (!isOpening() && !isOpen()) {
err = new errors.WriteError('Database is not open')
return dispatchError(err, callback)
}
@@ -292,7 +280,7 @@ var leveldown = require('leveldown')
arr = arr_
}
- this._db.batch(arr, options, function (err) {
+ defer(this, 'batch')(arr, options, function (err) {
if (err) {
err = new errors.WriteError(err)
return dispatchError(err, callback)
@@ -307,18 +295,12 @@ var leveldown = require('leveldown')
LevelUP.prototype.approximateSize = function(start, end, callback) {
var err
- if (isOpening()) {
- return this.once('ready', function () {
- this.approximateSize(start, end, callback)
- })
- }
-
- if (!isOpen()) {
+ if (!isOpening() && !isOpen()) {
err = new errors.WriteError('Database is not open')
return dispatchError(err, callback)
}
- this._db.approximateSize(start, end, function(err, size) {
+ defer(this, 'approximateSize')(start, end, function(err, size) {
if (err) {
err = new errors.OpenError(err)
return dispatchError(err, callback)

0 comments on commit 13cfb43

Please sign in to comment.