From 8e6ef6419168f238b53b134992450661f240db74 Mon Sep 17 00:00:00 2001 From: Endel Dreyer Date: Sun, 7 Jun 2020 20:14:48 -0300 Subject: [PATCH] wip restructuring filters to support maps #75 #61 #66 --- src/Schema.ts | 13 +++----- src/annotations.ts | 2 ++ src/changes/ChangeTree.ts | 9 +----- test/NextIterationTest.ts | 65 +++++++++++++++++++++++++++++++++------ 4 files changed, 63 insertions(+), 26 deletions(-) diff --git a/src/Schema.ts b/src/Schema.ts index 45d9228c..4c0dd954 100644 --- a/src/Schema.ts +++ b/src/Schema.ts @@ -583,10 +583,8 @@ 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); @@ -594,17 +592,14 @@ export abstract class Schema { 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; } diff --git a/src/annotations.ts b/src/annotations.ts index 14fed65c..92a98fe4 100644 --- a/src/annotations.ts +++ b/src/annotations.ts @@ -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 || {}); diff --git a/src/changes/ChangeTree.ts b/src/changes/ChangeTree.ts index 47df19f1..5c23bd75 100644 --- a/src/changes/ChangeTree.ts +++ b/src/changes/ChangeTree.ts @@ -109,7 +109,7 @@ export class ChangeTree { } getType(index: number) { - + throw new Error("not implemented"); } // @@ -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; diff --git a/test/NextIterationTest.ts b/test/NextIterationTest.ts index c40f92d2..e92be5c9 100644 --- a/test/NextIterationTest.ts +++ b/test/NextIterationTest.ts @@ -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(); @@ -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(); + } + + 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;