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