Skip to content

Instantly share code, notes, and snippets.

@redgeoff
Last active August 29, 2015 14:07
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 redgeoff/0c43bc7391aa0fb8c534 to your computer and use it in GitHub Desktop.
Save redgeoff/0c43bc7391aa0fb8c534 to your computer and use it in GitHub Desktop.
PouchDB Chatty
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="//cdn.jsdelivr.net/pouchdb/3.0.5/pouchdb.js"></script>
</head>
<body>
<script>
var db = new PouchDB('test_projects'), changes = null, to = null, from = null;
function onError(err) {
console.log(err);
}
db.info().then(function (info) {
var remoteCouch = 'https://pouchyng.iriscouch.com/projects',
opts = { live: true };
changes = db.changes({
since: info.update_seq,
live: true
});
registerListeners();
to = db.replicate.to(remoteCouch, opts, onError);
from = db.replicate.from(remoteCouch, opts, onError);
from.on('uptodate', fnFactory('uptodate'))
.on('complete', fnFactory('complete'))
.on('error', fnFactory('error'));
});
function fnFactory(str) {
return function () {
console.log(str);
};
}
function registerListeners() {
db.on('create', fnFactory('create'))
.on('update', fnFactory('update'))
.on('delete', fnFactory('delete'));
}
// Cancel replication for easier debugging
function cancelReplication() {
changes.cancel();
to.cancel();
from.cancel();
}
</script>
<button onclick="cancelReplication()">Cancel Replication</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment