@colyseus/schema 5.0.3 → 5.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.
@@ -36,12 +36,12 @@ export declare class StateCallbackStrategy<TState extends IRef> {
36
36
  /**
37
37
  * Listen to property changes on a nested instance.
38
38
  */
39
- listen<TInstance extends Schema, K extends PublicPropNames<TInstance>>(instance: TInstance, property: K, handler: PropertyChangeCallback<TInstance[K]>, immediate?: boolean): () => void;
39
+ listen<TInstance, K extends PublicPropNames<TInstance>>(instance: TInstance, property: K, handler: PropertyChangeCallback<TInstance[K]>, immediate?: boolean): () => void;
40
40
  protected listenInstance<TInstance extends IRef>(instance: TInstance, propertyName: string, handler: PropertyChangeCallback<any>, immediate?: boolean): () => void;
41
41
  /**
42
42
  * Listen to any property change on an instance.
43
43
  */
44
- onChange<TInstance extends Schema>(instance: TInstance, handler: InstanceChangeCallback): () => void;
44
+ onChange<TInstance extends object>(instance: TInstance, handler: InstanceChangeCallback): () => void;
45
45
  /**
46
46
  * Listen to item changes in a collection on root state.
47
47
  */
@@ -49,7 +49,7 @@ export declare class StateCallbackStrategy<TState extends IRef> {
49
49
  /**
50
50
  * Listen to item changes in a nested collection.
51
51
  */
52
- onChange<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: KeyValueCallback<CollectionKeyType<TInstance, K>, CollectionValueType<TInstance, K>>): () => void;
52
+ onChange<TInstance extends object, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: KeyValueCallback<CollectionKeyType<TInstance, K>, CollectionValueType<TInstance, K>>): () => void;
53
53
  /**
54
54
  * Listen to items added to a collection on root state.
55
55
  */
@@ -57,7 +57,7 @@ export declare class StateCallbackStrategy<TState extends IRef> {
57
57
  /**
58
58
  * Listen to items added to a nested collection.
59
59
  */
60
- onAdd<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: ValueKeyCallback<CollectionValueType<TInstance, K>, CollectionKeyType<TInstance, K>>, immediate?: boolean): () => void;
60
+ onAdd<TInstance, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: ValueKeyCallback<CollectionValueType<TInstance, K>, CollectionKeyType<TInstance, K>>, immediate?: boolean): () => void;
61
61
  /**
62
62
  * Listen to items removed from a collection on root state.
63
63
  */
@@ -65,12 +65,12 @@ export declare class StateCallbackStrategy<TState extends IRef> {
65
65
  /**
66
66
  * Listen to items removed from a nested collection.
67
67
  */
68
- onRemove<TInstance extends Schema, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: ValueKeyCallback<CollectionValueType<TInstance, K>, CollectionKeyType<TInstance, K>>): () => void;
68
+ onRemove<TInstance, K extends CollectionPropNames<TInstance>>(instance: TInstance, property: K, handler: ValueKeyCallback<CollectionValueType<TInstance, K>, CollectionKeyType<TInstance, K>>): () => void;
69
69
  /**
70
70
  * Bind properties from a Schema instance to a target object.
71
71
  * Changes will be automatically reflected on the target object.
72
72
  */
73
- bindTo<TInstance extends Schema, TTarget>(from: TInstance, to: TTarget, properties?: string[], immediate?: boolean): () => void;
73
+ bindTo<TInstance, TTarget>(from: TInstance, to: TTarget, properties?: string[], immediate?: boolean): () => void;
74
74
  protected triggerChanges(allChanges: DataChange[]): void;
75
75
  }
76
76
  /**
@@ -107,6 +107,23 @@ export declare class StateView {
107
107
  */
108
108
  private _markSubtreeVisible;
109
109
  protected addParentOf(childChangeTree: ChangeTree, tag: number): void;
110
+ /**
111
+ * Walk `tree`'s parent chain to root and insert an empty entry into
112
+ * `view.changes` for any ancestor not already present. Empty entries
113
+ * are skipped by `encodeView` (`changes.size === 0` continue), so no
114
+ * wire bytes are emitted — but the Map's insertion order now puts
115
+ * each ancestor BEFORE the descendant entry that the caller is about
116
+ * to write. Combined with `addParentOf`'s full-recursion walk on
117
+ * `view.add`, this preserves the global invariant that
118
+ * `view.changes` iteration order is topological.
119
+ *
120
+ * Iterative (not recursive) so the stack is bounded by tree depth
121
+ * regardless of call patterns. Stops the walk as soon as it hits an
122
+ * ancestor that's already in `view.changes` — at that point the
123
+ * remainder of the chain is guaranteed to also be present (invariant
124
+ * upheld by every prior caller).
125
+ */
126
+ private _touchAncestorsOf;
110
127
  remove(obj: Ref, tag?: number): this;
111
128
  remove(obj: Ref, tag?: number, _isClear?: boolean): this;
112
129
  has(obj: Ref): boolean;
package/build/index.cjs CHANGED
@@ -2110,11 +2110,24 @@ function checkInheritedFlags(tree, parent, parentIndex) {
2110
2110
  const refType = Metadata.isValidInstance(tree.ref)
2111
2111
  ? tree.ref.constructor
2112
2112
  : tree.ref[$childType];
2113
+ // #218: nested Schema fields inherit visibility from a @view-gated
2114
+ // parent regardless of whether the parent is a collection. The
2115
+ // `parentIsCollection` constraint that used to live here blocked
2116
+ // nested-Schema-field-of-@view-tagged-Schema from sharing visibility,
2117
+ // forcing users to wrap the child in an ArraySchema as a workaround.
2118
+ //
2119
+ // #226 (4.0.25): items inside a non-default-tag `@view(N)` collection
2120
+ // also inherit visibility from the parent collection, so items
2121
+ // pushed/set after `view.add(state, N)` show up automatically.
2122
+ // Default-tag `@view()` collections keep per-item gating —
2123
+ // `view.add(item)` is still required to opt each one in.
2124
+ // The `parentMetadata[parentIndex].tag` access is safe inside the
2125
+ // `fieldHasViewTag` short-circuit (the metadata entry and its `tag`
2126
+ // are guaranteed to exist when that flag is set).
2113
2127
  tree.isVisibilitySharedWithParent = (parentChangeTree.isFiltered
2114
2128
  && typeof refType !== "string"
2115
- && !fieldHasViewTag
2116
2129
  && !fieldHasStream
2117
- && parentIsCollection);
2130
+ && (!fieldHasViewTag || (parentIsCollection && parentMetadata[parentIndex].tag !== DEFAULT_VIEW_TAG)));
2118
2131
  }
2119
2132
  }
2120
2133
 
@@ -5447,8 +5460,10 @@ registerType("stream", { constructor: StreamSchema });
5447
5460
  */
5448
5461
  class FieldBuilder {
5449
5462
  [$builder] = true;
5450
- // Internal configuration. Public so schema() and tests can read it, but not
5451
- // meant to be mutated by users directly.
5463
+ // Internal configuration. Declared `private` (soft-private): hidden from
5464
+ // editor autocomplete and from normal external `.field` access, but still
5465
+ // reachable at runtime via element access (e.g. `builder['_noSync']`) for
5466
+ // internal tooling/tests. Not meant to be mutated by end users.
5452
5467
  _type;
5453
5468
  _default = undefined;
5454
5469
  _hasDefault = false;
@@ -5461,6 +5476,7 @@ class FieldBuilder {
5461
5476
  _static = false;
5462
5477
  _stream = false;
5463
5478
  _optional = false;
5479
+ _noSync = false;
5464
5480
  _streamPriority = undefined;
5465
5481
  constructor(type) {
5466
5482
  this._type = type;
@@ -5511,6 +5527,30 @@ class FieldBuilder {
5511
5527
  this._static = true;
5512
5528
  return this;
5513
5529
  }
5530
+ /**
5531
+ * Mark this field as **local-only** — it is typed and initialized on the
5532
+ * instance (so `.default()` and the inferred instance type still apply),
5533
+ * but is never registered for synchronization: it never enters change
5534
+ * tracking, never goes over the wire, and decoders never receive it.
5535
+ *
5536
+ * Useful for server-side scratch state, per-peer UI state, or values you
5537
+ * want on the class for typing convenience without paying any sync cost.
5538
+ *
5539
+ * Mutually exclusive with the sync-only modifiers (`.view()`, `.owned()`,
5540
+ * `.unreliable()`, `.transient()`, `.static()`, `.stream()`) — combining
5541
+ * them throws at `schema()` time.
5542
+ *
5543
+ * ```ts
5544
+ * const Player = schema({
5545
+ * hp: t.uint8().default(100), // synchronized
5546
+ * lastInputTick: t.number().noSync(), // local-only
5547
+ * }, 'Player');
5548
+ * ```
5549
+ */
5550
+ noSync() {
5551
+ this._noSync = true;
5552
+ return this;
5553
+ }
5514
5554
  /**
5515
5555
  * Opt a collection field into priority-batched streaming delivery —
5516
5556
  * ADDs drain at most `maxPerTick` per tick per view (or per broadcast
@@ -5565,6 +5605,11 @@ class FieldBuilder {
5565
5605
  this._optional = true;
5566
5606
  return this;
5567
5607
  }
5608
+ /**
5609
+ * @internal — snapshot of the builder's configuration consumed by
5610
+ * `schema()`. `private` keeps it out of autocomplete; internal callers
5611
+ * reach it via element access (`builder['toDefinition']()`).
5612
+ */
5568
5613
  toDefinition() {
5569
5614
  return {
5570
5615
  type: this._type,
@@ -5579,6 +5624,7 @@ class FieldBuilder {
5579
5624
  static: this._static,
5580
5625
  stream: this._stream,
5581
5626
  optional: this._optional,
5627
+ noSync: this._noSync,
5582
5628
  streamPriority: this._streamPriority,
5583
5629
  };
5584
5630
  }
@@ -5594,7 +5640,8 @@ function primitive(name) {
5594
5640
  }
5595
5641
  function resolveChild(child) {
5596
5642
  if (isBuilder(child)) {
5597
- return child._type;
5643
+ // `_type` is private; element access bypasses the visibility check.
5644
+ return child['_type'];
5598
5645
  }
5599
5646
  return child;
5600
5647
  }
@@ -5604,7 +5651,7 @@ const setFactory = ((child) => new FieldBuilder({ set: resolveChild(child) }));
5604
5651
  const collectionFactory = ((child) => new FieldBuilder({ collection: resolveChild(child) }));
5605
5652
  const streamFactory = ((child) => {
5606
5653
  const b = new FieldBuilder({ stream: resolveChild(child) });
5607
- b._stream = true;
5654
+ b['_stream'] = true; // element access bypasses `private`
5608
5655
  return b;
5609
5656
  });
5610
5657
  const refFactory = ((ctor) => new FieldBuilder(ctor));
@@ -6033,6 +6080,36 @@ function deprecated(throws = true) {
6033
6080
  });
6034
6081
  };
6035
6082
  }
6083
+ /**
6084
+ * Produce the auto-instantiated construction default for a builder type
6085
+ * (empty collection or zero-arg Schema ref), or `undefined` when the type
6086
+ * has no auto-default. Shared by synced and `.noSync()` field handling.
6087
+ */
6088
+ function autoInstantiateDefault(rawType) {
6089
+ if (rawType && typeof rawType === "object") {
6090
+ if (rawType.array !== undefined) {
6091
+ return new ArraySchema();
6092
+ }
6093
+ if (rawType.map !== undefined) {
6094
+ return new MapSchema();
6095
+ }
6096
+ if (rawType.set !== undefined) {
6097
+ return new SetSchema();
6098
+ }
6099
+ if (rawType.collection !== undefined) {
6100
+ return new CollectionSchema();
6101
+ }
6102
+ if (rawType.stream !== undefined) {
6103
+ return new StreamSchema();
6104
+ }
6105
+ }
6106
+ else if (typeof rawType === "function" && Schema.is(rawType)) {
6107
+ if (!rawType.prototype.initialize || rawType.prototype.initialize.length === 0) {
6108
+ return new rawType();
6109
+ }
6110
+ }
6111
+ return undefined;
6112
+ }
6036
6113
  /**
6037
6114
  * Define a Schema class declaratively.
6038
6115
  *
@@ -6068,7 +6145,28 @@ function schema(fieldsAndMethods, name, inherits = Schema) {
6068
6145
  for (const fieldName in fieldsAndMethods) {
6069
6146
  const value = fieldsAndMethods[fieldName];
6070
6147
  if (isBuilder(value)) {
6071
- const def = value.toDefinition();
6148
+ const def = value['toDefinition'](); // private; element access bypasses visibility
6149
+ if (def.noSync) {
6150
+ // Local-only field: skip metadata registration entirely so it is
6151
+ // never encoded/decoded, but still seed its construction default
6152
+ // (honoring `.default()` and collection/ref auto-instantiation).
6153
+ if (def.view !== undefined || def.owned || def.unreliable ||
6154
+ def.transient || def.static || def.stream) {
6155
+ throw new Error(`schema(${name ? `'${name}'` : ""}): field '${fieldName}' uses .noSync() ` +
6156
+ `together with a sync-only modifier (.view/.owned/.unreliable/.transient/.static/.stream). ` +
6157
+ `A local-only field cannot be synchronized.`);
6158
+ }
6159
+ if (def.hasDefault) {
6160
+ defaultValues[fieldName] = def.default;
6161
+ }
6162
+ else if (!def.optional) {
6163
+ const autoDefault = autoInstantiateDefault(def.type);
6164
+ if (autoDefault !== undefined) {
6165
+ defaultValues[fieldName] = autoDefault;
6166
+ }
6167
+ }
6168
+ continue;
6169
+ }
6072
6170
  fields[fieldName] = getNormalizedType(def.type);
6073
6171
  if (def.view !== undefined) {
6074
6172
  viewTagFields[fieldName] = def.view;
@@ -6103,28 +6201,9 @@ function schema(fieldsAndMethods, name, inherits = Schema) {
6103
6201
  else if (!def.optional) {
6104
6202
  // Auto-instantiate collection/Schema defaults when none is provided.
6105
6203
  // `.optional()` opts out — field starts as undefined.
6106
- const rawType = def.type;
6107
- if (rawType && typeof rawType === "object") {
6108
- if (rawType.array !== undefined) {
6109
- defaultValues[fieldName] = new ArraySchema();
6110
- }
6111
- else if (rawType.map !== undefined) {
6112
- defaultValues[fieldName] = new MapSchema();
6113
- }
6114
- else if (rawType.set !== undefined) {
6115
- defaultValues[fieldName] = new SetSchema();
6116
- }
6117
- else if (rawType.collection !== undefined) {
6118
- defaultValues[fieldName] = new CollectionSchema();
6119
- }
6120
- else if (rawType.stream !== undefined) {
6121
- defaultValues[fieldName] = new StreamSchema();
6122
- }
6123
- }
6124
- else if (typeof rawType === "function" && Schema.is(rawType)) {
6125
- if (!rawType.prototype.initialize || rawType.prototype.initialize.length === 0) {
6126
- defaultValues[fieldName] = new rawType();
6127
- }
6204
+ const autoDefault = autoInstantiateDefault(def.type);
6205
+ if (autoDefault !== undefined) {
6206
+ defaultValues[fieldName] = autoDefault;
6128
6207
  }
6129
6208
  }
6130
6209
  }
@@ -7363,8 +7442,19 @@ class Encoder {
7363
7442
  // selected element is passed to `view.add()` which populates
7364
7443
  // view.changes with the stream-link ADD + element-field ADDs.
7365
7444
  this._emitStreamPriority(view);
7366
- // encode visibility-triggered changes collected by view.add()
7367
- for (const [refId, changes] of view.changes) {
7445
+ //
7446
+ // `view.changes` Map insertion order IS topological order:
7447
+ // - `view.add` walks the parent chain to root via `addParentOf`
7448
+ // (depth-first ancestor-first), inserting every ancestor's
7449
+ // entry before the descendant's.
7450
+ // - `view.remove` calls `_touchAncestorsOf` before its own
7451
+ // write to insert any missing ancestors at the front of the
7452
+ // chain — empty entries that get skipped by the size==0
7453
+ // check below but establish Map position.
7454
+ // No per-encode topo sort needed.
7455
+ //
7456
+ for (const refId of view.changes.keys()) {
7457
+ const changes = view.changes.get(refId);
7368
7458
  const changeTree = this.root.changeTrees[refId];
7369
7459
  if (changeTree === undefined) {
7370
7460
  // detached instance, remove from view and skip.
@@ -8766,6 +8856,7 @@ const Callbacks = {
8766
8856
  else if ('decoder' in roomOrDecoder.serializer) {
8767
8857
  return getDecoderStateCallbacks(roomOrDecoder.serializer.decoder);
8768
8858
  }
8859
+ throw new Error('Invalid room or decoder');
8769
8860
  },
8770
8861
  getRawChanges(decoder, callback) {
8771
8862
  return getRawChangesCallback(decoder, callback);
@@ -9221,12 +9312,27 @@ class StateView {
9221
9312
  if (!this.isVisible(changeTree)) {
9222
9313
  // view must have all "changeTree" parent tree
9223
9314
  this.markVisible(changeTree);
9224
- // add parent's parent
9315
+ // Recurse all the way to the root regardless of whether the
9316
+ // parent is filtered. Walking the full chain is what makes
9317
+ // `view.changes` topologically ordered by construction — any
9318
+ // filtered ancestor up the chain is touched here, before the
9319
+ // descendant's entry. The actual entry-write below is gated
9320
+ // on `hasFilteredFields` so non-filtered ancestors don't
9321
+ // emit redundant wire bytes (the decoder already knows them
9322
+ // via the shared encode pass). Marking them visible is
9323
+ // still useful: it makes this short-circuit fire on the next
9324
+ // `view.add` instead of re-walking the chain.
9225
9325
  const parentChangeTree = changeTree.parent?.[$changes];
9226
- if (parentChangeTree && parentChangeTree.hasFilteredFields) {
9326
+ if (parentChangeTree) {
9227
9327
  this.addParentOf(changeTree, tag);
9228
9328
  }
9229
9329
  }
9330
+ // Skip the entry-write for non-filtered ancestors: their refIds
9331
+ // are already known to the decoder through the shared pass, and
9332
+ // an extra ADD on a non-filtered field's index would only emit
9333
+ // bytes for a no-op (`value === previousValue` on the decoder).
9334
+ if (!changeTree.hasFilteredFields)
9335
+ return;
9230
9336
  // add parent's tag properties
9231
9337
  if (changeTree.getChange(parentIndex) !== exports.OPERATION.DELETE) {
9232
9338
  let changes = this.changes.get(changeTree.ref[$refId]);
@@ -9238,6 +9344,45 @@ class StateView {
9238
9344
  changes.set(parentIndex, exports.OPERATION.ADD);
9239
9345
  }
9240
9346
  }
9347
+ /**
9348
+ * Walk `tree`'s parent chain to root and insert an empty entry into
9349
+ * `view.changes` for any ancestor not already present. Empty entries
9350
+ * are skipped by `encodeView` (`changes.size === 0` continue), so no
9351
+ * wire bytes are emitted — but the Map's insertion order now puts
9352
+ * each ancestor BEFORE the descendant entry that the caller is about
9353
+ * to write. Combined with `addParentOf`'s full-recursion walk on
9354
+ * `view.add`, this preserves the global invariant that
9355
+ * `view.changes` iteration order is topological.
9356
+ *
9357
+ * Iterative (not recursive) so the stack is bounded by tree depth
9358
+ * regardless of call patterns. Stops the walk as soon as it hits an
9359
+ * ancestor that's already in `view.changes` — at that point the
9360
+ * remainder of the chain is guaranteed to also be present (invariant
9361
+ * upheld by every prior caller).
9362
+ */
9363
+ _touchAncestorsOf(tree) {
9364
+ let cursor = tree.parent?.[$changes];
9365
+ if (cursor === undefined)
9366
+ return;
9367
+ // Collect the missing prefix of the chain, deepest-first. Only
9368
+ // FILTERED ancestors need entries — non-filtered ones never
9369
+ // appear in `view.changes` (mirrors the addParentOf gate), so
9370
+ // they don't need a Map slot reserved either.
9371
+ const stack = [];
9372
+ while (cursor !== undefined) {
9373
+ if (cursor.hasFilteredFields) {
9374
+ const refId = cursor.ref[$refId];
9375
+ if (this.changes.has(refId))
9376
+ break;
9377
+ stack.push(cursor);
9378
+ }
9379
+ cursor = cursor.parent?.[$changes];
9380
+ }
9381
+ // Insert root-first so Map order is topological.
9382
+ for (let i = stack.length - 1; i >= 0; i--) {
9383
+ this.changes.set(stack[i].ref[$refId], new Map());
9384
+ }
9385
+ }
9241
9386
  remove(obj, tag = DEFAULT_VIEW_TAG, _isClear = false) {
9242
9387
  const changeTree = obj[$changes];
9243
9388
  if (!changeTree) {
@@ -9298,6 +9443,11 @@ class StateView {
9298
9443
  const ref = changeTree.ref;
9299
9444
  const metadata = ref.constructor[Symbol.metadata]; // ArraySchema/MapSchema do not have metadata
9300
9445
  const refId = ref[$refId];
9446
+ // Pre-insert any missing ancestors into view.changes so the Map's
9447
+ // iteration order stays topological — the entries we're about to
9448
+ // write (either on this obj, or on its parent collection below)
9449
+ // must come AFTER every ancestor in the chain on the wire.
9450
+ this._touchAncestorsOf(changeTree);
9301
9451
  let changes = this.changes.get(refId);
9302
9452
  if (changes === undefined) {
9303
9453
  changes = new Map();