Skip to content

Instantly share code, notes, and snippets.

@crcn
Last active August 29, 2015 14:18
Show Gist options
  • Save crcn/cac5b5826590abed921d to your computer and use it in GitHub Desktop.
Save crcn/cac5b5826590abed921d to your computer and use it in GitHub Desktop.
var localStorage = require("crudlet-local-storage");
var http = require("crudlet-http");
var crud = require("crudlet");
var localdb = crud.tailable(localStorage());
var opsdb = crud.child(localdb, { collection: "operations" });
var api = http({ prefix: "/api" });
// hit the API first, then fallback to local storage if
// api doesn't properly persist
var db = crud.first(api, function(operation) {
return opsdb(crud.op("insert", { data: operation }));
});
// wait for an operation to persist to
opsdb(crud.op("tail")).on("data", function() {
online(drainLocalDatabase);
});
function drainLocalDatabase() {
opsdb(crud.op("load", {
multi: true
})).pipe(crud.open(api)).on("end", function() {
// remove all operations in the db
opsdb(crud.op("remove", {
multi: true
}))
});
}
function online(callback) {
// check if user is online.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment