deferred-leveldown
Advanced tools
Comparing version 1.1.0 to 2.0.0-0
@@ -5,5 +5,7 @@ var util = require('util') | ||
function DeferredLevelDOWN (location) { | ||
AbstractLevelDOWN.call(this, typeof location == 'string' ? location : '') // optional location, who cares? | ||
this._db = undefined | ||
, deferrables = 'put get del batch approximateSize'.split(' ') | ||
function DeferredLevelDOWN (db) { | ||
AbstractLevelDOWN.call(this, '') | ||
this._db = db | ||
this._operations = [] | ||
@@ -15,30 +17,48 @@ this._iterators = [] | ||
// called by LevelUP when we have a real DB to take its place | ||
DeferredLevelDOWN.prototype.setDb = function (db) { | ||
this._db = db | ||
this._operations.forEach(function (op) { | ||
db[op.method].apply(db, op.args) | ||
DeferredLevelDOWN.prototype._open = function (options, callback) { | ||
var self = this | ||
this._db.open(options, function (err) { | ||
if (err) return callback(err) | ||
self._operations.forEach(function (op) { | ||
self._db[op.method].apply(self._db, op.args) | ||
}) | ||
self._operations = [] | ||
self._iterators.forEach(function (it) { | ||
it.setDb(self._db) | ||
}) | ||
self._iterators = [] | ||
open(self) | ||
callback() | ||
}) | ||
this._iterators.forEach(function (it) { | ||
it.setDb(db) | ||
} | ||
DeferredLevelDOWN.prototype._close = function (callback) { | ||
var self = this | ||
this._db.close(function (err) { | ||
if (err) return callback(err) | ||
closed(self) | ||
callback() | ||
}) | ||
} | ||
DeferredLevelDOWN.prototype._open = function (options, callback) { | ||
return process.nextTick(callback) | ||
function open (obj) { | ||
deferrables.forEach(function (m) { | ||
obj['_' + m] = function () { | ||
this._db[m].apply(this._db, arguments) | ||
} | ||
}) | ||
} | ||
// queue a new deferred operation | ||
DeferredLevelDOWN.prototype._operation = function (method, args) { | ||
if (this._db) | ||
return this._db[method].apply(this._db, args) | ||
this._operations.push({ method: method, args: args }) | ||
function closed (obj) { | ||
deferrables.forEach(function (m) { | ||
obj['_' + m] = function () { | ||
this._operations.push({ method: m, args: arguments }) | ||
} | ||
}) | ||
} | ||
// deferrables | ||
'put get del batch approximateSize'.split(' ').forEach(function (m) { | ||
DeferredLevelDOWN.prototype['_' + m] = function () { | ||
this._operation(m, arguments) | ||
} | ||
}) | ||
closed(DeferredLevelDOWN.prototype) | ||
@@ -45,0 +65,0 @@ DeferredLevelDOWN.prototype._isBuffer = function (obj) { |
{ | ||
"name": "deferred-leveldown", | ||
"description": "For handling delayed-open on LevelDOWN compatible libraries", | ||
"version": "1.1.0", | ||
"version": "2.0.0-0", | ||
"contributors": [ | ||
@@ -6,0 +6,0 @@ "Rod Vagg <r@va.gg> (https://github.com/rvagg)", |
@@ -14,8 +14,21 @@ DeferredLevelDOWN | ||
`put()`, `get()`, `del()` and `batch()` operations are all queued and kept in memory until a new LevelDOWN-compatible object can be supplied. | ||
`put()`, `get()`, `del()` and `batch()` operations are all queued and kept in memory until the LevelDOWN-compatible object has been opened through **DeferredLevelDOWN**'s `open()` method. | ||
The `setDb(db)` method is used to supply a new LevelDOWN object. Once received, all queued operations are replayed against that object, in order. | ||
`batch()` operations will all be replayed as the array form. Chained-batch operations are converted before being stored. | ||
```js | ||
var Deferred = require('deferred-leveldown') | ||
, LevelDOWN = require('leveldown') | ||
var db = Deferred(LevelDOWN('location')) | ||
db.put('foo', 'bar', function (err) { | ||
}) | ||
db.open(function (err) { | ||
// ... | ||
}) | ||
``` | ||
Contributing | ||
@@ -22,0 +35,0 @@ ------------ |
134
test.js
@@ -6,3 +6,15 @@ var test = require('tape') | ||
var called = false | ||
var ld = new DeferredLevelDOWN('loc') | ||
var db = { | ||
put: function (key, value, options, callback) { | ||
t.equal(key, 'foo', 'correct key') | ||
t.equal(value, 'bar', 'correct value') | ||
t.deepEqual({}, options, 'empty options') | ||
callback('called') | ||
} | ||
, open: function (options, callback) { | ||
process.nextTick(callback) | ||
} | ||
} | ||
var ld = new DeferredLevelDOWN(db) | ||
ld.put('foo', 'bar', function (v) { | ||
@@ -12,12 +24,9 @@ called = v | ||
t.ok(called === false, 'not called') | ||
ld.setDb({ put: function (key, value, options, callback) { | ||
t.equal(key, 'foo', 'correct key') | ||
t.equal(value, 'bar', 'correct value') | ||
t.deepEqual({}, options, 'empty options') | ||
callback('called') | ||
}}) | ||
t.ok(called === 'called', 'function called') | ||
ld.open(function (err) { | ||
t.error(err) | ||
t.end() | ||
t.ok(called === 'called', 'function called') | ||
t.end() | ||
}) | ||
}) | ||
@@ -27,23 +36,3 @@ | ||
var calls = [] | ||
var ld = new DeferredLevelDOWN('loc') | ||
, puts = 0 | ||
, gets = 0 | ||
, batches = 0 | ||
ld.put('foo1', 'bar1', function (v) { calls.push({ type: 'put', key: 'foo1', v: v }) }) | ||
ld.get('woo1', function (v) { calls.push({ type: 'get', key: 'woo1', v: v }) }) | ||
ld.put('foo2', 'bar2', function (v) { calls.push({ type: 'put', key: 'foo2', v: v }) }) | ||
ld.get('woo2', function (v) { calls.push({ type: 'get', key: 'woo2', v: v }) }) | ||
ld.del('blergh', function (v) { calls.push({ type: 'del', key: 'blergh', v: v }) }) | ||
ld.batch([ | ||
{ type: 'put', key: 'k1', value: 'v1' } | ||
, { type: 'put', key: 'k2', value: 'v2' } | ||
], function () { calls.push({ type: 'batch', keys: 'k1,k2' }) }) | ||
ld.batch().put('k3', 'v3').put('k4', 'v4').write(function () { | ||
calls.push({ type: 'batch', keys: 'k3,k4' }) | ||
}) | ||
t.ok(calls.length === 0, 'not called') | ||
ld.setDb({ | ||
var db = { | ||
put: function (key, value, options, callback) { | ||
@@ -90,20 +79,64 @@ if (puts++ === 0) { | ||
} | ||
, open: function (options, callback) { | ||
process.nextTick(callback) | ||
} | ||
} | ||
var ld = new DeferredLevelDOWN(db) | ||
, puts = 0 | ||
, gets = 0 | ||
, batches = 0 | ||
ld.put('foo1', 'bar1', function (v) { calls.push({ type: 'put', key: 'foo1', v: v }) }) | ||
ld.get('woo1', function (v) { calls.push({ type: 'get', key: 'woo1', v: v }) }) | ||
ld.put('foo2', 'bar2', function (v) { calls.push({ type: 'put', key: 'foo2', v: v }) }) | ||
ld.get('woo2', function (v) { calls.push({ type: 'get', key: 'woo2', v: v }) }) | ||
ld.del('blergh', function (v) { calls.push({ type: 'del', key: 'blergh', v: v }) }) | ||
ld.batch([ | ||
{ type: 'put', key: 'k1', value: 'v1' } | ||
, { type: 'put', key: 'k2', value: 'v2' } | ||
], function () { calls.push({ type: 'batch', keys: 'k1,k2' }) }) | ||
ld.batch().put('k3', 'v3').put('k4', 'v4').write(function () { | ||
calls.push({ type: 'batch', keys: 'k3,k4' }) | ||
}) | ||
t.equal(calls.length, 7, 'all functions called') | ||
t.deepEqual(calls, [ | ||
{ type: 'put', key: 'foo1', v: 'put1' } | ||
, { type: 'get', key: 'woo1', v: 'gets1' } | ||
, { type: 'put', key: 'foo2', v: 'put2' } | ||
, { type: 'get', key: 'woo2', v: 'gets2' } | ||
, { type: 'del', key: 'blergh', v: 'del' } | ||
, { type: 'batch', keys: 'k1,k2' } | ||
, { type: 'batch', keys: 'k3,k4' } | ||
], 'calls correctly behaved') | ||
t.ok(calls.length === 0, 'not called') | ||
t.end() | ||
ld.open(function (err) { | ||
t.error(err) | ||
t.equal(calls.length, 7, 'all functions called') | ||
t.deepEqual(calls, [ | ||
{ type: 'put', key: 'foo1', v: 'put1' } | ||
, { type: 'get', key: 'woo1', v: 'gets1' } | ||
, { type: 'put', key: 'foo2', v: 'put2' } | ||
, { type: 'get', key: 'woo2', v: 'gets2' } | ||
, { type: 'del', key: 'blergh', v: 'del' } | ||
, { type: 'batch', keys: 'k1,k2' } | ||
, { type: 'batch', keys: 'k3,k4' } | ||
], 'calls correctly behaved') | ||
t.end() | ||
}) | ||
}) | ||
test('iterators', function (t) { | ||
var ld = new DeferredLevelDOWN('loc') | ||
t.plan(7) | ||
var db = { | ||
iterator: function (options) { | ||
return { | ||
next : function (cb) { | ||
cb(null, 'key', 'value') | ||
} | ||
, end : function (cb) { | ||
process.nextTick(cb) | ||
} | ||
} | ||
} | ||
, open: function (options, callback) { | ||
process.nextTick(callback) | ||
} | ||
} | ||
var ld = new DeferredLevelDOWN(db) | ||
var it = ld.iterator() | ||
@@ -122,19 +155,10 @@ var nextFirst = false | ||
t.ok(nextFirst) | ||
t.end() | ||
}) | ||
ld.setDb({ iterator: function (options) { | ||
return { | ||
next : function (cb) { | ||
cb(null, 'key', 'value') | ||
} | ||
, end : function (cb) { | ||
process.nextTick(cb) | ||
} | ||
} | ||
}}) | ||
ld.open(function (err) { | ||
t.error(err) | ||
}) | ||
t.ok(require('./').DeferredIterator) | ||
}) | ||
t.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15528
227
69
1