@colyseus/schema 4.0.1 → 4.0.2
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/build/cjs/{index.js → index.cjs} +1 -1
- package/build/cjs/index.cjs.map +1 -0
- package/build/esm/index.mjs.map +1 -1
- package/lib/Metadata.js +54 -58
- package/lib/Metadata.js.map +1 -1
- package/lib/Reflection.js +29 -32
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.js +41 -45
- package/lib/Schema.js.map +1 -1
- package/lib/annotations.d.ts +1 -1
- package/lib/annotations.js +58 -69
- package/lib/annotations.js.map +1 -1
- package/lib/bench_encode.js +24 -59
- package/lib/bench_encode.js.map +1 -1
- package/lib/benchmark.d.ts +1 -0
- package/lib/benchmark.js +218 -0
- package/lib/benchmark.js.map +1 -0
- package/lib/codegen/api.js +10 -41
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/argv.js +1 -3
- package/lib/codegen/argv.js.map +1 -1
- package/lib/codegen/cli.js +4 -9
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/languages/cpp.js +5 -8
- package/lib/codegen/languages/cpp.js.map +1 -1
- package/lib/codegen/languages/csharp.js +5 -8
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/languages/haxe.js +3 -6
- package/lib/codegen/languages/haxe.js.map +1 -1
- package/lib/codegen/languages/java.js +3 -6
- package/lib/codegen/languages/java.js.map +1 -1
- package/lib/codegen/languages/js.js +4 -7
- package/lib/codegen/languages/js.js.map +1 -1
- package/lib/codegen/languages/lua.js +4 -7
- package/lib/codegen/languages/lua.js.map +1 -1
- package/lib/codegen/languages/ts.js +5 -8
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.js +22 -59
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.js +12 -51
- package/lib/codegen/types.js.map +1 -1
- package/lib/decoder/DecodeOperation.js +44 -51
- package/lib/decoder/DecodeOperation.js.map +1 -1
- package/lib/decoder/Decoder.js +24 -28
- package/lib/decoder/Decoder.js.map +1 -1
- package/lib/decoder/ReferenceTracker.js +10 -14
- package/lib/decoder/ReferenceTracker.js.map +1 -1
- package/lib/decoder/strategy/Callbacks.js +39 -43
- package/lib/decoder/strategy/Callbacks.js.map +1 -1
- package/lib/decoder/strategy/RawChanges.js +1 -4
- package/lib/decoder/strategy/RawChanges.js.map +1 -1
- package/lib/decoder/strategy/getDecoderStateCallbacks.js +24 -27
- package/lib/decoder/strategy/getDecoderStateCallbacks.js.map +1 -1
- package/lib/encoder/ChangeTree.js +36 -44
- package/lib/encoder/ChangeTree.js.map +1 -1
- package/lib/encoder/EncodeOperation.js +25 -32
- package/lib/encoder/EncodeOperation.js.map +1 -1
- package/lib/encoder/Encoder.js +25 -29
- package/lib/encoder/Encoder.js.map +1 -1
- package/lib/encoder/Root.js +17 -21
- package/lib/encoder/Root.js.map +1 -1
- package/lib/encoder/StateView.js +40 -45
- package/lib/encoder/StateView.js.map +1 -1
- package/lib/encoding/assert.js +3 -9
- package/lib/encoding/assert.js.map +1 -1
- package/lib/encoding/decode.js +2 -6
- package/lib/encoding/decode.js.map +1 -1
- package/lib/encoding/encode.js +1 -4
- package/lib/encoding/encode.js.map +1 -1
- package/lib/encoding/spec.js +4 -7
- package/lib/encoding/spec.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +33 -75
- package/lib/index.js.map +1 -1
- package/lib/src/Metadata.d.ts +49 -0
- package/lib/src/Metadata.js +256 -0
- package/lib/src/Metadata.js.map +1 -0
- package/lib/src/Reflection.d.ts +71 -0
- package/lib/src/Reflection.js +179 -0
- package/lib/src/Reflection.js.map +1 -0
- package/lib/src/Schema.d.ts +86 -0
- package/lib/src/Schema.js +352 -0
- package/lib/src/Schema.js.map +1 -0
- package/lib/src/annotations.d.ts +109 -0
- package/lib/src/annotations.js +473 -0
- package/lib/src/annotations.js.map +1 -0
- package/lib/src/bench_encode.d.ts +1 -0
- package/lib/src/bench_encode.js +91 -0
- package/lib/src/bench_encode.js.map +1 -0
- package/lib/src/codegen/api.d.ts +7 -0
- package/lib/src/codegen/api.js +56 -0
- package/lib/src/codegen/api.js.map +1 -0
- package/lib/src/codegen/argv.d.ts +6 -0
- package/lib/src/codegen/argv.js +39 -0
- package/lib/src/codegen/argv.js.map +1 -0
- package/lib/src/codegen/cli.d.ts +1 -0
- package/lib/src/codegen/cli.js +58 -0
- package/lib/src/codegen/cli.js.map +1 -0
- package/lib/src/codegen/languages/cpp.d.ts +3 -0
- package/lib/src/codegen/languages/cpp.js +258 -0
- package/lib/src/codegen/languages/cpp.js.map +1 -0
- package/lib/src/codegen/languages/csharp.d.ts +4 -0
- package/lib/src/codegen/languages/csharp.js +154 -0
- package/lib/src/codegen/languages/csharp.js.map +1 -0
- package/lib/src/codegen/languages/haxe.d.ts +3 -0
- package/lib/src/codegen/languages/haxe.js +100 -0
- package/lib/src/codegen/languages/haxe.js.map +1 -0
- package/lib/src/codegen/languages/java.d.ts +6 -0
- package/lib/src/codegen/languages/java.js +100 -0
- package/lib/src/codegen/languages/java.js.map +1 -0
- package/lib/src/codegen/languages/js.d.ts +3 -0
- package/lib/src/codegen/languages/js.js +101 -0
- package/lib/src/codegen/languages/js.js.map +1 -0
- package/lib/src/codegen/languages/lua.d.ts +3 -0
- package/lib/src/codegen/languages/lua.js +103 -0
- package/lib/src/codegen/languages/lua.js.map +1 -0
- package/lib/src/codegen/languages/ts.d.ts +3 -0
- package/lib/src/codegen/languages/ts.js +116 -0
- package/lib/src/codegen/languages/ts.js.map +1 -0
- package/lib/src/codegen/parser.d.ts +13 -0
- package/lib/src/codegen/parser.js +327 -0
- package/lib/src/codegen/parser.js.map +1 -0
- package/lib/src/codegen/types.d.ts +52 -0
- package/lib/src/codegen/types.js +142 -0
- package/lib/src/codegen/types.js.map +1 -0
- package/lib/src/decoder/DecodeOperation.d.ts +23 -0
- package/lib/src/decoder/DecodeOperation.js +248 -0
- package/lib/src/decoder/DecodeOperation.js.map +1 -0
- package/lib/src/decoder/Decoder.d.ts +21 -0
- package/lib/src/decoder/Decoder.js +114 -0
- package/lib/src/decoder/Decoder.js.map +1 -0
- package/lib/src/decoder/ReferenceTracker.d.ts +25 -0
- package/lib/src/decoder/ReferenceTracker.js +135 -0
- package/lib/src/decoder/ReferenceTracker.js.map +1 -0
- package/lib/src/decoder/strategy/Callbacks.d.ts +154 -0
- package/lib/src/decoder/strategy/Callbacks.js +336 -0
- package/lib/src/decoder/strategy/Callbacks.js.map +1 -0
- package/lib/src/decoder/strategy/RawChanges.d.ts +3 -0
- package/lib/src/decoder/strategy/RawChanges.js +4 -0
- package/lib/src/decoder/strategy/RawChanges.js.map +1 -0
- package/lib/src/decoder/strategy/getDecoderStateCallbacks.d.ts +76 -0
- package/lib/src/decoder/strategy/getDecoderStateCallbacks.js +274 -0
- package/lib/src/decoder/strategy/getDecoderStateCallbacks.js.map +1 -0
- package/lib/src/encoder/ChangeTree.d.ts +135 -0
- package/lib/src/encoder/ChangeTree.js +534 -0
- package/lib/src/encoder/ChangeTree.js.map +1 -0
- package/lib/src/encoder/EncodeOperation.d.ts +22 -0
- package/lib/src/encoder/EncodeOperation.js +132 -0
- package/lib/src/encoder/EncodeOperation.js.map +1 -0
- package/lib/src/encoder/Encoder.d.ts +22 -0
- package/lib/src/encoder/Encoder.js +204 -0
- package/lib/src/encoder/Encoder.js.map +1 -0
- package/lib/src/encoder/Root.d.ts +28 -0
- package/lib/src/encoder/Root.js +229 -0
- package/lib/src/encoder/Root.js.map +1 -0
- package/lib/src/encoder/StateView.d.ts +34 -0
- package/lib/src/encoder/StateView.js +279 -0
- package/lib/src/encoder/StateView.js.map +1 -0
- package/lib/src/encoding/assert.d.ts +10 -0
- package/lib/src/encoding/assert.js +49 -0
- package/lib/src/encoding/assert.js.map +1 -0
- package/lib/src/encoding/decode.d.ts +67 -0
- package/lib/src/encoding/decode.js +217 -0
- package/lib/src/encoding/decode.js.map +1 -0
- package/lib/src/encoding/encode.d.ts +40 -0
- package/lib/src/encoding/encode.js +279 -0
- package/lib/src/encoding/encode.js.map +1 -0
- package/lib/src/encoding/spec.d.ts +24 -0
- package/lib/src/encoding/spec.js +26 -0
- package/lib/src/encoding/spec.js.map +1 -0
- package/lib/src/index.d.ts +32 -0
- package/lib/src/index.js +39 -0
- package/lib/src/index.js.map +1 -0
- package/lib/src/symbol.shim.d.ts +6 -0
- package/lib/src/symbol.shim.js +3 -0
- package/lib/src/symbol.shim.js.map +1 -0
- package/lib/src/types/HelperTypes.d.ts +77 -0
- package/lib/src/types/HelperTypes.js +2 -0
- package/lib/src/types/HelperTypes.js.map +1 -0
- package/lib/src/types/TypeContext.d.ts +31 -0
- package/lib/src/types/TypeContext.js +143 -0
- package/lib/src/types/TypeContext.js.map +1 -0
- package/lib/src/types/custom/ArraySchema.d.ts +270 -0
- package/lib/src/types/custom/ArraySchema.js +733 -0
- package/lib/src/types/custom/ArraySchema.js.map +1 -0
- package/lib/src/types/custom/CollectionSchema.d.ts +51 -0
- package/lib/src/types/custom/CollectionSchema.js +170 -0
- package/lib/src/types/custom/CollectionSchema.js.map +1 -0
- package/lib/src/types/custom/MapSchema.d.ts +51 -0
- package/lib/src/types/custom/MapSchema.js +222 -0
- package/lib/src/types/custom/MapSchema.js.map +1 -0
- package/lib/src/types/custom/SetSchema.d.ts +48 -0
- package/lib/src/types/custom/SetSchema.js +178 -0
- package/lib/src/types/custom/SetSchema.js.map +1 -0
- package/lib/src/types/registry.d.ts +16 -0
- package/lib/src/types/registry.js +30 -0
- package/lib/src/types/registry.js.map +1 -0
- package/lib/src/types/symbols.d.ts +33 -0
- package/lib/src/types/symbols.js +34 -0
- package/lib/src/types/symbols.js.map +1 -0
- package/lib/src/types/utils.d.ts +1 -0
- package/lib/src/types/utils.js +13 -0
- package/lib/src/types/utils.js.map +1 -0
- package/lib/src/utils.d.ts +13 -0
- package/lib/src/utils.js +49 -0
- package/lib/src/utils.js.map +1 -0
- package/lib/src/v3_bench.d.ts +1 -0
- package/lib/src/v3_bench.js +128 -0
- package/lib/src/v3_bench.js.map +1 -0
- package/lib/symbol.shim.js +1 -2
- package/lib/symbol.shim.js.map +1 -1
- package/lib/types/HelperTypes.js +1 -2
- package/lib/types/TypeContext.js +8 -12
- package/lib/types/TypeContext.js.map +1 -1
- package/lib/types/custom/ArraySchema.js +63 -67
- package/lib/types/custom/ArraySchema.js.map +1 -1
- package/lib/types/custom/CollectionSchema.js +27 -31
- package/lib/types/custom/CollectionSchema.js.map +1 -1
- package/lib/types/custom/MapSchema.js +37 -41
- package/lib/types/custom/MapSchema.js.map +1 -1
- package/lib/types/custom/SetSchema.js +28 -32
- package/lib/types/custom/SetSchema.js.map +1 -1
- package/lib/types/registry.js +13 -20
- package/lib/types/registry.js.map +1 -1
- package/lib/types/symbols.js +16 -19
- package/lib/types/symbols.js.map +1 -1
- package/lib/types/utils.js +1 -4
- package/lib/types/utils.js.map +1 -1
- package/lib/utils.js +9 -14
- package/lib/utils.js.map +1 -1
- package/lib/v3_bench.js +17 -19
- package/lib/v3_bench.js.map +1 -1
- package/package.json +9 -7
- package/src/codegen/api.ts +7 -0
- package/src/codegen/parser.ts +2 -2
- package/src/codegen/types.ts +5 -0
- package/src/index.ts +2 -2
- package/build/cjs/index.js.map +0 -1
- package/src/bench_encode.ts +0 -64
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { Collection, NonFunctionPropNames } from "../../types/HelperTypes";
|
|
2
|
+
import { Ref } from "../../encoder/ChangeTree";
|
|
3
|
+
import { Decoder } from "../Decoder";
|
|
4
|
+
import { DataChange } from "../DecodeOperation";
|
|
5
|
+
import { OPERATION } from "../../encoding/spec";
|
|
6
|
+
import { Schema } from "../../Schema";
|
|
7
|
+
import { $refId } from "../../types/symbols";
|
|
8
|
+
import { MapSchema } from "../../types/custom/MapSchema";
|
|
9
|
+
import { ArraySchema } from "../../types/custom/ArraySchema";
|
|
10
|
+
import { type SchemaCallbackProxy } from "./getDecoderStateCallbacks";
|
|
11
|
+
type PropertyChangeCallback<K> = (currentValue: K, previousValue: K) => void;
|
|
12
|
+
type KeyValueCallback<K, V> = (key: K, value: V) => void;
|
|
13
|
+
type InstanceChangeCallback = () => void;
|
|
14
|
+
type PublicPropNames<T> = Exclude<NonFunctionPropNames<T>, typeof $refId> & string;
|
|
15
|
+
type CollectionPropNames<T> = Exclude<{
|
|
16
|
+
[K in keyof T]: T[K] extends Collection<any, any> ? K : never;
|
|
17
|
+
}[keyof T] & string, typeof $refId>;
|
|
18
|
+
type CollectionValueType<T, K extends keyof T> = T[K] extends MapSchema<infer V, any> ? V : T[K] extends ArraySchema<infer V> ? V : T[K] extends Collection<any, infer V, any> ? V : never;
|
|
19
|
+
type CollectionKeyType<T, K extends keyof T> = T[K] extends MapSchema<any, infer Key> ? Key : T[K] extends ArraySchema<any> ? number : T[K] extends Collection<infer Key, any, any> ? Key : never;
|
|
20
|
+
/**
|
|
21
|
+
* State Callbacks handler
|
|
22
|
+
*
|
|
23
|
+
* Usage:
|
|
24
|
+
* ```ts
|
|
25
|
+
* const $ = Callbacks.get(decoder);
|
|
26
|
+
*
|
|
27
|
+
* // Listen to property changes
|
|
28
|
+
* $.listen("currentTurn", (currentValue, previousValue) => { ... });
|
|
29
|
+
*
|
|
30
|
+
* // Listen to collection additions
|
|
31
|
+
* $.onAdd("entities", (sessionId, entity) => {
|
|
32
|
+
* // Nested property listening
|
|
33
|
+
* $.listen(entity, "hp", (currentHp, previousHp) => { ... });
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* // Listen to collection removals
|
|
37
|
+
* $.onRemove("entities", (sessionId, entity) => { ... });
|
|
38
|
+
*
|
|
39
|
+
* // Listen to any property change on an instance
|
|
40
|
+
* $.onChange(entity, () => { ... });
|
|
41
|
+
*
|
|
42
|
+
* // Bind properties to another object
|
|
43
|
+
* $.bindTo(player, playerVisual);
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare class StateCallbackStrategy<TState extends Schema> {
|
|
47
|
+
protected decoder: Decoder<TState>;
|
|
48
|
+
protected uniqueRefIds: Set<number>;
|
|
49
|
+
protected isTriggering: boolean;
|
|
50
|
+
constructor(decoder: Decoder<TState>);
|
|
51
|
+
protected get callbacks(): {
|
|
52
|
+
[refId: number]: import("../ReferenceTracker").SchemaCallbacks;
|
|
53
|
+
};
|
|
54
|
+
protected get state(): TState;
|
|
55
|
+
protected addCallback(refId: number, operationOrProperty: OPERATION | string, handler: Function): () => void;
|
|
56
|
+
protected addCallbackOrWaitCollectionAvailable<TInstance extends Schema, TReturn extends Ref>(instance: TInstance, propertyName: string, operation: OPERATION, handler: Function, immediate?: boolean): () => void;
|
|
57
|
+
/**
|
|
58
|
+
* Listen to property changes on the root state.
|
|
59
|
+
*/
|
|
60
|
+
listen<K extends PublicPropNames<TState>>(property: K, handler: PropertyChangeCallback<TState[K]>, immediate?: boolean): () => void;
|
|
61
|
+
/**
|
|
62
|
+
* Listen to property changes on a nested instance.
|
|
63
|
+
*/
|
|
64
|
+
listen<TInstance extends Schema, K extends PublicPropNames<TInstance>>(instance: TInstance, property: K, handler: PropertyChangeCallback<TInstance[K]>, immediate?: boolean): () => void;
|
|
65
|
+
protected listenInstance<TInstance extends Schema>(instance: TInstance, propertyName: string, handler: PropertyChangeCallback<any>, immediate?: boolean): () => void;
|
|
66
|
+
/**
|
|
67
|
+
* Listen to any property change on an instance.
|
|
68
|
+
*/
|
|
69
|
+
onChange<TInstance extends Schema>(instance: TInstance, handler: InstanceChangeCallback): () => void;
|
|
70
|
+
/**
|
|
71
|
+
* Listen to item changes in a collection on root state.
|
|
72
|
+
*/
|
|
73
|
+
onChange<K extends CollectionPropNames<TState>>(property: K, handler: KeyValueCallback<CollectionKeyType<TState, K>, CollectionValueType<TState, K>>): () => void;
|
|
74
|
+
/**
|
|
75
|
+
* Listen to item changes in a nested collection.
|
|
76
|
+
*/
|
|
77
|
+
onChange<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: KeyValueCallback<CollectionKeyType<TInstance, K>, CollectionValueType<TInstance, K>>): () => void;
|
|
78
|
+
/**
|
|
79
|
+
* Listen to items added to a collection on root state.
|
|
80
|
+
*/
|
|
81
|
+
onAdd<K extends CollectionPropNames<TState>>(property: K, handler: KeyValueCallback<CollectionKeyType<TState, K>, CollectionValueType<TState, K>>, immediate?: boolean): () => void;
|
|
82
|
+
/**
|
|
83
|
+
* Listen to items added to a nested collection.
|
|
84
|
+
*/
|
|
85
|
+
onAdd<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: KeyValueCallback<CollectionKeyType<TInstance, K>, CollectionValueType<TInstance, K>>, immediate?: boolean): () => void;
|
|
86
|
+
/**
|
|
87
|
+
* Listen to items removed from a collection on root state.
|
|
88
|
+
*/
|
|
89
|
+
onRemove<K extends CollectionPropNames<TState>>(property: K, handler: KeyValueCallback<CollectionKeyType<TState, K>, CollectionValueType<TState, K>>): () => void;
|
|
90
|
+
/**
|
|
91
|
+
* Listen to items removed from a nested collection.
|
|
92
|
+
*/
|
|
93
|
+
onRemove<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: KeyValueCallback<CollectionKeyType<TInstance, K>, CollectionValueType<TInstance, K>>): () => void;
|
|
94
|
+
/**
|
|
95
|
+
* Bind properties from a Schema instance to a target object.
|
|
96
|
+
* Changes will be automatically reflected on the target object.
|
|
97
|
+
*/
|
|
98
|
+
bindTo<TInstance extends Schema, TTarget>(from: TInstance, to: TTarget, properties?: string[], immediate?: boolean): () => void;
|
|
99
|
+
protected triggerChanges(allChanges: DataChange[]): void;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Factory class for retrieving the callbacks API.
|
|
103
|
+
*/
|
|
104
|
+
export declare const Callbacks: {
|
|
105
|
+
/**
|
|
106
|
+
* Get the new callbacks standard API.
|
|
107
|
+
*
|
|
108
|
+
* Usage:
|
|
109
|
+
* ```ts
|
|
110
|
+
* const callbacks = Callbacks.get(roomOrDecoder);
|
|
111
|
+
*
|
|
112
|
+
* // Listen to property changes
|
|
113
|
+
* callbacks.listen("currentTurn", (currentValue, previousValue) => { ... });
|
|
114
|
+
*
|
|
115
|
+
* // Listen to collection additions
|
|
116
|
+
* callbacks.onAdd("entities", (sessionId, entity) => {
|
|
117
|
+
* // Nested property listening
|
|
118
|
+
* callbacks.listen(entity, "hp", (currentHp, previousHp) => { ... });
|
|
119
|
+
* });
|
|
120
|
+
*
|
|
121
|
+
* // Listen to collection removals
|
|
122
|
+
* callbacks.onRemove("entities", (sessionId, entity) => { ... });
|
|
123
|
+
*
|
|
124
|
+
* // Listen to any property change on an instance
|
|
125
|
+
* callbacks.onChange(entity, () => { ... });
|
|
126
|
+
*
|
|
127
|
+
* // Bind properties to another object
|
|
128
|
+
* callbacks.bindTo(player, playerVisual);
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* @param roomOrDecoder - Room or Decoder instance to get the callbacks for.
|
|
132
|
+
* @returns the new callbacks standard API.
|
|
133
|
+
*/
|
|
134
|
+
get<T extends Schema>(roomOrDecoder: Decoder<T> | {
|
|
135
|
+
serializer: {
|
|
136
|
+
decoder: Decoder<T>;
|
|
137
|
+
};
|
|
138
|
+
}): StateCallbackStrategy<T>;
|
|
139
|
+
/**
|
|
140
|
+
* Get the legacy callbacks API.
|
|
141
|
+
*
|
|
142
|
+
* We aim to deprecate this API on 1.0, and iterate on improving Callbacks.get() API.
|
|
143
|
+
*
|
|
144
|
+
* @param roomOrDecoder - Room or Decoder instance to get the legacy callbacks for.
|
|
145
|
+
* @returns the legacy callbacks API.
|
|
146
|
+
*/
|
|
147
|
+
getLegacy<T extends Schema>(roomOrDecoder: Decoder<T> | {
|
|
148
|
+
serializer: {
|
|
149
|
+
decoder: Decoder<T>;
|
|
150
|
+
};
|
|
151
|
+
}): SchemaCallbackProxy<T>;
|
|
152
|
+
getRawChanges(decoder: Decoder, callback: (changes: DataChange[]) => void): void;
|
|
153
|
+
};
|
|
154
|
+
export {};
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import { Decoder } from "../Decoder";
|
|
2
|
+
import { OPERATION } from "../../encoding/spec";
|
|
3
|
+
import { Schema } from "../../Schema";
|
|
4
|
+
import { $refId } from "../../types/symbols";
|
|
5
|
+
import { getDecoderStateCallbacks } from "./getDecoderStateCallbacks";
|
|
6
|
+
import { getRawChangesCallback } from "./RawChanges";
|
|
7
|
+
/**
|
|
8
|
+
* State Callbacks handler
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
* ```ts
|
|
12
|
+
* const $ = Callbacks.get(decoder);
|
|
13
|
+
*
|
|
14
|
+
* // Listen to property changes
|
|
15
|
+
* $.listen("currentTurn", (currentValue, previousValue) => { ... });
|
|
16
|
+
*
|
|
17
|
+
* // Listen to collection additions
|
|
18
|
+
* $.onAdd("entities", (sessionId, entity) => {
|
|
19
|
+
* // Nested property listening
|
|
20
|
+
* $.listen(entity, "hp", (currentHp, previousHp) => { ... });
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Listen to collection removals
|
|
24
|
+
* $.onRemove("entities", (sessionId, entity) => { ... });
|
|
25
|
+
*
|
|
26
|
+
* // Listen to any property change on an instance
|
|
27
|
+
* $.onChange(entity, () => { ... });
|
|
28
|
+
*
|
|
29
|
+
* // Bind properties to another object
|
|
30
|
+
* $.bindTo(player, playerVisual);
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export class StateCallbackStrategy {
|
|
34
|
+
constructor(decoder) {
|
|
35
|
+
this.uniqueRefIds = new Set();
|
|
36
|
+
this.isTriggering = false;
|
|
37
|
+
this.decoder = decoder;
|
|
38
|
+
this.decoder.triggerChanges = this.triggerChanges.bind(this);
|
|
39
|
+
}
|
|
40
|
+
get callbacks() {
|
|
41
|
+
return this.decoder.root.callbacks;
|
|
42
|
+
}
|
|
43
|
+
get state() {
|
|
44
|
+
return this.decoder.state;
|
|
45
|
+
}
|
|
46
|
+
addCallback(refId, operationOrProperty, handler) {
|
|
47
|
+
const $root = this.decoder.root;
|
|
48
|
+
return $root.addCallback(refId, operationOrProperty, handler);
|
|
49
|
+
}
|
|
50
|
+
addCallbackOrWaitCollectionAvailable(instance, propertyName, operation, handler, immediate = true) {
|
|
51
|
+
let removeHandler = () => { };
|
|
52
|
+
const removeOnAdd = () => removeHandler();
|
|
53
|
+
const collection = instance[propertyName];
|
|
54
|
+
// Collection not available yet. Listen for its availability before attaching the handler.
|
|
55
|
+
if (collection === null || collection === undefined) {
|
|
56
|
+
removeHandler = this.addCallback(instance[$refId], propertyName, (value, _) => {
|
|
57
|
+
if (value !== null && value !== undefined) {
|
|
58
|
+
removeHandler = this.addCallback(value[$refId], operation, handler);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
return removeOnAdd;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
//
|
|
65
|
+
// Call immediately if collection is already available, if it's an ADD operation.
|
|
66
|
+
//
|
|
67
|
+
immediate = immediate && this.isTriggering === false;
|
|
68
|
+
if (operation === OPERATION.ADD && immediate) {
|
|
69
|
+
collection.forEach((value, key) => {
|
|
70
|
+
handler(key, value);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return this.addCallback(collection[$refId], operation, handler);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
listen(...args) {
|
|
77
|
+
if (typeof args[0] === 'string') {
|
|
78
|
+
// listen(property, handler, immediate?)
|
|
79
|
+
return this.listenInstance(this.state, args[0], args[1], args[2]);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
// listen(instance, property, handler, immediate?)
|
|
83
|
+
return this.listenInstance(args[0], args[1], args[2], args[3]);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
listenInstance(instance, propertyName, handler, immediate = true) {
|
|
87
|
+
immediate = immediate && this.isTriggering === false;
|
|
88
|
+
//
|
|
89
|
+
// Call handler immediately if property is already available.
|
|
90
|
+
//
|
|
91
|
+
const currentValue = instance[propertyName];
|
|
92
|
+
if (immediate && currentValue !== null && currentValue !== undefined) {
|
|
93
|
+
handler(currentValue, undefined);
|
|
94
|
+
}
|
|
95
|
+
return this.addCallback(instance[$refId], propertyName, handler);
|
|
96
|
+
}
|
|
97
|
+
onChange(...args) {
|
|
98
|
+
if (args.length === 2 && typeof args[0] !== 'string') {
|
|
99
|
+
// onChange(instance, handler) - instance change
|
|
100
|
+
const instance = args[0];
|
|
101
|
+
const handler = args[1];
|
|
102
|
+
return this.addCallback(instance[$refId], OPERATION.REPLACE, handler);
|
|
103
|
+
}
|
|
104
|
+
if (typeof args[0] === 'string') {
|
|
105
|
+
// onChange(property, handler) - collection on root state
|
|
106
|
+
return this.addCallbackOrWaitCollectionAvailable(this.state, args[0], OPERATION.REPLACE, args[1]);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
// onChange(instance, property, handler) - nested collection
|
|
110
|
+
return this.addCallbackOrWaitCollectionAvailable(args[0], args[1], OPERATION.REPLACE, args[2]);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
onAdd(...args) {
|
|
114
|
+
if (typeof args[0] === 'string') {
|
|
115
|
+
// onAdd(property, handler, immediate?) - collection on root state
|
|
116
|
+
return this.addCallbackOrWaitCollectionAvailable(this.state, args[0], OPERATION.ADD, args[1], args[2] !== false);
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
// onAdd(instance, property, handler, immediate?) - nested collection
|
|
120
|
+
return this.addCallbackOrWaitCollectionAvailable(args[0], args[1], OPERATION.ADD, args[2], args[3] !== false);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
onRemove(...args) {
|
|
124
|
+
if (typeof args[0] === 'string') {
|
|
125
|
+
// onRemove(property, handler) - collection on root state
|
|
126
|
+
return this.addCallbackOrWaitCollectionAvailable(this.state, args[0], OPERATION.DELETE, args[1]);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
// onRemove(instance, property, handler) - nested collection
|
|
130
|
+
return this.addCallbackOrWaitCollectionAvailable(args[0], args[1], OPERATION.DELETE, args[2]);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Bind properties from a Schema instance to a target object.
|
|
135
|
+
* Changes will be automatically reflected on the target object.
|
|
136
|
+
*/
|
|
137
|
+
bindTo(from, to, properties, immediate = true) {
|
|
138
|
+
const metadata = from.constructor[Symbol.metadata];
|
|
139
|
+
// If no properties specified, bind all properties
|
|
140
|
+
if (!properties) {
|
|
141
|
+
properties = Object.keys(metadata)
|
|
142
|
+
.filter(key => !isNaN(Number(key)))
|
|
143
|
+
.map((index) => metadata[index].name);
|
|
144
|
+
}
|
|
145
|
+
const action = () => {
|
|
146
|
+
for (const prop of properties) {
|
|
147
|
+
const fromValue = from[prop];
|
|
148
|
+
if (fromValue !== undefined) {
|
|
149
|
+
to[prop] = fromValue;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
if (immediate) {
|
|
154
|
+
action();
|
|
155
|
+
}
|
|
156
|
+
return this.addCallback(from[$refId], OPERATION.REPLACE, action);
|
|
157
|
+
}
|
|
158
|
+
triggerChanges(allChanges) {
|
|
159
|
+
this.uniqueRefIds.clear();
|
|
160
|
+
for (let i = 0, l = allChanges.length; i < l; i++) {
|
|
161
|
+
const change = allChanges[i];
|
|
162
|
+
const refId = change.refId;
|
|
163
|
+
const ref = change.ref;
|
|
164
|
+
const $callbacks = this.callbacks[refId];
|
|
165
|
+
if (!$callbacks) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
//
|
|
169
|
+
// trigger onRemove on child structure.
|
|
170
|
+
//
|
|
171
|
+
if ((change.op & OPERATION.DELETE) === OPERATION.DELETE &&
|
|
172
|
+
change.previousValue instanceof Schema) {
|
|
173
|
+
const childRefId = change.previousValue[$refId];
|
|
174
|
+
const deleteCallbacks = this.callbacks[childRefId]?.[OPERATION.DELETE];
|
|
175
|
+
if (deleteCallbacks) {
|
|
176
|
+
for (let j = deleteCallbacks.length - 1; j >= 0; j--) {
|
|
177
|
+
deleteCallbacks[j]();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (ref instanceof Schema) {
|
|
182
|
+
//
|
|
183
|
+
// Handle Schema instance
|
|
184
|
+
//
|
|
185
|
+
if (!this.uniqueRefIds.has(refId)) {
|
|
186
|
+
// trigger onChange
|
|
187
|
+
const replaceCallbacks = $callbacks[OPERATION.REPLACE];
|
|
188
|
+
if (replaceCallbacks) {
|
|
189
|
+
for (let j = replaceCallbacks.length - 1; j >= 0; j--) {
|
|
190
|
+
try {
|
|
191
|
+
replaceCallbacks[j]();
|
|
192
|
+
}
|
|
193
|
+
catch (e) {
|
|
194
|
+
console.error(e);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
// trigger field callbacks
|
|
200
|
+
const fieldCallbacks = $callbacks[change.field];
|
|
201
|
+
if (fieldCallbacks) {
|
|
202
|
+
for (let j = fieldCallbacks.length - 1; j >= 0; j--) {
|
|
203
|
+
try {
|
|
204
|
+
this.isTriggering = true;
|
|
205
|
+
fieldCallbacks[j](change.value, change.previousValue);
|
|
206
|
+
}
|
|
207
|
+
catch (e) {
|
|
208
|
+
console.error(e);
|
|
209
|
+
}
|
|
210
|
+
finally {
|
|
211
|
+
this.isTriggering = false;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
//
|
|
218
|
+
// Handle collection of items
|
|
219
|
+
//
|
|
220
|
+
const dynamicIndex = change.dynamicIndex ?? change.field;
|
|
221
|
+
if ((change.op & OPERATION.DELETE) === OPERATION.DELETE) {
|
|
222
|
+
//
|
|
223
|
+
// FIXME: `previousValue` should always be available.
|
|
224
|
+
//
|
|
225
|
+
if (change.previousValue !== undefined) {
|
|
226
|
+
// trigger onRemove (key, value)
|
|
227
|
+
const deleteCallbacks = $callbacks[OPERATION.DELETE];
|
|
228
|
+
if (deleteCallbacks) {
|
|
229
|
+
for (let j = deleteCallbacks.length - 1; j >= 0; j--) {
|
|
230
|
+
deleteCallbacks[j](dynamicIndex, change.previousValue);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// Handle DELETE_AND_ADD operation
|
|
235
|
+
if ((change.op & OPERATION.ADD) === OPERATION.ADD) {
|
|
236
|
+
const addCallbacks = $callbacks[OPERATION.ADD];
|
|
237
|
+
if (addCallbacks) {
|
|
238
|
+
this.isTriggering = true;
|
|
239
|
+
for (let j = addCallbacks.length - 1; j >= 0; j--) {
|
|
240
|
+
addCallbacks[j](dynamicIndex, change.value);
|
|
241
|
+
}
|
|
242
|
+
this.isTriggering = false;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
else if ((change.op & OPERATION.ADD) === OPERATION.ADD &&
|
|
247
|
+
change.previousValue !== change.value) {
|
|
248
|
+
// trigger onAdd (key, value)
|
|
249
|
+
const addCallbacks = $callbacks[OPERATION.ADD];
|
|
250
|
+
if (addCallbacks) {
|
|
251
|
+
this.isTriggering = true;
|
|
252
|
+
for (let j = addCallbacks.length - 1; j >= 0; j--) {
|
|
253
|
+
addCallbacks[j](dynamicIndex, change.value);
|
|
254
|
+
}
|
|
255
|
+
this.isTriggering = false;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
// trigger onChange (key, value)
|
|
259
|
+
if (change.value !== change.previousValue) {
|
|
260
|
+
const replaceCallbacks = $callbacks[OPERATION.REPLACE];
|
|
261
|
+
if (replaceCallbacks) {
|
|
262
|
+
for (let j = replaceCallbacks.length - 1; j >= 0; j--) {
|
|
263
|
+
replaceCallbacks[j](dynamicIndex, change.value);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
this.uniqueRefIds.add(refId);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Factory class for retrieving the callbacks API.
|
|
274
|
+
*/
|
|
275
|
+
export const Callbacks = {
|
|
276
|
+
/**
|
|
277
|
+
* Get the new callbacks standard API.
|
|
278
|
+
*
|
|
279
|
+
* Usage:
|
|
280
|
+
* ```ts
|
|
281
|
+
* const callbacks = Callbacks.get(roomOrDecoder);
|
|
282
|
+
*
|
|
283
|
+
* // Listen to property changes
|
|
284
|
+
* callbacks.listen("currentTurn", (currentValue, previousValue) => { ... });
|
|
285
|
+
*
|
|
286
|
+
* // Listen to collection additions
|
|
287
|
+
* callbacks.onAdd("entities", (sessionId, entity) => {
|
|
288
|
+
* // Nested property listening
|
|
289
|
+
* callbacks.listen(entity, "hp", (currentHp, previousHp) => { ... });
|
|
290
|
+
* });
|
|
291
|
+
*
|
|
292
|
+
* // Listen to collection removals
|
|
293
|
+
* callbacks.onRemove("entities", (sessionId, entity) => { ... });
|
|
294
|
+
*
|
|
295
|
+
* // Listen to any property change on an instance
|
|
296
|
+
* callbacks.onChange(entity, () => { ... });
|
|
297
|
+
*
|
|
298
|
+
* // Bind properties to another object
|
|
299
|
+
* callbacks.bindTo(player, playerVisual);
|
|
300
|
+
* ```
|
|
301
|
+
*
|
|
302
|
+
* @param roomOrDecoder - Room or Decoder instance to get the callbacks for.
|
|
303
|
+
* @returns the new callbacks standard API.
|
|
304
|
+
*/
|
|
305
|
+
get(roomOrDecoder) {
|
|
306
|
+
if (roomOrDecoder instanceof Decoder) {
|
|
307
|
+
return new StateCallbackStrategy(roomOrDecoder);
|
|
308
|
+
}
|
|
309
|
+
else if (roomOrDecoder.serializer.decoder) {
|
|
310
|
+
return new StateCallbackStrategy(roomOrDecoder.serializer.decoder);
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
throw new Error('Invalid room or decoder');
|
|
314
|
+
}
|
|
315
|
+
},
|
|
316
|
+
/**
|
|
317
|
+
* Get the legacy callbacks API.
|
|
318
|
+
*
|
|
319
|
+
* We aim to deprecate this API on 1.0, and iterate on improving Callbacks.get() API.
|
|
320
|
+
*
|
|
321
|
+
* @param roomOrDecoder - Room or Decoder instance to get the legacy callbacks for.
|
|
322
|
+
* @returns the legacy callbacks API.
|
|
323
|
+
*/
|
|
324
|
+
getLegacy(roomOrDecoder) {
|
|
325
|
+
if (roomOrDecoder instanceof Decoder) {
|
|
326
|
+
return getDecoderStateCallbacks(roomOrDecoder);
|
|
327
|
+
}
|
|
328
|
+
else if (roomOrDecoder.serializer.decoder) {
|
|
329
|
+
return getDecoderStateCallbacks(roomOrDecoder.serializer.decoder);
|
|
330
|
+
}
|
|
331
|
+
},
|
|
332
|
+
getRawChanges(decoder, callback) {
|
|
333
|
+
return getRawChangesCallback(decoder, callback);
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
//# sourceMappingURL=Callbacks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Callbacks.js","sourceRoot":"","sources":["../../../../src/decoder/strategy/Callbacks.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAErC,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAG7C,OAAO,EAAE,wBAAwB,EAA4B,MAAM,4BAA4B,CAAC;AAChG,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAoCrD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,OAAO,qBAAqB;IAK9B,YAAY,OAAwB;QAH1B,iBAAY,GAAgB,IAAI,GAAG,EAAE,CAAC;QACtC,iBAAY,GAAY,KAAK,CAAC;QAGpC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,cAAc,C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AAC;YAED,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACjC,CAAC;IACL,CAAC;CACJ;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG;IACrB;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;IACH,GAAG,CAAmB,aAAmE;QACrF,IAAI,aAAa,YAAY,OAAO,EAAE,CAAC;YACnC,OAAO,IAAI,qBAAqB,CAAI,aAAa,CAAC,CAAC;QAEvD,CAAC;aAAM,IAAI,aAAa,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;YAC1C,OAAO,IAAI,qBAAqB,CAAI,aAAa,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QAE1E,CAAC;aAAM,CAAC;YACJ,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;QAC/C,CAAC;IACL,CAAC;IAED;;;;;;;OAOG;IACH,SAAS,CAAmB,aAAmE;QAC3F,IAAI,aAAa,YAAY,OAAO,EAAE,CAAC;YACnC,OAAO,wBAAwB,CAAC,aAAa,CAAC,CAAC;QAEnD,CAAC;aAAM,IAAI,aAAa,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;YAC1C,OAAO,wBAAwB,CAAC,aAAa,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QACtE,CAAC;IACL,CAAC;IAED,aAAa,CAAC,OAAgB,EAAE,QAAyC;QACrE,OAAO,qBAAqB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IACpD,CAAC;CACJ,CAAC","sourcesContent":["import { Metadata } from \"../../Metadata\";\nimport { Collection, NonFunctionPropNames } from \"../../types/HelperTypes\";\nimport { Ref } from \"../../encoder/ChangeTree\";\nimport { Decoder } from \"../Decoder\";\nimport { DataChange } from \"../DecodeOperation\";\nimport { OPERATION } from \"../../encoding/spec\";\nimport { Schema } from \"../../Schema\";\nimport { $refId } from \"../../types/symbols\";\nimport { MapSchema } from \"../../types/custom/MapSchema\";\nimport { ArraySchema } from \"../../types/custom/ArraySchema\";\nimport { getDecoderStateCallbacks, type SchemaCallbackProxy } from \"./getDecoderStateCallbacks\";\nimport { getRawChangesCallback } from \"./RawChanges\";\n\n//\n// C#-style Callbacks API\n// Matches the API from: https://docs.colyseus.io/state/callbacks\n//\n// Key features:\n// - Uses string property names with TypeScript auto-completion\n// - Consistent parameter order (key, value) for collection callbacks\n// - Overloaded methods for nested instance callbacks\n//\n\ntype PropertyChangeCallback<K> = (currentValue: K, previousValue: K) => void;\ntype KeyValueCallback<K, V> = (key: K, value: V) => void;\ntype InstanceChangeCallback = () => void;\n\n// Exclude internal properties from valid property names\ntype PublicPropNames<T> = Exclude<NonFunctionPropNames<T>, typeof $refId> & string;\n\n// Extract only properties that extend Collection\ntype CollectionPropNames<T> = Exclude<{\n [K in keyof T]: T[K] extends Collection<any, any> ? K : never\n}[keyof T] & string, typeof $refId>;\n\n// Infer the value type of a collection property\ntype CollectionValueType<T, K extends keyof T> =\n T[K] extends MapSchema<infer V, any> ? V :\n T[K] extends ArraySchema<infer V> ? V :\n T[K] extends Collection<any, infer V, any> ? V : never;\n\n// Infer the key type of a collection property\ntype CollectionKeyType<T, K extends keyof T> =\n T[K] extends MapSchema<any, infer Key> ? Key :\n T[K] extends ArraySchema<any> ? number :\n T[K] extends Collection<infer Key, any, any> ? Key : never;\n\n/**\n * State Callbacks handler\n *\n * Usage:\n * ```ts\n * const $ = Callbacks.get(decoder);\n *\n * // Listen to property changes\n * $.listen(\"currentTurn\", (currentValue, previousValue) => { ... });\n *\n * // Listen to collection additions\n * $.onAdd(\"entities\", (sessionId, entity) => {\n * // Nested property listening\n * $.listen(entity, \"hp\", (currentHp, previousHp) => { ... });\n * });\n *\n * // Listen to collection removals\n * $.onRemove(\"entities\", (sessionId, entity) => { ... });\n *\n * // Listen to any property change on an instance\n * $.onChange(entity, () => { ... });\n *\n * // Bind properties to another object\n * $.bindTo(player, playerVisual);\n * ```\n */\nexport class StateCallbackStrategy<TState extends Schema> {\n protected decoder: Decoder<TState>;\n protected uniqueRefIds: Set<number> = new Set();\n protected isTriggering: boolean = false;\n\n constructor(decoder: Decoder<TState>) {\n this.decoder = decoder;\n this.decoder.triggerChanges = this.triggerChanges.bind(this);\n }\n\n protected get callbacks() {\n return this.decoder.root.callbacks;\n }\n\n protected get state() {\n return this.decoder.state;\n }\n\n protected addCallback(\n refId: number,\n operationOrProperty: OPERATION | string,\n handler: Function\n ): () => void {\n const $root = this.decoder.root;\n return $root.addCallback(refId, operationOrProperty, handler);\n }\n\n protected addCallbackOrWaitCollectionAvailable<TInstance extends Schema, TReturn extends Ref>(\n instance: TInstance,\n propertyName: string,\n operation: OPERATION,\n handler: Function,\n immediate: boolean = true\n ): () => void {\n let removeHandler: () => void = () => {};\n const removeOnAdd = () => removeHandler();\n\n const collection = (instance as any)[propertyName] as TReturn;\n\n // Collection not available yet. Listen for its availability before attaching the handler.\n if (collection === null || collection === undefined) {\n removeHandler = this.addCallback(\n instance[$refId],\n propertyName,\n (value: TReturn, _: TReturn) => {\n if (value !== null && value !== undefined) {\n removeHandler = this.addCallback(value[$refId], operation, handler);\n }\n }\n );\n return removeOnAdd;\n\n } else {\n //\n // Call immediately if collection is already available, if it's an ADD operation.\n //\n immediate = immediate && this.isTriggering === false;\n\n if (operation === OPERATION.ADD && immediate) {\n (collection as Collection<any, any>).forEach((value: any, key: any) => {\n handler(key, value);\n });\n }\n\n return this.addCallback(collection[$refId], operation, handler);\n }\n }\n\n /**\n * Listen to property changes on the root state.\n */\n listen<K extends PublicPropNames<TState>>(\n property: K,\n handler: PropertyChangeCallback<TState[K]>,\n immediate?: boolean\n ): () => void;\n\n /**\n * Listen to property changes on a nested instance.\n */\n listen<TInstance extends Schema, K extends PublicPropNames<TInstance>>(\n instance: TInstance,\n property: K,\n handler: PropertyChangeCallback<TInstance[K]>,\n immediate?: boolean\n ): () => void;\n\n listen(...args: any[]): () => void {\n if (typeof args[0] === 'string') {\n // listen(property, handler, immediate?)\n return this.listenInstance(this.state, args[0], args[1], args[2]);\n } else {\n // listen(instance, property, handler, immediate?)\n return this.listenInstance(args[0], args[1], args[2], args[3]);\n }\n }\n\n protected listenInstance<TInstance extends Schema>(\n instance: TInstance,\n propertyName: string,\n handler: PropertyChangeCallback<any>,\n immediate: boolean = true\n ): () => void {\n immediate = immediate && this.isTriggering === false;\n\n //\n // Call handler immediately if property is already available.\n //\n const currentValue = (instance as any)[propertyName];\n if (immediate && currentValue !== null && currentValue !== undefined) {\n handler(currentValue, undefined as any);\n }\n\n return this.addCallback(instance[$refId], propertyName, handler);\n }\n\n /**\n * Listen to any property change on an instance.\n */\n onChange<TInstance extends Schema>(\n instance: TInstance,\n handler: InstanceChangeCallback\n ): () => void;\n\n /**\n * Listen to item changes in a collection on root state.\n */\n onChange<K extends CollectionPropNames<TState>>(\n property: K,\n handler: KeyValueCallback<CollectionKeyType<TState, K>, CollectionValueType<TState, K>>\n ): () => void;\n\n /**\n * Listen to item changes in a nested collection.\n */\n onChange<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(\n instance: TInstance,\n property: K,\n handler: KeyValueCallback<CollectionKeyType<TInstance, K>, CollectionValueType<TInstance, K>>\n ): () => void;\n\n onChange(...args: any[]): () => void {\n if (args.length === 2 && typeof args[0] !== 'string') {\n // onChange(instance, handler) - instance change\n const instance = args[0] as Schema;\n const handler = args[1] as InstanceChangeCallback;\n return this.addCallback(instance[$refId], OPERATION.REPLACE, handler);\n }\n\n if (typeof args[0] === 'string') {\n // onChange(property, handler) - collection on root state\n return this.addCallbackOrWaitCollectionAvailable(\n this.state,\n args[0],\n OPERATION.REPLACE,\n args[1]\n );\n } else {\n // onChange(instance, property, handler) - nested collection\n return this.addCallbackOrWaitCollectionAvailable(\n args[0],\n args[1],\n OPERATION.REPLACE,\n args[2]\n );\n }\n }\n\n /**\n * Listen to items added to a collection on root state.\n */\n onAdd<K extends CollectionPropNames<TState>>(\n property: K,\n handler: KeyValueCallback<CollectionKeyType<TState, K>, CollectionValueType<TState, K>>,\n immediate?: boolean\n ): () => void;\n\n /**\n * Listen to items added to a nested collection.\n */\n onAdd<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(\n instance: TInstance,\n property: K,\n handler: KeyValueCallback<CollectionKeyType<TInstance, K>, CollectionValueType<TInstance, K>>,\n immediate?: boolean\n ): () => void;\n\n onAdd(...args: any[]): () => void {\n if (typeof args[0] === 'string') {\n // onAdd(property, handler, immediate?) - collection on root state\n return this.addCallbackOrWaitCollectionAvailable(\n this.state,\n args[0],\n OPERATION.ADD,\n args[1],\n args[2] !== false\n );\n } else {\n // onAdd(instance, property, handler, immediate?) - nested collection\n return this.addCallbackOrWaitCollectionAvailable(\n args[0],\n args[1],\n OPERATION.ADD,\n args[2],\n args[3] !== false\n );\n }\n }\n\n /**\n * Listen to items removed from a collection on root state.\n */\n onRemove<K extends CollectionPropNames<TState>>(\n property: K,\n handler: KeyValueCallback<CollectionKeyType<TState, K>, CollectionValueType<TState, K>>\n ): () => void;\n\n /**\n * Listen to items removed from a nested collection.\n */\n onRemove<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(\n instance: TInstance,\n property: K,\n handler: KeyValueCallback<CollectionKeyType<TInstance, K>, CollectionValueType<TInstance, K>>\n ): () => void;\n\n onRemove(...args: any[]): () => void {\n if (typeof args[0] === 'string') {\n // onRemove(property, handler) - collection on root state\n return this.addCallbackOrWaitCollectionAvailable(\n this.state,\n args[0],\n OPERATION.DELETE,\n args[1]\n );\n } else {\n // onRemove(instance, property, handler) - nested collection\n return this.addCallbackOrWaitCollectionAvailable(\n args[0],\n args[1],\n OPERATION.DELETE,\n args[2]\n );\n }\n }\n\n /**\n * Bind properties from a Schema instance to a target object.\n * Changes will be automatically reflected on the target object.\n */\n bindTo<TInstance extends Schema, TTarget>(\n from: TInstance,\n to: TTarget,\n properties?: string[],\n immediate: boolean = true\n ): () => void {\n const metadata: Metadata = (from.constructor as typeof Schema)[Symbol.metadata];\n\n // If no properties specified, bind all properties\n if (!properties) {\n properties = Object.keys(metadata)\n .filter(key => !isNaN(Number(key)))\n .map((index) => metadata[index as any as number].name);\n }\n\n const action = () => {\n for (const prop of properties!) {\n const fromValue = (from as any)[prop];\n if (fromValue !== undefined) {\n (to as any)[prop] = fromValue;\n }\n }\n };\n\n if (immediate) {\n action();\n }\n\n return this.addCallback(from[$refId], OPERATION.REPLACE, action);\n }\n\n protected triggerChanges(allChanges: DataChange[]): void {\n this.uniqueRefIds.clear();\n\n for (let i = 0, l = allChanges.length; i < l; i++) {\n const change = allChanges[i];\n const refId = change.refId;\n const ref = change.ref;\n\n const $callbacks = this.callbacks[refId];\n if (!$callbacks) {\n continue;\n }\n\n //\n // trigger onRemove on child structure.\n //\n if (\n (change.op & OPERATION.DELETE) === OPERATION.DELETE &&\n change.previousValue instanceof Schema\n ) {\n const childRefId = (change.previousValue as Ref)[$refId];\n const deleteCallbacks = this.callbacks[childRefId]?.[OPERATION.DELETE];\n if (deleteCallbacks) {\n for (let j = deleteCallbacks.length - 1; j >= 0; j--) {\n deleteCallbacks[j]();\n }\n }\n }\n\n if (ref instanceof Schema) {\n //\n // Handle Schema instance\n //\n\n if (!this.uniqueRefIds.has(refId)) {\n // trigger onChange\n const replaceCallbacks = $callbacks[OPERATION.REPLACE];\n if (replaceCallbacks) {\n for (let j = replaceCallbacks.length - 1; j >= 0; j--) {\n try {\n replaceCallbacks[j]();\n } catch (e) {\n console.error(e);\n }\n }\n }\n }\n\n // trigger field callbacks\n const fieldCallbacks = $callbacks[change.field];\n if (fieldCallbacks) {\n for (let j = fieldCallbacks.length - 1; j >= 0; j--) {\n try {\n this.isTriggering = true;\n fieldCallbacks[j](change.value, change.previousValue);\n } catch (e) {\n console.error(e);\n } finally {\n this.isTriggering = false;\n }\n }\n }\n\n } else {\n //\n // Handle collection of items\n //\n const dynamicIndex = change.dynamicIndex ?? change.field;\n\n if ((change.op & OPERATION.DELETE) === OPERATION.DELETE) {\n //\n // FIXME: `previousValue` should always be available.\n //\n if (change.previousValue !== undefined) {\n // trigger onRemove (key, value)\n const deleteCallbacks = $callbacks[OPERATION.DELETE];\n if (deleteCallbacks) {\n for (let j = deleteCallbacks.length - 1; j >= 0; j--) {\n deleteCallbacks[j](dynamicIndex, change.previousValue);\n }\n }\n }\n\n // Handle DELETE_AND_ADD operation\n if ((change.op & OPERATION.ADD) === OPERATION.ADD) {\n const addCallbacks = $callbacks[OPERATION.ADD];\n if (addCallbacks) {\n this.isTriggering = true;\n for (let j = addCallbacks.length - 1; j >= 0; j--) {\n addCallbacks[j](dynamicIndex, change.value);\n }\n this.isTriggering = false;\n }\n }\n\n } else if (\n (change.op & OPERATION.ADD) === OPERATION.ADD &&\n change.previousValue !== change.value\n ) {\n // trigger onAdd (key, value)\n const addCallbacks = $callbacks[OPERATION.ADD];\n if (addCallbacks) {\n this.isTriggering = true;\n for (let j = addCallbacks.length - 1; j >= 0; j--) {\n addCallbacks[j](dynamicIndex, change.value);\n }\n this.isTriggering = false;\n }\n }\n\n // trigger onChange (key, value)\n if (change.value !== change.previousValue) {\n const replaceCallbacks = $callbacks[OPERATION.REPLACE];\n if (replaceCallbacks) {\n for (let j = replaceCallbacks.length - 1; j >= 0; j--) {\n replaceCallbacks[j](dynamicIndex, change.value);\n }\n }\n }\n }\n\n this.uniqueRefIds.add(refId);\n }\n }\n}\n\n/**\n * Factory class for retrieving the callbacks API.\n */\nexport const Callbacks = {\n /**\n * Get the new callbacks standard API.\n *\n * Usage:\n * ```ts\n * const callbacks = Callbacks.get(roomOrDecoder);\n *\n * // Listen to property changes\n * callbacks.listen(\"currentTurn\", (currentValue, previousValue) => { ... });\n *\n * // Listen to collection additions\n * callbacks.onAdd(\"entities\", (sessionId, entity) => {\n * // Nested property listening\n * callbacks.listen(entity, \"hp\", (currentHp, previousHp) => { ... });\n * });\n *\n * // Listen to collection removals\n * callbacks.onRemove(\"entities\", (sessionId, entity) => { ... });\n *\n * // Listen to any property change on an instance\n * callbacks.onChange(entity, () => { ... });\n *\n * // Bind properties to another object\n * callbacks.bindTo(player, playerVisual);\n * ```\n *\n * @param roomOrDecoder - Room or Decoder instance to get the callbacks for.\n * @returns the new callbacks standard API.\n */\n get<T extends Schema>(roomOrDecoder: Decoder<T> | { serializer: { decoder: Decoder<T> } }): StateCallbackStrategy<T> {\n if (roomOrDecoder instanceof Decoder) {\n return new StateCallbackStrategy<T>(roomOrDecoder);\n\n } else if (roomOrDecoder.serializer.decoder) {\n return new StateCallbackStrategy<T>(roomOrDecoder.serializer.decoder);\n\n } else {\n throw new Error('Invalid room or decoder');\n }\n },\n\n /**\n * Get the legacy callbacks API.\n *\n * We aim to deprecate this API on 1.0, and iterate on improving Callbacks.get() API.\n *\n * @param roomOrDecoder - Room or Decoder instance to get the legacy callbacks for.\n * @returns the legacy callbacks API.\n */\n getLegacy<T extends Schema>(roomOrDecoder: Decoder<T> | { serializer: { decoder: Decoder<T> } }): SchemaCallbackProxy<T> {\n if (roomOrDecoder instanceof Decoder) {\n return getDecoderStateCallbacks(roomOrDecoder);\n\n } else if (roomOrDecoder.serializer.decoder) {\n return getDecoderStateCallbacks(roomOrDecoder.serializer.decoder);\n }\n },\n\n getRawChanges(decoder: Decoder, callback: (changes: DataChange[]) => void) {\n return getRawChangesCallback(decoder, callback);\n }\n};\n\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RawChanges.js","sourceRoot":"","sources":["../../../../src/decoder/strategy/RawChanges.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,qBAAqB,CACjC,OAAgB,EAChB,QAAyC;IAEzC,OAAO,CAAC,cAAc,GAAG,QAAQ,CAAC;AACtC,CAAC","sourcesContent":["import { DataChange } from \"../DecodeOperation\";\nimport { Decoder } from \"../Decoder\";\n\nexport function getRawChangesCallback(\n decoder: Decoder,\n callback: (changes: DataChange[]) => void\n) {\n decoder.triggerChanges = callback;\n}"]}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Collection, NonFunctionNonPrimitivePropNames, NonFunctionPropNames } from "../../types/HelperTypes";
|
|
2
|
+
import { Decoder } from "../Decoder";
|
|
3
|
+
import { Schema } from "../../Schema";
|
|
4
|
+
/**
|
|
5
|
+
* TODO: define a schema interface, which even having duplicate definitions, it could be used to get the callback proxy.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* export type SchemaCallbackProxy<RoomState> = (<T extends Schema>(instance: T) => CallbackProxy<T>);
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export type SchemaCallbackProxy<RoomState> = (<T>(instance: T) => CallbackProxy<T>);
|
|
12
|
+
export type GetCallbackProxy = SchemaCallbackProxy<any>;
|
|
13
|
+
export type CallbackProxy<T> = unknown extends T ? SchemaCallback<T> & CollectionCallback<any, any> : T extends Collection<infer K, infer V, infer _> ? CollectionCallback<K, V> : SchemaCallback<T>;
|
|
14
|
+
export type SchemaCallback<T> = {
|
|
15
|
+
/**
|
|
16
|
+
* Trigger callback when value of a property changes.
|
|
17
|
+
*
|
|
18
|
+
* @param prop name of the property
|
|
19
|
+
* @param callback callback to be triggered on property change
|
|
20
|
+
* @param immediate trigger immediatelly if property has been already set.
|
|
21
|
+
* @return callback to detach the listener
|
|
22
|
+
*/
|
|
23
|
+
listen<K extends NonFunctionPropNames<T>>(prop: K, callback: (value: T[K], previousValue: T[K]) => void, immediate?: boolean): () => void;
|
|
24
|
+
/**
|
|
25
|
+
* Trigger callback whenever any property changed within this instance.
|
|
26
|
+
*
|
|
27
|
+
* @param prop name of the property
|
|
28
|
+
* @param callback callback to be triggered on property change
|
|
29
|
+
* @param immediate trigger immediatelly if property has been already set.
|
|
30
|
+
* @return callback to detach the listener
|
|
31
|
+
*/
|
|
32
|
+
onChange(callback: () => void): () => void;
|
|
33
|
+
/**
|
|
34
|
+
* Bind properties to another object. Changes on the properties will be reflected on the target object.
|
|
35
|
+
*
|
|
36
|
+
* @param targetObject object to bind properties to
|
|
37
|
+
* @param properties list of properties to bind. If not provided, all properties will be bound.
|
|
38
|
+
*/
|
|
39
|
+
bindTo(targetObject: any, properties?: Array<NonFunctionPropNames<T>>): void;
|
|
40
|
+
} & {
|
|
41
|
+
[K in NonFunctionNonPrimitivePropNames<T>]: CallbackProxy<T[K]>;
|
|
42
|
+
};
|
|
43
|
+
export type CollectionCallback<K, V> = {
|
|
44
|
+
/**
|
|
45
|
+
* Trigger callback when an item has been added to the collection.
|
|
46
|
+
*
|
|
47
|
+
* @param callback
|
|
48
|
+
* @param immediate
|
|
49
|
+
* @return callback to detach the onAdd listener
|
|
50
|
+
*/
|
|
51
|
+
onAdd(callback: (item: V, index: K) => void, immediate?: boolean): () => void;
|
|
52
|
+
/**
|
|
53
|
+
* Trigger callback when an item has been removed to the collection.
|
|
54
|
+
*
|
|
55
|
+
* @param callback
|
|
56
|
+
* @return callback to detach the onRemove listener
|
|
57
|
+
*/
|
|
58
|
+
onRemove(callback: (item: V, index: K) => void): () => void;
|
|
59
|
+
/**
|
|
60
|
+
* Trigger callback when the value on a key has changed.
|
|
61
|
+
*
|
|
62
|
+
* THIS METHOD IS NOT RECURSIVE!
|
|
63
|
+
* If you want to listen to changes on individual items, you need to attach callbacks to the them directly inside the `onAdd` callback.
|
|
64
|
+
*
|
|
65
|
+
* @param callback
|
|
66
|
+
* @return callback to detach the onChange listener
|
|
67
|
+
*/
|
|
68
|
+
onChange(callback: (item: V, index: K) => void): () => void;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Legacy callback system
|
|
72
|
+
*
|
|
73
|
+
* @param decoder
|
|
74
|
+
* @returns
|
|
75
|
+
*/
|
|
76
|
+
export declare function getDecoderStateCallbacks<T extends Schema>(decoder: Decoder<T>): SchemaCallbackProxy<T>;
|