[Pkg-javascript-commits] [node-leveldown] 17/492: objects defined using 'Creator'

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:40 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 dd0e96191f11432e10f41ac5ff4fc929f5269f25
Author: Rod Vagg <rod at vagg.org>
Date:   Tue Aug 14 23:08:39 2012 +1000

    objects defined using 'Creator'
---
 lib/creator.js      |  87 +++++++++++++++++++
 lib/levelup.js      |  64 ++++++--------
 lib/read-stream.js  | 160 ++++++++++++++++++++---------------
 lib/write-stream.js | 240 +++++++++++++++++++++++++++++-----------------------
 package.json        |   1 -
 5 files changed, 342 insertions(+), 210 deletions(-)

diff --git a/lib/creator.js b/lib/creator.js
new file mode 100644
index 0000000..b0ae528
--- /dev/null
+++ b/lib/creator.js
@@ -0,0 +1,87 @@
+function defineReadOnlyProperty (obj, key, value) {
+  Object.defineProperty(obj, key, {
+      value        : value
+    , writeable    : false
+    , enumerable   : true
+    , configurable : false
+  })
+}
+
+function Creator (name) {
+  this._name = name
+}
+
+Creator.prototype = {
+    setBase: function (base) {
+      this._base = base
+      return this
+    }
+
+  , setConstructor: function (constructor) {
+      this._constructor = constructor
+      return this
+    }
+
+  , setPrototype: function (proto) {
+      this._proto = proto
+      return this
+    }
+
+  , setMethods: function (methods) {
+      this._methods = methods
+      return this
+    }
+
+  , setPrivateMethods: function (privateMethods) {
+      this._privateMethods = privateMethods
+      return this
+    }
+
+  , setReadOnlyProperties: function (readOnlyPropertiesCallback) {
+      this._readOnlyPropertiesCallback = readOnlyPropertiesCallback
+      return this
+    }
+
+  , setGetters: function (getters) {
+      this._getters = getters
+      return this
+    }
+
+  , create: function () {
+      var args     = Array.prototype.slice.call(arguments)
+        , obj      = !!this._base ? new this._base() : {}
+        , ctx      = { pub: obj }
+        , readOnly = this._readOnlyPropertiesCallback && this._readOnlyPropertiesCallback(args)
+        , name     = this._name
+
+      if (this._proto)
+        obj.__proto__ = Object.create(this._proto.prototype)
+
+      readOnly && Object.keys(readOnly).forEach(function (p) {
+        defineReadOnlyProperty(obj, p, readOnly[p])
+        defineReadOnlyProperty(ctx, p, readOnly[p])
+      })
+
+      this._methods && Object.keys(this._methods).forEach(function (m) {
+        obj[m] = ctx[m] = this._methods[m].bind(ctx)
+      }.bind(this))
+
+      this._privateMethods && Object.keys(this._privateMethods).forEach(function (m) {
+        ctx[m] = this._privateMethods[m].bind(ctx)
+      }.bind(this))
+
+      this._getters && Object.keys(this._getters).forEach(function (g) {
+        obj.__defineGetter__(g, this._getters[g].bind(ctx))
+      }.bind(this))
+
+      if (name)
+        obj.toString = function () { return name }
+
+      if (this._constructor)
+        this._constructor.apply(ctx, args)
+
+      return obj
+    }
+}
+
+module.exports.Creator = Creator
\ No newline at end of file
diff --git a/lib/levelup.js b/lib/levelup.js
index 0d0fc16..a0d78e6 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -3,11 +3,12 @@
 var bridge       = require('bindings')('levelup.node')
 
   , errors       = require('./errors')
-  , ReadStream   = require('./read-stream')
-  , WriteStream  = require('./write-stream')
+  , readStream   = require('./read-stream')
+  , writeStream  = require('./write-stream')
   , toEncoding   = require('./util').toEncoding
   , toBuffer     = require('./util').toBuffer
   , EventEmitter = require('events').EventEmitter
+  , Creator      = require('./creator').Creator
 
   , defaultOptions = {
         createIfMissing : false
@@ -55,11 +56,11 @@ var bridge       = require('bindings')('levelup.node')
                     err = new errors.OpenError(err)
                     if (callback)
                       return callback(err)
-                    this.ee.emit('error', err)
+                    this.pub.emit('error', err)
                   } else {
                     this.db = db
                     callback()
-                    this.ee.emit('ready')
+                    this.pub.emit('ready')
                   }
                 }.bind(this))
               }.bind(this)
@@ -73,7 +74,7 @@ var bridge       = require('bindings')('levelup.node')
       , close: function (callback) {
           if (this.isOpen()) {
             this.db.close(function () {
-              this.ee.emit('closed')
+              this.pub.emit('closed')
               callback.apply(null, arguments)
             }.bind(this))
             this.db = null
@@ -124,9 +125,9 @@ var bridge       = require('bindings')('levelup.node')
                 err = new errors.WriteError(err)
                 if (callback)
                   return callback(err)
-                this.ee.emit('error', err)
+                this.pub.emit('error', err)
               } else {
-                this.ee.emit('put', key, value)
+                this.pub.emit('put', key, value)
                 callback && callback(null, key, value)
               }
             }.bind(this))
@@ -151,9 +152,9 @@ var bridge       = require('bindings')('levelup.node')
                 err = new errors.WriteError(err)
                 if (callback)
                   return callback(err)
-                this.ee.emit('error', err)
+                this.pub.emit('error', err)
               } else {
-                this.ee.emit('del', key)
+                this.pub.emit('del', key)
                 callback && callback(null, key)
               }
             }.bind(this))
@@ -199,16 +200,16 @@ var bridge       = require('bindings')('levelup.node')
               err = new errors.WriteError(err)
               if (callback)
                 return callback(err)
-              this.ee.emit('error', err)
+              this.pub.emit('error', err)
             } else {
-              this.ee.emit('batch', arr)
+              this.pub.emit('batch', arr)
               callback && callback()
             }
           }.bind(this))
         }
 
       , readStream: function (options) {
-          return new ReadStream(
+          return readStream.create(
               options || {}
             , this
             , function () {
@@ -218,15 +219,11 @@ var bridge       = require('bindings')('levelup.node')
         }
 
       , writeStream: function (options) {
-         return new WriteStream(
+         return writeStream.create(
               options || {}
             , this
           )
         }
-
-      , toString: function () {
-          return "LevelUPDatabase"
-        }
     }
 
   , defineReadOnlyProperty = function (obj, key, value) {
@@ -238,30 +235,23 @@ var bridge       = require('bindings')('levelup.node')
       })
     }
 
-  , createDatabase = function (location, options) {
-      var database, ctx
+  , databaseCreator = new Creator('LevelUPDatabase')
+      .setBase(EventEmitter)
+      .setMethods(Database)
+      .setReadOnlyProperties(function (args) {
+        var props = {}
+        Object.keys(defaultOptions).forEach(function (p) {
+          props[p] = (args[1] && args[1][p]) || defaultOptions[p]
+        })
+        props.location = args[0]
+        return props
+      })
 
+  , createDatabase = function (location, options) {
       if (typeof location != 'string')
         throw new errors.InitializationError('Must provide a location for the database')
 
-      database = new EventEmitter()
-      ctx = {
-          ee       : database
-        , location : location
-      }
-
-      defineReadOnlyProperty(database, 'location', location)
-      Object.keys(defaultOptions).forEach(function (p) {
-        var value = (options && options[p]) || defaultOptions[p]
-        defineReadOnlyProperty(database, p, value)
-        ctx[p] = value
-      })
-
-      Object.keys(Database).forEach(function (p) {
-        database[p] = ctx[p] = Database[p].bind(ctx)
-      })
-
-      return database
+      return databaseCreator.create(location, options)
     }
 
 module.exports = {
diff --git a/lib/read-stream.js b/lib/read-stream.js
index 446ff55..8232b4e 100644
--- a/lib/read-stream.js
+++ b/lib/read-stream.js
@@ -4,80 +4,108 @@ var Stream     = require("stream").Stream
   , inherits   = require("inherits")
 
   , toEncoding = require('./util').toEncoding
+  , Creator    = require('./creator').Creator
 
-function ReadStream (options, db, iteratorFactory) {
-  Stream.call(this)
-  this._options = options
-  this._status  = 'ready'
-  this.readable = true
-  this.writable = false
+  , defaultOptions = {}
 
-  var ready = function () {
-    if (this._status == 'ended')
-      return
-    this._iterator = iteratorFactory()
-    this.emit('ready')
-    this._read()
-  }.bind(this)
+  , ReadStream = function (options, db, iteratorFactory) {
+      Stream.call(this)
+      this._options = options
+      this._status  = 'ready'
+      this.readable = true
+      this.writable = false
 
-  if (db.isOpen())
-    process.nextTick(ready)
-  else
-    db.ee.once('ready', ready)
-}
+      var ready = function () {
+        if (this._status == 'ended')
+          return
+        this._iterator = iteratorFactory()
+        this.pub.emit('ready')
+        this._read()
+      }.bind(this)
 
-inherits(ReadStream, Stream)
+      if (db.isOpen())
+        process.nextTick(ready)
+      else
+        db.pub.once('ready', ready)
+    }
 
-ReadStream.prototype._read = function () {
-  if (this._status == 'ready' || this._status == 'reading') {
-    this._iterator.next(this._cleanup.bind(this), this._onData.bind(this))
-  }
-}
+  , Methods = {
+      destroy: function() {
+        this._status = 'destroyed'
+        this._cleanup()
+      }
 
-ReadStream.prototype._onData = function (err, key, value) {
-  if (err)
-    return this._cleanup(err)
-  if (this._status == 'ended')
-    return
-  if (this._status == 'ready') this._status = 'reading'
-  this._read()
-  this.emit('data', {
-      key   : toEncoding(key   , this._options.keyEncoding   || this._options.encoding)
-    , value : toEncoding(value , this._options.valueEncoding || this._options.encoding)
-  })
-}
+    , pause: function() {
+        if (this._status != 'ended')
+          this._status += '+paused' // preserve existing status
+      }
 
-ReadStream.prototype._cleanup = function(err) {
-  var s = this._status
-  this._status = 'ended'
-  this.readable = false
-  if (this._iterator) {
-    this._iterator.end(function () {
-      this.emit('close')
-    }.bind(this))
-  } else
-    this.emit('close')
-  if (err)
-    this.emit('error', err)
-  else (s != 'destroyed')
-    this.emit('end')
-}
+    , resume: function() {
+        if (this._status != 'ended') {
+          this._status = this._status.replace(/\+paused$/, '')
+          this._read()
+        }
+      }
+    }
 
-ReadStream.prototype.destroy = function() {
-  this._status = 'destroyed'
-  this._cleanup()
-}
+  , PrivateMethods = {
+        _read: function () {
+          if (this._status == 'ready' || this._status == 'reading') {
+            this._iterator.next(this._cleanup.bind(this), this._onData.bind(this))
+          }
+        }
 
-ReadStream.prototype.pause = function() {
-  if (this._status != 'ended')
-    this._status += '+paused' // preserve existing status
-}
+      , _onData: function (err, key, value) {
+          if (err)
+            return this._cleanup(err)
+          if (this._status == 'ended')
+            return
+          if (this._status == 'ready') this._status = 'reading'
+          this._read()
+          this.pub.emit('data', {
+              key   : toEncoding(key   , this._options.keyEncoding   || this._options.encoding)
+            , value : toEncoding(value , this._options.valueEncoding || this._options.encoding)
+          })
+        }
 
-ReadStream.prototype.resume = function() {
-  if (this._status != 'ended') {
-    this._status = this._status.replace(/\+paused$/, '')
-    this._read()
-  }
-}
+      , _cleanup: function(err) {
+          var s = this._status
+          this._status = 'ended'
+          this.readable = false
+          if (this._iterator) {
+            this._iterator.end(function () {
+              this.pub.emit('close')
+            }.bind(this))
+          } else
+            this.pub.emit('close')
+          if (err)
+            this.pub.emit('error', err)
+          else (s != 'destroyed')
+            this.pub.emit('end')
+        }
+    }
 
-module.exports = ReadStream
\ No newline at end of file
+  , Getters = {
+        writable: function () {
+          return this.writable
+        }
+      , readable: function () {
+          return this.readable
+        }
+    }
+
+  , readStreamCreator = new Creator('LevelUP.ReadStream')
+      .setConstructor(ReadStream)
+      .setPrototype(Stream)
+      .setMethods(Methods)
+      .setPrivateMethods(PrivateMethods)
+      .setReadOnlyProperties(function (args) {
+        var props = {}
+        Object.keys(defaultOptions).forEach(function (p) {
+          props[p] = (args[0] && args[0][p]) || defaultOptions[p]
+        })
+        return props
+      })
+      .setGetters(Getters)
+
+module.exports.create = readStreamCreator.create.bind(readStreamCreator)
\ No newline at end of file
diff --git a/lib/write-stream.js b/lib/write-stream.js
index d8f58cb..30455f7 100644
--- a/lib/write-stream.js
+++ b/lib/write-stream.js
@@ -1,115 +1,143 @@
 /* Copyright (c) 2012 Rod Vagg <@rvagg> */
 
-var Stream     = require("stream").Stream
-  , inherits   = require("inherits")
-
-  , toEncoding = require('./util').toEncoding
-
-function WriteStream (options, db) {
-  Stream.call(this)
-  this._options = options
-  this._db      = db
-  this._buffer  = []
-  this._status  = 'init'
-  this._end     = false
-  this.writable = true
-  this.readable = false
-
-  var ready = function () {
-    if (!this.writable)
-      return
-    this._status = 'ready'
-    this.emit('ready')
-    this._process()
-  }.bind(this)
-
-  if (db.isOpen())
-    process.nextTick(ready)
-  else
-    db.ee.once('ready', ready)
-}
-
-inherits(WriteStream, Stream)
-
-WriteStream.prototype.write = function (data) {
-  if (!this.writable)
-    return false
-  this._buffer.push(data)
-  if (this._status != 'init')
-    this._processDelayed()
-  if (this._options.maxBufferLength && this._buffer.length > this._options.maxBufferLength) {
-    this._writeBlock = true
-    return false
-  }
-  return true
-}
-
-WriteStream.prototype._processDelayed = function() {
-  process.nextTick(this._process.bind(this))
-}
-
-WriteStream.prototype._process = function() {
-  var entry
-    , cb = function (err) {
+var Stream      = require("stream").Stream
+  , inherits    = require("inherits")
+
+  , toEncoding  = require('./util').toEncoding
+  , Creator     = require('./creator').Creator
+
+  , defaultOptions = {}
+
+  , WriteStream = function (options, db) {
+      Stream.call(this)
+      this._options = options
+      this._db      = db
+      this._buffer  = []
+      this._status  = 'init'
+      this._end     = false
+      this.writable = true
+      this.readable = false
+
+      var ready = function () {
         if (!this.writable)
           return
-        if (this._status != 'closed')
-          this._status = 'ready'
-        if (err) {
-          this.writable = false
-          return this.emit('error', err)
-        }
+        this._status = 'ready'
+        this.pub.emit('ready')
         this._process()
       }.bind(this)
 
-  if (this._status != 'ready' && this.writable) {
-    if (this._buffer.length && this._status != 'closed')
-      this._processDelayed()
-    return
-  }
-
-  if (this._buffer.length && this.writable) {
-    if (this._buffer.length == 1) {
-      entry = this._buffer.pop()
-      if (entry.key !== undefined && entry.value !== undefined) {
-        this._status = 'writing'
-        this._db.put(entry.key, entry.value, cb)
-      }
-    } else {
-      this._status = 'writing'
-      this._db.batch(this._buffer.map(function (d) {
-        return { type: 'put', key: d.key, value: d.value }
-      }), cb)
-      this._buffer = []
+      if (db.isOpen())
+        process.nextTick(ready)
+      else
+        db.pub.once('ready', ready)
     }
-    if (this._writeBlock) {
-      this._writeBlock = false
-      this.emit('drain')
+
+  , Methods = {
+        write: function (data) {
+          if (!this.writable)
+            return false
+          this._buffer.push(data)
+          if (this._status != 'init')
+            this._processDelayed()
+          if (this._options.maxBufferLength && this._buffer.length > this._options.maxBufferLength) {
+            this._writeBlock = true
+            return false
+          }
+          return true
+        }
+
+      , end: function() {
+          process.nextTick(function () {
+            this._end = true
+            this._process()
+          }.bind(this))
+        }
+
+      , destroy: function() {
+          this.writable = false
+          this.end()
+        }
+
+      , destroySoon: function() {
+          this.end()
+        }
+    }
+
+  , PrivateMethods = {
+        _processDelayed: function() {
+          process.nextTick(this._process.bind(this))
+        }
+
+      , _process: function() {
+          var entry
+            , cb = function (err) {
+                if (!this.writable)
+                  return
+                if (this._status != 'closed')
+                  this._status = 'ready'
+                if (err) {
+                  this.writable = false
+                  return this.pub.emit('error', err)
+                }
+                this._process()
+              }.bind(this)
+
+          if (this._status != 'ready' && this.writable) {
+            if (this._buffer.length && this._status != 'closed')
+              this._processDelayed()
+            return
+          }
+
+          if (this._buffer.length && this.writable) {
+            if (this._buffer.length == 1) {
+              entry = this._buffer.pop()
+              if (entry.key !== undefined && entry.value !== undefined) {
+                this._status = 'writing'
+                this._db.put(entry.key, entry.value, cb)
+              }
+            } else {
+              this._status = 'writing'
+              this._db.batch(this._buffer.map(function (d) {
+                return { type: 'put', key: d.key, value: d.value }
+              }), cb)
+              this._buffer = []
+            }
+            if (this._writeBlock) {
+              this._writeBlock = false
+              this.pub.emit('drain')
+            }
+          }
+
+          if (this._end && this._status != 'closed') {
+            this._status = 'closed'
+            this.writable = false
+            this.pub.emit('close')
+            return
+          }
+        }
+    }
+
+  , Getters = {
+        writable: function () {
+          return this.writable
+        }
+      , readable: function () {
+          return this.readable
+        }
     }
-  }
-
-  if (this._end && this._status != 'closed') {
-    this._status = 'closed'
-    this.writable = false
-    this.emit('close')
-    return
-  }
-}
-
-WriteStream.prototype.end = function() {
-  process.nextTick(function () {
-    this._end = true
-    this._process()
-  }.bind(this))
-}
-
-WriteStream.prototype.destroy = function() {
-  this.writable = false
-  this.end()
-}
-
-WriteStream.prototype.destroySoon = function() {
-  this.end()
-}
-
-module.exports = WriteStream
\ No newline at end of file
+
+  , writeStreamCreator = new Creator('LevelUP.WriteStream')
+      .setConstructor(WriteStream)
+      .setPrototype(Stream)
+      .setMethods(Methods)
+      .setPrivateMethods(PrivateMethods)
+      .setReadOnlyProperties(function (args) {
+        var props = {}
+        Object.keys(defaultOptions).forEach(function (p) {
+          props[p] = (args[0] && args[0][p]) || defaultOptions[p]
+        })
+        return props
+      })
+      .setGetters(Getters)
+
+module.exports.create = writeStreamCreator.create.bind(writeStreamCreator)
\ No newline at end of file
diff --git a/package.json b/package.json
index d411e8e..894f341 100644
--- a/package.json
+++ b/package.json
@@ -4,7 +4,6 @@
   , "main"            : "lib/levelup.js"
   , "dependencies"    : {
         "errno"         : ">=0.0.2"
-      , "inherits"      : "~1.0.0"
       , "bindings"      : "~1.0.0"
     }
   , "devDependencies" : {

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