@colyseus/core 0.18.17 → 0.18.18

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/Room.mjs CHANGED
@@ -40,6 +40,7 @@ var NOOP = () => {
40
40
  };
41
41
  var noneSerializer = new NoneSerializer();
42
42
  var AFTER_PATCH_OPTS = Object.freeze({ afterNextPatch: true });
43
+ var ROOM_ACCESSORS = ["state", "maxClients", "autoDispose", "patchRate", "unreliablePatchRate"];
43
44
  var DEFAULT_SEAT_RESERVATION_TIME = Number(process.env.COLYSEUS_SEAT_RESERVATION_TIME || 15);
44
45
  function validate(format, handler) {
45
46
  return { format, handler };
@@ -57,53 +58,10 @@ var Room = class _Room {
57
58
  */
58
59
  this.clock = new Clock();
59
60
  this.#_onLeaveConcurrent = 0;
60
- // number of onLeave calls in progress
61
- /**
62
- * Maximum number of clients allowed to connect into the room. When room reaches this limit,
63
- * it is locked automatically. Unless the room was explicitly locked by you via `lock()` method,
64
- * the room will be unlocked as soon as a client disconnects from it.
65
- */
66
- this.maxClients = Infinity;
67
61
  this.#_maxClientsReached = false;
68
- /**
69
- * Automatically dispose the room when last client disconnects.
70
- *
71
- * @default true
72
- */
73
- this.autoDispose = true;
74
- /**
75
- * Frequency to send the room state to connected clients, in milliseconds.
76
- *
77
- * @default 50ms (20fps)
78
- */
79
- this.patchRate = DEFAULT_PATCH_RATE;
80
- /**
81
- * Frequency to flush `@unreliable` state fields, in milliseconds.
82
- *
83
- * Those fields never ride a state patch — they go out over the transport's
84
- * unreliable channel (a WebTransport datagram), so a dropped frame costs one
85
- * stale value instead of stalling the reliable stream behind a retransmit.
86
- * Setting this decouples them from {@link patchRate}, which is the point:
87
- * 60Hz movement over a 20Hz structural patch.
88
- *
89
- * KNOWN COST of a rate faster than {@link patchRate}: an entity's ADD travels
90
- * the reliable channel, so datagrams sent between patches can reference a
91
- * refId the client hasn't been told about yet. Those frames are skipped by the
92
- * client's decoder — safe (the ref graph can't desync, since `@unreliable` is
93
- * primitives-only) but each one logs `"refId" not found`, and that entity's
94
- * first value lands one mutation later. Measured at `patchRate/this` reports
95
- * per mid-session spawn. Leave this unset and the flush rides
96
- * {@link broadcastPatch}, which ships the ADD first and avoids it entirely.
97
- *
98
- * Requires a transport with a datagram channel — today only
99
- * `@colyseus/h3-transport` (WebTransport), which is **experimental**. Every
100
- * WebSocket transport lacks one, and those clients are skipped entirely (the
101
- * room warns once), so `@unreliable` fields keep their join-time value there.
102
- *
103
- * @default null — flush alongside every {@link broadcastPatch}, and only when
104
- * the state actually declares an `@unreliable` field.
105
- */
106
- this.unreliablePatchRate = null;
62
+ this.#_maxClients = Infinity;
63
+ this.#_autoDispose = true;
64
+ this.#_patchRate = DEFAULT_PATCH_RATE;
107
65
  this.#_unreliablePatchRate = null;
108
66
  /**
109
67
  * The unreliable flush, called unconditionally at the end of every
@@ -120,6 +78,8 @@ var Room = class _Room {
120
78
  * @default Infinity
121
79
  */
122
80
  this.maxMessagesPerSecond = Infinity;
81
+ /** Until `__init()`, the prototype accessor setters only store the value. */
82
+ this.#_ready = false;
123
83
  /**
124
84
  * The array of connected clients.
125
85
  *
@@ -210,6 +170,7 @@ var Room = class _Room {
210
170
  #_unreliablePatchRate;
211
171
  #_unreliablePatchInterval;
212
172
  #_state;
173
+ #_ready;
213
174
  #_messages;
214
175
  // Re-expose the registry for @colyseus/playground introspection and
215
176
  // @colyseus/testing handler-swapping (both reach in via bracket access).
@@ -222,21 +183,19 @@ var Room = class _Room {
222
183
  #pendingFrameClients;
223
184
  #afterPatchBroadcasts;
224
185
  #_locked;
225
- /**
226
- * This method is called by the MatchMaker before onCreate()
227
- * @internal
228
- */
229
- __init() {
230
- this.#_state = this.state;
231
- this.#_autoDispose = this.autoDispose;
232
- this.#_patchRate = this.patchRate;
233
- this.#_unreliablePatchRate = this.unreliablePatchRate;
234
- this.#_maxClients = this.maxClients;
235
- Object.defineProperties(this, {
186
+ static {
187
+ Object.defineProperties(this.prototype, {
236
188
  state: {
237
189
  enumerable: true,
238
- get: () => this.#_state,
239
- set: (newState) => {
190
+ configurable: true,
191
+ get() {
192
+ return this.#_state;
193
+ },
194
+ set(newState) {
195
+ if (!this.#_ready) {
196
+ this.#_state = newState;
197
+ return;
198
+ }
240
199
  if (newState?.constructor[Symbol.metadata] !== void 0 || newState[$changes] !== void 0) {
241
200
  this.setSerializer(new SchemaSerializer());
242
201
  } else if ("_definition" in newState) {
@@ -251,15 +210,29 @@ var Room = class _Room {
251
210
  },
252
211
  maxClients: {
253
212
  enumerable: true,
254
- get: () => this.#_maxClients,
255
- set: (value) => {
213
+ configurable: true,
214
+ get() {
215
+ return this.#_maxClients;
216
+ },
217
+ set(value) {
218
+ if (!this.#_ready) {
219
+ this.#_maxClients = value;
220
+ return;
221
+ }
256
222
  this.setMatchmaking({ maxClients: value });
257
223
  }
258
224
  },
259
225
  autoDispose: {
260
226
  enumerable: true,
261
- get: () => this.#_autoDispose,
262
- set: (value) => {
227
+ configurable: true,
228
+ get() {
229
+ return this.#_autoDispose;
230
+ },
231
+ set(value) {
232
+ if (!this.#_ready) {
233
+ this.#_autoDispose = value;
234
+ return;
235
+ }
263
236
  if (value !== this.#_autoDispose && this._internalState !== RoomInternalState.DISPOSING) {
264
237
  this.#_autoDispose = value;
265
238
  this.resetAutoDisposeTimeout();
@@ -268,9 +241,15 @@ var Room = class _Room {
268
241
  },
269
242
  patchRate: {
270
243
  enumerable: true,
271
- get: () => this.#_patchRate,
272
- set: (milliseconds) => {
244
+ configurable: true,
245
+ get() {
246
+ return this.#_patchRate;
247
+ },
248
+ set(milliseconds) {
273
249
  this.#_patchRate = milliseconds;
250
+ if (!this.#_ready) {
251
+ return;
252
+ }
274
253
  if (this.#_patchInterval) {
275
254
  clearInterval(this.#_patchInterval);
276
255
  this.#_patchInterval = void 0;
@@ -284,13 +263,35 @@ var Room = class _Room {
284
263
  },
285
264
  unreliablePatchRate: {
286
265
  enumerable: true,
287
- get: () => this.#_unreliablePatchRate,
288
- set: (milliseconds) => {
266
+ configurable: true,
267
+ get() {
268
+ return this.#_unreliablePatchRate;
269
+ },
270
+ set(milliseconds) {
289
271
  this.#_unreliablePatchRate = milliseconds;
272
+ if (!this.#_ready) {
273
+ return;
274
+ }
290
275
  this._armUnreliablePatches();
291
276
  }
292
277
  }
293
278
  });
279
+ }
280
+ /**
281
+ * This method is called by the MatchMaker before onCreate()
282
+ * @internal
283
+ */
284
+ __init() {
285
+ for (let i = 0; i < ROOM_ACCESSORS.length; i++) {
286
+ const key = ROOM_ACCESSORS[i];
287
+ if (!Object.hasOwn(this, key)) {
288
+ continue;
289
+ }
290
+ const value = this[key];
291
+ Object.defineProperty(this, key, Object.getOwnPropertyDescriptor(_Room.prototype, key));
292
+ this[key] = value;
293
+ }
294
+ this.#_ready = true;
294
295
  this.patchRate = this.#_patchRate;
295
296
  if (this.#_state) {
296
297
  this.state = this.#_state;
@@ -1424,7 +1425,7 @@ var Room = class _Room {
1424
1425
  if (client._enqueuedMessages.length > 0) {
1425
1426
  client._enqueuedMessages.forEach((enqueued) => client.raw(enqueued));
1426
1427
  }
1427
- delete client._enqueuedMessages;
1428
+ client._enqueuedMessages = void 0;
1428
1429
  } else if (code === Protocol.PING) {
1429
1430
  client.raw(getMessageBytes[Protocol.PING]());
1430
1431
  } else if (code === Protocol.LEAVE_ROOM) {