added memory data store (actually adding it..)
This commit is contained in:
parent
75793d0ced
commit
7a274565e5
102
src/OperationStores/Memory.js
Normal file
102
src/OperationStores/Memory.js
Normal 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;
|
||||||
|
})();
|
25
src/Types/Object.js
Normal file
25
src/Types/Object.js
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
(function(){
|
||||||
|
class Map {
|
||||||
|
constructor (_model) {
|
||||||
|
this._model = _model;
|
||||||
|
}
|
||||||
|
*val () {
|
||||||
|
var transaction = yield "transaction";
|
||||||
|
var model = transaction.getOperation(this._model);
|
||||||
|
if (arguments.length === 0) {
|
||||||
|
throw new Error("Implement this case!");
|
||||||
|
} else if (arguments.length === 1) {
|
||||||
|
return yield* this.Struct.Map.get.call(transaction, model, arguments[0]);
|
||||||
|
} else {
|
||||||
|
return yield* this.Struct.Map.set.call(transaction, model, arguments[0], arguments[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Y.Map = function* YMap(){
|
||||||
|
var model = yield* this.Struct.map.create.call(this);
|
||||||
|
return new Map(model);
|
||||||
|
};
|
||||||
|
Y.Map.Create = Map;
|
||||||
|
})();
|
30
src/y.spec.js
Normal file
30
src/y.spec.js
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/* @flow */
|
||||||
|
/*eslint-env browser,jasmine */
|
||||||
|
|
||||||
|
describe("Yjs (basic)", function(){
|
||||||
|
beforeEach(function(){
|
||||||
|
this.users = [];
|
||||||
|
for (var i = 0; i < 5; i++) {
|
||||||
|
this.users.push(new Y({
|
||||||
|
db: {
|
||||||
|
name: "Memory"
|
||||||
|
},
|
||||||
|
connector: {
|
||||||
|
name: "Test"
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
afterEach(function(){
|
||||||
|
for (var y of this.users) {
|
||||||
|
y.destroy();
|
||||||
|
}
|
||||||
|
this.users = [];
|
||||||
|
});
|
||||||
|
it("can List.insert and get value from the other user", function(done){
|
||||||
|
this.users[0].val("name", 1);
|
||||||
|
this.users[0].connector.whenSynced(function(){
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user