[Pkg-javascript-commits] [node-leveldown] 69/492: status state machine to make close semantics correct

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:45 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 11395585a78fce084c738592c34e8c76365da13e
Author: Raynos <raynos2 at gmail.com>
Date:   Sat Nov 17 14:41:55 2012 -0800

    status state machine to make close semantics correct
---
 lib/errors.js  |  3 ++-
 lib/levelup.js | 20 ++++++++++++++++++--
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/lib/errors.js b/lib/errors.js
index 3d2569b..4942015 100644
--- a/lib/errors.js
+++ b/lib/errors.js
@@ -11,4 +11,5 @@ module.exports = {
   , ReadError           : errno.custom.createError('ReadError', LevelUPError)
   , WriteError          : errno.custom.createError('WriteError', LevelUPError)
   , NotFoundError       : errno.custom.createError('NotFoundError', LevelUPError)
-}
\ No newline at end of file
+  , CloseError          : errno.custom.createError('CloseError', LevelUPError)
+}
diff --git a/lib/levelup.js b/lib/levelup.js
index e4bed0c..fede69d 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -41,10 +41,12 @@ function LevelUP (location, options) {
   EventEmitter.call(this)
   this._options = extend(extend({}, defaultOptions), options)
   this._location = location
+  this.status = "new"
 }
 
 LevelUP.prototype = {
     open: function (callback) {
+      this.status = "opening"
       var execute = function () {
         var db = bridge.createDatabase()
         db.open(this._location, this._options, function (err) {
@@ -55,6 +57,7 @@ LevelUP.prototype = {
             this.emit('error', err)
           } else {
             this._db = db
+            this.status = "open"
             callback && callback(null, this)
             this.emit('ready')
           }
@@ -70,18 +73,31 @@ LevelUP.prototype = {
     //TODO: we can crash Node by submitting an operation between close() and the actual closing of the database
   , close: function (callback) {
       if (this.isOpen()) {
+        this.status = "closing"
         this._db.close(function () {
+          this.status = "closed"
           this.emit('closed')
           callback.apply(null, arguments)
         }.bind(this))
         this._db = null
-      } else {
+      } else if (this.status === "closing" ||
+        this.status === "closed"
+      ) {
         callback()
+      } else if (this.status === "opening") {
+        this.on("ready", function () {
+          this.close(callback)
+        })
+      } else {
+        var err = new errors.CloseError('Cannot close unopened database')
+        if (callback)
+          return callback(err)
+        throw err
       }
     }
 
   , isOpen: function () {
-      return !!this._db
+      return this.status === "open"
     }
 
   , get: function (key_, options_, callback_) {

-- 
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