Permalink
Browse files

merge

  • Loading branch information...
2 parents 6d13923 + 8bea4e9 commit 5ef97f23774b858a363000b8d984060d4adc3be4 @dominictarr committed Jan 19, 2013
Showing with 39 additions and 3 deletions.
  1. +5 −0 README.md
  2. +5 −1 set.js
  3. +29 −2 test/sets.js
View
@@ -175,6 +175,11 @@ get an item in this set, if it exists.
Iterate over the `Rows` in the set.
+### Set#onEach(iter)
+
+Iterate over the `Rows` in the set and any new row that may be
+added to the set in the future.
+
### Set#remove(row)
removes a row from the set. sets the set's `key`, to null.
View
6 set.js
@@ -110,13 +110,17 @@ function Set(doc, key, value) {
} else if (filter && filter(row.state)) {
add(row)
}
-
}
this.setMaxListeners(Infinity)
}
+Set.prototype.onEach = function (callback) {
+ this.forEach(callback)
+ this.on("add", callback)
+}
+
Set.prototype.asArray = function () {
return this._array
}
View
@@ -74,9 +74,8 @@ exports['test - post'] = function (t) {
}
exports['test - filters'] = function (t) {
- console.log("HELLO")
-
var doc = new crdt.Doc()
+ console.log("# Filters")
var set = doc.createSet(function (state) {
return state.type === 'thing' && state.what <= 5
@@ -93,6 +92,7 @@ exports['test - filters'] = function (t) {
doc.add({id: 'a', type: 'thing', what: 3})
doc.add({id: 'b', type: 'thing', what: 5})
+ //overwrite the first 'a'
doc.add({id: 'a', type: 'other', what: 7})
doc.add({id: 'c', type: 'thing', what: 9})
@@ -121,6 +121,32 @@ exports['set caching'] = function (t) {
t.end()
}
+exports['test - create set later'] = function (t) {
+ var doc = new crdt.Doc()
+
+ console.log("LATER")
+
+ doc.add({id: 'a', type: 'thing', what: 3})
+ doc.add({id: 'b', type: 'thing', what: 5})
+ doc.add({id: 'a', type: 'other', what: 7})
+ doc.add({id: 'c', type: 'thing', what: 9})
+
+ var set = doc.createSet("type", "thing")
+ var states = []
+
+ set.onEach(function (row, state) {
+ console.log(state)
+ states.push(row.state)
+ })
+
+ a.deepEqual(states, [
+ { id: 'b', type: 'thing', what: 5 },
+ { id: 'c', type: 'thing', what: 9 }
+ ])
+
+ t.end()
+}
+
function log(set) {
set.on('add', function (row) {
@@ -131,3 +157,4 @@ function log(set) {
})
}
+

0 comments on commit 5ef97f2

Please sign in to comment.