@colyseus/schema 4.0.3 → 4.0.4

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.
@@ -10,6 +10,7 @@ import { ArraySchema } from "../../types/custom/ArraySchema.js";
10
10
  import { type SchemaCallbackProxy } from "./getDecoderStateCallbacks.js";
11
11
  type PropertyChangeCallback<K> = (currentValue: K, previousValue: K) => void;
12
12
  type KeyValueCallback<K, V> = (key: K, value: V) => void;
13
+ type ValueKeyCallback<V, K> = (value: V, key: K) => void;
13
14
  type InstanceChangeCallback = () => void;
14
15
  type PublicPropNames<T> = Exclude<NonFunctionPropNames<T>, typeof $refId> & string;
15
16
  type CollectionPropNames<T> = Exclude<{
@@ -17,32 +18,6 @@ type CollectionPropNames<T> = Exclude<{
17
18
  }[keyof T] & string, typeof $refId>;
18
19
  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
20
  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
21
  export declare class StateCallbackStrategy<TState extends Schema> {
47
22
  protected decoder: Decoder<TState>;
48
23
  protected uniqueRefIds: Set<number>;
@@ -78,19 +53,19 @@ export declare class StateCallbackStrategy<TState extends Schema> {
78
53
  /**
79
54
  * Listen to items added to a collection on root state.
80
55
  */
81
- onAdd<K extends CollectionPropNames<TState>>(property: K, handler: KeyValueCallback<CollectionKeyType<TState, K>, CollectionValueType<TState, K>>, immediate?: boolean): () => void;
56
+ onAdd<K extends CollectionPropNames<TState>>(property: K, handler: ValueKeyCallback<CollectionValueType<TState, K>, CollectionKeyType<TState, K>>, immediate?: boolean): () => void;
82
57
  /**
83
58
  * Listen to items added to a nested collection.
84
59
  */
85
- onAdd<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: KeyValueCallback<CollectionKeyType<TInstance, K>, CollectionValueType<TInstance, K>>, immediate?: boolean): () => void;
60
+ onAdd<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: ValueKeyCallback<CollectionValueType<TInstance, K>, CollectionKeyType<TInstance, K>>, immediate?: boolean): () => void;
86
61
  /**
87
62
  * Listen to items removed from a collection on root state.
88
63
  */
89
- onRemove<K extends CollectionPropNames<TState>>(property: K, handler: KeyValueCallback<CollectionKeyType<TState, K>, CollectionValueType<TState, K>>): () => void;
64
+ onRemove<K extends CollectionPropNames<TState>>(property: K, handler: ValueKeyCallback<CollectionValueType<TState, K>, CollectionKeyType<TState, K>>): () => void;
90
65
  /**
91
66
  * Listen to items removed from a nested collection.
92
67
  */
93
- onRemove<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: KeyValueCallback<CollectionKeyType<TInstance, K>, CollectionValueType<TInstance, K>>): () => void;
68
+ onRemove<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: ValueKeyCallback<CollectionValueType<TInstance, K>, CollectionKeyType<TInstance, K>>): () => void;
94
69
  /**
95
70
  * Bind properties from a Schema instance to a target object.
96
71
  * Changes will be automatically reflected on the target object.
@@ -113,13 +88,13 @@ export declare const Callbacks: {
113
88
  * callbacks.listen("currentTurn", (currentValue, previousValue) => { ... });
114
89
  *
115
90
  * // Listen to collection additions
116
- * callbacks.onAdd("entities", (sessionId, entity) => {
91
+ * callbacks.onAdd("entities", (entity, sessionId) => {
117
92
  * // Nested property listening
118
93
  * callbacks.listen(entity, "hp", (currentHp, previousHp) => { ... });
119
94
  * });
120
95
  *
121
96
  * // Listen to collection removals
122
- * callbacks.onRemove("entities", (sessionId, entity) => { ... });
97
+ * callbacks.onRemove("entities", (entity, sessionId) => { ... });
123
98
  *
124
99
  * // Listen to any property change on an instance
125
100
  * callbacks.onChange(entity, () => { ... });
package/build/index.cjs CHANGED
@@ -5122,32 +5122,6 @@ function getRawChangesCallback(decoder, callback) {
5122
5122
  decoder.triggerChanges = callback;
5123
5123
  }
5124
5124
 
5125
- /**
5126
- * State Callbacks handler
5127
- *
5128
- * Usage:
5129
- * ```ts
5130
- * const $ = Callbacks.get(decoder);
5131
- *
5132
- * // Listen to property changes
5133
- * $.listen("currentTurn", (currentValue, previousValue) => { ... });
5134
- *
5135
- * // Listen to collection additions
5136
- * $.onAdd("entities", (sessionId, entity) => {
5137
- * // Nested property listening
5138
- * $.listen(entity, "hp", (currentHp, previousHp) => { ... });
5139
- * });
5140
- *
5141
- * // Listen to collection removals
5142
- * $.onRemove("entities", (sessionId, entity) => { ... });
5143
- *
5144
- * // Listen to any property change on an instance
5145
- * $.onChange(entity, () => { ... });
5146
- *
5147
- * // Bind properties to another object
5148
- * $.bindTo(player, playerVisual);
5149
- * ```
5150
- */
5151
5125
  class StateCallbackStrategy {
5152
5126
  decoder;
5153
5127
  uniqueRefIds = new Set();
@@ -5186,7 +5160,7 @@ class StateCallbackStrategy {
5186
5160
  immediate = immediate && this.isTriggering === false;
5187
5161
  if (operation === exports.OPERATION.ADD && immediate) {
5188
5162
  collection.forEach((value, key) => {
5189
- handler(key, value);
5163
+ handler(value, key);
5190
5164
  });
5191
5165
  }
5192
5166
  return this.addCallback(collection[$refId], operation, handler);
@@ -5342,11 +5316,11 @@ class StateCallbackStrategy {
5342
5316
  // FIXME: `previousValue` should always be available.
5343
5317
  //
5344
5318
  if (change.previousValue !== undefined) {
5345
- // trigger onRemove (key, value)
5319
+ // trigger onRemove (value, key)
5346
5320
  const deleteCallbacks = $callbacks[exports.OPERATION.DELETE];
5347
5321
  if (deleteCallbacks) {
5348
5322
  for (let j = deleteCallbacks.length - 1; j >= 0; j--) {
5349
- deleteCallbacks[j](dynamicIndex, change.previousValue);
5323
+ deleteCallbacks[j](change.previousValue, dynamicIndex);
5350
5324
  }
5351
5325
  }
5352
5326
  }
@@ -5356,7 +5330,7 @@ class StateCallbackStrategy {
5356
5330
  if (addCallbacks) {
5357
5331
  this.isTriggering = true;
5358
5332
  for (let j = addCallbacks.length - 1; j >= 0; j--) {
5359
- addCallbacks[j](dynamicIndex, change.value);
5333
+ addCallbacks[j](change.value, dynamicIndex);
5360
5334
  }
5361
5335
  this.isTriggering = false;
5362
5336
  }
@@ -5364,12 +5338,12 @@ class StateCallbackStrategy {
5364
5338
  }
5365
5339
  else if ((change.op & exports.OPERATION.ADD) === exports.OPERATION.ADD &&
5366
5340
  change.previousValue !== change.value) {
5367
- // trigger onAdd (key, value)
5341
+ // trigger onAdd (value, key)
5368
5342
  const addCallbacks = $callbacks[exports.OPERATION.ADD];
5369
5343
  if (addCallbacks) {
5370
5344
  this.isTriggering = true;
5371
5345
  for (let j = addCallbacks.length - 1; j >= 0; j--) {
5372
- addCallbacks[j](dynamicIndex, change.value);
5346
+ addCallbacks[j](change.value, dynamicIndex);
5373
5347
  }
5374
5348
  this.isTriggering = false;
5375
5349
  }
@@ -5403,13 +5377,13 @@ const Callbacks = {
5403
5377
  * callbacks.listen("currentTurn", (currentValue, previousValue) => { ... });
5404
5378
  *
5405
5379
  * // Listen to collection additions
5406
- * callbacks.onAdd("entities", (sessionId, entity) => {
5380
+ * callbacks.onAdd("entities", (entity, sessionId) => {
5407
5381
  * // Nested property listening
5408
5382
  * callbacks.listen(entity, "hp", (currentHp, previousHp) => { ... });
5409
5383
  * });
5410
5384
  *
5411
5385
  * // Listen to collection removals
5412
- * callbacks.onRemove("entities", (sessionId, entity) => { ... });
5386
+ * callbacks.onRemove("entities", (entity, sessionId) => { ... });
5413
5387
  *
5414
5388
  * // Listen to any property change on an instance
5415
5389
  * callbacks.onChange(entity, () => { ... });