Created
November 1, 2012 04:39
-
-
Save jcrugzz/3991797 to your computer and use it in GitHub Desktop.
crdt ordering example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var doc = new(require('crdt').Doc); | |
var seq = doc.createSeq('type', 'rank'); | |
doc.on('row_update', function (row) { | |
if (row.state.type !== 'rank') return; | |
var spans = document.querySelectorAll('.row span'); | |
[].forEach.call(spans, function (span, ix) { | |
if (span.textContent === row.state.value) { | |
document.body.removeChild(span.parentNode); | |
} | |
}); | |
var div = document.createElement('div'); | |
div.className = 'row'; | |
var ix = seq.indexOf(row); | |
if (ix === seq.length() - 1) { | |
document.body.appendChild(div); | |
} | |
else { | |
var e = document.querySelectorAll('.row')[ix]; | |
document.body.insertBefore(div, e); | |
} | |
var span = document.createElement('span'); | |
span.textContent = row.state.value; | |
div.appendChild(span); | |
var up = document.createElement('input'); | |
up.setAttribute('type', 'button'); | |
up.value = '+'; | |
div.appendChild(up); | |
var down = document.createElement('input'); | |
down.setAttribute('type', 'button'); | |
down.value = '-'; | |
div.appendChild(down); | |
up.addEventListener('click', function () { | |
var ix = seq.indexOf(row); | |
if (ix === 0) return; | |
seq.remove(row); | |
seq.before(row, seq.at(ix - 1)); | |
}); | |
down.addEventListener('click', function () { | |
var ix = seq.indexOf(row); | |
if (ix === seq.length() - 1) return; | |
seq.remove(row); | |
seq.after(row, seq.at(ix)); | |
}); | |
}); | |
var shoe = require('shoe'); | |
var c = shoe('/sock'); | |
c.pipe(doc.createStream()).pipe(c); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var doc = new(require('crdt').Doc); | |
var seq = doc.createSeq('type', 'rank'); | |
var request = require('request'); | |
var r = request.put('http://localhost:' + process.argv[2] + '/_replicate'); | |
r.pipe(doc.createStream()).pipe(r); | |
var shuffle = require('deck').shuffle; | |
shuffle('abcdefg'.split('')).forEach(function (letter) { | |
seq.push({ value : letter }); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var doc = new(require('crdt').Doc); | |
var http = require('http'); | |
var ecstatic = require('ecstatic')(__dirname + '/static'); | |
var server = http.createServer(function (req, res) { | |
if (req.url === '/_replicate') { | |
req.pipe(doc.createStream()).pipe(res); | |
} | |
else ecstatic(req, res); | |
}); | |
server.listen(Number(process.argv[2])); | |
var shoe = require('shoe'); | |
var sock = shoe(function (stream) { | |
stream.pipe(doc.createStream()).pipe(stream); | |
}); | |
sock.install(server, '/sock'); | |
var request = require('request'); | |
process.argv.slice(3).forEach(function (port) { | |
var s = doc.createStream(); | |
s.pipe(request.put('http://localhost:' + port + '/_replicate')).pipe(s); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment