[Pkg-javascript-commits] [node-leveldown] 153/492: idempotent open and close, and emit _state as events

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:54 UTC 2014


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to annotated tag rocksdb-0.10.1
in repository node-leveldown.

commit ee7175d05cda17a8f9c33ab371adaf7285be9296
Author: Dominic Tarr <dominic.tarr at gmail.com>
Date:   Tue Jan 8 18:03:08 2013 +0700

    idempotent open and close, and emit _state as events
---
 lib/levelup.js | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/lib/levelup.js b/lib/levelup.js
index 0f904b9..130c187 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -57,6 +57,21 @@ function LevelUP (location, options) {
 inherits(LevelUP, EventEmitter)
 
 LevelUP.prototype.open = function (callback) {
+
+  if(this.isOpen()) {
+    var self = this
+    process.nextTick(function () {
+      callback(null, self)
+    })
+    return this
+  }
+
+  if(this._status === 'opening')
+    return this.once('open', function () {
+      if(callback)
+        callback(null, this)
+    })
+
   this._status = 'opening'
   var execute = function () {
     var db = bridge.createDatabase()
@@ -70,15 +85,14 @@ LevelUP.prototype.open = function (callback) {
         this._db = db
         this._status = 'open'
         callback && callback(null, this)
+        this.emit('open')
         this.emit('ready')
       }
     }.bind(this))
   }.bind(this)
-  
-  if (this.isOpen())
-    this.close(execute)
-  else
-    execute()
+
+  execute()
+  this.emit('opening')
 }
 
 //TODO: we can crash Node by submitting an operation between close() and the actual closing of the database
@@ -90,20 +104,21 @@ LevelUP.prototype.close = function (callback) {
       this.emit('closed')
       callback && callback.apply(null, arguments)
     }.bind(this))
+    this.emit('closing')
     this._db = null
   } else if (this._status == 'closed') {
     callback && callback()
   } else if (this._status == 'closing') {
-    this.on('closed', callback)
+    callback && this.once('closed', callback)
   } else if (this._status == 'opening') {
-    this.on('ready', function () {
+    this.once('open', function () {
       this.close(callback)
     })
   } else {
     var err = new errors.CloseError('Cannot close unopened database')
     if (callback)
       return callback(err)
-    throw err
+    this.emit('error', err)
   }
 }
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-leveldown.git



More information about the Pkg-javascript-commits mailing list