@colyseus/core 0.18.17 → 0.18.19

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.cjs CHANGED
@@ -69,6 +69,7 @@ var NOOP = () => {
69
69
  };
70
70
  var noneSerializer = new import_NoneSerializer.NoneSerializer();
71
71
  var AFTER_PATCH_OPTS = Object.freeze({ afterNextPatch: true });
72
+ var ROOM_ACCESSORS = ["state", "maxClients", "autoDispose", "patchRate", "unreliablePatchRate"];
72
73
  var DEFAULT_SEAT_RESERVATION_TIME = Number(process.env.COLYSEUS_SEAT_RESERVATION_TIME || 15);
73
74
  function validate(format, handler) {
74
75
  return { format, handler };
@@ -86,53 +87,10 @@ var Room = class _Room {
86
87
  */
87
88
  this.clock = new import_timer.ClockTimer();
88
89
  this.#_onLeaveConcurrent = 0;
89
- // number of onLeave calls in progress
90
- /**
91
- * Maximum number of clients allowed to connect into the room. When room reaches this limit,
92
- * it is locked automatically. Unless the room was explicitly locked by you via `lock()` method,
93
- * the room will be unlocked as soon as a client disconnects from it.
94
- */
95
- this.maxClients = Infinity;
96
90
  this.#_maxClientsReached = false;
97
- /**
98
- * Automatically dispose the room when last client disconnects.
99
- *
100
- * @default true
101
- */
102
- this.autoDispose = true;
103
- /**
104
- * Frequency to send the room state to connected clients, in milliseconds.
105
- *
106
- * @default 50ms (20fps)
107
- */
108
- this.patchRate = DEFAULT_PATCH_RATE;
109
- /**
110
- * Frequency to flush `@unreliable` state fields, in milliseconds.
111
- *
112
- * Those fields never ride a state patch — they go out over the transport's
113
- * unreliable channel (a WebTransport datagram), so a dropped frame costs one
114
- * stale value instead of stalling the reliable stream behind a retransmit.
115
- * Setting this decouples them from {@link patchRate}, which is the point:
116
- * 60Hz movement over a 20Hz structural patch.
117
- *
118
- * KNOWN COST of a rate faster than {@link patchRate}: an entity's ADD travels
119
- * the reliable channel, so datagrams sent between patches can reference a
120
- * refId the client hasn't been told about yet. Those frames are skipped by the
121
- * client's decoder — safe (the ref graph can't desync, since `@unreliable` is
122
- * primitives-only) but each one logs `"refId" not found`, and that entity's
123
- * first value lands one mutation later. Measured at `patchRate/this` reports
124
- * per mid-session spawn. Leave this unset and the flush rides
125
- * {@link broadcastPatch}, which ships the ADD first and avoids it entirely.
126
- *
127
- * Requires a transport with a datagram channel — today only
128
- * `@colyseus/h3-transport` (WebTransport), which is **experimental**. Every
129
- * WebSocket transport lacks one, and those clients are skipped entirely (the
130
- * room warns once), so `@unreliable` fields keep their join-time value there.
131
- *
132
- * @default null — flush alongside every {@link broadcastPatch}, and only when
133
- * the state actually declares an `@unreliable` field.
134
- */
135
- this.unreliablePatchRate = null;
91
+ this.#_maxClients = Infinity;
92
+ this.#_autoDispose = true;
93
+ this.#_patchRate = DEFAULT_PATCH_RATE;
136
94
  this.#_unreliablePatchRate = null;
137
95
  /**
138
96
  * The unreliable flush, called unconditionally at the end of every
@@ -149,6 +107,8 @@ var Room = class _Room {
149
107
  * @default Infinity
150
108
  */
151
109
  this.maxMessagesPerSecond = Infinity;
110
+ /** Until `__init()`, the prototype accessor setters only store the value. */
111
+ this.#_ready = false;
152
112
  /**
153
113
  * The array of connected clients.
154
114
  *
@@ -188,9 +148,6 @@ var Room = class _Room {
188
148
  this._events.once("dispose", () => {
189
149
  this.#_dispose().catch((e) => (0, import_Debug.debugAndPrintError)(`onDispose error: ${e && e.stack || e.message || e || "promise rejected"} (roomId: ${this.roomId})`)).finally(() => this._events.emit("disconnect"));
190
150
  });
191
- if (this.onUncaughtException !== void 0) {
192
- this.#registerUncaughtExceptionHandlers();
193
- }
194
151
  }
195
152
  /**
196
153
  * This property will change on these situations:
@@ -239,6 +196,7 @@ var Room = class _Room {
239
196
  #_unreliablePatchRate;
240
197
  #_unreliablePatchInterval;
241
198
  #_state;
199
+ #_ready;
242
200
  #_messages;
243
201
  // Re-expose the registry for @colyseus/playground introspection and
244
202
  // @colyseus/testing handler-swapping (both reach in via bracket access).
@@ -251,21 +209,19 @@ var Room = class _Room {
251
209
  #pendingFrameClients;
252
210
  #afterPatchBroadcasts;
253
211
  #_locked;
254
- /**
255
- * This method is called by the MatchMaker before onCreate()
256
- * @internal
257
- */
258
- __init() {
259
- this.#_state = this.state;
260
- this.#_autoDispose = this.autoDispose;
261
- this.#_patchRate = this.patchRate;
262
- this.#_unreliablePatchRate = this.unreliablePatchRate;
263
- this.#_maxClients = this.maxClients;
264
- Object.defineProperties(this, {
212
+ static {
213
+ Object.defineProperties(this.prototype, {
265
214
  state: {
266
215
  enumerable: true,
267
- get: () => this.#_state,
268
- set: (newState) => {
216
+ configurable: true,
217
+ get() {
218
+ return this.#_state;
219
+ },
220
+ set(newState) {
221
+ if (!this.#_ready) {
222
+ this.#_state = newState;
223
+ return;
224
+ }
269
225
  if (newState?.constructor[Symbol.metadata] !== void 0 || newState[import_schema.$changes] !== void 0) {
270
226
  this.setSerializer(new import_SchemaSerializer.SchemaSerializer());
271
227
  } else if ("_definition" in newState) {
@@ -280,15 +236,29 @@ var Room = class _Room {
280
236
  },
281
237
  maxClients: {
282
238
  enumerable: true,
283
- get: () => this.#_maxClients,
284
- set: (value) => {
239
+ configurable: true,
240
+ get() {
241
+ return this.#_maxClients;
242
+ },
243
+ set(value) {
244
+ if (!this.#_ready) {
245
+ this.#_maxClients = value;
246
+ return;
247
+ }
285
248
  this.setMatchmaking({ maxClients: value });
286
249
  }
287
250
  },
288
251
  autoDispose: {
289
252
  enumerable: true,
290
- get: () => this.#_autoDispose,
291
- set: (value) => {
253
+ configurable: true,
254
+ get() {
255
+ return this.#_autoDispose;
256
+ },
257
+ set(value) {
258
+ if (!this.#_ready) {
259
+ this.#_autoDispose = value;
260
+ return;
261
+ }
292
262
  if (value !== this.#_autoDispose && this._internalState !== RoomInternalState.DISPOSING) {
293
263
  this.#_autoDispose = value;
294
264
  this.resetAutoDisposeTimeout();
@@ -297,9 +267,15 @@ var Room = class _Room {
297
267
  },
298
268
  patchRate: {
299
269
  enumerable: true,
300
- get: () => this.#_patchRate,
301
- set: (milliseconds) => {
270
+ configurable: true,
271
+ get() {
272
+ return this.#_patchRate;
273
+ },
274
+ set(milliseconds) {
302
275
  this.#_patchRate = milliseconds;
276
+ if (!this.#_ready) {
277
+ return;
278
+ }
303
279
  if (this.#_patchInterval) {
304
280
  clearInterval(this.#_patchInterval);
305
281
  this.#_patchInterval = void 0;
@@ -313,13 +289,35 @@ var Room = class _Room {
313
289
  },
314
290
  unreliablePatchRate: {
315
291
  enumerable: true,
316
- get: () => this.#_unreliablePatchRate,
317
- set: (milliseconds) => {
292
+ configurable: true,
293
+ get() {
294
+ return this.#_unreliablePatchRate;
295
+ },
296
+ set(milliseconds) {
318
297
  this.#_unreliablePatchRate = milliseconds;
298
+ if (!this.#_ready) {
299
+ return;
300
+ }
319
301
  this._armUnreliablePatches();
320
302
  }
321
303
  }
322
304
  });
305
+ }
306
+ /**
307
+ * This method is called by the MatchMaker before onCreate()
308
+ * @internal
309
+ */
310
+ __init() {
311
+ for (let i = 0; i < ROOM_ACCESSORS.length; i++) {
312
+ const key = ROOM_ACCESSORS[i];
313
+ if (!Object.hasOwn(this, key)) {
314
+ continue;
315
+ }
316
+ const value = this[key];
317
+ Object.defineProperty(this, key, Object.getOwnPropertyDescriptor(_Room.prototype, key));
318
+ this[key] = value;
319
+ }
320
+ this.#_ready = true;
323
321
  this.patchRate = this.#_patchRate;
324
322
  if (this.#_state) {
325
323
  this.state = this.#_state;
@@ -327,6 +325,9 @@ var Room = class _Room {
327
325
  if (this.plugins !== void 0) {
328
326
  (0, import_RoomPlugin.setupRoomPlugins)(this);
329
327
  }
328
+ if (this.onUncaughtException !== void 0) {
329
+ this.#registerUncaughtExceptionHandlers();
330
+ }
330
331
  if (this.messages !== void 0) {
331
332
  if (this.messages["_"]) {
332
333
  this.onMessage("*", this.messages["_"].bind(this));
@@ -1453,7 +1454,7 @@ var Room = class _Room {
1453
1454
  if (client._enqueuedMessages.length > 0) {
1454
1455
  client._enqueuedMessages.forEach((enqueued) => client.raw(enqueued));
1455
1456
  }
1456
- delete client._enqueuedMessages;
1457
+ client._enqueuedMessages = void 0;
1457
1458
  } else if (code === import_shared_types.Protocol.PING) {
1458
1459
  client.raw(import_Protocol.getMessageBytes[import_shared_types.Protocol.PING]());
1459
1460
  } else if (code === import_shared_types.Protocol.LEAVE_ROOM) {
@@ -1550,7 +1551,7 @@ var Room = class _Room {
1550
1551
  if (this.onCreate !== void 0) {
1551
1552
  this.onCreate = (0, import_Utils.wrapTryCatch)(this.onCreate.bind(this), onUncaughtException, import_RoomExceptions.OnCreateException, "onCreate", true);
1552
1553
  }
1553
- if (this.onAuth !== void 0) {
1554
+ if (this.onAuth !== _Room.prototype.onAuth) {
1554
1555
  this.onAuth = (0, import_Utils.wrapTryCatch)(this.onAuth.bind(this), onUncaughtException, import_RoomExceptions.OnAuthException, "onAuth", true);
1555
1556
  }
1556
1557
  if (this.onJoin !== void 0) {