Skip to content

Instantly share code, notes, and snippets.

@juliangruber
Created November 12, 2012 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliangruber/4061867 to your computer and use it in GitHub Desktop.
Save juliangruber/4061867 to your computer and use it in GitHub Desktop.
var state = require('state')({
getLastUpdate : db.get.bind(db, 'lastUpdate'),
setLastUpdate : db.put.bind(db)
})
// 1) EE interface
// receive updates if not connected to master
state.on('update', function (update, ts) {
db.put(update.key, update.value)
})
// replicate updates if connected to master
db.on('update', state.put.bind(state))
// 2) with kv wrapper
var kvStream = kv.createDeltaStream()
// connected as required
state.pipe(kvStream).pipe(state)
// distribute all the things!
state.pipe(net.connect(1234)).pipe(state)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment