Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

crdt

Package Overview
Dependencies
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

crdt - npm Package Compare versions

Comparing version 3.2.3 to 3.3.0

2

package.json

@@ -5,3 +5,3 @@ {

"description": "Commutative Replicated Data Types for easy distributed/collaborative apps",
"version": "3.2.3",
"version": "3.3.0",
"homepage": "http://github.com/dominictarr/crdt",

@@ -8,0 +8,0 @@ "repository": {

@@ -37,3 +37,3 @@ #CRDT - Commutative Replicated Data Types

notice the pattern is basically the same...
notice the pattern is basically the same...

@@ -93,3 +93,12 @@ the client side ...

### Doc#createSet (filter)
You can also create a `Set` using a filter function.
```js
var cheeses = doc.createSet(function (state) {
return state.type === 'cheese'
})
```
### Doc#createSeq (key, value)

@@ -99,2 +108,4 @@

sequences can also be created with a filter using `Doc#createSeq(filter)`
### Doc#createStream (opts)

@@ -121,4 +132,4 @@

This causes a 'change' event to be emitted, and an update message
to be sent down the stream. (note, if the stream in not yet connected,
This causes a 'change' event to be emitted, and an update message
to be sent down the stream. (note, if the stream in not yet connected,
that is okay, current state of the document is replicated as soon as the

@@ -133,3 +144,3 @@ streams are connected.)

return a raw object ready for serialization.
return a raw object ready for serialization.
this is not a JSON string yet, misleading name,

@@ -140,3 +151,3 @@ but that is the correct JSON.stringify api.

Emitted when a row is changed. this may be the result of a local or a
Emitted when a row is changed. this may be the result of a local or a
remote update.

@@ -176,2 +187,15 @@

### event: Set.emit('add', Row)
Emitted when a row is added to the set.
### event: Set.emit('changes', Row, changed)
Emitted when a row in the set changed. The changed value contains a hash
of the key / values that changed.
### event: Set.emit('remove', Row)
Emitted when a row is removed from the set
## Seq

@@ -190,3 +214,3 @@

### Set#has(row|id)
### Seq#has(row|id)

@@ -231,1 +255,12 @@ check if a row|id is a member of the seq. (inherited from `Set`)

### Seq#next(key)
Finds the item that is after this key
### Seq#prev(key)
Finds the item that is before this key
### event: Seq.emit('move', Row)
Emitted when the row has changed it's position in the sequence

@@ -19,3 +19,3 @@ 'use strict';

function find (obj, iter) {
for(var k in obj) {

@@ -31,2 +31,7 @@ var v = obj[k]

Set.call(this, doc, key, val)
if (typeof key !== 'string') {
key = null
}
var seq = this

@@ -37,3 +42,3 @@ this.on('changes', function (row, changes) {

//check if there is already an item with this sort key.
var prev =
var prev =
find(seq._array, function (other) {

@@ -43,5 +48,5 @@ return other != row && other.get('_sort') == row.get('_sort')

//nudge it forward if it has the same key.
//nudge it forward if it has the same key.
if(prev)
seq.insert(row, prev, seq.next(row))
seq.insert(row, prev, seq.next(row))
else

@@ -60,8 +65,8 @@ seq.emit('move', row)

var _sort =
between.between(before, after )
var _sort =
between.between(before, after )
+ between.randstr(3) //add a random tail so it's hard
//to concurrently add two items with the
//same sort.
var r, changes

@@ -71,10 +76,13 @@ if(obj instanceof Row) {

changes = {_sort: _sort}
if(r.get(key) != val)
if (key && r.get(key) != val) {
changes[key] = val
}
r.set(changes)
} else {
obj._sort = _sort
obj[key] = val
if (key) {
obj[key] = val
}
r = doc.set(id(obj), obj)
}
}
sort(this._array)

@@ -88,3 +96,3 @@ return r

return (
'string' === typeof key ? key
'string' === typeof key ? key
: key instanceof Row ? key.get()._sort

@@ -135,5 +143,5 @@ : key ? key._sort

function id(obj) {
return (obj.id
|| obj._id
|| '_' + Date.now()
return (obj.id
|| obj._id
|| '_' + Date.now()
+ '_' + Math.round(Math.random()*1000)

@@ -172,3 +180,3 @@ )

Seq.prototype.push = function (obj) {
return this.insert(obj, this.last(), '~')
return this.insert(obj, this.last(), '~')
}

@@ -175,0 +183,0 @@

@@ -22,3 +22,3 @@ 'use strict';

that is my vibe. don't make a database you have to
that is my vibe. don't make a database you have to
_map_ to your application. pre-map the database.

@@ -38,3 +38,3 @@

or use map-reduces. remember, if the the reduce is
or use map-reduces. remember, if the the reduce is
monotonic you don't have to remember each input.

@@ -49,6 +49,12 @@ */

var set = this
var filter
//DO NOT CHANGE once you have created the set.
this.key = key
this.value = value
if ('function' === typeof key) {
filter = this.filter = key
key = null
} else {
//DO NOT CHANGE once you have created the set.
this.key = key
this.value = value
}

@@ -61,3 +67,5 @@ function add(row) {

function remove (_, changed) {
if(row.state[key] === value) {
if ((key && row.state[key] === value) ||
(filter && filter(row.state))
) {
set.emit('changes', row, changed)

@@ -75,14 +83,27 @@ return

row.on('changes', remove)
}
doc.sets.on(key, function (row, changed) {
if(changed[key] !== value) return
add(row)
})
if (!filter) {
doc.sets.on(key, function (row, changed) {
if(changed[key] !== value) return
add(row)
})
} else {
doc.on('create', function (row) {
if (filter(row.state)) {
add(row)
}
})
}
this.rm = this.remove = function (row) {
row = this.get(row)
row = this.get(row)
if(!row) return
return row.set(key, null)
if (key) {
return row.set(key, null)
} else {
throw new Error("Set cannot remove rows with arbitary filters")
}
}

@@ -92,3 +113,8 @@

var row = doc.get(id)
if(row.get(key) === value) add(row)
if (key && row.get(key) === value) {
add(row)
} else if (filter && filter(row.state)) {
add(row)
}
}

@@ -112,3 +138,3 @@

Set.prototype.each =
Set.prototype.each =
Set.prototype.forEach = function (iter) {

@@ -122,4 +148,4 @@ return this._array.forEach(iter)

return (
'string' === typeof id ? this.rows[id]
: 'number' === typeof id ? this.rows[id]
'string' === typeof id ? this.rows[id]
: 'number' === typeof id ? this.rows[id]
: id && id.id ? this.rows[id.id]

@@ -126,0 +152,0 @@ : null

@@ -57,13 +57,2 @@ var crdt = require('..')

function log(set) {
set.on('add', function (row) {
console.log('add', set.value,'->', row.state)
})
set.on('remove', function (row) {
console.log('rm', set.value,'->', row.state)
})
}
log(set)

@@ -88,2 +77,44 @@ log(set2)

exports['test - filters'] = function (t) {
console.log("HELLO")
var doc = new crdt.Doc()
var set = doc.createSet(function (state) {
return state.type === 'thing' && state.what <= 5
})
var set2 = doc.createSet(function (state) {
return state.type === 'other' && state.what > 8
})
log(set)
log(set2)
console.log(set.toJSON())
console.log(set2.toJSON())
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})
a.deepEqual(set.toJSON(), [
{ id: 'b', type: 'thing', what: 5 }
])
a.deepEqual(set2.toJSON(), [])
console.log("passed")
t.end()
}
function log(set) {
set.on('add', function (row) {
console.log('add', set.value,'->', row.state)
})
set.on('remove', function (row) {
console.log('rm', set.value,'->', row.state)
})
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc