Permalink
Browse files

update protocol in subclasses

  • Loading branch information...
dominictarr committed Nov 29, 2012
1 parent 492e17e commit 0ee2e31d8d0c9a0282884887ae818eb94d70d483
Showing with 13 additions and 10 deletions.
  1. +4 −4 events.js
  2. +9 −6 model.js
View
@@ -20,16 +20,16 @@ emitter.emit = function (event) {
var args = [].slice.call(arguments)
if(event == 'newListener')
return emit.apply(this, args)
- return this.localUpdate.apply(this, args)
+ return this.localUpdate(args)
}
emitter.applyUpdate = function (update) {
- var key = update[0]
+ var key = update[0][0]
this.events = this.events || {}
this.events[key] = this.events[key] || []
this.events[key].push(update)
//emit the event.
- emit.apply(this, update)
+ emit.apply(this, update[0])
return true
}
@@ -48,7 +48,7 @@ emitter.history = function (filter) {
//sort my timestamps.
// so will be A1 B1 A2 B2,
// not A1 A2 B1 B2
- return a[2] - b[2]
+ return a[1] - b[1]
})
}
View
@@ -16,23 +16,26 @@ function Model (opts) {
var m = Model.prototype
m.set = function (k, v) {
- return this.localUpdate(k, v)
+ return this.localUpdate([k, v])
}
m.get = function (k) {
if(this.store[k])
- return this.store[k][1]
+ return this.store[k][0][1]
}
//return this history since sources.
//sources is a hash of { ID: TIMESTAMP }
m.applyUpdate = function (update) {
- var key = update[0]
+ var key = update[0][0]
//ignore if we already have a more recent value
- if('undefined' !== typeof this.store[key]
- && this.store[key][2] > update[2])
- return
+ if('undefined' !== typeof this.store[key]
+ && this.store[key][1] > update[1])
+ return this.emit('_remove', update)
+
+ if(this.store[key]) this.emit('_remove', this.store[key])
+
this.store[key] = update
this.emit.apply(this, ['update'].concat(update))
return true

0 comments on commit 0ee2e31

Please sign in to comment.