custom types work. Now I need to re-implement the test case from 0.5

This commit is contained in:
Kevin Jahns 2015-07-06 18:37:54 +02:00
parent 79ec71d559
commit 9b6183ea70
8 changed files with 86 additions and 39 deletions

View File

@ -7,7 +7,8 @@
"camelcase": [1, {"properties": "never"}],
"no-underscore-dangle": 0,
"no-constant-condition": 0,
"no-empty": 0
"no-empty": 0,
"new-cap": [2, { "capIsNewExceptions": ["List"] }],
},
"parser": "babel-eslint",
"globals": {

View File

@ -3,6 +3,10 @@ class AbstractTransaction { //eslint-disable-line no-unused-vars
constructor (store : OperationStore) {
this.store = store;
}
*getType (id) {
var op = yield* this.getOperation(id);
return new Y[op.type].Create(op);
}
// returns false if operation is not expected.
*addOperation (op) {
var state = yield* this.getState(op.id[0]);

View File

@ -20,12 +20,11 @@ type Insert = {
};
function compareIds(id1, id2) {
if (id1 == null) {
if (id2 == null) {
return true;
} else {
return false;
}
if (id1 == null && id2 == null) {
return true;
}
if (id1 == null || id2 == null) {
return false;
}
if (id1[0] === id2[0] && id1[1] === id2[1]) {
return true;
@ -47,6 +46,7 @@ var Struct = {
type: "update",
ops: [Struct[op.struct].encode(op)]
});
return op;
}
},
Insert: {
@ -126,12 +126,15 @@ var Struct = {
if(op.right == null && op.left == null) {
ids.push(op.parent);
}
if (op.opContent != null) {
ids.push(op.opContent);
}
return ids;
},
getDistanceToOrigin: function *(op){
var d = 0;
var o = yield* this.getOperation(op.left);
while (op.origin !== (o ? o.id : null)) {
while (!compareIds(op.origin, (o ? o.id : null))) {
d++;
o = yield* this.getOperation(o.left);
}
@ -265,10 +268,12 @@ var Struct = {
encode: function(op){
return {
struct: "List",
id: op.id
id: op.id,
type: op.type
};
},
requiredOps: function(op){
requiredOps: function(){
/*
var ids = [];
if (op.start != null) {
ids.push(op.start);
@ -277,6 +282,8 @@ var Struct = {
ids.push(op.end);
}
return ids;
*/
return [];
},
execute: function* (op) {
if ((yield* this.addOperation(op)) === false) {
@ -284,12 +291,15 @@ var Struct = {
}
},
ref: function* (op : Op, pos : number) : Insert {
var o = op.start;
while ( pos !== 0 || o != null) {
o = (yield* this.getOperation(o)).right;
if (op.start == null) {
return null;
}
var o = yield* this.getOperation(op.start);
while ( pos !== 0 && o.right != null) {
o = (yield* this.getOperation(o.right));
pos--;
}
return (o == null) ? null : yield* this.getOperation(o);
return o;
},
map: function* (o : Op, f : Function) : Array<any> {
o = o.start;
@ -302,16 +312,17 @@ var Struct = {
return res;
},
insert: function* (op, pos : number, contents : Array<any>) {
var o = yield* Struct.List.ref.call(this, op, pos);
var or = yield* this.getOperation(o.right);
var ref = yield* Struct.List.ref.call(this, op, pos);
var right = ref != null ? ref.id : null;
var left = ref != null ? ref.left : null;
for (var key in contents) {
var insert = {
left: o,
right: or,
left: left,
right: right,
content: contents[key],
parent: op
parent: op.id
};
o = yield* Struct.Insert.create.call(this, insert);
left = (yield* Struct.Insert.create.call(this, insert)).id;
}
}
},
@ -329,15 +340,19 @@ var Struct = {
encode: function(op){
return {
struct: "Map",
type: op.type,
id: op.id
};
},
requiredOps: function(op){
requiredOps: function(){
/*
var ids = [];
for (var end in op.map) {
ids.push(op.map[end]);
}
return ids;
*/
return [];
},
execute: function* (op) {
if ((yield* this.addOperation(op)) === false) {
@ -346,16 +361,23 @@ var Struct = {
},
get: function* (op, name) {
var res = yield* this.getOperation(op.map[name]);
return (res != null) ? res.content : void 0;
return (res == null) ? void 0 : (res.opContent == null
? res.content : yield* this.getType(res.opContent));
},
set: function* (op, name, value) {
var insert = {
left: null,
right: op.map[name] || null,
content: value,
parent: op.id,
parentSub: name
};
var oid;
if ( value != null && value._model != null
&& (oid = value._model.id) != null && oid.length === 2) {
insert.opContent = oid;
} else {
insert.content = value;
}
yield* Struct.Insert.create.call(this, insert);
}
}

View File

@ -7,20 +7,23 @@
this._model = _model;
}
*val (pos) {
var t = yield "transaction";
if (pos != null) {
var o = yield* this.Struct.List.ref(this._model, pos);
var o = yield* Y.Struct.List.ref.call(t, this._model, pos);
return o ? o.content : null;
} else {
return yield* this.Struct.List.map(this._model, function(c){return c; });
return yield* Y.Struct.List.map.call(t, this._model, function(c){return c; });
}
}
*insert (pos, contents) {
yield* this.Struct.List.insert(pos, contents);
var t = yield "transaction";
yield* Y.Struct.List.insert.call(t, this._model, pos, contents);
}
}
Y.List = function* YList(){
var model = yield* this.Struct.List.create();
var t = yield "transaction";
var model = yield* Y.Struct.List.create.call(t, {type: "List"});
return new List(model);
};
Y.List.Create = List;

View File

@ -5,21 +5,22 @@
this._model = _model;
}
*val () {
var transaction = yield "transaction";
var model = yield* transaction.getOperation(this._model);
if (arguments.length === 0) {
throw new Error("Implement this case!");
} else if (arguments.length === 1) {
return yield* Y.Struct.Map.get.call(transaction, model, arguments[0]);
var t = yield "transaction";
var model = yield* t.getOperation(this._model);
if (arguments.length === 1) {
return yield* Y.Struct.Map.get.call(t, model, arguments[0]);
} else if (arguments.length === 2) {
return yield* Y.Struct.Map.set.call(t, model, arguments[0], arguments[1]);
} else {
return yield* Y.Struct.Map.set.call(transaction, model, arguments[0], arguments[1]);
throw new Error("Implement this case!");
}
}
}
Y.Map = function* YMap(){
var t = yield "transaction";
if (this instanceof Y.AbstractOperationStore) {
var model = yield* Y.Struct.map.create.call(this);
var model = yield* Y.Struct.map.create.call(t, {type: "Map"});
return new Map(model);
} else {
throw new Error("Don't use `new` to create this type!");

View File

@ -109,5 +109,21 @@ describe("Yjs (basic)", function(){
u.transact(transaction);
}
});
it("can create a List type", function(){
var y = this.users[0];
y.transact(function*(root){
var list = yield* Y.List();
yield* root.val("list", list);
yield* list.insert(0, [1, 2, 3, 4]);
expect(yield* root.val("list")).not.toBeUndefined();
});
y.connector.flushAll();
function* transaction (root) {
var list = yield* root.val("list");
expect(yield* list.val()).toEqual([1, 2, 3, 4]);
}
for (var u of this.users) {
u.transact(transaction);
}
});
});

4
y.js

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long