Last active
August 29, 2015 14:18
-
-
Save crcn/cac5b5826590abed921d to your computer and use it in GitHub Desktop.
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 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