Comparing version 5.1.0 to 5.1.1
17
index.js
@@ -12,2 +12,3 @@ const events = require('events') | ||
const alru = require('array-lru') | ||
const set = require('unordered-set') | ||
@@ -78,2 +79,18 @@ const Extension = require('./lib/extension') | ||
HyperTrie.prototype._removeWatch = function (w) { | ||
set.remove(this._watchers, w) | ||
} | ||
HyperTrie.prototype._addWatch = function (w) { | ||
const self = this | ||
set.add(this._watchers, w) | ||
if (this._watchers.length > 1 || !this.feed.sparse) return | ||
this.feed.update({ ifAvailable: false }, function loop () { | ||
if (self._watchers.length === 0) return | ||
self.feed.update({ ifAvailable: false }, loop) | ||
}) | ||
} | ||
HyperTrie.prototype.reconnect = function (from, opts) { | ||
@@ -80,0 +97,0 @@ opts = opts ? Object.assign({}, opts, { reconnect: true }) : { reconnect: true } |
@@ -1,2 +0,1 @@ | ||
const set = require('unordered-set') | ||
const inherits = require('inherits') | ||
@@ -18,19 +17,5 @@ const events = require('events') | ||
const self = this | ||
const feed = this._db.feed | ||
const watchers = this._db._watchers | ||
if (onchange) this.on('change', onchange) | ||
set.add(watchers, this) | ||
this._db._addWatch(this) | ||
this.update() | ||
if (watchers.length === 1 && feed.sparse) { | ||
feedUpdateLoop() | ||
} | ||
function feedUpdateLoop () { | ||
if (self._destroyed) return | ||
// TODO: Expose a way to cancel this update when the watcher is destroyed, since it is not ifAvailable. | ||
feed.update({ ifAvailable: false }, feedUpdateLoop) | ||
} | ||
} | ||
@@ -41,3 +26,3 @@ | ||
Watch.prototype.destroy = function () { | ||
set.remove(this._db._watchers, this) | ||
this._db._removeWatch(this) | ||
this._destroyed = true | ||
@@ -44,0 +29,0 @@ } |
{ | ||
"name": "hypertrie", | ||
"version": "5.1.0", | ||
"version": "5.1.1", | ||
"description": "Distributed single writer key/value store", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -64,2 +64,35 @@ const tape = require('tape') | ||
tape('watch, watch and stop watch first one', function (t) { | ||
t.plan(5 + 1) | ||
const db = create() | ||
var bs = 5 | ||
db.ready(function () { | ||
const clone = create(db.key, { sparse: true }) | ||
const a = clone.watch('foo', function () { | ||
t.pass('got a update') | ||
a.destroy() | ||
}) | ||
const b = clone.watch('foo', function () { | ||
t.pass('got b update') | ||
if (!--bs) { | ||
b.destroy() | ||
t.end() | ||
return | ||
} | ||
db.put('foo/bar', 'baz') | ||
}) | ||
db.put('foo/bar', 'baz') | ||
const stream = db.replicate(true, { live: true }) | ||
stream.pipe(clone.replicate(false, { live: true })).pipe(stream) | ||
}) | ||
}) | ||
tape('remote watch', function (t) { | ||
@@ -66,0 +99,0 @@ const db = create() |
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
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
132868
4104