Skip to content

Commit

Permalink
wip restructuring filters to support maps #75 #61 #66
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Jun 7, 2020
1 parent cdf4d0e commit 8e6ef64
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 26 deletions.
13 changes: 4 additions & 9 deletions src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,28 +583,23 @@ export abstract class Schema {

applyFilters(encodedBytes: number[], client: Client, root = this, encodeAll: boolean = false) {
let filteredBytes: number[] = [];
const enqueuedStrutures: Schema[] = [];

const schema = this._schema;
const fieldsByIndex = this._fieldsByIndex;
const enqueuedStrutures: Schema[] = [];
const filters = this._filters;

encode.uint8(filteredBytes, SWITCH_TO_STRUCTURE);
encode.number(filteredBytes, this.$changes.refId);

this.$changes.changes.forEach((change, fieldIndex) => {
const cache = this.$changes.caches[fieldIndex];
const field = fieldsByIndex[fieldIndex];

const type = schema[field];
const value = this.$changes.getValue(fieldIndex);
const filter = (filters && filters[field]);
const _field = `_${field}`;
const value = this[_field];

console.log({ field, filter, change, cache });
console.log({ fieldIndex, filter, change, cache });

if (filter && !filter.call(this, client, value, root)) {
console.log("SKIP", field, "AT", fieldIndex)
console.log("SKIPPING", fieldIndex)
return;
}

Expand Down
2 changes: 2 additions & 0 deletions src/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export function type (type: DefinitionType, context: Context = globalContext): P
if (!context.has(constructor)) {
context.add(constructor);

// TODO: move all this stuff to its own holder object.

// support inheritance
constructor._schema = Object.assign({}, constructor._schema || {});
constructor._indexes = Object.assign({}, constructor._indexes || {});
Expand Down
9 changes: 1 addition & 8 deletions src/changes/ChangeTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class ChangeTree {
}

getType(index: number) {

throw new Error("not implemented");
}

//
Expand All @@ -119,13 +119,6 @@ export class ChangeTree {
return this.ref['getByIndex'](index);
}

//
// used during `.decode()`
//
setValue(index: number, value: any, dynamicIndex: string | number) {
this.ref
}

delete(fieldName: string | number) {
// const fieldIndex = this.indexes[fieldName];
// const field = (typeof (fieldIndex) === "number") ? fieldIndex : fieldName;
Expand Down
65 changes: 56 additions & 9 deletions test/NextIterationTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("Next Iteration", () => {
assert.deepEqual(decoded.arr, ['one', 'twotwo', 'three']);
});

it("add and modify a map item", () => {
xit("add and modify a map item", () => {
class State extends Schema {
@type({ map: "number" })
map = new Map<string, number>();
Expand All @@ -41,28 +41,75 @@ describe("Next Iteration", () => {
const decoded = new State();

let encoded = state.encode();
console.log("ENCODED (FULL), bytes =>", encoded.length, encoded);

console.log("\n\nWILL DECODE:\n");
decoded.decode(encoded);

assert.deepEqual(decoded.map.get("one"), 1);
assert.deepEqual(decoded.map.get("two"), 2);
assert.deepEqual(decoded.map.get("three"), 3);
console.log("DECODED =>", decoded.toJSON());

state.map.set("two", 22);

console.log("\n\nWILL ENCODE PATCH:\n");
encoded = state.encode();
console.log("ENCODED (PATCH), bytes =>", encoded.length, encoded);

console.log("\n\nWILL DECODE:\n");
decoded.decode(encoded);

assert.deepEqual(decoded.map.get("two"), 22);
});

it("add and modify a filtered map item", () => {
class State extends Schema {
@filter(function(client, value, root) {
return client.sessionId === value;
})
@type({ map: "number" })
map = new Map<string, number>();
}

const state = new State();
state.map.set("one", 1);
state.map.set("two", 2);
state.map.set("three", 3);

const client1 = { sessionId: "one" };
const client2 = { sessionId: "two" };
const client3 = { sessionId: "three" };

const decoded1 = new State();
const decoded2 = new State();
const decoded3 = new State();

let encoded = state.encode(undefined, undefined, undefined, true);

console.log("ENCODED (FULL)", encoded.length, encoded);

let encoded1 = state.applyFilters(encoded, client1);
console.log("ENCODED (CLIENT 1)", encoded1.length, encoded1);

let encoded2 = state.applyFilters(encoded, client2);
console.log("ENCODED (CLIENT 2)", encoded2.length, encoded2);

let encoded3 = state.applyFilters(encoded, client3);
console.log("ENCODED (CLIENT 3)", encoded3.length, encoded3);

decoded1.decode(encoded1);
assert.equal(decoded1.map.size, 1);
assert.equal(decoded1.map.get("one"), 1);

decoded2.decode(encoded2);
assert.equal(decoded2.map.size, 1);
assert.equal(decoded2.map.get("two"), 2);

decoded3.decode(encoded3);
assert.equal(decoded3.map.size, 1);
assert.equal(decoded3.map.get("three"), 3);

// state.map.set("two", 22);

// encoded = state.encode();
// decoded1.decode(encoded);

assert.deepEqual(decoded1.map.get("two"), 22);
});

xit("should encode string", () => {
class Item extends Schema {
@type("number") damage: number;
Expand Down

0 comments on commit 8e6ef64

Please sign in to comment.