@colyseus/schema 2.0.28 → 2.0.30

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.
Files changed (55) hide show
  1. package/build/cjs/index.js +29 -31
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.mjs +26 -43
  4. package/build/esm/index.mjs.map +1 -1
  5. package/build/umd/index.js +29 -31
  6. package/lib/index.js +2 -4
  7. package/lib/index.js.map +1 -1
  8. package/lib/types/ArraySchema.js +7 -0
  9. package/lib/types/ArraySchema.js.map +1 -1
  10. package/lib/utils.d.ts +0 -1
  11. package/lib/utils.js +1 -26
  12. package/lib/utils.js.map +1 -1
  13. package/package.json +1 -1
  14. package/src/index.ts +0 -3
  15. package/src/types/ArraySchema.ts +7 -0
  16. package/src/utils.ts +0 -25
  17. package/lib/Decoder.d.ts +0 -16
  18. package/lib/Decoder.js +0 -182
  19. package/lib/Decoder.js.map +0 -1
  20. package/lib/Encoder.d.ts +0 -13
  21. package/lib/Encoder.js +0 -79
  22. package/lib/Encoder.js.map +0 -1
  23. package/lib/Metadata.d.ts +0 -22
  24. package/lib/Metadata.js +0 -65
  25. package/lib/Metadata.js.map +0 -1
  26. package/lib/changes/ChangeSet.d.ts +0 -12
  27. package/lib/changes/ChangeSet.js +0 -35
  28. package/lib/changes/ChangeSet.js.map +0 -1
  29. package/lib/changes/DecodeOperation.d.ts +0 -15
  30. package/lib/changes/DecodeOperation.js +0 -186
  31. package/lib/changes/DecodeOperation.js.map +0 -1
  32. package/lib/changes/EncodeOperation.d.ts +0 -18
  33. package/lib/changes/EncodeOperation.js +0 -130
  34. package/lib/changes/EncodeOperation.js.map +0 -1
  35. package/lib/changes/consts.d.ts +0 -14
  36. package/lib/changes/consts.js +0 -18
  37. package/lib/changes/consts.js.map +0 -1
  38. package/lib/decoder/strategy/StateCallbacks.d.ts +0 -6
  39. package/lib/decoder/strategy/StateCallbacks.js +0 -20
  40. package/lib/decoder/strategy/StateCallbacks.js.map +0 -1
  41. package/lib/decoding/decode.d.ts +0 -48
  42. package/lib/decoding/decode.js +0 -267
  43. package/lib/decoding/decode.js.map +0 -1
  44. package/lib/encoding/assert.d.ts +0 -9
  45. package/lib/encoding/assert.js +0 -47
  46. package/lib/encoding/assert.js.map +0 -1
  47. package/lib/symbol.shim.d.ts +0 -6
  48. package/lib/symbol.shim.js +0 -4
  49. package/lib/symbol.shim.js.map +0 -1
  50. package/lib/usage.d.ts +0 -1
  51. package/lib/usage.js +0 -22
  52. package/lib/usage.js.map +0 -1
  53. package/lib/v3.d.ts +0 -1
  54. package/lib/v3.js +0 -313
  55. package/lib/v3.js.map +0 -1
@@ -1,186 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.decodeKeyValueOperation = exports.decodeSchemaOperation = exports.decodeValue = exports.DecodeState = void 0;
4
- const spec_1 = require("../spec");
5
- const Schema_1 = require("../Schema");
6
- const decode = require("../encoding/decode");
7
- const typeRegistry_1 = require("../types/typeRegistry");
8
- const consts_1 = require("./consts");
9
- const __1 = require("..");
10
- var DecodeState;
11
- (function (DecodeState) {
12
- DecodeState[DecodeState["DEFINITION_MISMATCH"] = 0] = "DEFINITION_MISMATCH";
13
- })(DecodeState || (exports.DecodeState = DecodeState = {}));
14
- function decodeValue(decoder, operation, ref, index, type, bytes, it, allChanges) {
15
- const $root = decoder.refs;
16
- const previousValue = ref[consts_1.$getByIndex](index);
17
- let value;
18
- if ((operation & spec_1.OPERATION.DELETE) === spec_1.OPERATION.DELETE) {
19
- //
20
- // Delete operations
21
- //
22
- if (operation !== spec_1.OPERATION.DELETE_AND_ADD) {
23
- ref[consts_1.$deleteByIndex](index);
24
- }
25
- // Flag `refId` for garbage collection.
26
- const previousRefId = $root.refIds.get(previousValue);
27
- if (previousRefId) {
28
- $root.removeRef(previousRefId);
29
- }
30
- value = null;
31
- }
32
- else if (Schema_1.Schema.is(type)) {
33
- const refId = decode.number(bytes, it);
34
- value = $root.refs.get(refId);
35
- if (operation !== spec_1.OPERATION.REPLACE) {
36
- const childType = decoder.getInstanceType(bytes, it, type);
37
- if (!value) {
38
- value = decoder.createInstanceOfType(childType);
39
- if (previousValue) {
40
- // value.$callbacks = previousValue.$callbacks;
41
- // value.$listeners = previousValue.$listeners;
42
- const previousRefId = $root.refIds.get(previousValue);
43
- if (previousRefId && refId !== previousRefId) {
44
- $root.removeRef(previousRefId);
45
- }
46
- }
47
- }
48
- // console.log("ADD REF!", refId, value, ", TYPE =>", Metadata.getFor(childType));
49
- $root.addRef(refId, value, (value !== previousValue));
50
- }
51
- }
52
- else if (typeof (type) === "string") {
53
- //
54
- // primitive value (number, string, boolean, etc)
55
- //
56
- value = decode[type](bytes, it);
57
- }
58
- else {
59
- const typeDef = (0, typeRegistry_1.getType)(Object.keys(type)[0]);
60
- const refId = decode.number(bytes, it);
61
- const valueRef = ($root.refs.has(refId))
62
- ? previousValue || $root.refs.get(refId)
63
- : new typeDef.constructor();
64
- value = valueRef.clone(true);
65
- value[consts_1.$childType] = Object.values(type)[0]; // cache childType for ArraySchema and MapSchema
66
- // preserve schema callbacks
67
- if (previousValue) {
68
- // value['$callbacks'] = previousValue['$callbacks'];
69
- const previousRefId = $root.refIds.get(previousValue);
70
- if (previousRefId && refId !== previousRefId) {
71
- $root.removeRef(previousRefId);
72
- //
73
- // Trigger onRemove if structure has been replaced.
74
- //
75
- const entries = previousValue.entries();
76
- let iter;
77
- while ((iter = entries.next()) && !iter.done) {
78
- const [key, value] = iter.value;
79
- allChanges.push({
80
- refId,
81
- op: spec_1.OPERATION.DELETE,
82
- field: key,
83
- value: undefined,
84
- previousValue: value,
85
- });
86
- }
87
- }
88
- }
89
- // console.log("ADD REF!", { refId, value });
90
- $root.addRef(refId, value, (valueRef !== previousValue));
91
- }
92
- return { value, previousValue };
93
- }
94
- exports.decodeValue = decodeValue;
95
- const decodeSchemaOperation = function (decoder, bytes, it, ref, allChanges) {
96
- const first_byte = bytes[it.offset++];
97
- const metadata = ref['constructor'][Symbol.metadata];
98
- // "compressed" index + operation
99
- const operation = (first_byte >> 6) << 6;
100
- const index = first_byte % (operation || 255);
101
- // skip early if field is not defined
102
- const field = metadata[index];
103
- if (field === undefined) {
104
- return DecodeState.DEFINITION_MISMATCH;
105
- }
106
- const { value, previousValue } = decodeValue(decoder, operation, ref, index, metadata[field].type, bytes, it, allChanges);
107
- if (value !== null && value !== undefined) {
108
- ref[field] = value;
109
- }
110
- // add change
111
- if (previousValue !== value) {
112
- allChanges.push({
113
- refId: decoder.currentRefId,
114
- op: operation,
115
- field: field,
116
- value,
117
- previousValue,
118
- });
119
- }
120
- };
121
- exports.decodeSchemaOperation = decodeSchemaOperation;
122
- const decodeKeyValueOperation = function (decoder, bytes, it, ref, allChanges) {
123
- const first_byte = bytes[it.offset++];
124
- // "uncompressed" index + operation (array/map items)
125
- const operation = first_byte;
126
- if (operation === spec_1.OPERATION.CLEAR) {
127
- //
128
- // TODO: refactor me!
129
- // The `.clear()` method is calling `$root.removeRef(refId)` for
130
- // each item inside this collection
131
- //
132
- ref.clear(allChanges);
133
- return;
134
- }
135
- const index = decode.number(bytes, it);
136
- const type = ref[consts_1.$childType];
137
- let dynamicIndex;
138
- if ((operation & spec_1.OPERATION.ADD) === spec_1.OPERATION.ADD) { // ADD or DELETE_AND_ADD
139
- dynamicIndex = (ref instanceof __1.MapSchema)
140
- ? decode.string(bytes, it)
141
- : index;
142
- ref['setIndex'](index, dynamicIndex);
143
- }
144
- else {
145
- // here
146
- dynamicIndex = ref['getIndex'](index);
147
- }
148
- const { value, previousValue } = decodeValue(decoder, operation, ref, dynamicIndex, type, bytes, it, allChanges);
149
- if (value !== null && value !== undefined) {
150
- if (ref instanceof __1.MapSchema) {
151
- // const key = ref['$indexes'].get(field);
152
- const key = dynamicIndex;
153
- // ref.set(key, value);
154
- ref['$items'].set(key, value);
155
- }
156
- else if (ref instanceof __1.ArraySchema) {
157
- // const key = ref['$indexes'][field];
158
- // console.log("SETTING FOR ArraySchema =>", { field, key, value });
159
- // ref[key] = value;
160
- ref.setAt(index, value);
161
- }
162
- else if (ref instanceof __1.CollectionSchema) {
163
- const index = ref.add(value);
164
- ref['setIndex'](index, index);
165
- }
166
- else if (ref instanceof __1.SetSchema) {
167
- const index = ref.add(value);
168
- if (index !== false) {
169
- ref['setIndex'](index, index);
170
- }
171
- }
172
- }
173
- // add change
174
- if (previousValue !== value) {
175
- allChanges.push({
176
- refId: decoder.currentRefId,
177
- op: operation,
178
- field: "", // FIXME: remove this
179
- dynamicIndex,
180
- value,
181
- previousValue,
182
- });
183
- }
184
- };
185
- exports.decodeKeyValueOperation = decodeKeyValueOperation;
186
- //# sourceMappingURL=DecodeOperation.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"DecodeOperation.js","sourceRoot":"","sources":["../../src/changes/DecodeOperation.ts"],"names":[],"mappings":";;;AAAA,kCAAoC;AAEpC,sCAAuE;AAGvE,6CAA6C;AAC7C,wDAAgD;AAChD,qCAAmE;AACnE,0BAAyE;AAEzE,IAAY,WAEX;AAFD,WAAY,WAAW;IACnB,2EAAuB,CAAA;AAC3B,CAAC,EAFW,WAAW,2BAAX,WAAW,QAEtB;AAUD,SAAgB,WAAW,CAAC,OAAgB,EAAE,SAAoB,EAAE,GAAQ,EAAE,KAAa,EAAE,IAAS,EAAE,KAAe,EAAE,EAAmB,EAAE,UAAwB;IAClK,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC;IAC3B,MAAM,aAAa,GAAG,GAAG,CAAC,oBAAW,CAAC,CAAC,KAAK,CAAC,CAAC;IAE9C,IAAI,KAAU,CAAC;IAEf,IAAI,CAAC,SAAS,GAAG,gBAAS,CAAC,MAAM,CAAC,KAAK,gBAAS,CAAC,MAAM,EACvD,CAAC;QACG,EAAE;QACF,oBAAoB;QACpB,EAAE;QACF,IAAI,SAAS,KAAK,gBAAS,CAAC,cAAc,EAAE,CAAC;YACzC,GAAG,CAAC,uBAAc,CAAC,CAAC,KAAK,CAAC,CAAC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;YAC3B,EAAE,EAAE,SAAS;YACb,KAAK,EAAE,EAAE,EAAE,qBAAqB;YAChC,YAAY;YACZ,KAAK;YACL,aAAa;SAChB,CAAC,CAAC;IACP,CAAC;AACL,CAAC,CAAA;AAtFY,QAAA,uBAAuB,2BAsFnC","sourcesContent":["import { OPERATION } from \"../spec\";\nimport { Metadata } from \"../Metadata\";\nimport { DataChange, Schema, SchemaDecoderCallbacks } from \"../Schema\";\nimport type { Ref } from \"./ChangeTree\";\nimport type { Decoder } from \"../Decoder\";\nimport * as decode from \"../encoding/decode\";\nimport { getType } from \"../types/typeRegistry\";\nimport { $childType, $deleteByIndex, $getByIndex } from \"./consts\";\nimport { ArraySchema, CollectionSchema, MapSchema, SetSchema } from \"..\";\n\nexport enum DecodeState {\n DEFINITION_MISMATCH = 0,\n}\n\nexport type DecodeOperation<T extends Schema = any> = (\n decoder: Decoder<T>,\n bytes: number[],\n it: decode.Iterator,\n ref: Ref,\n allChanges: DataChange[],\n) => void;\n\nexport function decodeValue(decoder: Decoder, operation: OPERATION, ref: Ref, index: number, type: any, bytes: number[], it: decode.Iterator, allChanges: DataChange[]) {\n const $root = decoder.refs;\n const previousValue = ref[$getByIndex](index);\n\n let value: any;\n\n if ((operation & OPERATION.DELETE) === OPERATION.DELETE)\n {\n //\n // Delete operations\n //\n if (operation !== OPERATION.DELETE_AND_ADD) {\n ref[$deleteByIndex](index);\n }\n\n // Flag `refId` for garbage collection.\n const previousRefId = $root.refIds.get(previousValue);\n if (previousRefId) {\n $root.removeRef(previousRefId);\n }\n\n value = null;\n\n } else if (Schema.is(type)) {\n const refId = decode.number(bytes, it);\n value = $root.refs.get(refId);\n\n if (operation !== OPERATION.REPLACE) {\n const childType = decoder.getInstanceType(bytes, it, type);\n\n if (!value) {\n value = decoder.createInstanceOfType(childType);\n\n if (previousValue) {\n // value.$callbacks = previousValue.$callbacks;\n // value.$listeners = previousValue.$listeners;\n const previousRefId = $root.refIds.get(previousValue);\n if (previousRefId && refId !== previousRefId) {\n $root.removeRef(previousRefId);\n }\n }\n }\n\n // console.log(\"ADD REF!\", refId, value, \", TYPE =>\", Metadata.getFor(childType));\n $root.addRef(refId, value, (value !== previousValue));\n }\n\n } else if (typeof(type) === \"string\") {\n //\n // primitive value (number, string, boolean, etc)\n //\n value = decode[type as string](bytes, it);\n\n } else {\n const typeDef = getType(Object.keys(type)[0]);\n const refId = decode.number(bytes, it);\n\n const valueRef: SchemaDecoderCallbacks = ($root.refs.has(refId))\n ? previousValue || $root.refs.get(refId)\n : new typeDef.constructor();\n\n value = valueRef.clone(true);\n value[$childType] = Object.values(type)[0]; // cache childType for ArraySchema and MapSchema\n\n // preserve schema callbacks\n if (previousValue) {\n // value['$callbacks'] = previousValue['$callbacks'];\n const previousRefId = $root.refIds.get(previousValue);\n\n if (previousRefId && refId !== previousRefId) {\n $root.removeRef(previousRefId);\n\n //\n // Trigger onRemove if structure has been replaced.\n //\n const entries: IterableIterator<[any, any]> = previousValue.entries();\n let iter: IteratorResult<[any, any]>;\n while ((iter = entries.next()) && !iter.done) {\n const [key, value] = iter.value;\n allChanges.push({\n refId,\n op: OPERATION.DELETE,\n field: key,\n value: undefined,\n previousValue: value,\n });\n }\n\n }\n }\n\n // console.log(\"ADD REF!\", { refId, value });\n $root.addRef(refId, value, (valueRef !== previousValue));\n }\n\n return { value, previousValue };\n}\n\nexport const decodeSchemaOperation: DecodeOperation = function (\n decoder: Decoder<any>,\n bytes: number[],\n it: decode.Iterator,\n ref: Ref,\n allChanges: DataChange[]\n) {\n const first_byte = bytes[it.offset++];\n const metadata: Metadata = ref['constructor'][Symbol.metadata];\n\n // \"compressed\" index + operation\n const operation = (first_byte >> 6) << 6\n const index = first_byte % (operation || 255);\n\n // skip early if field is not defined\n const field = metadata[index];\n if (field === undefined) {\n return DecodeState.DEFINITION_MISMATCH;\n }\n\n const { value, previousValue } = decodeValue(\n decoder,\n operation,\n ref,\n index,\n metadata[field].type,\n bytes,\n it,\n allChanges\n );\n\n if (value !== null && value !== undefined) {\n ref[field] = value;\n }\n\n // add change\n if (previousValue !== value) {\n allChanges.push({\n refId: decoder.currentRefId,\n op: operation,\n field: field,\n value,\n previousValue,\n });\n }\n}\n\nexport const decodeKeyValueOperation: DecodeOperation = function (\n decoder: Decoder<any>,\n bytes: number[],\n it: decode.Iterator,\n ref: Ref,\n allChanges: DataChange[]\n) {\n const first_byte = bytes[it.offset++];\n\n // \"uncompressed\" index + operation (array/map items)\n const operation = first_byte;\n\n if (operation === OPERATION.CLEAR) {\n //\n // TODO: refactor me!\n // The `.clear()` method is calling `$root.removeRef(refId)` for\n // each item inside this collection\n //\n (ref as SchemaDecoderCallbacks).clear(allChanges);\n return;\n }\n\n const index = decode.number(bytes, it);\n const type = ref[$childType];\n\n let dynamicIndex: number | string;\n\n if ((operation & OPERATION.ADD) === OPERATION.ADD) { // ADD or DELETE_AND_ADD\n dynamicIndex = (ref instanceof MapSchema)\n ? decode.string(bytes, it)\n : index;\n ref['setIndex'](index, dynamicIndex);\n\n } else {\n // here\n dynamicIndex = ref['getIndex'](index);\n }\n\n const { value, previousValue } = decodeValue(\n decoder,\n operation,\n ref,\n dynamicIndex as number,\n type,\n bytes,\n it,\n allChanges\n );\n\n if (value !== null && value !== undefined) {\n if (ref instanceof MapSchema) {\n // const key = ref['$indexes'].get(field);\n const key = dynamicIndex as string;\n\n // ref.set(key, value);\n ref['$items'].set(key, value);\n\n } else if (ref instanceof ArraySchema) {\n // const key = ref['$indexes'][field];\n // console.log(\"SETTING FOR ArraySchema =>\", { field, key, value });\n // ref[key] = value;\n ref.setAt(index, value);\n\n } else if (ref instanceof CollectionSchema) {\n const index = ref.add(value);\n ref['setIndex'](index, index);\n\n } else if (ref instanceof SetSchema) {\n const index = ref.add(value);\n if (index !== false) {\n ref['setIndex'](index, index);\n }\n }\n }\n\n // add change\n if (previousValue !== value) {\n allChanges.push({\n refId: decoder.currentRefId,\n op: operation,\n field: \"\", // FIXME: remove this\n dynamicIndex,\n value,\n previousValue,\n });\n }\n}"]}
@@ -1,18 +0,0 @@
1
- import { OPERATION } from "../spec";
2
- import type { ChangeTracker, Ref } from "./ChangeTree";
3
- import type { Encoder } from "../Encoder";
4
- import type { Schema } from "../Schema";
5
- import type { PrimitiveType } from "../annotations";
6
- export type EncodeOperation<T extends Ref = any> = (encoder: Encoder, bytes: number[], changeTree: ChangeTracker<T>, index: number, operation: OPERATION) => void;
7
- export declare function encodePrimitiveType(type: PrimitiveType, bytes: number[], value: any, klass: Schema, field: string | number): void;
8
- export declare function encodeValue(encoder: Encoder, bytes: number[], ref: Schema, type: any, value: any, field: string | number, operation: OPERATION): void;
9
- /**
10
- * Used for Schema instances.
11
- * @private
12
- */
13
- export declare const encodeSchemaOperation: EncodeOperation;
14
- /**
15
- * Used for collections (MapSchema, ArraySchema, etc.)
16
- * @private
17
- */
18
- export declare const encodeKeyValueOperation: EncodeOperation;
@@ -1,130 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.encodeKeyValueOperation = exports.encodeSchemaOperation = exports.encodeValue = exports.encodePrimitiveType = void 0;
4
- const spec_1 = require("../spec");
5
- const consts_1 = require("./consts");
6
- const typeRegistry_1 = require("../types/typeRegistry");
7
- const encode = require("../encoding/encode");
8
- const assert_1 = require("../encoding/assert");
9
- const MapSchema_1 = require("../types/MapSchema");
10
- function encodePrimitiveType(type, bytes, value, klass, field) {
11
- (0, assert_1.assertType)(value, type, klass, field);
12
- const encodeFunc = encode[type];
13
- if (encodeFunc) {
14
- encodeFunc(bytes, value);
15
- }
16
- else {
17
- throw new assert_1.EncodeSchemaError(`a '${type}' was expected, but ${value} was provided in ${klass.constructor.name}#${field}`);
18
- }
19
- }
20
- exports.encodePrimitiveType = encodePrimitiveType;
21
- function encodeValue(encoder, bytes, ref, type, value, field, operation) {
22
- if (type[Symbol.metadata] !== undefined) {
23
- (0, assert_1.assertInstanceType)(value, type, ref, field);
24
- //
25
- // Encode refId for this instance.
26
- // The actual instance is going to be encoded on next `changeTree` iteration.
27
- //
28
- encode.number(bytes, value[consts_1.$changes].refId);
29
- // Try to encode inherited TYPE_ID if it's an ADD operation.
30
- if ((operation & spec_1.OPERATION.ADD) === spec_1.OPERATION.ADD) {
31
- encoder.tryEncodeTypeId(bytes, type, value.constructor);
32
- }
33
- }
34
- else if (typeof (type) === "string") {
35
- //
36
- // Primitive values
37
- //
38
- encodePrimitiveType(type, bytes, value, ref, field);
39
- }
40
- else {
41
- //
42
- // Custom type (MapSchema, ArraySchema, etc)
43
- //
44
- const definition = (0, typeRegistry_1.getType)(Object.keys(type)[0]);
45
- //
46
- // ensure a ArraySchema has been provided
47
- //
48
- (0, assert_1.assertInstanceType)(ref[field], definition.constructor, ref, field);
49
- //
50
- // Encode refId for this instance.
51
- // The actual instance is going to be encoded on next `changeTree` iteration.
52
- //
53
- encode.number(bytes, value[consts_1.$changes].refId);
54
- }
55
- }
56
- exports.encodeValue = encodeValue;
57
- /**
58
- * Used for Schema instances.
59
- * @private
60
- */
61
- const encodeSchemaOperation = function (encoder, bytes, changeTree, index, operation) {
62
- const ref = changeTree.ref;
63
- const metadata = ref['constructor'][Symbol.metadata];
64
- const field = metadata[index];
65
- const type = metadata[field].type;
66
- const value = ref[field];
67
- // "compress" field index + operation
68
- encode.uint8(bytes, (index | operation));
69
- // ensure refId for the value
70
- if (value && value[consts_1.$changes]) {
71
- value[consts_1.$changes].ensureRefId();
72
- }
73
- if (operation === spec_1.OPERATION.TOUCH) {
74
- return;
75
- }
76
- // TODO: inline this function call small performance gain
77
- encodeValue(encoder, bytes, ref, type, value, field, operation);
78
- };
79
- exports.encodeSchemaOperation = encodeSchemaOperation;
80
- /**
81
- * Used for collections (MapSchema, ArraySchema, etc.)
82
- * @private
83
- */
84
- const encodeKeyValueOperation = function (encoder, bytes, changeTree, field, operation) {
85
- const ref = changeTree.ref;
86
- // encode field index + operation
87
- if (operation !== spec_1.OPERATION.TOUCH) {
88
- encode.uint8(bytes, operation);
89
- // custom operations
90
- if (operation === spec_1.OPERATION.CLEAR) {
91
- return;
92
- }
93
- // indexed operations
94
- encode.number(bytes, field);
95
- }
96
- //
97
- // encode "alias" for dynamic fields (maps)
98
- //
99
- if ((operation & spec_1.OPERATION.ADD) == spec_1.OPERATION.ADD) { // ADD or DELETE_AND_ADD
100
- if (ref instanceof MapSchema_1.MapSchema) {
101
- //
102
- // MapSchema dynamic key
103
- //
104
- const dynamicIndex = changeTree.ref['$indexes'].get(field);
105
- encode.string(bytes, dynamicIndex);
106
- }
107
- }
108
- if (operation === spec_1.OPERATION.DELETE) {
109
- //
110
- // TODO: delete from filter cache data.
111
- //
112
- // if (useFilters) {
113
- // delete changeTree.caches[fieldIndex];
114
- // }
115
- return;
116
- }
117
- const type = changeTree.getType(field);
118
- const value = changeTree.getValue(field);
119
- // ensure refId for the value
120
- if (value && value[consts_1.$changes]) {
121
- value[consts_1.$changes].ensureRefId();
122
- }
123
- if (operation === spec_1.OPERATION.TOUCH) {
124
- return;
125
- }
126
- // TODO: inline this function call small performance gain
127
- encodeValue(encoder, bytes, ref, type, value, field, operation);
128
- };
129
- exports.encodeKeyValueOperation = encodeKeyValueOperation;
130
- //# sourceMappingURL=EncodeOperation.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"EncodeOperation.js","sourceRoot":"","sources":["../../src/changes/EncodeOperation.ts"],"names":[],"mappings":";;;AAAA,kCAAoC;AACpC,qCAAoC;AACpC,wDAAgD;AAEhD,6CAA6C;AAC7C,+CAAuF;AAOvF,kDAA+C;AAU/C,SAAgB,mBAAmB,CAC/B,IAAmB,EACnB,KAAe,EACf,KAAU,EACV,KAAa,EACb,KAAsB;IAEtB,IAAA,mBAAU,EAAC,KAAK,EAAE,IAAc,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;IAEhD,MAAM,UAAU,GAAG,MAAM,CAAC,IAAc,CAAC,CAAC;IAE1C,IAAI,UAAU,EAAE,CAAC;QACb,UAAU,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IAE7B,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CAAC;IAClC,CAAC;IAED,IAAI,SAAS,KAAK,gBAAS,CAAC,KAAK,EAAE,CAAC;QAChC,OAAO;IACX,CAAC;IAED,yDAAyD;IACzD,WAAW,CAAC,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC;AACpE,CAAC,CAAA;AA3DY,QAAA,uBAAuB,2BA2DnC","sourcesContent":["import { OPERATION } from \"../spec\";\nimport { $changes } from \"./consts\";\nimport { getType } from \"../types/typeRegistry\";\n\nimport * as encode from \"../encoding/encode\";\nimport { EncodeSchemaError, assertInstanceType, assertType } from \"../encoding/assert\";\n\nimport type { ChangeTracker, Ref } from \"./ChangeTree\";\nimport type { Encoder } from \"../Encoder\";\nimport type { Schema } from \"../Schema\";\nimport type { PrimitiveType } from \"../annotations\";\n\nimport { MapSchema } from \"../types/MapSchema\";\n\nexport type EncodeOperation<T extends Ref = any> = (\n encoder: Encoder,\n bytes: number[],\n changeTree: ChangeTracker<T>,\n index: number,\n operation: OPERATION,\n) => void;\n\nexport function encodePrimitiveType(\n type: PrimitiveType,\n bytes: number[],\n value: any,\n klass: Schema,\n field: string | number,\n) {\n assertType(value, type as string, klass, field);\n\n const encodeFunc = encode[type as string];\n\n if (encodeFunc) {\n encodeFunc(bytes, value);\n\n } else {\n throw new EncodeSchemaError(`a '${type}' was expected, but ${value} was provided in ${klass.constructor.name}#${field}`);\n }\n}\n\nexport function encodeValue(\n encoder: Encoder,\n bytes: number[],\n ref: Schema,\n type: any,\n value: any,\n field: string | number,\n operation: OPERATION\n) {\n if (type[Symbol.metadata] !== undefined) {\n assertInstanceType(value, type as typeof Schema, ref as Schema, field);\n\n //\n // Encode refId for this instance.\n // The actual instance is going to be encoded on next `changeTree` iteration.\n //\n encode.number(bytes, value[$changes].refId);\n\n // Try to encode inherited TYPE_ID if it's an ADD operation.\n if ((operation & OPERATION.ADD) === OPERATION.ADD) {\n encoder.tryEncodeTypeId(bytes, type as typeof Schema, value.constructor as typeof Schema);\n }\n\n } else if (typeof (type) === \"string\") {\n //\n // Primitive values\n //\n encodePrimitiveType(type as PrimitiveType, bytes, value, ref as Schema, field);\n\n } else {\n //\n // Custom type (MapSchema, ArraySchema, etc)\n //\n const definition = getType(Object.keys(type)[0]);\n\n //\n // ensure a ArraySchema has been provided\n //\n assertInstanceType(ref[field], definition.constructor, ref as Schema, field);\n\n //\n // Encode refId for this instance.\n // The actual instance is going to be encoded on next `changeTree` iteration.\n //\n encode.number(bytes, value[$changes].refId);\n }\n}\n\n/**\n * Used for Schema instances.\n * @private\n */\nexport const encodeSchemaOperation: EncodeOperation = function (\n encoder: Encoder,\n bytes: number[],\n changeTree: ChangeTracker,\n index: number,\n operation: OPERATION,\n) {\n const ref = changeTree.ref;\n const metadata = ref['constructor'][Symbol.metadata];\n\n const field = metadata[index];\n const type = metadata[field].type;\n const value = ref[field];\n\n // \"compress\" field index + operation\n encode.uint8(bytes, (index | operation));\n\n // ensure refId for the value\n if (value && value[$changes]) {\n value[$changes].ensureRefId();\n }\n\n if (operation === OPERATION.TOUCH) {\n return;\n }\n\n // TODO: inline this function call small performance gain\n encodeValue(encoder, bytes, ref, type, value, field, operation);\n}\n\n/**\n * Used for collections (MapSchema, ArraySchema, etc.)\n * @private\n */\nexport const encodeKeyValueOperation: EncodeOperation = function (\n encoder: Encoder,\n bytes: number[],\n changeTree: ChangeTracker,\n field: number,\n operation: OPERATION,\n) {\n const ref = changeTree.ref;\n\n // encode field index + operation\n if (operation !== OPERATION.TOUCH) {\n encode.uint8(bytes, operation);\n\n // custom operations\n if (operation === OPERATION.CLEAR) {\n return;\n }\n\n // indexed operations\n encode.number(bytes, field);\n }\n\n //\n // encode \"alias\" for dynamic fields (maps)\n //\n if ((operation & OPERATION.ADD) == OPERATION.ADD) { // ADD or DELETE_AND_ADD\n if (ref instanceof MapSchema) {\n //\n // MapSchema dynamic key\n //\n const dynamicIndex = changeTree.ref['$indexes'].get(field);\n encode.string(bytes, dynamicIndex);\n }\n }\n\n if (operation === OPERATION.DELETE) {\n //\n // TODO: delete from filter cache data.\n //\n // if (useFilters) {\n // delete changeTree.caches[fieldIndex];\n // }\n return;\n }\n\n const type = changeTree.getType(field);\n const value = changeTree.getValue(field);\n\n // ensure refId for the value\n if (value && value[$changes]) {\n value[$changes].ensureRefId();\n }\n\n if (operation === OPERATION.TOUCH) {\n return;\n }\n\n // TODO: inline this function call small performance gain\n encodeValue(encoder, bytes, ref, type, value, field, operation);\n}"]}
@@ -1,14 +0,0 @@
1
- export declare const $track: unique symbol;
2
- export declare const $encoder: unique symbol;
3
- export declare const $decoder: unique symbol;
4
- export declare const $getByIndex: unique symbol;
5
- export declare const $deleteByIndex: unique symbol;
6
- /**
7
- * Used to hold ChangeTree instances whitin the structures
8
- */
9
- export declare const $changes: unique symbol;
10
- /**
11
- * Used to keep track of the type of the child elements of a collection
12
- * (MapSchema, ArraySchema, etc.)
13
- */
14
- export declare const $childType: unique symbol;
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.$childType = exports.$changes = exports.$deleteByIndex = exports.$getByIndex = exports.$decoder = exports.$encoder = exports.$track = void 0;
4
- exports.$track = Symbol("$track");
5
- exports.$encoder = Symbol("$encoder");
6
- exports.$decoder = Symbol("$decoder");
7
- exports.$getByIndex = Symbol("$getByIndex");
8
- exports.$deleteByIndex = Symbol("$deleteByIndex");
9
- /**
10
- * Used to hold ChangeTree instances whitin the structures
11
- */
12
- exports.$changes = Symbol('$changes');
13
- /**
14
- * Used to keep track of the type of the child elements of a collection
15
- * (MapSchema, ArraySchema, etc.)
16
- */
17
- exports.$childType = Symbol('$childType');
18
- //# sourceMappingURL=consts.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"consts.js","sourceRoot":"","sources":["../../src/changes/consts.ts"],"names":[],"mappings":";;;AAAa,QAAA,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;AAC1B,QAAA,QAAQ,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;AAC9B,QAAA,QAAQ,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;AAE9B,QAAA,WAAW,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC;AACpC,QAAA,cAAc,GAAG,MAAM,CAAC,gBAAgB,CAAC,CAAC;AAEvD;;GAEG;AACU,QAAA,QAAQ,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;AAE3C;;;GAGG;AACU,QAAA,UAAU,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC","sourcesContent":["export const $track = Symbol(\"$track\");\nexport const $encoder = Symbol(\"$encoder\");\nexport const $decoder = Symbol(\"$decoder\");\n\nexport const $getByIndex = Symbol(\"$getByIndex\");\nexport const $deleteByIndex = Symbol(\"$deleteByIndex\");\n\n/**\n * Used to hold ChangeTree instances whitin the structures\n */\nexport const $changes = Symbol('$changes');\n\n/**\n * Used to keep track of the type of the child elements of a collection\n * (MapSchema, ArraySchema, etc.)\n */\nexport const $childType = Symbol('$childType');"]}
@@ -1,6 +0,0 @@
1
- import { Schema } from "../../Schema";
2
- export declare class StateCallbacks<S extends Schema> {
3
- state: S;
4
- constructor(state: S);
5
- }
6
- export declare function getStateCallbacks<S extends Schema>(state: S): void;
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getStateCallbacks = exports.StateCallbacks = void 0;
4
- //
5
- // Discussion: https://github.com/colyseus/schema/issues/155
6
- //
7
- // Main points:
8
- // - Decouple structures from their callbacks.
9
- // - Registering deep callbacks can be confusing.
10
- // - Avoid closures by allowing to pass a context. (https://github.com/colyseus/schema/issues/155#issuecomment-1804694081)
11
- //
12
- class StateCallbacks {
13
- constructor(state) { }
14
- }
15
- exports.StateCallbacks = StateCallbacks;
16
- function getStateCallbacks(state) {
17
- console.log(state.constructor[Symbol.metadata]);
18
- }
19
- exports.getStateCallbacks = getStateCallbacks;
20
- //# sourceMappingURL=StateCallbacks.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"StateCallbacks.js","sourceRoot":"","sources":["../../../src/decoder/strategy/StateCallbacks.ts"],"names":[],"mappings":";;;AAEA,EAAE;AACF,4DAA4D;AAC5D,EAAE;AACF,eAAe;AACf,8CAA8C;AAC9C,iDAAiD;AACjD,0HAA0H;AAC1H,EAAE;AAEF,MAAa,cAAc;IAGvB,YAAY,KAAQ,IAAG,CAAC;CAC3B;AAJD,wCAIC;AAED,SAAgB,iBAAiB,CAAmB,KAAQ;IACxD,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC;AACpD,CAAC;AAFD,8CAEC","sourcesContent":["import { Schema } from \"../../Schema\";\n\n//\n// Discussion: https://github.com/colyseus/schema/issues/155\n//\n// Main points:\n// - Decouple structures from their callbacks.\n// - Registering deep callbacks can be confusing.\n// - Avoid closures by allowing to pass a context. (https://github.com/colyseus/schema/issues/155#issuecomment-1804694081)\n//\n\nexport class StateCallbacks<S extends Schema> {\n state: S;\n\n constructor(state: S) {}\n}\n\nexport function getStateCallbacks<S extends Schema>(state: S) {\n console.log(state.constructor[Symbol.metadata]);\n}"]}
@@ -1,48 +0,0 @@
1
- /**
2
- * Copyright (c) 2018 Endel Dreyer
3
- * Copyright (c) 2014 Ion Drive Software Ltd.
4
- *
5
- * Permission is hereby granted, free of charge, to any person obtaining a copy
6
- * of this software and associated documentation files (the "Software"), to deal
7
- * in the Software without restriction, including without limitation the rights
8
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- * copies of the Software, and to permit persons to whom the Software is
10
- * furnished to do so, subject to the following conditions:
11
- *
12
- * The above copyright notice and this permission notice shall be included in all
13
- * copies or substantial portions of the Software.
14
- *
15
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- * SOFTWARE
22
- */
23
- /**
24
- * msgpack implementation highly based on notepack.io
25
- * https://github.com/darrachequesne/notepack
26
- */
27
- export interface Iterator {
28
- offset: number;
29
- }
30
- export declare function int8(bytes: number[], it: Iterator): number;
31
- export declare function uint8(bytes: number[], it: Iterator): number;
32
- export declare function int16(bytes: number[], it: Iterator): number;
33
- export declare function uint16(bytes: number[], it: Iterator): number;
34
- export declare function int32(bytes: number[], it: Iterator): number;
35
- export declare function uint32(bytes: number[], it: Iterator): number;
36
- export declare function float32(bytes: number[], it: Iterator): number;
37
- export declare function float64(bytes: number[], it: Iterator): number;
38
- export declare function int64(bytes: number[], it: Iterator): number;
39
- export declare function uint64(bytes: number[], it: Iterator): number;
40
- export declare function readFloat32(bytes: number[], it: Iterator): number;
41
- export declare function readFloat64(bytes: number[], it: Iterator): number;
42
- export declare function boolean(bytes: number[], it: Iterator): boolean;
43
- export declare function string(bytes: any, it: Iterator): string;
44
- export declare function stringCheck(bytes: any, it: Iterator): boolean;
45
- export declare function number(bytes: any, it: Iterator): any;
46
- export declare function numberCheck(bytes: any, it: Iterator): boolean;
47
- export declare function arrayCheck(bytes: any, it: Iterator): boolean;
48
- export declare function switchStructureCheck(bytes: any, it: Iterator): boolean;