@colyseus/schema 4.0.5 → 4.0.7
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/bin/schema-codegen +1 -1
- package/build/codegen/cli.cjs +1555 -0
- package/build/codegen/cli.cjs.map +1 -0
- package/build/decoder/DecodeOperation.d.ts +3 -3
- package/build/decoder/Decoder.d.ts +3 -3
- package/build/decoder/ReferenceTracker.d.ts +3 -3
- package/build/decoder/strategy/Callbacks.d.ts +5 -5
- package/build/index.cjs +38 -5
- package/build/index.cjs.map +1 -1
- package/build/index.js +38 -5
- package/build/index.mjs +38 -5
- package/build/index.mjs.map +1 -1
- package/build/types/HelperTypes.d.ts +5 -1
- package/package.json +21 -20
- package/src/Metadata.ts +5 -1
- package/src/annotations.ts +34 -5
- package/src/codegen/api.ts +12 -11
- package/src/decoder/DecodeOperation.ts +3 -5
- package/src/decoder/Decoder.ts +3 -3
- package/src/decoder/ReferenceTracker.ts +4 -4
- package/src/decoder/strategy/Callbacks.ts +5 -5
- package/src/types/HelperTypes.ts +2 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Definition, DefinitionType, PrimitiveType } from "../annotations.js";
|
|
1
|
+
import type { Definition, DefinitionType, PrimitiveType, RawPrimitiveType } from "../annotations.js";
|
|
2
2
|
import type { Schema } from "../Schema.js";
|
|
3
3
|
import type { ArraySchema } from "./custom/ArraySchema.js";
|
|
4
4
|
import type { CollectionSchema } from "./custom/CollectionSchema.js";
|
|
@@ -42,10 +42,14 @@ export type InferValueType<T extends DefinitionType> = T extends "string" ? stri
|
|
|
42
42
|
} ? (ChildType extends Record<string | number, string | number> ? MapSchema<ChildType[keyof ChildType]> : MapSchema<PrimitiveStringToType<ChildType>>) : T extends {
|
|
43
43
|
set: infer ChildType extends Constructor;
|
|
44
44
|
} ? SetSchema<InstanceType<ChildType>> : T extends {
|
|
45
|
+
set: infer ChildType extends RawPrimitiveType;
|
|
46
|
+
} ? SetSchema<InferValueType<ChildType>> : T extends {
|
|
45
47
|
set: infer ChildType;
|
|
46
48
|
} ? (ChildType extends Record<string | number, string | number> ? SetSchema<ChildType[keyof ChildType]> : SetSchema<ChildType>) : T extends {
|
|
47
49
|
collection: infer ChildType extends Constructor;
|
|
48
50
|
} ? CollectionSchema<InstanceType<ChildType>> : T extends {
|
|
51
|
+
collection: infer ChildType extends RawPrimitiveType;
|
|
52
|
+
} ? CollectionSchema<InferValueType<ChildType>> : T extends {
|
|
49
53
|
collection: infer ChildType;
|
|
50
54
|
} ? (ChildType extends Record<string | number, string | number> ? CollectionSchema<ChildType[keyof ChildType]> : CollectionSchema<ChildType>) : T extends Constructor ? InstanceType<T> : T extends Record<string | number, string | number> ? T[keyof T] : T extends PrimitiveType ? T : never;
|
|
51
55
|
export type InferSchemaInstanceType<T extends Definition> = {
|
package/package.json
CHANGED
|
@@ -1,12 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@colyseus/schema",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.7",
|
|
4
4
|
"description": "Binary state serializer with delta encoding for games",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"schema-codegen": "bin/schema-codegen",
|
|
8
8
|
"schema-debug": "bin/schema-debug"
|
|
9
9
|
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc -p tsconfig.build.json && rollup -c rollup.config.mjs",
|
|
12
|
+
"watch": "tsc -p tsconfig.build.json -w",
|
|
13
|
+
"test": "tsx --tsconfig tsconfig.test.json ./node_modules/.bin/mocha test/*.test.ts test/**/*.test.ts",
|
|
14
|
+
"coverage": "c8 npm run test",
|
|
15
|
+
"generate-test-1": "bin/schema-codegen test-external/PrimitiveTypes.ts --namespace SchemaTest.PrimitiveTypes --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/PrimitiveTypes",
|
|
16
|
+
"generate-test-2": "bin/schema-codegen test-external/ChildSchemaTypes.ts --namespace SchemaTest.ChildSchemaTypes --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/ChildSchemaTypes",
|
|
17
|
+
"generate-test-3": "bin/schema-codegen test-external/ArraySchemaTypes.ts --namespace SchemaTest.ArraySchemaTypes --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/ArraySchemaTypes",
|
|
18
|
+
"generate-test-4": "bin/schema-codegen test-external/MapSchemaTypes.ts --namespace SchemaTest.MapSchemaTypes --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/MapSchemaTypes",
|
|
19
|
+
"generate-test-5": "bin/schema-codegen test-external/InheritedTypes.ts --namespace SchemaTest.InheritedTypes --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/InheritedTypes",
|
|
20
|
+
"generate-test-6": "bin/schema-codegen test-external/MapSchemaInt8.ts --namespace SchemaTest.MapSchemaInt8 --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/MapSchemaInt8",
|
|
21
|
+
"generate-test-7": "bin/schema-codegen test-external/BackwardsForwards.ts --namespace SchemaTest.BackwardsForwards --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/BackwardsForwards",
|
|
22
|
+
"generate-test-8": "bin/schema-codegen test-external/FilteredTypes.ts --namespace SchemaTest.FilteredTypes --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/FilteredTypes",
|
|
23
|
+
"generate-test-9": "bin/schema-codegen test-external/InstanceSharingTypes.ts --namespace SchemaTest.InstanceSharingTypes --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/InstanceSharingTypes",
|
|
24
|
+
"generate-test-10": "bin/schema-codegen test-external/Callbacks.ts --namespace SchemaTest.Callbacks --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/Callbacks",
|
|
25
|
+
"generate-test-11": "bin/schema-codegen test-external/MapSchemaMoveNullifyType.ts --namespace SchemaTest.MapSchemaMoveNullifyType --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/MapSchemaMoveNullifyType",
|
|
26
|
+
"generate-test-12": "bin/schema-codegen test-external/ArraySchemaClear --namespace SchemaTest.ArraySchemaClear --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/ArraySchemaClear",
|
|
27
|
+
"prepublishOnly": "npm run build"
|
|
28
|
+
},
|
|
10
29
|
"files": [
|
|
11
30
|
"src",
|
|
12
31
|
"build",
|
|
@@ -74,23 +93,5 @@
|
|
|
74
93
|
"text"
|
|
75
94
|
],
|
|
76
95
|
"all": true
|
|
77
|
-
},
|
|
78
|
-
"scripts": {
|
|
79
|
-
"build": "tsc -p tsconfig.build.json && rollup -c rollup.config.mjs",
|
|
80
|
-
"watch": "tsc -p tsconfig.build.json -w",
|
|
81
|
-
"test": "tsx --tsconfig tsconfig.test.json ./node_modules/.bin/mocha test/*.test.ts test/**/*.test.ts",
|
|
82
|
-
"coverage": "c8 npm run test",
|
|
83
|
-
"generate-test-1": "bin/schema-codegen test-external/PrimitiveTypes.ts --namespace SchemaTest.PrimitiveTypes --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/PrimitiveTypes",
|
|
84
|
-
"generate-test-2": "bin/schema-codegen test-external/ChildSchemaTypes.ts --namespace SchemaTest.ChildSchemaTypes --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/ChildSchemaTypes",
|
|
85
|
-
"generate-test-3": "bin/schema-codegen test-external/ArraySchemaTypes.ts --namespace SchemaTest.ArraySchemaTypes --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/ArraySchemaTypes",
|
|
86
|
-
"generate-test-4": "bin/schema-codegen test-external/MapSchemaTypes.ts --namespace SchemaTest.MapSchemaTypes --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/MapSchemaTypes",
|
|
87
|
-
"generate-test-5": "bin/schema-codegen test-external/InheritedTypes.ts --namespace SchemaTest.InheritedTypes --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/InheritedTypes",
|
|
88
|
-
"generate-test-6": "bin/schema-codegen test-external/MapSchemaInt8.ts --namespace SchemaTest.MapSchemaInt8 --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/MapSchemaInt8",
|
|
89
|
-
"generate-test-7": "bin/schema-codegen test-external/BackwardsForwards.ts --namespace SchemaTest.BackwardsForwards --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/BackwardsForwards",
|
|
90
|
-
"generate-test-8": "bin/schema-codegen test-external/FilteredTypes.ts --namespace SchemaTest.FilteredTypes --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/FilteredTypes",
|
|
91
|
-
"generate-test-9": "bin/schema-codegen test-external/InstanceSharingTypes.ts --namespace SchemaTest.InstanceSharingTypes --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/InstanceSharingTypes",
|
|
92
|
-
"generate-test-10": "bin/schema-codegen test-external/Callbacks.ts --namespace SchemaTest.Callbacks --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/Callbacks",
|
|
93
|
-
"generate-test-11": "bin/schema-codegen test-external/MapSchemaMoveNullifyType.ts --namespace SchemaTest.MapSchemaMoveNullifyType --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/MapSchemaMoveNullifyType",
|
|
94
|
-
"generate-test-12": "bin/schema-codegen test-external/ArraySchemaClear --namespace SchemaTest.ArraySchemaClear --output ../colyseus-unity-sdk/Assets/Colyseus/Tests/Editor/ColyseusTests/Schema/ArraySchemaClear"
|
|
95
96
|
}
|
|
96
|
-
}
|
|
97
|
+
}
|
package/src/Metadata.ts
CHANGED
|
@@ -294,7 +294,11 @@ export const Metadata = {
|
|
|
294
294
|
}
|
|
295
295
|
}
|
|
296
296
|
|
|
297
|
-
constructor
|
|
297
|
+
Object.defineProperty(constructor, Symbol.metadata, {
|
|
298
|
+
value: metadata,
|
|
299
|
+
writable: false,
|
|
300
|
+
configurable: true
|
|
301
|
+
});
|
|
298
302
|
|
|
299
303
|
return metadata;
|
|
300
304
|
},
|
package/src/annotations.ts
CHANGED
|
@@ -600,7 +600,12 @@ export function schema<
|
|
|
600
600
|
|
|
601
601
|
} else if (value['type'] !== undefined && Schema.is(value['type'])) {
|
|
602
602
|
// Direct Schema type: Type → new Type()
|
|
603
|
-
|
|
603
|
+
if (!value['type'].prototype.initialize || value['type'].prototype.initialize.length === 0) {
|
|
604
|
+
// only auto-initialize Schema instances if:
|
|
605
|
+
// - they don't have an initialize method
|
|
606
|
+
// - or initialize method doesn't accept any parameters
|
|
607
|
+
defaultValues[fieldName] = new value['type']();
|
|
608
|
+
}
|
|
604
609
|
}
|
|
605
610
|
} else {
|
|
606
611
|
defaultValues[fieldName] = value['default'];
|
|
@@ -610,7 +615,12 @@ export function schema<
|
|
|
610
615
|
} else if (typeof (value) === "function") {
|
|
611
616
|
if (Schema.is(value)) {
|
|
612
617
|
// Direct Schema type: Type → new Type()
|
|
613
|
-
|
|
618
|
+
if (!value.prototype.initialize || value.prototype.initialize.length === 0) {
|
|
619
|
+
// only auto-initialize Schema instances if:
|
|
620
|
+
// - they don't have an initialize method
|
|
621
|
+
// - or initialize method doesn't accept any parameters
|
|
622
|
+
defaultValues[fieldName] = new value();
|
|
623
|
+
}
|
|
614
624
|
fields[fieldName] = getNormalizedType(value);
|
|
615
625
|
} else {
|
|
616
626
|
methods[fieldName] = value;
|
|
@@ -638,14 +648,33 @@ export function schema<
|
|
|
638
648
|
return defaults;
|
|
639
649
|
};
|
|
640
650
|
|
|
651
|
+
const getParentProps = (props: any) => {
|
|
652
|
+
const fieldNames = Object.keys(fields);
|
|
653
|
+
const parentProps: any = {};
|
|
654
|
+
for (const key in props) {
|
|
655
|
+
if (!fieldNames.includes(key)) {
|
|
656
|
+
parentProps[key] = props[key];
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
return parentProps;
|
|
660
|
+
}
|
|
661
|
+
|
|
641
662
|
/** @codegen-ignore */
|
|
642
663
|
const klass = Metadata.setFields<any>(class extends (inherits as any) {
|
|
643
664
|
constructor(...args: any[]) {
|
|
644
|
-
super(Object.assign({}, getDefaultValues(), args[0] || {}));
|
|
645
|
-
|
|
646
665
|
// call initialize method
|
|
647
666
|
if (methods.initialize && typeof methods.initialize === 'function') {
|
|
648
|
-
|
|
667
|
+
super(Object.assign({}, getDefaultValues(), getParentProps(args[0] || {})));
|
|
668
|
+
/**
|
|
669
|
+
* only call initialize() in the current class, not the parent ones.
|
|
670
|
+
* see "should not call initialize automatically when creating an instance of inherited Schema"
|
|
671
|
+
*/
|
|
672
|
+
if (new.target === klass) {
|
|
673
|
+
methods.initialize.apply(this, args);
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
} else {
|
|
677
|
+
super(Object.assign({}, getDefaultValues(), args[0] || {}));
|
|
649
678
|
}
|
|
650
679
|
}
|
|
651
680
|
}, fields) as SchemaWithExtendsConstructor<T, ExtractInitProps<T>, P>;
|
package/src/codegen/api.ts
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import * as fs from "fs";
|
|
2
2
|
import * as path from "path";
|
|
3
|
-
import { createRequire } from "module";
|
|
4
|
-
// import { fileURLToPath } from "url";
|
|
5
3
|
|
|
6
4
|
import { File } from "./types.js";
|
|
7
5
|
import { parseFiles } from "./parser.js";
|
|
8
6
|
|
|
9
|
-
//
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
// Statically import all language generators (for bundling)
|
|
8
|
+
import { generate as csharp } from "./languages/csharp.js";
|
|
9
|
+
import { generate as cpp } from "./languages/cpp.js";
|
|
10
|
+
import { generate as haxe } from "./languages/haxe.js";
|
|
11
|
+
import { generate as ts } from "./languages/ts.js";
|
|
12
|
+
import { generate as js } from "./languages/js.js";
|
|
13
|
+
import { generate as java } from "./languages/java.js";
|
|
14
|
+
import { generate as lua } from "./languages/lua.js";
|
|
15
|
+
|
|
16
|
+
const generators: Record<string, Function> = { csharp, cpp, haxe, ts, js, java, lua, };
|
|
13
17
|
|
|
14
18
|
export interface GenerateOptions {
|
|
15
19
|
files: string[],
|
|
@@ -19,12 +23,9 @@ export interface GenerateOptions {
|
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
export function generate(targetId: string, options: GenerateOptions) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
try {
|
|
25
|
-
generator = require('./languages/' + targetId + '.js').generate;
|
|
26
|
+
const generator = generators[targetId];
|
|
26
27
|
|
|
27
|
-
|
|
28
|
+
if (!generator) {
|
|
28
29
|
throw new Error("You must provide a valid generator as argument, such as: --csharp, --haxe or --cpp");
|
|
29
30
|
}
|
|
30
31
|
|
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
import { OPERATION } from "../encoding/spec.js";
|
|
2
2
|
import { Metadata } from "../Metadata.js";
|
|
3
3
|
import { Schema } from "../Schema.js";
|
|
4
|
-
import type { Ref } from "../encoder/ChangeTree.js";
|
|
4
|
+
import type { IRef, Ref } from "../encoder/ChangeTree.js";
|
|
5
5
|
import type { Decoder } from "./Decoder.js";
|
|
6
6
|
import { Iterator, decode } from "../encoding/decode.js";
|
|
7
7
|
import { $childType, $deleteByIndex, $getByIndex, $refId } from "../types/symbols.js";
|
|
8
8
|
|
|
9
|
-
import type { MapSchema } from "../types/custom/MapSchema.js";
|
|
10
9
|
import type { ArraySchema } from "../types/custom/ArraySchema.js";
|
|
11
|
-
import type { CollectionSchema } from "../types/custom/CollectionSchema.js";
|
|
12
10
|
|
|
13
11
|
import { getType } from "../types/registry.js";
|
|
14
12
|
import { Collection } from "../types/HelperTypes.js";
|
|
15
13
|
|
|
16
14
|
export interface DataChange<T = any, F = string> {
|
|
17
|
-
ref:
|
|
15
|
+
ref: IRef,
|
|
18
16
|
refId: number,
|
|
19
17
|
op: OPERATION,
|
|
20
18
|
field: F;
|
|
@@ -29,7 +27,7 @@ export type DecodeOperation<T extends Schema = any> = (
|
|
|
29
27
|
decoder: Decoder<T>,
|
|
30
28
|
bytes: Uint8Array,
|
|
31
29
|
it: Iterator,
|
|
32
|
-
ref:
|
|
30
|
+
ref: IRef,
|
|
33
31
|
allChanges: DataChange[],
|
|
34
32
|
) => number | void;
|
|
35
33
|
|
package/src/decoder/Decoder.ts
CHANGED
|
@@ -4,13 +4,13 @@ import { Schema } from "../Schema.js";
|
|
|
4
4
|
|
|
5
5
|
import { decode } from "../encoding/decode.js";
|
|
6
6
|
import { OPERATION, SWITCH_TO_STRUCTURE, TYPE_ID } from '../encoding/spec.js';
|
|
7
|
-
import type { Ref } from "../encoder/ChangeTree.js";
|
|
7
|
+
import type { IRef, Ref } from "../encoder/ChangeTree.js";
|
|
8
8
|
import type { Iterator } from "../encoding/decode.js";
|
|
9
9
|
import { ReferenceTracker } from "./ReferenceTracker.js";
|
|
10
10
|
import { DEFINITION_MISMATCH, type DataChange, type DecodeOperation } from "./DecodeOperation.js";
|
|
11
11
|
import { Collection } from "../types/HelperTypes.js";
|
|
12
12
|
|
|
13
|
-
export class Decoder<T extends
|
|
13
|
+
export class Decoder<T extends IRef = any> {
|
|
14
14
|
context: TypeContext;
|
|
15
15
|
|
|
16
16
|
state: T;
|
|
@@ -40,7 +40,7 @@ export class Decoder<T extends Schema = any> {
|
|
|
40
40
|
decode(
|
|
41
41
|
bytes: Uint8Array,
|
|
42
42
|
it: Iterator = { offset: 0 },
|
|
43
|
-
ref:
|
|
43
|
+
ref: IRef = this.state,
|
|
44
44
|
) {
|
|
45
45
|
const allChanges: DataChange[] = [];
|
|
46
46
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Metadata } from "../Metadata.js";
|
|
2
2
|
import { $childType, $refId } from "../types/symbols.js";
|
|
3
|
-
import {
|
|
3
|
+
import type { IRef } from "../encoder/ChangeTree.js";
|
|
4
4
|
import { spliceOne } from "../types/utils.js";
|
|
5
5
|
import { OPERATION } from "../encoding/spec.js";
|
|
6
6
|
|
|
@@ -25,7 +25,7 @@ export class ReferenceTracker {
|
|
|
25
25
|
// Relation of refId => Schema structure
|
|
26
26
|
// For direct access of structures during decoding time.
|
|
27
27
|
//
|
|
28
|
-
public refs = new Map<number,
|
|
28
|
+
public refs = new Map<number, IRef>();
|
|
29
29
|
|
|
30
30
|
public refCount: { [refId: number]: number; } = {};
|
|
31
31
|
public deletedRefs = new Set<number>();
|
|
@@ -38,7 +38,7 @@ export class ReferenceTracker {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
// for decoding
|
|
41
|
-
addRef(refId: number, ref:
|
|
41
|
+
addRef(refId: number, ref: IRef, incrementCount: boolean = true) {
|
|
42
42
|
this.refs.set(refId, ref);
|
|
43
43
|
ref[$refId] = refId;
|
|
44
44
|
|
|
@@ -103,7 +103,7 @@ export class ReferenceTracker {
|
|
|
103
103
|
const metadata: Metadata = (ref.constructor as typeof Schema)[Symbol.metadata];
|
|
104
104
|
for (const index in metadata) {
|
|
105
105
|
const field = metadata[index as any as number].name;
|
|
106
|
-
const child = ref[field as keyof
|
|
106
|
+
const child = ref[field as keyof IRef];
|
|
107
107
|
if (typeof(child) === "object" && child) {
|
|
108
108
|
const childRefId = (child as any)[$refId];
|
|
109
109
|
if (childRefId !== undefined && !this.deletedRefs.has(childRefId)) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Metadata } from "../../Metadata.js";
|
|
2
2
|
import { Collection, NonFunctionPropNames } from "../../types/HelperTypes.js";
|
|
3
|
-
import { Ref } from "../../encoder/ChangeTree.js";
|
|
3
|
+
import type { IRef, Ref } from "../../encoder/ChangeTree.js";
|
|
4
4
|
import { Decoder } from "../Decoder.js";
|
|
5
5
|
import { DataChange } from "../DecodeOperation.js";
|
|
6
6
|
import { OPERATION } from "../../encoding/spec.js";
|
|
@@ -45,7 +45,7 @@ type CollectionKeyType<T, K extends keyof T> =
|
|
|
45
45
|
T[K] extends ArraySchema<any> ? number :
|
|
46
46
|
T[K] extends Collection<infer Key, any, any> ? Key : never;
|
|
47
47
|
|
|
48
|
-
export class StateCallbackStrategy<TState extends
|
|
48
|
+
export class StateCallbackStrategy<TState extends IRef> {
|
|
49
49
|
protected decoder: Decoder<TState>;
|
|
50
50
|
protected uniqueRefIds: Set<number> = new Set();
|
|
51
51
|
protected isTriggering: boolean = false;
|
|
@@ -72,7 +72,7 @@ export class StateCallbackStrategy<TState extends Schema> {
|
|
|
72
72
|
return $root.addCallback(refId, operationOrProperty, handler);
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
protected addCallbackOrWaitCollectionAvailable<TInstance extends
|
|
75
|
+
protected addCallbackOrWaitCollectionAvailable<TInstance extends IRef, TReturn extends Ref>(
|
|
76
76
|
instance: TInstance,
|
|
77
77
|
propertyName: string,
|
|
78
78
|
operation: OPERATION,
|
|
@@ -142,7 +142,7 @@ export class StateCallbackStrategy<TState extends Schema> {
|
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
-
protected listenInstance<TInstance extends
|
|
145
|
+
protected listenInstance<TInstance extends IRef>(
|
|
146
146
|
instance: TInstance,
|
|
147
147
|
propertyName: string,
|
|
148
148
|
handler: PropertyChangeCallback<any>,
|
|
@@ -485,7 +485,7 @@ export const Callbacks = {
|
|
|
485
485
|
* @param roomOrDecoder - Room or Decoder instance to get the callbacks for.
|
|
486
486
|
* @returns the new callbacks standard API.
|
|
487
487
|
*/
|
|
488
|
-
get<T extends
|
|
488
|
+
get<T extends IRef>(roomOrDecoder: Decoder<T> | { serializer: { decoder: Decoder<T> } }): StateCallbackStrategy<T> {
|
|
489
489
|
if (roomOrDecoder instanceof Decoder) {
|
|
490
490
|
return new StateCallbackStrategy<T>(roomOrDecoder);
|
|
491
491
|
|
package/src/types/HelperTypes.ts
CHANGED
|
@@ -56,9 +56,11 @@ export type InferValueType<T extends DefinitionType> =
|
|
|
56
56
|
: T extends { map: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? MapSchema<ChildType[keyof ChildType]> : MapSchema<PrimitiveStringToType<ChildType>>) // TS ENUM
|
|
57
57
|
|
|
58
58
|
: T extends { set: infer ChildType extends Constructor } ? SetSchema<InstanceType<ChildType>>
|
|
59
|
+
: T extends { set: infer ChildType extends RawPrimitiveType } ? SetSchema<InferValueType<ChildType>> // primitive types
|
|
59
60
|
: T extends { set: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? SetSchema<ChildType[keyof ChildType]> : SetSchema<ChildType>) // TS ENUM
|
|
60
61
|
|
|
61
62
|
: T extends { collection: infer ChildType extends Constructor } ? CollectionSchema<InstanceType<ChildType>>
|
|
63
|
+
: T extends { collection: infer ChildType extends RawPrimitiveType } ? CollectionSchema<InferValueType<ChildType>> // primitive types
|
|
62
64
|
: T extends { collection: infer ChildType } ? (ChildType extends Record<string | number, string | number> ? CollectionSchema<ChildType[keyof ChildType]> : CollectionSchema<ChildType>) // TS ENUM
|
|
63
65
|
|
|
64
66
|
// Handle direct types
|