added memory data store (actually adding it..)

This commit is contained in:
Kevin Jahns
2015-06-28 11:14:40 +02:00
parent 75793d0ced
commit 7a274565e5
5 changed files with 160 additions and 2 deletions

View File

@@ -0,0 +1,102 @@
type State = {
user: string,
clock: number
};
type StateVector = Array<State>;
type OperationSet = Object; // os[Id] = op
type StateSet = Object;
Y.Memory = (function(){ //eslint-disable-line no-unused-vars
class Transaction extends AbstractTransaction { //eslint-disable-line
ss: StateSet;
os: OperationSet;
store: OperationStore;
constructor (store : OperationStore) {
super(store);
this.sv = store.ss;
this.os = store.os;
}
*setOperation (op) {
this.os[JSON.stringify(op.id)] = op;
return op;
}
*getOperation (id) {
return this.os[JSON.stringify(id)];
}
*removeOperation (id) {
delete this.os[JSON.stringify(id)];
}
*setState (state : State) : State {
this.sv[state.user] = state.clock;
}
*getState (user : string) : State {
var clock = this.ss[user];
if (clock == null){
clock = 0;
}
return {
user: user,
clock: clock
};
}
*getStateVector () : StateVector {
var stateVector = [];
for (var user in this.ss) {
var clock = this.ss[user];
stateVector.push({
user: user,
clock: clock
});
}
return stateVector;
}
*getStateSet () : StateSet {
return this.ss;
}
*getOperations (startSS : StateSet) {
if (startSS == null){
startSS = {};
}
var ops = [];
var endSV : StateVector = yield* this.getStateVector();
for (var endState of endSV) {
var user = endState.user;
var startPos = startSS[user] || 0;
var endPos = endState.clock;
for (var clock = startPos; clock <= endPos; clock++) {
var op = yield* this.getOperation([user, clock]);
if (op != null) {
ops.push(op);
}
}
}
return ops;
}
}
class OperationStore extends AbstractOperationStore { //eslint-disable-line no-undef
namespace: string;
ready: Promise;
whenReadyListeners: Array<Function>;
constructor (y) {
super(y);
}
requestTransaction (makeGen : Function) {
var t = new Transaction(this);
var gen = makeGen.call(t);
gen.next();
if (gen.done !== true) {
throw new Error("transaction is supposed to be done. Note: you may not yield with this transaction! (yield* is allowed though)");
}
}
*removeDatabase () {
delete this.os;
}
}
return OperationStore;
})();