Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support new _remove event on scuttlebutt

  • Loading branch information...
commit 1525ea4b7b2a77ddb7c1d158a6e6ef54af1c115e 1 parent 27d7704
@dominictarr authored
Showing with 17 additions and 63 deletions.
  1. +11 −7 doc.js
  2. +0 −1  index.js
  3. +4 −4 seq.js
  4. +2 −2 set.js
  5. +0 −49 utils.js
View
18 doc.js
@@ -1,6 +1,6 @@
var inherits = require('util').inherits
var Row = require('./row')
-var u = require('./utils')
+var between = require('between')
var Set = require('./set')
var Seq = require('./seq')
var Scuttlebutt = require('scuttlebutt')
@@ -8,6 +8,13 @@ var EventEmitter = require('events').EventEmitter
inherits(Doc, Scuttlebutt)
+function merge(to, from) {
+ for(var k in from)
+ to[k] = from[k]
+ return to
+}
+
+
module.exports = Doc
//doc
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -40,7 +47,7 @@ module.exports = Doc
*/
function order (a, b) {
- return u.strord(a[1], b[1]) || u.strord(a[2], b[2])
+ return between.strord(a[1], b[1]) || between.strord(a[2], b[2])
}
function Doc (id) {
@@ -78,7 +85,6 @@ Doc.prototype._add = function (id, source, change) {
this.rows[r.id] = r
function track (changes, source) {
-// var update = [r.id, changes, u.timestamp(), doc.id]
doc.localUpdate([r.id, changes])
}
@@ -129,14 +135,12 @@ Doc.prototype.applyUpdate = function (update, source) {
//remember the most recent update from each node.
//now handled my scuttlebutt.
-// if(!this.recieved[from] || this.recieved[from] < timestamp)
-// this.recieved[from] = timestamp
-
// if(!row.validate(changes)) return
for(var key in changes) {
var value = changes[key]
if(!hist[key] || order(hist[key], update) < 0) {
+ if(hist[key]) this.emit('_remove', hist[key])
hist[key] = update
changed[key] = changes[key]
emit = true
@@ -149,7 +153,7 @@ Doc.prototype.applyUpdate = function (update, source) {
// hang on, in the mean time, I will probably only be managing n < 10 sets.
// at once,
- u.merge(row.state, changed)
+ merge(row.state, changed)
for(var k in changed)
this.sets.emit(k, row, changed)
View
1  index.js
@@ -3,7 +3,6 @@
var inherits = require('util').inherits
var EventEmitter = require('events').EventEmitter
-var u = require('./utils')
exports = module.exports = require('./doc')
exports.Row = require('./row')
View
8 seq.js
@@ -3,13 +3,13 @@
var Set = require('./set')
var Row = require('./row')
var inherits = require('util').inherits
-var u = require('./utils')
+var between = require('between')
module.exports = Seq
function sort (array) {
return array.sort(function (a, b) {
- return u.strord(a.get('_sort'), b.get('_sort'))
+ return between.strord(a.get('_sort'), b.get('_sort'))
})
}
@@ -54,8 +54,8 @@ function Seq (doc, key, val) {
obj = doc.rows[obj]
var _sort =
- u.between(before, after )
- + u.randstr(3) //add a random tail so it's hard
+ between.between(before, after )
+ + between.randstr(3) //add a random tail so it's hard
//to concurrently add two items with the
//same sort.
View
4 set.js
@@ -1,8 +1,8 @@
'use strict';
var inherits = require('util').inherits
var EventEmitter = require('events').EventEmitter
-var u = require('./utils')
var Row = require('./row')
+var between = require('between')
inherits(Set, EventEmitter)
@@ -99,7 +99,7 @@ Set.prototype.toJSON = function () {
return this._array.map(function (e) {
return e.state
}).sort(function (a, b) {
- return u.strord(a._sort || a.id, b._sort || b.id)
+ return between.strord(a._sort || a.id, b._sort || b.id)
})
}
View
49 utils.js
@@ -1,49 +0,0 @@
-'use strict';
-var b = require('between')
-
-exports.clone =
-function (ary) {
- return ary.map(function (e) {
- return Array.isArray(e) ? exports.clone(e) : e
- })
-}
-
-exports.randstr = b.randstr
-exports.between = b.between
-exports.strord = b.strord
-exports.merge = merge
-exports.concat = concat
-exports.timestamp = timestamp
-
-
-function merge(to, from) {
- for(var k in from)
- to[k] = from[k]
- return to
-}
-
-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
-}
-
-function concat(to, from) {
- while(from.length)
- to.push(from.shift())
- return to
-}

0 comments on commit 1525ea4

Please sign in to comment.
Something went wrong with that request. Please try again.