Emit deltas in change over a stream
[key, value, sourceIdentifier, timestamp]
A delta-stream emits an array, where the first value is the key that has changed and the second value is the new value. This represents the delta in change on the object.
The third value is a source identifier. It's used to identify who created this delta
The fourth value is a time stamp which can be used for clever synchronization.
Create deltas that handle changes in state. Then create stream reprensentations of deltas
var Delta = require("delta-stream")
var delta1 = Delta()
, delta2 = Delta()
var stream1 = delta1.createStream()
, stream2 = delta2.createStream()
delta1.on("change:delta1", function (value) {
console.log("[CHANGE:DELTA1]", value)
})
delta2.on("change:delta2", function (value) {
console.log("[CHANGE:DELTA2]", value)
})
stream1.pipe(stream2).pipe(stream1)
delta1.set("delta2", "hello")
delta2.set("delta1", "world")
delta.set
sets a key value pair on a delta
var delta = Delta()
delta.set("foo", "bar")
assert.equal(delta.get("foo"), "bar")
If you set a value to undefined it will be deleted from the internal state object.
delta.get
get's a value by it's key
var delta = Delta()
delta.set("foo", "bar")
assert.equal(delta.get("foo"), "bar")
delta.has
returns true or false based on whether the the key is set
var delta = Delta()
assert.equal(delta.has("foo"), false)
delta.delete
deletes the value attached to a key. Internally this is the
same as setting a value to undefined
.
var delta = Delta()
delta.set("foo", "bar")
delta.delete("foo")
assert.equal(delta.has("foo"), false)
Returns the entire internal state as an object
Create a scuttlebutt stream of the delta. See scuttlebutt
npm install delta-stream
- Raynos