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"}], "camelcase": [1, {"properties": "never"}],
"no-underscore-dangle": 0, "no-underscore-dangle": 0,
"no-constant-condition": 0, "no-constant-condition": 0,
"no-empty": 0 "no-empty": 0,
"new-cap": [2, { "capIsNewExceptions": ["List"] }],
}, },
"parser": "babel-eslint", "parser": "babel-eslint",
"globals": { "globals": {

View File

@ -3,6 +3,10 @@ class AbstractTransaction { //eslint-disable-line no-unused-vars
constructor (store : OperationStore) { constructor (store : OperationStore) {
this.store = store; 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. // returns false if operation is not expected.
*addOperation (op) { *addOperation (op) {
var state = yield* this.getState(op.id[0]); var state = yield* this.getState(op.id[0]);

View File

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

View File

@ -7,20 +7,23 @@
this._model = _model; this._model = _model;
} }
*val (pos) { *val (pos) {
var t = yield "transaction";
if (pos != null) { 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; return o ? o.content : null;
} else { } 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) { *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(){ 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); return new List(model);
}; };
Y.List.Create = List; Y.List.Create = List;

View File

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

View File

@ -109,5 +109,21 @@ describe("Yjs (basic)", function(){
u.transact(transaction); 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