no more promises in requestTransaction :)
This commit is contained in:
parent
01173879a0
commit
4b08cbe875
@ -9,6 +9,7 @@
|
|||||||
"parser": "babel-eslint",
|
"parser": "babel-eslint",
|
||||||
"globals": {
|
"globals": {
|
||||||
"OperationBuffer": true,
|
"OperationBuffer": true,
|
||||||
"IndexedDB": true
|
"IndexedDB": true,
|
||||||
|
"IDBRequest": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,32 +1,46 @@
|
|||||||
|
|
||||||
|
type State = {
|
||||||
|
user: string,
|
||||||
|
clock: number
|
||||||
|
};
|
||||||
|
|
||||||
|
type StateVector = Array<State>;
|
||||||
|
|
||||||
|
type StateSet = Object<number>;
|
||||||
|
|
||||||
var IndexedDB = (function(){ //eslint-disable-line no-unused-vars
|
var IndexedDB = (function(){ //eslint-disable-line no-unused-vars
|
||||||
var GeneratorFunction = (function*(){}).constructor;
|
|
||||||
|
|
||||||
class Transaction {
|
class Transaction {
|
||||||
|
|
||||||
constructor (transaction) {
|
constructor (transaction) {
|
||||||
this.transaction = transaction;
|
this.transaction = transaction;
|
||||||
|
this.sv = transaction.objectStore("StateVector");
|
||||||
|
this.ob = transaction.objectStore("OperationBuffer");
|
||||||
}
|
}
|
||||||
setOperation (op) {
|
*setOperation (op) {
|
||||||
return new Promise((resolve, reject)=> {
|
yield this.ob.put(op);
|
||||||
var req = this.transaction.objectStore("OperationBuffer").put(op);
|
return op;
|
||||||
req.onsuccess = function () {
|
|
||||||
resolve(op);
|
|
||||||
};
|
|
||||||
req.onerror = function () {
|
|
||||||
reject("Could not set Operation!");
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
getOperation (uid) {
|
*getOperation (uid) {
|
||||||
return new Promise((resolve, reject)=>{
|
return yield this.ob.get(uid);
|
||||||
var req = this.transaction.objectStore("OperationBuffer").get(uid);
|
}
|
||||||
req.onsuccess = function () {
|
*setState (state : State) : State {
|
||||||
resolve(req.result);
|
return yield this.sv.put(state);
|
||||||
|
}
|
||||||
|
*getState (user : string) : State {
|
||||||
|
return (yield this.sv.get(user)) || {
|
||||||
|
user: user,
|
||||||
|
clock: 0
|
||||||
};
|
};
|
||||||
req.onerror = function () {
|
}
|
||||||
reject("Could not get Operation");
|
*getStateVector () : StateVector {
|
||||||
};
|
var stateVector = [];
|
||||||
});
|
var cursor = yield this.sv.openCursor();
|
||||||
|
while ((cursor = yield cursor.continue) != null) {
|
||||||
|
stateVector.push(cursor.value);
|
||||||
|
}
|
||||||
|
return stateVector;
|
||||||
|
}
|
||||||
|
*getStateSet () : StateSet {
|
||||||
}
|
}
|
||||||
getOperations () {
|
getOperations () {
|
||||||
return function* () {
|
return function* () {
|
||||||
@ -86,22 +100,23 @@ var IndexedDB = (function(){ //eslint-disable-line no-unused-vars
|
|||||||
this.ready.then(function(db){
|
this.ready.then(function(db){
|
||||||
var transaction = new Transaction(db.transaction(["OperationBuffer", "StateVector"], "readwrite"));
|
var transaction = new Transaction(db.transaction(["OperationBuffer", "StateVector"], "readwrite"));
|
||||||
var gen = makeGen.apply(transaction);
|
var gen = makeGen.apply(transaction);
|
||||||
function handle(result : Object){
|
|
||||||
var v = result.value;
|
function handle(res){
|
||||||
if (result.done) {
|
var request = res.value;
|
||||||
return v;
|
if (res.done){
|
||||||
} else if (v.constructor === Promise) {
|
return;
|
||||||
return result.value.then(function(res){
|
} else if (request.constructor === IDBRequest) {
|
||||||
return handle(gen.next(res));
|
request.onsuccess = function(){
|
||||||
}, function(err){
|
handle(gen.next(request.result));
|
||||||
return handle(gen.throw(err));
|
};
|
||||||
});
|
request.onerror = function(err){
|
||||||
} else if (v.constructor === GeneratorFunction){
|
gen.throw(err);
|
||||||
return handle(v.apply(transaction).next());
|
};
|
||||||
} else {
|
} else {
|
||||||
throw new Error("I do only accept Promises and Generators!");
|
gen.throw("You may not yield this type!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return handle(gen.next());
|
return handle(gen.next());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -7,23 +7,35 @@ if(typeof window !== "undefined"){
|
|||||||
|
|
||||||
it("can create transactions", function(done) {
|
it("can create transactions", function(done) {
|
||||||
ob.requestTransaction(function*(){
|
ob.requestTransaction(function*(){
|
||||||
var op = yield this.setOperation({
|
var op = yield* this.setOperation({
|
||||||
"uid": ["u1", 0],
|
"uid": ["u1", 0],
|
||||||
"stuff": true
|
"stuff": true
|
||||||
});
|
});
|
||||||
expect(yield this.getOperation(["u1", 0]))
|
expect(yield* this.getOperation(["u1", 0]))
|
||||||
.toEqual(op);
|
.toEqual(op);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("receive remaining operations", function(done){
|
it("getOperation(op) returns undefined if op does not exist", function(done){
|
||||||
ob.requestTransaction(function*(){
|
ob.requestTransaction(function*(){
|
||||||
expect(yield this.getOperations(["u1", 0]))
|
var op = yield* this.getOperation("plzDon'tBeThere");
|
||||||
.toEqual({
|
expect(op).toBeUndefined();
|
||||||
"uid": ["u1", 0],
|
done();
|
||||||
"stuff": true
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("yield throws if request is unknown", function(done){
|
||||||
|
|
||||||
|
ob.requestTransaction(function*(){
|
||||||
|
try {
|
||||||
|
yield this.getOperations(["u1", 0]);
|
||||||
|
} catch (e) {
|
||||||
|
expect(true).toEqual(true);
|
||||||
|
done();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
expect("Expected an Error!").toEqual(true);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user