Skip to content

Commit

Permalink
add compatibility layer with previous MapSchema version. add deleteBy…
Browse files Browse the repository at this point in the history
…Index for DELETE operations. #62 #75
  • Loading branch information
endel committed Jun 13, 2020
1 parent 6c5fd69 commit a2a6378
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 16 deletions.
45 changes: 33 additions & 12 deletions src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ export abstract class Schema {
//
// TODO: remove from $root.refs
//
console.log("DELETE OPERATION!", {
ref, type, _field, fieldIndex,
});
ref['deleteByIndex'](fieldIndex);

value = null;
hasChange = true;
Expand Down Expand Up @@ -286,22 +290,35 @@ export abstract class Schema {
});
}

if (value['$changes']) {
value['$changes'].setParent(
changeTree.ref,
changeTree.root,
fieldIndex,
);
}
if (value) {
if (value['$changes']) {
value['$changes'].setParent(
changeTree.ref,
changeTree.root,
fieldIndex,
);
}

if (ref instanceof Schema) {
ref[_field] = value;
if (ref instanceof Schema) {
ref[field] = value;

} else if (ref instanceof MapSchema) {
const key = ref['$indexes'].get(field);
ref.set(key, value);
//
// FIXME: use `_field` instead of `field`.
//
// `field` is going to use the setter of the PropertyDescriptor
// and create a proxy for array/map. This is only useful for
// backwards-compatibility with @colyseus/[email protected]
//
// // ref[_field] = value;

} else if (ref instanceof MapSchema) {
const key = ref['$indexes'].get(field);
ref.set(key, value);
}
}



}

this._triggerChanges(changes);
Expand Down Expand Up @@ -670,6 +687,10 @@ export abstract class Schema {
return this[this._fieldsByIndex[index]];
}

protected deleteByIndex(index: number) {
delete this[this._fieldsByIndex[index]];
}

private tryEncodeTypeId (bytes: number[], type: typeof Schema, targetType: typeof Schema) {
if (type._typeid !== targetType._typeid) {
encode.uint8(bytes, TYPE_ID);
Expand Down
32 changes: 30 additions & 2 deletions src/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,32 @@ export function type (type: DefinitionType, context: Context = globalContext): P
value = new MapSchema(value);
}

// if (isArray || isMap) {
if (isArray) {
//
// compatibility with @colyseus/schema 0.5.x
// allow accessing map["key"]
//
if (isMap) {
value = new Proxy(value, {
get: (obj, prop) => {
if (typeof (obj[prop]) === "undefined") {
return obj.get(prop);

} else {
return obj[prop];
}
},

set: (obj, prop, setValue) => {
return obj.set(prop, setValue);
},

deleteProperty: (obj, prop) => {
obj.delete(prop);
return true;
},
});

} else if (isArray) {
value = new Proxy(value, {
get: (obj, prop) => obj[prop],
set: (obj, prop, setValue) => {
Expand Down Expand Up @@ -259,6 +283,10 @@ export function type (type: DefinitionType, context: Context = globalContext): P
this[fieldCached] = value;
this.$changes.change(field);

//
// call setParent() recursively for this and its child
// structures.
//
if (value['$changes']) {
(value['$changes'] as ChangeTree).setParent(
this,
Expand Down
2 changes: 2 additions & 0 deletions src/changes/ChangeTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ export class ChangeTree {
if (this.ref[fieldName] instanceof Schema) {
this.root.delete(this.ref[fieldName].$changes);
}

this._root?.dirty(this);
}

discard() {
Expand Down
9 changes: 7 additions & 2 deletions src/types/MapSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ export class MapSchema<V=any> {
? value['$changes'].refId
: this.$refId++

// console.log("ROOT?", value['$changes'].root);
console.log(`MapSchema#set() =>`, { isRef, key, index, value });
// console.log(`MapSchema#set() =>`, { isRef, key, index, value });

this.$changes.indexes[key] = index;

Expand Down Expand Up @@ -166,6 +165,12 @@ export class MapSchema<V=any> {
return this.$items.get(this.$indexes.get(index));
}

protected deleteByIndex(index: number) {
const key = this.$indexes.get(index);
this.$items.delete(key);
this.$indexes.delete(index);
}

//
// Decoding utilities
//
Expand Down
38 changes: 38 additions & 0 deletions test/NextIterationTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,44 @@ describe("Next Iteration", () => {
assert.deepEqual(decoded.map.get("two"), 22);
});

it.only("MapSchema should be backwards-compatible with @colyseus/schema 0.5", () => {
class State extends Schema {
@type({ map: "number" })
map = new MapSchema<number>();
}

const state = new State();
state.map["one"] = 1;
state.map["two"] = 2;

const decoded = new State();
decoded.decode(state.encode());

console.log("DECODED =>", util.inspect(decoded.toJSON(), true, Infinity));

console.log("\n\nLETS ASSERT!\n\n");
assert.equal(2, decoded.map.size);
assert.equal(1, decoded.map.get("one"));
assert.equal(2, decoded.map.get("two"));

console.log("MAP =>", decoded.map);
assert.equal(1, decoded.map["one"]);
assert.equal(2, decoded.map["two"]);

delete state.map['one'];

const encoded = state.encode();

console.log("\n\nLETS DECODE!!", encoded.length, encoded);
decoded.decode(encoded);

console.log("DECODED =>", util.inspect(decoded.toJSON(), true, Infinity));

assert.equal(1, decoded.map.size);
assert.equal(undefined, decoded.map["one"]);
assert.equal(2, decoded.map["two"]);
});

it("add and modify a filtered primitive map item", () => {
class State extends Schema {
@filterChildren(function(client, key, value, root) {
Expand Down

0 comments on commit a2a6378

Please sign in to comment.