Skip to content
This repository has been archived by the owner on Nov 12, 2019. It is now read-only.

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictarr committed Jan 19, 2013
2 parents 6d13923 + 8bea4e9 commit 5ef97f2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion set.js
Expand Up @@ -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
}
Expand Down
31 changes: 29 additions & 2 deletions test/sets.js
Expand Up @@ -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
Expand All @@ -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})

Expand Down Expand Up @@ -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) {
Expand All @@ -131,3 +157,4 @@ function log(set) {
})

}

0 comments on commit 5ef97f2

Please sign in to comment.