@colyseus/schema 3.0.0-alpha.11 → 3.0.0-alpha.13

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/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAAkC;AAAzB,gGAAA,MAAM,OAAA;AAGf,6CAAyH;AAChH,uFADA,gBAAM,OACA;AAAE,yFADA,kBAAQ,OACA;AAAE,yFADA,kBAAQ,OACA;AAAE,wFADA,iBAAO,OACA;AAAE,4FADA,qBAAW,OACA;AAAE,+FADA,wBAAc,OACA;AAAE,yFADA,kBAAQ,OACA;AAAE,2FADA,oBAAU,OACA;AAE/F,wDAAoD;AAC3C,0FADA,qBAAS,OACA;AAElB,4DAAyD;AAChD,4FADA,yBAAW,OACA;AAEpB,sEAAmE;AAC1D,iGADA,mCAAgB,OACA;AAEzB,wDAAqD;AAC5C,0FADA,qBAAS,OACA;AAElB,+CAAgD;AACvC,6FADA,uBAAY,OACA;AAErB,IAAA,uBAAY,EAAC,KAAK,EAAE,EAAE,WAAW,EAAE,qBAAS,EAAE,CAAC,CAAC;AAChD,IAAA,uBAAY,EAAC,OAAO,EAAE,EAAE,WAAW,EAAE,yBAAW,EAAE,CAAC,CAAC;AACpD,IAAA,uBAAY,EAAC,KAAK,EAAE,EAAE,WAAW,EAAE,qBAAS,EAAE,CAAC,CAAC;AAChD,IAAA,uBAAY,EAAC,YAAY,EAAE,EAAE,WAAW,EAAE,mCAAgB,GAAG,CAAC,CAAC;AAE/D,QAAQ;AACR,iCAAsC;AAA7B,oGAAA,WAAW,OAAA;AAIpB,4CAA4C;AAEnC,wBAAM;AADf,4CAA4C;AAC3B,wBAAM;AAEvB,aAAa;AACb,2CAIsB;AAHlB,wGAAA,UAAU,OAAA;AACV,4GAAA,cAAc,OAAA;AACd,6GAAA,eAAe,OAAA;AAGnB,uCAAsC;AAA7B,oGAAA,QAAQ,OAAA;AAEjB,6CASuB;AARnB,cAAc;AACd,mGAAA,IAAI,OAAA;AACJ,yGAAA,UAAU,OAAA;AACV,0GAAA,WAAW,OAAA;AACX,mGAAA,IAAI,OAAA;AAEJ,YAAY;AACZ,0GAAA,WAAW,OAAA;AAMf,6CAA4C;AAAnC,kGAAA,OAAO,OAAA;AAChB,6DAA0G;AAAjG,wHAAA,qBAAqB,OAAA;AAAE,0HAAA,WAAW,OAA2B;AACtE,mDAAuD;AAA9C,wGAAA,UAAU,OAAA;AACnB,iDAAgD;AAAvC,sGAAA,SAAS,OAAA;AAElB,6CAA4C;AAAnC,kGAAA,OAAO,OAAA;AAChB,6DAA2F;AAAlF,wHAAA,qBAAqB,OAAA;AAAE,0HAAA,uBAAuB,OAAA;AAEvD,wCAA4C;AAAnC,iGAAA,SAAS,OAAA","sourcesContent":["export { Schema } from \"./Schema\";\nexport type { DataChange } from \"./decoder/DecodeOperation\";\n\nimport { $track, $encoder, $decoder, $filter, $getByIndex, $deleteByIndex, $changes, $childType } from \"./types/symbols\";\nexport { $track, $encoder, $decoder, $filter, $getByIndex, $deleteByIndex, $changes, $childType };\n\nimport { MapSchema } from \"./types/custom/MapSchema\"\nexport { MapSchema };\n\nimport { ArraySchema } from \"./types/custom/ArraySchema\";\nexport { ArraySchema };\n\nimport { CollectionSchema } from \"./types/custom/CollectionSchema\";\nexport { CollectionSchema };\n\nimport { SetSchema } from \"./types/custom/SetSchema\";\nexport { SetSchema };\n\nimport { registerType } from \"./types/registry\";\nexport { registerType };\n\nregisterType(\"map\", { constructor: MapSchema });\nregisterType(\"array\", { constructor: ArraySchema });\nregisterType(\"set\", { constructor: SetSchema });\nregisterType(\"collection\", { constructor: CollectionSchema, });\n\n// Utils\nexport { dumpChanges } from \"./utils\";\n\n// Encoder / Decoder\nexport type { Iterator } from \"./encoding/decode\";\nimport * as encode from \"./encoding/encode\";\nimport * as decode from \"./encoding/decode\";\nexport { encode, decode };\n\n// Reflection\nexport {\n Reflection,\n ReflectionType,\n ReflectionField,\n} from \"./Reflection\";\n\nexport { Metadata } from \"./Metadata\";\n\nexport {\n // Annotations\n type,\n deprecated,\n defineTypes,\n view,\n\n // Internals\n TypeContext,\n} from \"./annotations\";\n\n// Annotation types\nexport type { DefinitionType, PrimitiveType, Definition, } from \"./annotations\";\n\nexport { Encoder } from \"./encoder/Encoder\";\nexport { encodeSchemaOperation, encodeArray as encodeKeyValueOperation } from \"./encoder/EncodeOperation\";\nexport { ChangeTree, Ref } from \"./encoder/ChangeTree\";\nexport { StateView } from \"./encoder/StateView\";\n\nexport { Decoder } from \"./decoder/Decoder\";\nexport { decodeSchemaOperation, decodeKeyValueOperation } from \"./decoder/DecodeOperation\";\n\nexport { OPERATION } from \"./encoding/spec\";"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAAkC;AAAzB,gGAAA,MAAM,OAAA;AAGf,6CAAyH;AAChH,uFADA,gBAAM,OACA;AAAE,yFADA,kBAAQ,OACA;AAAE,yFADA,kBAAQ,OACA;AAAE,wFADA,iBAAO,OACA;AAAE,4FADA,qBAAW,OACA;AAAE,+FADA,wBAAc,OACA;AAAE,yFADA,kBAAQ,OACA;AAAE,2FADA,oBAAU,OACA;AAE/F,wDAAoD;AAC3C,0FADA,qBAAS,OACA;AAElB,4DAAyD;AAChD,4FADA,yBAAW,OACA;AAEpB,sEAAmE;AAC1D,iGADA,mCAAgB,OACA;AAEzB,wDAAqD;AAC5C,0FADA,qBAAS,OACA;AAElB,+CAAgD;AACvC,6FADA,uBAAY,OACA;AAErB,IAAA,uBAAY,EAAC,KAAK,EAAE,EAAE,WAAW,EAAE,qBAAS,EAAE,CAAC,CAAC;AAChD,IAAA,uBAAY,EAAC,OAAO,EAAE,EAAE,WAAW,EAAE,yBAAW,EAAE,CAAC,CAAC;AACpD,IAAA,uBAAY,EAAC,KAAK,EAAE,EAAE,WAAW,EAAE,qBAAS,EAAE,CAAC,CAAC;AAChD,IAAA,uBAAY,EAAC,YAAY,EAAE,EAAE,WAAW,EAAE,mCAAgB,GAAG,CAAC,CAAC;AAE/D,QAAQ;AACR,iCAAsC;AAA7B,oGAAA,WAAW,OAAA;AAIpB,4CAA4C;AAEnC,wBAAM;AADf,4CAA4C;AAC3B,wBAAM;AAEvB,aAAa;AACb,2CAIsB;AAHlB,wGAAA,UAAU,OAAA;AACV,4GAAA,cAAc,OAAA;AACd,6GAAA,eAAe,OAAA;AAGnB,uCAAsC;AAA7B,oGAAA,QAAQ,OAAA;AAEjB,6CASuB;AARnB,cAAc;AACd,mGAAA,IAAI,OAAA;AACJ,yGAAA,UAAU,OAAA;AACV,0GAAA,WAAW,OAAA;AACX,mGAAA,IAAI,OAAA;AAEJ,YAAY;AACZ,0GAAA,WAAW,OAAA;AAMf,oEAAqF;AAA5E,mHAAA,iBAAiB,OAAA;AAC1B,4DAAsE;AAA7D,mHAAA,qBAAqB,OAAA;AAE9B,6CAA4C;AAAnC,kGAAA,OAAO,OAAA;AAChB,6DAA0G;AAAjG,wHAAA,qBAAqB,OAAA;AAAE,0HAAA,WAAW,OAA2B;AACtE,mDAAuD;AAA9C,wGAAA,UAAU,OAAA;AACnB,iDAAgD;AAAvC,sGAAA,SAAS,OAAA;AAElB,6CAA4C;AAAnC,kGAAA,OAAO,OAAA;AAChB,6DAA2F;AAAlF,wHAAA,qBAAqB,OAAA;AAAE,0HAAA,uBAAuB,OAAA;AAEvD,wCAA4C;AAAnC,iGAAA,SAAS,OAAA","sourcesContent":["export { Schema } from \"./Schema\";\nexport type { DataChange } from \"./decoder/DecodeOperation\";\n\nimport { $track, $encoder, $decoder, $filter, $getByIndex, $deleteByIndex, $changes, $childType } from \"./types/symbols\";\nexport { $track, $encoder, $decoder, $filter, $getByIndex, $deleteByIndex, $changes, $childType };\n\nimport { MapSchema } from \"./types/custom/MapSchema\"\nexport { MapSchema };\n\nimport { ArraySchema } from \"./types/custom/ArraySchema\";\nexport { ArraySchema };\n\nimport { CollectionSchema } from \"./types/custom/CollectionSchema\";\nexport { CollectionSchema };\n\nimport { SetSchema } from \"./types/custom/SetSchema\";\nexport { SetSchema };\n\nimport { registerType } from \"./types/registry\";\nexport { registerType };\n\nregisterType(\"map\", { constructor: MapSchema });\nregisterType(\"array\", { constructor: ArraySchema });\nregisterType(\"set\", { constructor: SetSchema });\nregisterType(\"collection\", { constructor: CollectionSchema, });\n\n// Utils\nexport { dumpChanges } from \"./utils\";\n\n// Encoder / Decoder\nexport type { Iterator } from \"./encoding/decode\";\nimport * as encode from \"./encoding/encode\";\nimport * as decode from \"./encoding/decode\";\nexport { encode, decode };\n\n// Reflection\nexport {\n Reflection,\n ReflectionType,\n ReflectionField,\n} from \"./Reflection\";\n\nexport { Metadata } from \"./Metadata\";\n\nexport {\n // Annotations\n type,\n deprecated,\n defineTypes,\n view,\n\n // Internals\n TypeContext,\n} from \"./annotations\";\n\n// Annotation types\nexport type { DefinitionType, PrimitiveType, Definition, } from \"./annotations\";\n\nexport { getStateCallbacks, CallbackProxy } from \"./decoder/strategy/StateCallbacks\";\nexport { getRawChangesCallback } from \"./decoder/strategy/RawChanges\";\n\nexport { Encoder } from \"./encoder/Encoder\";\nexport { encodeSchemaOperation, encodeArray as encodeKeyValueOperation } from \"./encoder/EncodeOperation\";\nexport { ChangeTree, Ref } from \"./encoder/ChangeTree\";\nexport { StateView } from \"./encoder/StateView\";\n\nexport { Decoder } from \"./decoder/Decoder\";\nexport { decodeSchemaOperation, decodeKeyValueOperation } from \"./decoder/DecodeOperation\";\n\nexport { OPERATION } from \"./encoding/spec\";"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/schema",
3
- "version": "3.0.0-alpha.11",
3
+ "version": "3.0.0-alpha.13",
4
4
  "description": "Binary state serializer with delta encoding for games",
5
5
  "bin": {
6
6
  "schema-codegen": "./bin/schema-codegen"
@@ -17,7 +17,7 @@ import type { ArraySchema } from "../../types/custom/ArraySchema";
17
17
  // - Avoid closures by allowing to pass a context. (https://github.com/colyseus/schema/issues/155#issuecomment-1804694081)
18
18
  //
19
19
 
20
- type GetProxyType<T> = unknown extends T // is "any"?
20
+ export type CallbackProxy<T> = unknown extends T // is "any"?
21
21
  ? InstanceCallback<T> & CollectionCallback<any, any>
22
22
  : T extends Collection<infer K, infer V, infer _>
23
23
  ? CollectionCallback<K, V>
@@ -32,7 +32,7 @@ type InstanceCallback<T> = {
32
32
  onChange(callback: () => void): void;
33
33
  bindTo(targetObject: any, properties?: Array<NonFunctionPropNames<T>>): void;
34
34
  } & {
35
- [K in NonFunctionNonPrimitivePropNames<T>]: GetProxyType<T[K]>;
35
+ [K in NonFunctionNonPrimitivePropNames<T>]: CallbackProxy<T[K]>;
36
36
  }
37
37
 
38
38
  type CollectionCallback<K, V> = {
@@ -48,7 +48,7 @@ type CallContext = {
48
48
  onInstanceAvailable?: OnInstanceAvailableCallback,
49
49
  }
50
50
 
51
- export function getStateCallbacks(decoder: Decoder) {
51
+ export function getStateCallbacks<T extends Schema>(decoder: Decoder<T>): CallbackProxy<T> {
52
52
  const $root = decoder.root;
53
53
  const callbacks = $root.callbacks;
54
54
 
@@ -76,8 +76,6 @@ export function getStateCallbacks(decoder: Decoder) {
76
76
  for (let i = deleteCallbacks?.length - 1; i >= 0; i--) {
77
77
  deleteCallbacks[i]();
78
78
  }
79
- // callbacks[$root.refIds.get(change.previousValue)]?.[OPERATION.DELETE]?.forEach(callback =>
80
- // callback());
81
79
  }
82
80
 
83
81
  if (ref instanceof Schema) {
@@ -206,9 +204,13 @@ export function getStateCallbacks(decoder: Decoder) {
206
204
  OPERATION.REPLACE,
207
205
  callback
208
206
  );
209
-
210
207
  },
211
208
  bindTo: function bindTo(targetObject: any, properties?: string[]) {
209
+ // return $root.addCallback(
210
+ // $root.refIds.get(context.instance),
211
+ // OPERATION.BIND,
212
+ // callback
213
+ // );
212
214
  console.log("bindTo", targetObject, properties);
213
215
  }
214
216
  }, {
@@ -307,20 +309,9 @@ export function getStateCallbacks(decoder: Decoder) {
307
309
  }
308
310
  }
309
311
 
310
- function $<T>(instance: T): GetProxyType<T> {
311
- return getProxy(undefined, { instance }) as GetProxyType<T>;
312
- }
313
-
314
- return {
315
- $,
316
- trigger: function trigger(changes: DataChange[]) {
317
- for (let i = 0, l = changes.length; i < l; i++) {
318
- const change = changes[i];
319
-
320
- change.op
321
- change.ref
322
- }
323
- }
312
+ function $<T>(instance: T): CallbackProxy<T> {
313
+ return getProxy(undefined, { instance }) as CallbackProxy<T>;
324
314
  }
325
315
 
316
+ return $(decoder.state);
326
317
  }
@@ -44,11 +44,11 @@ export class Encoder<T extends Schema = any> {
44
44
  it: Iterator = { offset: 0 },
45
45
  view?: StateView,
46
46
  buffer = this.sharedBuffer,
47
- changeTrees = this.root.changes
47
+ changeTrees = this.root.changes,
48
+ isEncodeAll = this.root.allChanges === changeTrees,
48
49
  ): Buffer {
49
50
  const initialOffset = it.offset; // cache current offset in case we need to resize the buffer
50
51
 
51
- const isEncodeAll = this.root.allChanges === changeTrees;
52
52
  const hasView = (view !== undefined);
53
53
  const rootChangeTree = this.state[$changes];
54
54
 
@@ -131,7 +131,7 @@ export class Encoder<T extends Schema = any> {
131
131
  this.sharedBuffer = buffer;
132
132
  }
133
133
 
134
- return this.encode({ offset: initialOffset }, view, buffer);
134
+ return this.encode({ offset: initialOffset }, view, buffer, changeTrees, isEncodeAll);
135
135
 
136
136
  } else {
137
137
  //
@@ -155,7 +155,7 @@ export class Encoder<T extends Schema = any> {
155
155
  // console.log("->", { ref: item[0].ref.constructor.name, refId: item[0].refId, changes: item[1].size });
156
156
  // });
157
157
 
158
- return this.encode(it, undefined, buffer, this.root.allChanges);
158
+ return this.encode(it, undefined, buffer, this.root.allChanges, true);
159
159
  }
160
160
 
161
161
  encodeAllView(view: StateView, sharedOffset: number, it: Iterator, bytes = this.sharedBuffer) {
@@ -165,7 +165,7 @@ export class Encoder<T extends Schema = any> {
165
165
  // this.debugAllFilteredChanges();
166
166
 
167
167
  // try to encode "filtered" changes
168
- this.encode(it, view, bytes, this.root.allFilteredChanges);
168
+ this.encode(it, view, bytes, this.root.allFilteredChanges, true);
169
169
 
170
170
  return Buffer.concat([
171
171
  bytes.subarray(0, sharedOffset),
package/src/index.ts CHANGED
@@ -56,6 +56,9 @@ export {
56
56
  // Annotation types
57
57
  export type { DefinitionType, PrimitiveType, Definition, } from "./annotations";
58
58
 
59
+ export { getStateCallbacks, CallbackProxy } from "./decoder/strategy/StateCallbacks";
60
+ export { getRawChangesCallback } from "./decoder/strategy/RawChanges";
61
+
59
62
  export { Encoder } from "./encoder/Encoder";
60
63
  export { encodeSchemaOperation, encodeArray as encodeKeyValueOperation } from "./encoder/EncodeOperation";
61
64
  export { ChangeTree, Ref } from "./encoder/ChangeTree";