diff --git a/events.js b/events.js index 35b12c3..e57c927 100644 --- a/events.js +++ b/events.js @@ -34,7 +34,7 @@ emitter.applyUpdate = function (update) { } -emitter.histroy = function (filter) { +emitter.history = function (filter) { var self = this var h = [] this.events = this.events || {} diff --git a/id.js b/id.js deleted file mode 100644 index 2633d05..0000000 --- a/id.js +++ /dev/null @@ -1,9 +0,0 @@ -var map = require('iterate').map - -module.exports = -function createID () { - return map(3, function (i) { - return Math.random().toString(16).substring(2).toUpperCase() - }).join('') -} - diff --git a/index.js b/index.js index 4fb0983..62c303f 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,7 @@ exports = module.exports = Scuttlebutt exports.createID = u.createID +exports.updateIsRecent = u.filter exports.timestamp = u.timestamp function dutyOfSubclass() { @@ -56,16 +57,16 @@ var sb = Scuttlebutt.prototype var emit = EventEmitter.prototype.emit sb.applyUpdate = dutyOfSubclass -sb.histroy = dutyOfSubclass +sb.history = dutyOfSubclass sb.localUpdate = function (key, value) { - this._update([key, value, this.id, u.timestamp()]) + this.write([key, value, this.id, u.timestamp()]) return this } //checks whether this update is valid. -sb._update = function (update) { +sb.write = function (update) { var source = update[2] var ts = update[3] @@ -103,18 +104,18 @@ sb.createStream = function () { //if it's an array, it's an update. //if it's an object, it's a scuttlebut digest. if(Array.isArray(data) && validate(data)) - return self._update(data) + return self.write(data) if('object' === typeof data && data) { //when the digest is recieved from the other end, - //send the histroy. + //send the history. //merge with the current list of sources. sources = data - i.each(self.histroy(sources), d.emitData.bind(d)) + i.each(self.history(sources), d.emitData.bind(d)) d.emit('sync') } }).on('ended', function () { d.emitEnd() }) .on('close', function () { - self.removeListener('update', onUpdate) + self.removeListener('data', onUpdate) }) function onUpdate (update) { //key, value, source, ts) { @@ -122,7 +123,7 @@ sb.createStream = function () { return //if I put this after source[source]= ... it breaks tests - d.emit('data', update) + d.emitData(update) //really, this should happen before emitting. var source = update[2] diff --git a/model.js b/model.js index 77ecad7..75b3441 100644 --- a/model.js +++ b/model.js @@ -38,7 +38,7 @@ m.applyUpdate = function (update) { return true } -m.histroy = function (sources) { +m.history = function (sources) { var self = this var h = [] each(this.store, function (e) { diff --git a/test/index.js b/test/index.js index 24b8857..633d4ef 100644 --- a/test/index.js +++ b/test/index.js @@ -17,23 +17,23 @@ test('updates appear in histroy', function (g) { var ts = timestamp() - assert.equal(g._update([key, value, source, ts]) + assert.equal(g.write([key, value, source, ts]) , true - , '_update returns true to indicate update applied') + , 'write returns true to indicate update applied') console.log(g.store) assert.equal(g.get(key), value) - assert.deepEqual(g.histroy(), [['key', value, source, ts]]) + assert.deepEqual(g.history(), [['key', value, source, ts]]) var value2 = Math.random() //older timestamps are not appled. - assert.equal(g._update([key, value2, source, ts - 1]) + assert.equal(g.write([key, value2, source, ts - 1]) , false - , '_update returns false to indicate update did not apply') + , 'write returns false to indicate update did not apply') - //the second update was older, so must not be in the histroy - assert.deepEqual(g.histroy(), [['key', value, source, ts]]) + //the second update was older, so must not be in the history + assert.deepEqual(g.history(), [['key', value, source, ts]]) assert.equal(g.get(key), value) }) @@ -44,9 +44,9 @@ test('can filter histroy with {sources: timestamps}', function (g) { var C = createID() var ts = timestamp() - g._update(['A', 'aaa', A, ts]) - g._update(['B', 'bbb', B, ts]) - g._update(['C', 'ccc', C, ts]) + g.write(['A', 'aaa', A, ts]) + g.write(['B', 'bbb', B, ts]) + g.write(['C', 'ccc', C, ts]) //filter should only return timestamps that are after //the given timestamps. @@ -56,22 +56,22 @@ test('can filter histroy with {sources: timestamps}', function (g) { filter[C] = ts assert.deepEqual( - g.histroy(filter) + g.history(filter) , []) filter[B] = ts - 1 assert.deepEqual( - g.histroy(filter) - , [['B', 'bbb', B, ts]]) + g.history(filter) + , [['B', 'bbb', B, ts]]) - //if an item is not available, it + //if an item is not available, it filter[C] = null assert.deepEqual( - g.histroy(filter) + g.history(filter) , [ ['B', 'bbb', B, ts] - , ['C', 'ccc', C, ts]]) + , ['C', 'ccc', C, ts]]) }) diff --git a/test/integrate.js b/test/integrate.js index 6c3b2c0..9d926f3 100644 --- a/test/integrate.js +++ b/test/integrate.js @@ -3,11 +3,12 @@ var assert = require('assert') var g1 = gossip() var g2 = gossip() -var s1, s2 +var s1, s2 (s1 = g1.createStream()) .pipe(s2 = g2.createStream()).pipe(s1) -s1.on('data', console.log) +s1.on('data', console.log.bind(console, "s1")) +s2.on('data', console.log.bind(console, "s2")) //I like to have streams that work sync. //if you can do that, you know it's tight. diff --git a/timestamp.js b/timestamp.js deleted file mode 100644 index 2e39dc3..0000000 --- a/timestamp.js +++ /dev/null @@ -1,47 +0,0 @@ -/* -this is just copied from crdt - -this is NOT a wholly accurate representation of the time. -since js only measures time as ms, if you call Date.now() -twice quickly, it's possible to get two identical time stamps. - -subsequent calls to timestamp() are ALWAYS strictly ordered. - -which is the important part. - -maybe call this something other than timestamp? - -what about 'close-enough' since that's what it is. - -also, it may be a very good idea to add something to syncronize -network time. - -I'm guessing you ping your time stamps back in time, and make the minimal adjustment so that all messages are measured to -arrive on one machine after the time they claim to make left the other machine. - -will need to spin up a cluster to test this. -*/ - -module.exports = timestamp - -var _last = 0 -var _count = 1 -var LAST -function timestamp () { - var t = Date.now() - var _t = t - if(_last == t) { -// while(_last == _t) - _t += ((_count++)/1000) - } - else _count = 1 - - _last = t - - if(_t === LAST) - throw new Error('LAST:' + LAST + ',' + _t) - LAST = _t - return _t -} - -