now using one master generator, that rulez them all
This commit is contained in:
parent
b3e09d001f
commit
042bcee482
@ -13,6 +13,7 @@
|
||||
"AbstractTransaction": true,
|
||||
"Transaction": true,
|
||||
"IndexedDB": true,
|
||||
"IDBRequest": true
|
||||
"IDBRequest": true,
|
||||
"GeneratorFunction": true
|
||||
}
|
||||
}
|
||||
|
@ -119,10 +119,10 @@ gulp.task("build_jasmine_browser", function(){
|
||||
});
|
||||
|
||||
|
||||
gulp.task("develop", ["build_jasmine_browser", "build"], function(){
|
||||
gulp.task("develop", ["build_jasmine_browser", "test", "build"], function(){
|
||||
|
||||
gulp.watch(files.test, ["build_jasmine_browser"]);
|
||||
// gulp.watch(files.test, ["test"]);
|
||||
gulp.watch(files.test, ["test"]);
|
||||
gulp.watch(files.test, ["build"]);
|
||||
|
||||
return gulp.src("build/jasmine_browser.js")
|
||||
|
@ -22,8 +22,6 @@ type Listener = {
|
||||
missing : number // number of operations that are missing
|
||||
}
|
||||
|
||||
type GeneratorFunction = Function;
|
||||
|
||||
type Id = [string, number];
|
||||
|
||||
class AbstractOperationStore { //eslint-disable-line no-unused-vars
|
||||
@ -105,7 +103,6 @@ class AbstractOperationStore { //eslint-disable-line no-unused-vars
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
// called by a transaction when an operation is added
|
||||
operationAdded (op) {
|
||||
|
@ -18,8 +18,6 @@ type IDBOpenDBRequest = Function;
|
||||
|
||||
declare var indexedDB : Object;
|
||||
|
||||
declare var setTimeout : Function;
|
||||
|
||||
var IndexedDB = (function(){ //eslint-disable-line no-unused-vars
|
||||
class Transaction extends AbstractTransaction { //eslint-disable-line
|
||||
transaction: IDBTransaction;
|
||||
@ -36,7 +34,7 @@ var IndexedDB = (function(){ //eslint-disable-line no-unused-vars
|
||||
}
|
||||
*setOperation (op) {
|
||||
yield this.os.put(op);
|
||||
this.buffer[JSON.stringify(op.uid)] = op;
|
||||
this.buffer[JSON.stringify(op.id)] = op;
|
||||
return op;
|
||||
}
|
||||
*getOperation (id) {
|
||||
@ -112,59 +110,77 @@ var IndexedDB = (function(){ //eslint-disable-line no-unused-vars
|
||||
whenReadyListeners: Array<Function>;
|
||||
constructor (namespace : string) {
|
||||
super();
|
||||
this.whenReadyListeners = [];
|
||||
this.namespace = namespace;
|
||||
this.ready = false;
|
||||
this.transactionQueue = {
|
||||
queue: [],
|
||||
onRequest: null
|
||||
};
|
||||
|
||||
var req = indexedDB.open(namespace, 2); //eslint-disable-line no-undef
|
||||
req.onerror = function(){
|
||||
throw new Error("Couldn't open the IndexedDB database!");
|
||||
};
|
||||
req.onsuccess = (event)=>{
|
||||
this.db = event.target.result;
|
||||
this.whenReadyListeners.forEach(function(f){
|
||||
setTimeout(f, 0);
|
||||
});
|
||||
this.whenReadyListeners = null;
|
||||
this.ready = true;
|
||||
};
|
||||
req.onupgradeneeded = function(event){
|
||||
var db = event.target.result;
|
||||
db.createObjectStore("OperationStore", {keyPath: "id"});
|
||||
db.createObjectStore("StateVector", {keyPath: "user"});
|
||||
};
|
||||
}
|
||||
whenReady (f : Function) {
|
||||
if (this.ready){
|
||||
setTimeout(f, 0);
|
||||
} else {
|
||||
this.whenReadyListeners.push(f);
|
||||
var store = this;
|
||||
|
||||
var tGen = (function *transactionGen(){
|
||||
store.db = yield indexedDB.open(namespace, 3);
|
||||
var transactionQueue = store.transactionQueue;
|
||||
|
||||
var transaction = null;
|
||||
var cont = true;
|
||||
while (cont) {
|
||||
var request = yield transactionQueue;
|
||||
transaction = new Transaction(store);
|
||||
|
||||
yield* request.call(transaction);/*
|
||||
while (transactionQueue.queue.length > 0) {
|
||||
yield* transactionQueue.queue.shift().call(transaction);
|
||||
}*/
|
||||
}
|
||||
})();
|
||||
|
||||
function handleTransactions(t){ //eslint-disable-line no-unused-vars
|
||||
var request = t.value;
|
||||
if (t.done){
|
||||
return;
|
||||
} else if (request.constructor === IDBRequest
|
||||
|| request.constructor === IDBCursor ) {
|
||||
request.onsuccess = function(){
|
||||
handleTransactions(tGen.next(request.result));
|
||||
};
|
||||
request.onerror = function(err){
|
||||
tGen.throw(err);
|
||||
};
|
||||
} else if (request === store.transactionQueue) {
|
||||
if (request.queue.length > 0){
|
||||
handleTransactions(tGen.next(request.queue.shift()));
|
||||
} else {
|
||||
request.onRequest = function(){
|
||||
request.onRequest = null;
|
||||
handleTransactions(tGen.next(request.queue.shift()));
|
||||
};
|
||||
}
|
||||
} else if ( request.constructor === IDBOpenDBRequest ) {
|
||||
request.onsuccess = function(event){
|
||||
var db = event.target.result;
|
||||
handleTransactions(tGen.next(db));
|
||||
};
|
||||
request.onerror = function(){
|
||||
tGen.throw("Couldn't open IndexedDB database!");
|
||||
};
|
||||
request.onupgradeneeded = function(event){
|
||||
var db = event.target.result;
|
||||
db.createObjectStore("OperationStore", {keyPath: "id"});
|
||||
db.createObjectStore("StateVector", {keyPath: "user"});
|
||||
};
|
||||
} else {
|
||||
tGen.throw("You can not yield this type!");
|
||||
}
|
||||
}
|
||||
handleTransactions(tGen.next());
|
||||
|
||||
}
|
||||
requestTransaction (makeGen : Function) {
|
||||
this.whenReady(()=>{
|
||||
var transaction = new Transaction(this);
|
||||
var gen = makeGen.apply(transaction);
|
||||
|
||||
function handle(res : any){
|
||||
var request : any = res.value;
|
||||
if (res.done){
|
||||
return;
|
||||
} else if (request.constructor === IDBRequest
|
||||
|| request.constructor === IDBCursor
|
||||
|| request.constructor === IDBOpenDBRequest) {
|
||||
request.onsuccess = function(){
|
||||
handle(gen.next(request.result));
|
||||
};
|
||||
request.onerror = function(err){
|
||||
gen.throw(err);
|
||||
};
|
||||
} else {
|
||||
gen.throw("You can not yield this type!");
|
||||
}
|
||||
}
|
||||
handle(gen.next());
|
||||
});
|
||||
this.transactionQueue.queue.push(makeGen);
|
||||
if (this.transactionQueue.onRequest != null) {
|
||||
this.transactionQueue.onRequest();
|
||||
}
|
||||
}
|
||||
*removeDatabase () {
|
||||
this.db.close();
|
||||
|
@ -2,6 +2,7 @@
|
||||
/*eslint-env browser,jasmine */
|
||||
|
||||
if(typeof window !== "undefined"){
|
||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;
|
||||
describe("IndexedDB", function() {
|
||||
var ob = new IndexedDB("Test");
|
||||
|
||||
@ -43,7 +44,7 @@ if(typeof window !== "undefined"){
|
||||
it("yield throws if request is unknown", function(done){
|
||||
ob.requestTransaction(function*(){
|
||||
try {
|
||||
yield this.getOperations(["u1", 0]);
|
||||
yield* this.setOperation();
|
||||
} catch (e) {
|
||||
expect(true).toEqual(true);
|
||||
done();
|
||||
|
Loading…
x
Reference in New Issue
Block a user