@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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/schema",
3
- "version": "4.0.3",
3
+ "version": "4.0.4",
4
4
  "description": "Binary state serializer with delta encoding for games",
5
5
  "type": "module",
6
6
  "bin": {
@@ -12,17 +12,17 @@ import { getDecoderStateCallbacks, type SchemaCallbackProxy } from "./getDecoder
12
12
  import { getRawChangesCallback } from "./RawChanges.js";
13
13
 
14
14
  //
15
- // C#-style Callbacks API
16
- // Matches the API from: https://docs.colyseus.io/state/callbacks
15
+ // C#-style Callbacks API (https://docs.colyseus.io/state/callbacks)
17
16
  //
18
17
  // Key features:
19
18
  // - Uses string property names with TypeScript auto-completion
20
- // - Consistent parameter order (key, value) for collection callbacks
19
+ // - Parameter order (value, key) for onAdd/onRemove callbacks
21
20
  // - Overloaded methods for nested instance callbacks
22
21
  //
23
22
 
24
23
  type PropertyChangeCallback<K> = (currentValue: K, previousValue: K) => void;
25
24
  type KeyValueCallback<K, V> = (key: K, value: V) => void;
25
+ type ValueKeyCallback<V, K> = (value: V, key: K) => void;
26
26
  type InstanceChangeCallback = () => void;
27
27
 
28
28
  // Exclude internal properties from valid property names
@@ -45,32 +45,6 @@ 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
- /**
49
- * State Callbacks handler
50
- *
51
- * Usage:
52
- * ```ts
53
- * const $ = Callbacks.get(decoder);
54
- *
55
- * // Listen to property changes
56
- * $.listen("currentTurn", (currentValue, previousValue) => { ... });
57
- *
58
- * // Listen to collection additions
59
- * $.onAdd("entities", (sessionId, entity) => {
60
- * // Nested property listening
61
- * $.listen(entity, "hp", (currentHp, previousHp) => { ... });
62
- * });
63
- *
64
- * // Listen to collection removals
65
- * $.onRemove("entities", (sessionId, entity) => { ... });
66
- *
67
- * // Listen to any property change on an instance
68
- * $.onChange(entity, () => { ... });
69
- *
70
- * // Bind properties to another object
71
- * $.bindTo(player, playerVisual);
72
- * ```
73
- */
74
48
  export class StateCallbackStrategy<TState extends Schema> {
75
49
  protected decoder: Decoder<TState>;
76
50
  protected uniqueRefIds: Set<number> = new Set();
@@ -131,7 +105,7 @@ export class StateCallbackStrategy<TState extends Schema> {
131
105
 
132
106
  if (operation === OPERATION.ADD && immediate) {
133
107
  (collection as Collection<any, any>).forEach((value: any, key: any) => {
134
- handler(key, value);
108
+ handler(value, key);
135
109
  });
136
110
  }
137
111
 
@@ -244,7 +218,7 @@ export class StateCallbackStrategy<TState extends Schema> {
244
218
  */
245
219
  onAdd<K extends CollectionPropNames<TState>>(
246
220
  property: K,
247
- handler: KeyValueCallback<CollectionKeyType<TState, K>, CollectionValueType<TState, K>>,
221
+ handler: ValueKeyCallback<CollectionValueType<TState, K>, CollectionKeyType<TState, K>>,
248
222
  immediate?: boolean
249
223
  ): () => void;
250
224
 
@@ -254,7 +228,7 @@ export class StateCallbackStrategy<TState extends Schema> {
254
228
  onAdd<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(
255
229
  instance: TInstance,
256
230
  property: K,
257
- handler: KeyValueCallback<CollectionKeyType<TInstance, K>, CollectionValueType<TInstance, K>>,
231
+ handler: ValueKeyCallback<CollectionValueType<TInstance, K>, CollectionKeyType<TInstance, K>>,
258
232
  immediate?: boolean
259
233
  ): () => void;
260
234
 
@@ -285,7 +259,7 @@ export class StateCallbackStrategy<TState extends Schema> {
285
259
  */
286
260
  onRemove<K extends CollectionPropNames<TState>>(
287
261
  property: K,
288
- handler: KeyValueCallback<CollectionKeyType<TState, K>, CollectionValueType<TState, K>>
262
+ handler: ValueKeyCallback<CollectionValueType<TState, K>, CollectionKeyType<TState, K>>
289
263
  ): () => void;
290
264
 
291
265
  /**
@@ -294,7 +268,7 @@ export class StateCallbackStrategy<TState extends Schema> {
294
268
  onRemove<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(
295
269
  instance: TInstance,
296
270
  property: K,
297
- handler: KeyValueCallback<CollectionKeyType<TInstance, K>, CollectionValueType<TInstance, K>>
271
+ handler: ValueKeyCallback<CollectionValueType<TInstance, K>, CollectionKeyType<TInstance, K>>
298
272
  ): () => void;
299
273
 
300
274
  onRemove(...args: any[]): () => void {
@@ -426,11 +400,11 @@ export class StateCallbackStrategy<TState extends Schema> {
426
400
  // FIXME: `previousValue` should always be available.
427
401
  //
428
402
  if (change.previousValue !== undefined) {
429
- // trigger onRemove (key, value)
403
+ // trigger onRemove (value, key)
430
404
  const deleteCallbacks = $callbacks[OPERATION.DELETE];
431
405
  if (deleteCallbacks) {
432
406
  for (let j = deleteCallbacks.length - 1; j >= 0; j--) {
433
- deleteCallbacks[j](dynamicIndex, change.previousValue);
407
+ deleteCallbacks[j](change.previousValue, dynamicIndex);
434
408
  }
435
409
  }
436
410
  }
@@ -441,7 +415,7 @@ export class StateCallbackStrategy<TState extends Schema> {
441
415
  if (addCallbacks) {
442
416
  this.isTriggering = true;
443
417
  for (let j = addCallbacks.length - 1; j >= 0; j--) {
444
- addCallbacks[j](dynamicIndex, change.value);
418
+ addCallbacks[j](change.value, dynamicIndex);
445
419
  }
446
420
  this.isTriggering = false;
447
421
  }
@@ -451,12 +425,12 @@ export class StateCallbackStrategy<TState extends Schema> {
451
425
  (change.op & OPERATION.ADD) === OPERATION.ADD &&
452
426
  change.previousValue !== change.value
453
427
  ) {
454
- // trigger onAdd (key, value)
428
+ // trigger onAdd (value, key)
455
429
  const addCallbacks = $callbacks[OPERATION.ADD];
456
430
  if (addCallbacks) {
457
431
  this.isTriggering = true;
458
432
  for (let j = addCallbacks.length - 1; j >= 0; j--) {
459
- addCallbacks[j](dynamicIndex, change.value);
433
+ addCallbacks[j](change.value, dynamicIndex);
460
434
  }
461
435
  this.isTriggering = false;
462
436
  }
@@ -493,13 +467,13 @@ export const Callbacks = {
493
467
  * callbacks.listen("currentTurn", (currentValue, previousValue) => { ... });
494
468
  *
495
469
  * // Listen to collection additions
496
- * callbacks.onAdd("entities", (sessionId, entity) => {
470
+ * callbacks.onAdd("entities", (entity, sessionId) => {
497
471
  * // Nested property listening
498
472
  * callbacks.listen(entity, "hp", (currentHp, previousHp) => { ... });
499
473
  * });
500
474
  *
501
475
  * // Listen to collection removals
502
- * callbacks.onRemove("entities", (sessionId, entity) => { ... });
476
+ * callbacks.onRemove("entities", (entity, sessionId) => { ... });
503
477
  *
504
478
  * // Listen to any property change on an instance
505
479
  * callbacks.onChange(entity, () => { ... });