@colyseus/schema 4.0.1 → 4.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{lib → build}/Metadata.d.ts +2 -2
- package/{lib → build}/Reflection.d.ts +13 -13
- package/{lib → build}/Schema.d.ts +10 -10
- package/{lib → build}/annotations.d.ts +10 -10
- package/build/codegen/languages/cpp.d.ts +3 -0
- package/build/codegen/languages/csharp.d.ts +4 -0
- package/build/codegen/languages/haxe.d.ts +3 -0
- package/{lib → build}/codegen/languages/java.d.ts +2 -2
- package/build/codegen/languages/js.d.ts +3 -0
- package/build/codegen/languages/lua.d.ts +3 -0
- package/build/codegen/languages/ts.d.ts +3 -0
- package/{lib → build}/codegen/parser.d.ts +1 -1
- package/{lib → build}/decoder/DecodeOperation.d.ts +5 -5
- package/{lib → build}/decoder/Decoder.d.ts +7 -7
- package/{lib → build}/decoder/ReferenceTracker.d.ts +1 -1
- package/{lib → build}/decoder/strategy/Callbacks.d.ts +11 -11
- package/{lib → build}/decoder/strategy/RawChanges.d.ts +2 -2
- package/{lib → build}/decoder/strategy/getDecoderStateCallbacks.d.ts +3 -3
- package/{lib → build}/encoder/ChangeTree.d.ts +11 -11
- package/{lib → build}/encoder/EncodeOperation.d.ts +5 -5
- package/{lib → build}/encoder/Encoder.d.ts +6 -6
- package/{lib → build}/encoder/Root.d.ts +2 -2
- package/{lib → build}/encoder/StateView.d.ts +1 -1
- package/{lib → build}/encoding/assert.d.ts +6 -6
- package/{lib → build}/encoding/decode.d.ts +1 -1
- package/{lib → build}/encoding/encode.d.ts +1 -1
- package/build/{cjs/index.js → index.cjs} +1 -1
- package/build/index.cjs.map +1 -0
- package/build/index.d.ts +32 -0
- package/build/index.mjs.map +1 -0
- package/{lib → build}/types/HelperTypes.d.ts +6 -6
- package/{lib → build}/types/TypeContext.d.ts +1 -1
- package/{lib → build}/types/custom/ArraySchema.d.ts +8 -8
- package/{lib → build}/types/custom/CollectionSchema.d.ts +7 -7
- package/{lib → build}/types/custom/MapSchema.d.ts +7 -7
- package/{lib → build}/types/custom/SetSchema.d.ts +7 -7
- package/{lib → build}/types/registry.d.ts +2 -2
- package/{lib → build}/utils.d.ts +1 -1
- package/package.json +15 -14
- package/src/Metadata.ts +5 -5
- package/src/Reflection.ts +7 -7
- package/src/Schema.ts +11 -11
- package/src/annotations.ts +13 -13
- package/src/codegen/api.ts +10 -3
- package/src/codegen/cli.ts +2 -2
- package/src/codegen/languages/cpp.ts +2 -2
- package/src/codegen/languages/csharp.ts +3 -3
- package/src/codegen/languages/haxe.ts +2 -2
- package/src/codegen/languages/java.ts +2 -2
- package/src/codegen/languages/js.ts +2 -2
- package/src/codegen/languages/lua.ts +2 -2
- package/src/codegen/languages/ts.ts +2 -2
- package/src/codegen/parser.ts +7 -2
- package/src/codegen/types.ts +5 -0
- package/src/decoder/DecodeOperation.ts +14 -14
- package/src/decoder/Decoder.ts +11 -11
- package/src/decoder/ReferenceTracker.ts +8 -8
- package/src/decoder/strategy/Callbacks.ts +12 -12
- package/src/decoder/strategy/RawChanges.ts +2 -2
- package/src/decoder/strategy/getDecoderStateCallbacks.ts +10 -10
- package/src/encoder/ChangeTree.ts +13 -13
- package/src/encoder/EncodeOperation.ts +9 -9
- package/src/encoder/Encoder.ts +10 -10
- package/src/encoder/Root.ts +4 -4
- package/src/encoder/StateView.ts +7 -7
- package/src/encoding/assert.ts +6 -6
- package/src/encoding/decode.ts +1 -1
- package/src/encoding/encode.ts +1 -1
- package/src/index.ts +27 -27
- package/src/types/HelperTypes.ts +6 -6
- package/src/types/TypeContext.ts +3 -3
- package/src/types/custom/ArraySchema.ts +11 -11
- package/src/types/custom/CollectionSchema.ts +9 -9
- package/src/types/custom/MapSchema.ts +10 -10
- package/src/types/custom/SetSchema.ts +9 -9
- package/src/types/registry.ts +3 -3
- package/src/utils.ts +3 -3
- package/src/v3_bench.ts +1 -1
- package/build/cjs/index.js.map +0 -1
- package/build/esm/index.mjs.map +0 -1
- package/lib/Metadata.js +0 -260
- package/lib/Metadata.js.map +0 -1
- package/lib/Reflection.js +0 -182
- package/lib/Reflection.js.map +0 -1
- package/lib/Schema.js +0 -356
- package/lib/Schema.js.map +0 -1
- package/lib/annotations.js +0 -484
- package/lib/annotations.js.map +0 -1
- package/lib/bench_encode.d.ts +0 -1
- package/lib/bench_encode.js +0 -126
- package/lib/bench_encode.js.map +0 -1
- package/lib/codegen/api.js +0 -87
- package/lib/codegen/api.js.map +0 -1
- package/lib/codegen/argv.js +0 -41
- package/lib/codegen/argv.js.map +0 -1
- package/lib/codegen/cli.js +0 -63
- package/lib/codegen/cli.js.map +0 -1
- package/lib/codegen/languages/cpp.d.ts +0 -3
- package/lib/codegen/languages/cpp.js +0 -261
- package/lib/codegen/languages/cpp.js.map +0 -1
- package/lib/codegen/languages/csharp.d.ts +0 -4
- package/lib/codegen/languages/csharp.js +0 -157
- package/lib/codegen/languages/csharp.js.map +0 -1
- package/lib/codegen/languages/haxe.d.ts +0 -3
- package/lib/codegen/languages/haxe.js +0 -103
- package/lib/codegen/languages/haxe.js.map +0 -1
- package/lib/codegen/languages/java.js +0 -103
- package/lib/codegen/languages/java.js.map +0 -1
- package/lib/codegen/languages/js.d.ts +0 -3
- package/lib/codegen/languages/js.js +0 -104
- package/lib/codegen/languages/js.js.map +0 -1
- package/lib/codegen/languages/lua.d.ts +0 -3
- package/lib/codegen/languages/lua.js +0 -106
- package/lib/codegen/languages/lua.js.map +0 -1
- package/lib/codegen/languages/ts.d.ts +0 -3
- package/lib/codegen/languages/ts.js +0 -119
- package/lib/codegen/languages/ts.js.map +0 -1
- package/lib/codegen/parser.js +0 -364
- package/lib/codegen/parser.js.map +0 -1
- package/lib/codegen/types.js +0 -181
- package/lib/codegen/types.js.map +0 -1
- package/lib/decoder/DecodeOperation.js +0 -255
- package/lib/decoder/DecodeOperation.js.map +0 -1
- package/lib/decoder/Decoder.js +0 -118
- package/lib/decoder/Decoder.js.map +0 -1
- package/lib/decoder/ReferenceTracker.js +0 -139
- package/lib/decoder/ReferenceTracker.js.map +0 -1
- package/lib/decoder/strategy/Callbacks.js +0 -340
- package/lib/decoder/strategy/Callbacks.js.map +0 -1
- package/lib/decoder/strategy/RawChanges.js +0 -7
- package/lib/decoder/strategy/RawChanges.js.map +0 -1
- package/lib/decoder/strategy/getDecoderStateCallbacks.js +0 -277
- package/lib/decoder/strategy/getDecoderStateCallbacks.js.map +0 -1
- package/lib/encoder/ChangeTree.js +0 -542
- package/lib/encoder/ChangeTree.js.map +0 -1
- package/lib/encoder/EncodeOperation.js +0 -139
- package/lib/encoder/EncodeOperation.js.map +0 -1
- package/lib/encoder/Encoder.js +0 -208
- package/lib/encoder/Encoder.js.map +0 -1
- package/lib/encoder/Root.js +0 -233
- package/lib/encoder/Root.js.map +0 -1
- package/lib/encoder/StateView.js +0 -284
- package/lib/encoder/StateView.js.map +0 -1
- package/lib/encoding/assert.js +0 -55
- package/lib/encoding/assert.js.map +0 -1
- package/lib/encoding/decode.js +0 -221
- package/lib/encoding/decode.js.map +0 -1
- package/lib/encoding/encode.js +0 -282
- package/lib/encoding/encode.js.map +0 -1
- package/lib/encoding/spec.js +0 -29
- package/lib/encoding/spec.js.map +0 -1
- package/lib/index.d.ts +0 -32
- package/lib/index.js +0 -81
- package/lib/index.js.map +0 -1
- package/lib/symbol.shim.js +0 -4
- package/lib/symbol.shim.js.map +0 -1
- package/lib/types/HelperTypes.js +0 -3
- package/lib/types/HelperTypes.js.map +0 -1
- package/lib/types/TypeContext.js +0 -147
- package/lib/types/TypeContext.js.map +0 -1
- package/lib/types/custom/ArraySchema.js +0 -737
- package/lib/types/custom/ArraySchema.js.map +0 -1
- package/lib/types/custom/CollectionSchema.js +0 -174
- package/lib/types/custom/CollectionSchema.js.map +0 -1
- package/lib/types/custom/MapSchema.js +0 -226
- package/lib/types/custom/MapSchema.js.map +0 -1
- package/lib/types/custom/SetSchema.js +0 -182
- package/lib/types/custom/SetSchema.js.map +0 -1
- package/lib/types/registry.js +0 -37
- package/lib/types/registry.js.map +0 -1
- package/lib/types/symbols.js +0 -37
- package/lib/types/symbols.js.map +0 -1
- package/lib/types/utils.js +0 -16
- package/lib/types/utils.js.map +0 -1
- package/lib/utils.js +0 -54
- package/lib/utils.js.map +0 -1
- package/lib/v3_bench.js +0 -130
- package/lib/v3_bench.js.map +0 -1
- package/src/bench_encode.ts +0 -64
- /package/{lib → build}/codegen/api.d.ts +0 -0
- /package/{lib → build}/codegen/argv.d.ts +0 -0
- /package/{lib → build}/codegen/cli.d.ts +0 -0
- /package/{lib → build}/codegen/types.d.ts +0 -0
- /package/{lib → build}/encoding/spec.d.ts +0 -0
- /package/build/{umd/index.js → index.js} +0 -0
- /package/build/{esm/index.mjs → index.mjs} +0 -0
- /package/{lib → build}/symbol.shim.d.ts +0 -0
- /package/{lib → build}/types/symbols.d.ts +0 -0
- /package/{lib → build}/types/utils.d.ts +0 -0
- /package/{lib → build}/v3_bench.d.ts +0 -0
package/lib/decoder/Decoder.js
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Decoder = void 0;
|
|
4
|
-
const TypeContext_1 = require("../types/TypeContext");
|
|
5
|
-
const symbols_1 = require("../types/symbols");
|
|
6
|
-
const decode_1 = require("../encoding/decode");
|
|
7
|
-
const spec_1 = require("../encoding/spec");
|
|
8
|
-
const ReferenceTracker_1 = require("./ReferenceTracker");
|
|
9
|
-
const DecodeOperation_1 = require("./DecodeOperation");
|
|
10
|
-
class Decoder {
|
|
11
|
-
constructor(root, context) {
|
|
12
|
-
this.currentRefId = 0;
|
|
13
|
-
this.setState(root);
|
|
14
|
-
this.context = context || new TypeContext_1.TypeContext(root.constructor);
|
|
15
|
-
// console.log(">>>>>>>>>>>>>>>> Decoder types");
|
|
16
|
-
// this.context.schemas.forEach((id, schema) => {
|
|
17
|
-
// console.log("type:", id, schema.name, Object.keys(schema[Symbol.metadata]));
|
|
18
|
-
// });
|
|
19
|
-
}
|
|
20
|
-
setState(root) {
|
|
21
|
-
this.state = root;
|
|
22
|
-
this.root = new ReferenceTracker_1.ReferenceTracker();
|
|
23
|
-
this.root.addRef(0, root);
|
|
24
|
-
}
|
|
25
|
-
decode(bytes, it = { offset: 0 }, ref = this.state) {
|
|
26
|
-
const allChanges = [];
|
|
27
|
-
const $root = this.root;
|
|
28
|
-
const totalBytes = bytes.byteLength;
|
|
29
|
-
let decoder = ref['constructor'][symbols_1.$decoder];
|
|
30
|
-
this.currentRefId = 0;
|
|
31
|
-
while (it.offset < totalBytes) {
|
|
32
|
-
//
|
|
33
|
-
// Peek ahead, check if it's a switch to a different structure
|
|
34
|
-
//
|
|
35
|
-
if (bytes[it.offset] == spec_1.SWITCH_TO_STRUCTURE) {
|
|
36
|
-
it.offset++;
|
|
37
|
-
ref[symbols_1.$onDecodeEnd]?.();
|
|
38
|
-
const nextRefId = decode_1.decode.number(bytes, it);
|
|
39
|
-
const nextRef = $root.refs.get(nextRefId);
|
|
40
|
-
//
|
|
41
|
-
// Trying to access a reference that haven't been decoded yet.
|
|
42
|
-
//
|
|
43
|
-
if (!nextRef) {
|
|
44
|
-
// throw new Error(`"refId" not found: ${nextRefId}`);
|
|
45
|
-
console.error(`"refId" not found: ${nextRefId}`, { previousRef: ref, previousRefId: this.currentRefId });
|
|
46
|
-
console.warn("Please report this issue to the developers.");
|
|
47
|
-
this.skipCurrentStructure(bytes, it, totalBytes);
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
ref = nextRef;
|
|
51
|
-
decoder = ref.constructor[symbols_1.$decoder];
|
|
52
|
-
this.currentRefId = nextRefId;
|
|
53
|
-
}
|
|
54
|
-
continue;
|
|
55
|
-
}
|
|
56
|
-
const result = decoder(this, bytes, it, ref, allChanges);
|
|
57
|
-
if (result === DecodeOperation_1.DEFINITION_MISMATCH) {
|
|
58
|
-
console.warn("@colyseus/schema: definition mismatch");
|
|
59
|
-
this.skipCurrentStructure(bytes, it, totalBytes);
|
|
60
|
-
continue;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
// FIXME: DRY with SWITCH_TO_STRUCTURE block.
|
|
64
|
-
ref[symbols_1.$onDecodeEnd]?.();
|
|
65
|
-
// trigger changes
|
|
66
|
-
this.triggerChanges?.(allChanges);
|
|
67
|
-
// drop references of unused schemas
|
|
68
|
-
$root.garbageCollectDeletedRefs();
|
|
69
|
-
return allChanges;
|
|
70
|
-
}
|
|
71
|
-
skipCurrentStructure(bytes, it, totalBytes) {
|
|
72
|
-
//
|
|
73
|
-
// keep skipping next bytes until reaches a known structure
|
|
74
|
-
// by local decoder.
|
|
75
|
-
//
|
|
76
|
-
const nextIterator = { offset: it.offset };
|
|
77
|
-
while (it.offset < totalBytes) {
|
|
78
|
-
if (bytes[it.offset] === spec_1.SWITCH_TO_STRUCTURE) {
|
|
79
|
-
nextIterator.offset = it.offset + 1;
|
|
80
|
-
if (this.root.refs.has(decode_1.decode.number(bytes, nextIterator))) {
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
it.offset++;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
getInstanceType(bytes, it, defaultType) {
|
|
88
|
-
let type;
|
|
89
|
-
if (bytes[it.offset] === spec_1.TYPE_ID) {
|
|
90
|
-
it.offset++;
|
|
91
|
-
const type_id = decode_1.decode.number(bytes, it);
|
|
92
|
-
type = this.context.get(type_id);
|
|
93
|
-
}
|
|
94
|
-
return type || defaultType;
|
|
95
|
-
}
|
|
96
|
-
createInstanceOfType(type) {
|
|
97
|
-
return new type();
|
|
98
|
-
}
|
|
99
|
-
removeChildRefs(ref, allChanges) {
|
|
100
|
-
const needRemoveRef = typeof (ref[symbols_1.$childType]) !== "string";
|
|
101
|
-
const refId = ref[symbols_1.$refId];
|
|
102
|
-
ref.forEach((value, key) => {
|
|
103
|
-
allChanges.push({
|
|
104
|
-
ref: ref,
|
|
105
|
-
refId,
|
|
106
|
-
op: spec_1.OPERATION.DELETE,
|
|
107
|
-
field: key,
|
|
108
|
-
value: undefined,
|
|
109
|
-
previousValue: value
|
|
110
|
-
});
|
|
111
|
-
if (needRemoveRef) {
|
|
112
|
-
this.root.removeRef(value[symbols_1.$refId]);
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
exports.Decoder = Decoder;
|
|
118
|
-
//# sourceMappingURL=Decoder.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Decoder.js","sourceRoot":"","sources":["../../src/decoder/Decoder.ts"],"names":[],"mappings":";;;AAAA,sDAAmD;AACnD,8CAAwF;AAGxF,+CAA4C;AAC5C,2CAA2E;AAG3E,yDAAsD;AACtD,uDAA+F;AAG/F,MAAa,OAAO;IAUhB,YAAY,IAAO,EAAE,OAAqB;QAJ1C,iBAAY,GAAW,CAAC,CAAC;QAKrB,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAEpB,IAAI,CAAC,OAAO,GAAG,OAAO,IAAI,IAAI,yBAAW,CAAC,IAAI,CAAC,WAA4B,CAAC,CAAC;QAE7E,iDAAiD;QACjD,iDAAiD;QACjD,mFAAmF;QACnF,MAAM;IACV,CAAC;IAES,QAAQ,CAAC,IAAO;QACtB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,IAAI,GAAG,IAAI,mCAAgB,EAAE,CAAC;QACnC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;IAC9B,CAAC;IAED,MAAM,CACF,KAAiB,EACjB,KAAe,EAAE,MAAM,EAAE,CAAC,EAAE,EAC5B,MAAW,IAAI,CAAC,KAAK;QAErB,MAAM,UAAU,GAAiB,EAAE,CAAC;QAEpC,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC;QACxB,MAAM,UAAU,GAAG,KAAK,CAAC,UAAU,CAAC;QAEpC,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AE,CAAC,CAAC;YACzC,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QACrC,CAAC;QAED,OAAO,IAAI,IAAI,WAAW,CAAC;IAC/B,CAAC;IAED,oBAAoB,CAAE,IAAmB;QACrC,OAAO,IAAK,IAAY,EAAE,CAAC;IAC/B,CAAC;IAED,eAAe,CAAC,GAAe,EAAE,UAAwB;QACrD,MAAM,aAAa,GAAG,OAAO,CAAE,GAAW,CAAC,oBAAU,CAAC,CAAC,KAAK,QAAQ,CAAC;QACrE,MAAM,KAAK,GAAI,GAAW,CAAC,gBAAM,CAAC,CAAC;QAEnC,GAAG,CAAC,OAAO,CAAC,CAAC,KAAU,EAAE,GAAQ,EAAE,EAAE;YACjC,UAAU,CAAC,IAAI,CAAC;gBACZ,GAAG,EAAE,GAAU;gBACf,KAAK;gBACL,EAAE,EAAE,gBAAS,CAAC,MAAM;gBACpB,KAAK,EAAE,GAAG;gBACV,KAAK,EAAE,SAAS;gBAChB,aAAa,EAAE,KAAK;aACvB,CAAC,CAAC;YAEH,IAAI,aAAa,EAAE,CAAC;gBAChB,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,gBAAM,CAAC,CAAC,CAAC;YACvC,CAAC;QACL,CAAC,CAAC,CAAC;IACP,CAAC;CAEJ;AAjJD,0BAiJC","sourcesContent":["import { TypeContext } from \"../types/TypeContext\";\nimport { $changes, $childType, $decoder, $onDecodeEnd, $refId } from \"../types/symbols\";\nimport { Schema } from \"../Schema\";\n\nimport { decode } from \"../encoding/decode\";\nimport { OPERATION, SWITCH_TO_STRUCTURE, TYPE_ID } from '../encoding/spec';\nimport type { Ref } from \"../encoder/ChangeTree\";\nimport type { Iterator } from \"../encoding/decode\";\nimport { ReferenceTracker } from \"./ReferenceTracker\";\nimport { DEFINITION_MISMATCH, type DataChange, type DecodeOperation } from \"./DecodeOperation\";\nimport { Collection } from \"../types/HelperTypes\";\n\nexport class Decoder<T extends Schema = any> {\n context: TypeContext;\n\n state: T;\n root: ReferenceTracker;\n\n currentRefId: number = 0;\n\n triggerChanges?: (allChanges: DataChange[]) => void;\n\n constructor(root: T, context?: TypeContext) {\n this.setState(root);\n\n this.context = context || new TypeContext(root.constructor as typeof Schema);\n\n // console.log(\">>>>>>>>>>>>>>>> Decoder types\");\n // this.context.schemas.forEach((id, schema) => {\n // console.log(\"type:\", id, schema.name, Object.keys(schema[Symbol.metadata]));\n // });\n }\n\n protected setState(root: T) {\n this.state = root;\n this.root = new ReferenceTracker();\n this.root.addRef(0, root);\n }\n\n decode(\n bytes: Uint8Array,\n it: Iterator = { offset: 0 },\n ref: Ref = this.state,\n ) {\n const allChanges: DataChange[] = [];\n\n const $root = this.root;\n const totalBytes = bytes.byteLength;\n\n let decoder: DecodeOperation = ref['constructor'][$decoder];\n\n this.currentRefId = 0;\n\n while (it.offset < totalBytes) {\n //\n // Peek ahead, check if it's a switch to a different structure\n //\n if (bytes[it.offset] == SWITCH_TO_STRUCTURE) {\n it.offset++;\n\n (ref as any)[$onDecodeEnd]?.()\n\n const nextRefId = decode.number(bytes, it);\n const nextRef = $root.refs.get(nextRefId);\n\n //\n // Trying to access a reference that haven't been decoded yet.\n //\n if (!nextRef) {\n // throw new Error(`\"refId\" not found: ${nextRefId}`);\n console.error(`\"refId\" not found: ${nextRefId}`, { previousRef: ref, previousRefId: this.currentRefId });\n console.warn(\"Please report this issue to the developers.\");\n this.skipCurrentStructure(bytes, it, totalBytes);\n\n } else {\n ref = nextRef;\n decoder = ref.constructor[$decoder];\n this.currentRefId = nextRefId;\n }\n\n continue;\n }\n\n const result = decoder(this, bytes, it, ref, allChanges);\n\n if (result === DEFINITION_MISMATCH) {\n console.warn(\"@colyseus/schema: definition mismatch\");\n this.skipCurrentStructure(bytes, it, totalBytes);\n continue;\n }\n }\n\n // FIXME: DRY with SWITCH_TO_STRUCTURE block.\n (ref as any)[$onDecodeEnd]?.()\n\n // trigger changes\n this.triggerChanges?.(allChanges);\n\n // drop references of unused schemas\n $root.garbageCollectDeletedRefs();\n\n return allChanges;\n }\n\n skipCurrentStructure(bytes: Uint8Array, it: Iterator, totalBytes: number) {\n //\n // keep skipping next bytes until reaches a known structure\n // by local decoder.\n //\n const nextIterator: Iterator = { offset: it.offset };\n while (it.offset < totalBytes) {\n if (bytes[it.offset] === SWITCH_TO_STRUCTURE) {\n nextIterator.offset = it.offset + 1;\n if (this.root.refs.has(decode.number(bytes, nextIterator))) {\n break;\n }\n }\n it.offset++;\n }\n }\n\n getInstanceType(bytes: Uint8Array, it: Iterator, defaultType: typeof Schema): typeof Schema {\n let type: typeof Schema;\n\n if (bytes[it.offset] === TYPE_ID) {\n it.offset++;\n const type_id = decode.number(bytes, it);\n type = this.context.get(type_id);\n }\n\n return type || defaultType;\n }\n\n createInstanceOfType (type: typeof Schema): Schema {\n return new (type as any)();\n }\n\n removeChildRefs(ref: Collection, allChanges: DataChange[]) {\n const needRemoveRef = typeof ((ref as any)[$childType]) !== \"string\";\n const refId = (ref as Ref)[$refId];\n\n ref.forEach((value: any, key: any) => {\n allChanges.push({\n ref: ref as Ref,\n refId,\n op: OPERATION.DELETE,\n field: key,\n value: undefined,\n previousValue: value\n });\n\n if (needRemoveRef) {\n this.root.removeRef(value[$refId]);\n }\n });\n }\n\n}\n"]}
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ReferenceTracker = void 0;
|
|
4
|
-
const symbols_1 = require("../types/symbols");
|
|
5
|
-
const utils_1 = require("../types/utils");
|
|
6
|
-
const spec_1 = require("../encoding/spec");
|
|
7
|
-
class DecodingWarning extends Error {
|
|
8
|
-
constructor(message) {
|
|
9
|
-
super(message);
|
|
10
|
-
this.name = "DecodingWarning";
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
class ReferenceTracker {
|
|
14
|
-
constructor() {
|
|
15
|
-
//
|
|
16
|
-
// Relation of refId => Schema structure
|
|
17
|
-
// For direct access of structures during decoding time.
|
|
18
|
-
//
|
|
19
|
-
this.refs = new Map();
|
|
20
|
-
this.refCount = {};
|
|
21
|
-
this.deletedRefs = new Set();
|
|
22
|
-
this.callbacks = {};
|
|
23
|
-
this.nextUniqueId = 0;
|
|
24
|
-
}
|
|
25
|
-
getNextUniqueId() {
|
|
26
|
-
return this.nextUniqueId++;
|
|
27
|
-
}
|
|
28
|
-
// for decoding
|
|
29
|
-
addRef(refId, ref, incrementCount = true) {
|
|
30
|
-
this.refs.set(refId, ref);
|
|
31
|
-
ref[symbols_1.$refId] = refId;
|
|
32
|
-
if (incrementCount) {
|
|
33
|
-
this.refCount[refId] = (this.refCount[refId] || 0) + 1;
|
|
34
|
-
}
|
|
35
|
-
if (this.deletedRefs.has(refId)) {
|
|
36
|
-
this.deletedRefs.delete(refId);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
// for decoding
|
|
40
|
-
removeRef(refId) {
|
|
41
|
-
const refCount = this.refCount[refId];
|
|
42
|
-
if (refCount === undefined) {
|
|
43
|
-
try {
|
|
44
|
-
throw new DecodingWarning("trying to remove refId that doesn't exist: " + refId);
|
|
45
|
-
}
|
|
46
|
-
catch (e) {
|
|
47
|
-
console.warn(e);
|
|
48
|
-
}
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
if (refCount === 0) {
|
|
52
|
-
try {
|
|
53
|
-
const ref = this.refs.get(refId);
|
|
54
|
-
throw new DecodingWarning(`trying to remove refId '${refId}' with 0 refCount (${ref.constructor.name}: ${JSON.stringify(ref)})`);
|
|
55
|
-
}
|
|
56
|
-
catch (e) {
|
|
57
|
-
console.warn(e);
|
|
58
|
-
}
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
if ((this.refCount[refId] = refCount - 1) <= 0) {
|
|
62
|
-
this.deletedRefs.add(refId);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
clearRefs() {
|
|
66
|
-
this.refs.clear();
|
|
67
|
-
this.deletedRefs.clear();
|
|
68
|
-
this.callbacks = {};
|
|
69
|
-
this.refCount = {};
|
|
70
|
-
}
|
|
71
|
-
// for decoding
|
|
72
|
-
garbageCollectDeletedRefs() {
|
|
73
|
-
this.deletedRefs.forEach((refId) => {
|
|
74
|
-
//
|
|
75
|
-
// Skip active references.
|
|
76
|
-
//
|
|
77
|
-
if (this.refCount[refId] > 0) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
const ref = this.refs.get(refId);
|
|
81
|
-
//
|
|
82
|
-
// Ensure child schema instances have their references removed as well.
|
|
83
|
-
//
|
|
84
|
-
if (ref.constructor[Symbol.metadata] !== undefined) {
|
|
85
|
-
const metadata = ref.constructor[Symbol.metadata];
|
|
86
|
-
for (const index in metadata) {
|
|
87
|
-
const field = metadata[index].name;
|
|
88
|
-
const child = ref[field];
|
|
89
|
-
if (typeof (child) === "object" && child) {
|
|
90
|
-
const childRefId = child[symbols_1.$refId];
|
|
91
|
-
if (childRefId !== undefined && !this.deletedRefs.has(childRefId)) {
|
|
92
|
-
this.removeRef(childRefId);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
if (typeof (ref[symbols_1.$childType]) === "function") {
|
|
99
|
-
Array.from(ref.values())
|
|
100
|
-
.forEach((child) => {
|
|
101
|
-
const childRefId = child[symbols_1.$refId];
|
|
102
|
-
if (childRefId !== undefined && !this.deletedRefs.has(childRefId)) {
|
|
103
|
-
this.removeRef(childRefId);
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
this.refs.delete(refId); // remove ref
|
|
109
|
-
delete this.refCount[refId]; // remove ref count
|
|
110
|
-
delete this.callbacks[refId]; // remove callbacks
|
|
111
|
-
});
|
|
112
|
-
// clear deleted refs.
|
|
113
|
-
this.deletedRefs.clear();
|
|
114
|
-
}
|
|
115
|
-
addCallback(refId, fieldOrOperation, callback) {
|
|
116
|
-
if (refId === undefined) {
|
|
117
|
-
const name = (typeof (fieldOrOperation) === "number")
|
|
118
|
-
? spec_1.OPERATION[fieldOrOperation]
|
|
119
|
-
: fieldOrOperation;
|
|
120
|
-
throw new Error(`Can't addCallback on '${name}' (refId is undefined)`);
|
|
121
|
-
}
|
|
122
|
-
if (!this.callbacks[refId]) {
|
|
123
|
-
this.callbacks[refId] = {};
|
|
124
|
-
}
|
|
125
|
-
if (!this.callbacks[refId][fieldOrOperation]) {
|
|
126
|
-
this.callbacks[refId][fieldOrOperation] = [];
|
|
127
|
-
}
|
|
128
|
-
this.callbacks[refId][fieldOrOperation].push(callback);
|
|
129
|
-
return () => this.removeCallback(refId, fieldOrOperation, callback);
|
|
130
|
-
}
|
|
131
|
-
removeCallback(refId, field, callback) {
|
|
132
|
-
const index = this.callbacks?.[refId]?.[field]?.indexOf(callback);
|
|
133
|
-
if (index !== undefined && index !== -1) {
|
|
134
|
-
(0, utils_1.spliceOne)(this.callbacks[refId][field], index);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
exports.ReferenceTracker = ReferenceTracker;
|
|
139
|
-
//# sourceMappingURL=ReferenceTracker.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ReferenceTracker.js","sourceRoot":"","sources":["../../src/decoder/ReferenceTracker.ts"],"names":[],"mappings":";;;AACA,8CAAsD;AAEtD,0CAA2C;AAC3C,2CAA6C;AAK7C,MAAM,eAAgB,SAAQ,KAAK;IAC/B,YAAY,OAAe;QACvB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAClC,CAAC;CACJ;AAQD,MAAa,gBAAgB;IAA7B;QACI,EAAE;QACF,wCAAwC;QACxC,wDAAwD;QACxD,EAAE;QACK,SAAI,GAAG,IAAI,GAAG,EAAe,CAAC;QAE9B,aAAQ,GAAiC,EAAE,CAAC;QA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cesContent":["import { Metadata } from \"../Metadata\";\nimport { $childType, $refId } from \"../types/symbols\";\nimport { Ref } from \"../encoder/ChangeTree\";\nimport { spliceOne } from \"../types/utils\";\nimport { OPERATION } from \"../encoding/spec\";\n\nimport type { MapSchema } from \"../types/custom/MapSchema\";\nimport type { Schema } from \"../Schema\";\n\nclass DecodingWarning extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"DecodingWarning\";\n }\n}\n\n/**\n * Used for decoding only.\n */\n\nexport type SchemaCallbacks = { [field: string | number]: Function[] };\n\nexport class ReferenceTracker {\n //\n // Relation of refId => Schema structure\n // For direct access of structures during decoding time.\n //\n public refs = new Map<number, Ref>();\n\n public refCount: { [refId: number]: number; } = {};\n public deletedRefs = new Set<number>();\n\n public callbacks: { [refId: number]: SchemaCallbacks } = {};\n protected nextUniqueId: number = 0;\n\n getNextUniqueId() {\n return this.nextUniqueId++;\n }\n\n // for decoding\n addRef(refId: number, ref: Ref, incrementCount: boolean = true) {\n this.refs.set(refId, ref);\n ref[$refId] = refId;\n\n if (incrementCount) {\n this.refCount[refId] = (this.refCount[refId] || 0) + 1;\n }\n\n if (this.deletedRefs.has(refId)) {\n this.deletedRefs.delete(refId);\n }\n }\n\n // for decoding\n removeRef(refId: number) {\n const refCount = this.refCount[refId];\n\n if (refCount === undefined) {\n try {\n throw new DecodingWarning(\"trying to remove refId that doesn't exist: \" + refId);\n } catch (e) {\n console.warn(e);\n }\n return;\n }\n\n if (refCount === 0) {\n try {\n const ref = this.refs.get(refId);\n throw new DecodingWarning(`trying to remove refId '${refId}' with 0 refCount (${ref.constructor.name}: ${JSON.stringify(ref)})`);\n } catch (e) {\n console.warn(e);\n }\n return;\n }\n\n if ((this.refCount[refId] = refCount - 1) <= 0) {\n this.deletedRefs.add(refId);\n }\n }\n\n clearRefs() {\n this.refs.clear();\n this.deletedRefs.clear();\n this.callbacks = {};\n this.refCount = {};\n }\n\n // for decoding\n garbageCollectDeletedRefs() {\n this.deletedRefs.forEach((refId) => {\n //\n // Skip active references.\n //\n if (this.refCount[refId] > 0) { return; }\n\n const ref = this.refs.get(refId);\n\n //\n // Ensure child schema instances have their references removed as well.\n //\n if ((ref.constructor as typeof Schema)[Symbol.metadata] !== undefined) {\n const metadata: Metadata = (ref.constructor as typeof Schema)[Symbol.metadata];\n for (const index in metadata) {\n const field = metadata[index as any as number].name;\n const child = ref[field as keyof Ref];\n if (typeof(child) === \"object\" && child) {\n const childRefId = (child as any)[$refId];\n if (childRefId !== undefined && !this.deletedRefs.has(childRefId)) {\n this.removeRef(childRefId);\n }\n }\n }\n\n } else {\n if (typeof ((ref as any)[$childType]) === \"function\") {\n Array.from((ref as MapSchema).values())\n .forEach((child) => {\n const childRefId = child[$refId];\n if (childRefId !== undefined && !this.deletedRefs.has(childRefId)) {\n this.removeRef(childRefId);\n }\n });\n }\n }\n\n this.refs.delete(refId); // remove ref\n delete this.refCount[refId]; // remove ref count\n delete this.callbacks[refId]; // remove callbacks\n });\n\n // clear deleted refs.\n this.deletedRefs.clear();\n }\n\n addCallback(refId: number, fieldOrOperation: string | number, callback: Function) {\n if (refId === undefined) {\n const name = (typeof(fieldOrOperation) === \"number\")\n ? OPERATION[fieldOrOperation]\n : fieldOrOperation\n throw new Error(\n `Can't addCallback on '${name}' (refId is undefined)`\n );\n }\n if (!this.callbacks[refId]) {\n this.callbacks[refId] = {};\n }\n if (!this.callbacks[refId][fieldOrOperation]) {\n this.callbacks[refId][fieldOrOperation] = [];\n }\n this.callbacks[refId][fieldOrOperation].push(callback);\n return () => this.removeCallback(refId, fieldOrOperation, callback);\n }\n\n removeCallback(refId: number, field: string | number, callback: Function) {\n const index: number | undefined = this.callbacks?.[refId]?.[field]?.indexOf(callback);\n if (index !== undefined && index !== -1) {\n spliceOne(this.callbacks[refId][field], index);\n }\n }\n\n}\n"]}
|
|
@@ -1,340 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Callbacks = exports.StateCallbackStrategy = void 0;
|
|
4
|
-
const Decoder_1 = require("../Decoder");
|
|
5
|
-
const spec_1 = require("../../encoding/spec");
|
|
6
|
-
const Schema_1 = require("../../Schema");
|
|
7
|
-
const symbols_1 = require("../../types/symbols");
|
|
8
|
-
const getDecoderStateCallbacks_1 = require("./getDecoderStateCallbacks");
|
|
9
|
-
const RawChanges_1 = require("./RawChanges");
|
|
10
|
-
/**
|
|
11
|
-
* State Callbacks handler
|
|
12
|
-
*
|
|
13
|
-
* Usage:
|
|
14
|
-
* ```ts
|
|
15
|
-
* const $ = Callbacks.get(decoder);
|
|
16
|
-
*
|
|
17
|
-
* // Listen to property changes
|
|
18
|
-
* $.listen("currentTurn", (currentValue, previousValue) => { ... });
|
|
19
|
-
*
|
|
20
|
-
* // Listen to collection additions
|
|
21
|
-
* $.onAdd("entities", (sessionId, entity) => {
|
|
22
|
-
* // Nested property listening
|
|
23
|
-
* $.listen(entity, "hp", (currentHp, previousHp) => { ... });
|
|
24
|
-
* });
|
|
25
|
-
*
|
|
26
|
-
* // Listen to collection removals
|
|
27
|
-
* $.onRemove("entities", (sessionId, entity) => { ... });
|
|
28
|
-
*
|
|
29
|
-
* // Listen to any property change on an instance
|
|
30
|
-
* $.onChange(entity, () => { ... });
|
|
31
|
-
*
|
|
32
|
-
* // Bind properties to another object
|
|
33
|
-
* $.bindTo(player, playerVisual);
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
class StateCallbackStrategy {
|
|
37
|
-
constructor(decoder) {
|
|
38
|
-
this.uniqueRefIds = new Set();
|
|
39
|
-
this.isTriggering = false;
|
|
40
|
-
this.decoder = decoder;
|
|
41
|
-
this.decoder.triggerChanges = this.triggerChanges.bind(this);
|
|
42
|
-
}
|
|
43
|
-
get callbacks() {
|
|
44
|
-
return this.decoder.root.callbacks;
|
|
45
|
-
}
|
|
46
|
-
get state() {
|
|
47
|
-
return this.decoder.state;
|
|
48
|
-
}
|
|
49
|
-
addCallback(refId, operationOrProperty, handler) {
|
|
50
|
-
const $root = this.decoder.root;
|
|
51
|
-
return $root.addCallback(refId, operationOrProperty, handler);
|
|
52
|
-
}
|
|
53
|
-
addCallbackOrWaitCollectionAvailable(instance, propertyName, operation, handler, immediate = true) {
|
|
54
|
-
let removeHandler = () => { };
|
|
55
|
-
const removeOnAdd = () => removeHandler();
|
|
56
|
-
const collection = instance[propertyName];
|
|
57
|
-
// Collection not available yet. Listen for its availability before attaching the handler.
|
|
58
|
-
if (collection === null || collection === undefined) {
|
|
59
|
-
removeHandler = this.addCallback(instance[symbols_1.$refId], propertyName, (value, _) => {
|
|
60
|
-
if (value !== null && value !== undefined) {
|
|
61
|
-
removeHandler = this.addCallback(value[symbols_1.$refId], operation, handler);
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
return removeOnAdd;
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
//
|
|
68
|
-
// Call immediately if collection is already available, if it's an ADD operation.
|
|
69
|
-
//
|
|
70
|
-
immediate = immediate && this.isTriggering === false;
|
|
71
|
-
if (operation === spec_1.OPERATION.ADD && immediate) {
|
|
72
|
-
collection.forEach((value, key) => {
|
|
73
|
-
handler(key, value);
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
return this.addCallback(collection[symbols_1.$refId], operation, handler);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
listen(...args) {
|
|
80
|
-
if (typeof args[0] === 'string') {
|
|
81
|
-
// listen(property, handler, immediate?)
|
|
82
|
-
return this.listenInstance(this.state, args[0], args[1], args[2]);
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
// listen(instance, property, handler, immediate?)
|
|
86
|
-
return this.listenInstance(args[0], args[1], args[2], args[3]);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
listenInstance(instance, propertyName, handler, immediate = true) {
|
|
90
|
-
immediate = immediate && this.isTriggering === false;
|
|
91
|
-
//
|
|
92
|
-
// Call handler immediately if property is already available.
|
|
93
|
-
//
|
|
94
|
-
const currentValue = instance[propertyName];
|
|
95
|
-
if (immediate && currentValue !== null && currentValue !== undefined) {
|
|
96
|
-
handler(currentValue, undefined);
|
|
97
|
-
}
|
|
98
|
-
return this.addCallback(instance[symbols_1.$refId], propertyName, handler);
|
|
99
|
-
}
|
|
100
|
-
onChange(...args) {
|
|
101
|
-
if (args.length === 2 && typeof args[0] !== 'string') {
|
|
102
|
-
// onChange(instance, handler) - instance change
|
|
103
|
-
const instance = args[0];
|
|
104
|
-
const handler = args[1];
|
|
105
|
-
return this.addCallback(instance[symbols_1.$refId], spec_1.OPERATION.REPLACE, handler);
|
|
106
|
-
}
|
|
107
|
-
if (typeof args[0] === 'string') {
|
|
108
|
-
// onChange(property, handler) - collection on root state
|
|
109
|
-
return this.addCallbackOrWaitCollectionAvailable(this.state, args[0], spec_1.OPERATION.REPLACE, args[1]);
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
// onChange(instance, property, handler) - nested collection
|
|
113
|
-
return this.addCallbackOrWaitCollectionAvailable(args[0], args[1], spec_1.OPERATION.REPLACE, args[2]);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
onAdd(...args) {
|
|
117
|
-
if (typeof args[0] === 'string') {
|
|
118
|
-
// onAdd(property, handler, immediate?) - collection on root state
|
|
119
|
-
return this.addCallbackOrWaitCollectionAvailable(this.state, args[0], spec_1.OPERATION.ADD, args[1], args[2] !== false);
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
// onAdd(instance, property, handler, immediate?) - nested collection
|
|
123
|
-
return this.addCallbackOrWaitCollectionAvailable(args[0], args[1], spec_1.OPERATION.ADD, args[2], args[3] !== false);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
onRemove(...args) {
|
|
127
|
-
if (typeof args[0] === 'string') {
|
|
128
|
-
// onRemove(property, handler) - collection on root state
|
|
129
|
-
return this.addCallbackOrWaitCollectionAvailable(this.state, args[0], spec_1.OPERATION.DELETE, args[1]);
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
// onRemove(instance, property, handler) - nested collection
|
|
133
|
-
return this.addCallbackOrWaitCollectionAvailable(args[0], args[1], spec_1.OPERATION.DELETE, args[2]);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Bind properties from a Schema instance to a target object.
|
|
138
|
-
* Changes will be automatically reflected on the target object.
|
|
139
|
-
*/
|
|
140
|
-
bindTo(from, to, properties, immediate = true) {
|
|
141
|
-
const metadata = from.constructor[Symbol.metadata];
|
|
142
|
-
// If no properties specified, bind all properties
|
|
143
|
-
if (!properties) {
|
|
144
|
-
properties = Object.keys(metadata)
|
|
145
|
-
.filter(key => !isNaN(Number(key)))
|
|
146
|
-
.map((index) => metadata[index].name);
|
|
147
|
-
}
|
|
148
|
-
const action = () => {
|
|
149
|
-
for (const prop of properties) {
|
|
150
|
-
const fromValue = from[prop];
|
|
151
|
-
if (fromValue !== undefined) {
|
|
152
|
-
to[prop] = fromValue;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
if (immediate) {
|
|
157
|
-
action();
|
|
158
|
-
}
|
|
159
|
-
return this.addCallback(from[symbols_1.$refId], spec_1.OPERATION.REPLACE, action);
|
|
160
|
-
}
|
|
161
|
-
triggerChanges(allChanges) {
|
|
162
|
-
this.uniqueRefIds.clear();
|
|
163
|
-
for (let i = 0, l = allChanges.length; i < l; i++) {
|
|
164
|
-
const change = allChanges[i];
|
|
165
|
-
const refId = change.refId;
|
|
166
|
-
const ref = change.ref;
|
|
167
|
-
const $callbacks = this.callbacks[refId];
|
|
168
|
-
if (!$callbacks) {
|
|
169
|
-
continue;
|
|
170
|
-
}
|
|
171
|
-
//
|
|
172
|
-
// trigger onRemove on child structure.
|
|
173
|
-
//
|
|
174
|
-
if ((change.op & spec_1.OPERATION.DELETE) === spec_1.OPERATION.DELETE &&
|
|
175
|
-
change.previousValue instanceof Schema_1.Schema) {
|
|
176
|
-
const childRefId = change.previousValue[symbols_1.$refId];
|
|
177
|
-
const deleteCallbacks = this.callbacks[childRefId]?.[spec_1.OPERATION.DELETE];
|
|
178
|
-
if (deleteCallbacks) {
|
|
179
|
-
for (let j = deleteCallbacks.length - 1; j >= 0; j--) {
|
|
180
|
-
deleteCallbacks[j]();
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
if (ref instanceof Schema_1.Schema) {
|
|
185
|
-
//
|
|
186
|
-
// Handle Schema instance
|
|
187
|
-
//
|
|
188
|
-
if (!this.uniqueRefIds.has(refId)) {
|
|
189
|
-
// trigger onChange
|
|
190
|
-
const replaceCallbacks = $callbacks[spec_1.OPERATION.REPLACE];
|
|
191
|
-
if (replaceCallbacks) {
|
|
192
|
-
for (let j = replaceCallbacks.length - 1; j >= 0; j--) {
|
|
193
|
-
try {
|
|
194
|
-
replaceCallbacks[j]();
|
|
195
|
-
}
|
|
196
|
-
catch (e) {
|
|
197
|
-
console.error(e);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
// trigger field callbacks
|
|
203
|
-
const fieldCallbacks = $callbacks[change.field];
|
|
204
|
-
if (fieldCallbacks) {
|
|
205
|
-
for (let j = fieldCallbacks.length - 1; j >= 0; j--) {
|
|
206
|
-
try {
|
|
207
|
-
this.isTriggering = true;
|
|
208
|
-
fieldCallbacks[j](change.value, change.previousValue);
|
|
209
|
-
}
|
|
210
|
-
catch (e) {
|
|
211
|
-
console.error(e);
|
|
212
|
-
}
|
|
213
|
-
finally {
|
|
214
|
-
this.isTriggering = false;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
else {
|
|
220
|
-
//
|
|
221
|
-
// Handle collection of items
|
|
222
|
-
//
|
|
223
|
-
const dynamicIndex = change.dynamicIndex ?? change.field;
|
|
224
|
-
if ((change.op & spec_1.OPERATION.DELETE) === spec_1.OPERATION.DELETE) {
|
|
225
|
-
//
|
|
226
|
-
// FIXME: `previousValue` should always be available.
|
|
227
|
-
//
|
|
228
|
-
if (change.previousValue !== undefined) {
|
|
229
|
-
// trigger onRemove (key, value)
|
|
230
|
-
const deleteCallbacks = $callbacks[spec_1.OPERATION.DELETE];
|
|
231
|
-
if (deleteCallbacks) {
|
|
232
|
-
for (let j = deleteCallbacks.length - 1; j >= 0; j--) {
|
|
233
|
-
deleteCallbacks[j](dynamicIndex, change.previousValue);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
// Handle DELETE_AND_ADD operation
|
|
238
|
-
if ((change.op & spec_1.OPERATION.ADD) === spec_1.OPERATION.ADD) {
|
|
239
|
-
const addCallbacks = $callbacks[spec_1.OPERATION.ADD];
|
|
240
|
-
if (addCallbacks) {
|
|
241
|
-
this.isTriggering = true;
|
|
242
|
-
for (let j = addCallbacks.length - 1; j >= 0; j--) {
|
|
243
|
-
addCallbacks[j](dynamicIndex, change.value);
|
|
244
|
-
}
|
|
245
|
-
this.isTriggering = false;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
else if ((change.op & spec_1.OPERATION.ADD) === spec_1.OPERATION.ADD &&
|
|
250
|
-
change.previousValue !== change.value) {
|
|
251
|
-
// trigger onAdd (key, value)
|
|
252
|
-
const addCallbacks = $callbacks[spec_1.OPERATION.ADD];
|
|
253
|
-
if (addCallbacks) {
|
|
254
|
-
this.isTriggering = true;
|
|
255
|
-
for (let j = addCallbacks.length - 1; j >= 0; j--) {
|
|
256
|
-
addCallbacks[j](dynamicIndex, change.value);
|
|
257
|
-
}
|
|
258
|
-
this.isTriggering = false;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
// trigger onChange (key, value)
|
|
262
|
-
if (change.value !== change.previousValue) {
|
|
263
|
-
const replaceCallbacks = $callbacks[spec_1.OPERATION.REPLACE];
|
|
264
|
-
if (replaceCallbacks) {
|
|
265
|
-
for (let j = replaceCallbacks.length - 1; j >= 0; j--) {
|
|
266
|
-
replaceCallbacks[j](dynamicIndex, change.value);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
this.uniqueRefIds.add(refId);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
exports.StateCallbackStrategy = StateCallbackStrategy;
|
|
276
|
-
/**
|
|
277
|
-
* Factory class for retrieving the callbacks API.
|
|
278
|
-
*/
|
|
279
|
-
exports.Callbacks = {
|
|
280
|
-
/**
|
|
281
|
-
* Get the new callbacks standard API.
|
|
282
|
-
*
|
|
283
|
-
* Usage:
|
|
284
|
-
* ```ts
|
|
285
|
-
* const callbacks = Callbacks.get(roomOrDecoder);
|
|
286
|
-
*
|
|
287
|
-
* // Listen to property changes
|
|
288
|
-
* callbacks.listen("currentTurn", (currentValue, previousValue) => { ... });
|
|
289
|
-
*
|
|
290
|
-
* // Listen to collection additions
|
|
291
|
-
* callbacks.onAdd("entities", (sessionId, entity) => {
|
|
292
|
-
* // Nested property listening
|
|
293
|
-
* callbacks.listen(entity, "hp", (currentHp, previousHp) => { ... });
|
|
294
|
-
* });
|
|
295
|
-
*
|
|
296
|
-
* // Listen to collection removals
|
|
297
|
-
* callbacks.onRemove("entities", (sessionId, entity) => { ... });
|
|
298
|
-
*
|
|
299
|
-
* // Listen to any property change on an instance
|
|
300
|
-
* callbacks.onChange(entity, () => { ... });
|
|
301
|
-
*
|
|
302
|
-
* // Bind properties to another object
|
|
303
|
-
* callbacks.bindTo(player, playerVisual);
|
|
304
|
-
* ```
|
|
305
|
-
*
|
|
306
|
-
* @param roomOrDecoder - Room or Decoder instance to get the callbacks for.
|
|
307
|
-
* @returns the new callbacks standard API.
|
|
308
|
-
*/
|
|
309
|
-
get(roomOrDecoder) {
|
|
310
|
-
if (roomOrDecoder instanceof Decoder_1.Decoder) {
|
|
311
|
-
return new StateCallbackStrategy(roomOrDecoder);
|
|
312
|
-
}
|
|
313
|
-
else if (roomOrDecoder.serializer.decoder) {
|
|
314
|
-
return new StateCallbackStrategy(roomOrDecoder.serializer.decoder);
|
|
315
|
-
}
|
|
316
|
-
else {
|
|
317
|
-
throw new Error('Invalid room or decoder');
|
|
318
|
-
}
|
|
319
|
-
},
|
|
320
|
-
/**
|
|
321
|
-
* Get the legacy callbacks API.
|
|
322
|
-
*
|
|
323
|
-
* We aim to deprecate this API on 1.0, and iterate on improving Callbacks.get() API.
|
|
324
|
-
*
|
|
325
|
-
* @param roomOrDecoder - Room or Decoder instance to get the legacy callbacks for.
|
|
326
|
-
* @returns the legacy callbacks API.
|
|
327
|
-
*/
|
|
328
|
-
getLegacy(roomOrDecoder) {
|
|
329
|
-
if (roomOrDecoder instanceof Decoder_1.Decoder) {
|
|
330
|
-
return (0, getDecoderStateCallbacks_1.getDecoderStateCallbacks)(roomOrDecoder);
|
|
331
|
-
}
|
|
332
|
-
else if (roomOrDecoder.serializer.decoder) {
|
|
333
|
-
return (0, getDecoderStateCallbacks_1.getDecoderStateCallbacks)(roomOrDecoder.serializer.decoder);
|
|
334
|
-
}
|
|
335
|
-
},
|
|
336
|
-
getRawChanges(decoder, callback) {
|
|
337
|
-
return (0, RawChanges_1.getRawChangesCallback)(decoder, callback);
|
|
338
|
-
}
|
|
339
|
-
};
|
|
340
|
-
//# sourceMappingURL=Callbacks.js.map
|