@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.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
  *
@@ -159,9 +119,6 @@ var Room = class _Room {
159
119
  this._events.once("dispose", () => {
160
120
  this.#_dispose().catch((e) => debugAndPrintError(`onDispose error: ${e && e.stack || e.message || e || "promise rejected"} (roomId: ${this.roomId})`)).finally(() => this._events.emit("disconnect"));
161
121
  });
162
- if (this.onUncaughtException !== void 0) {
163
- this.#registerUncaughtExceptionHandlers();
164
- }
165
122
  }
166
123
  /**
167
124
  * This property will change on these situations:
@@ -210,6 +167,7 @@ var Room = class _Room {
210
167
  #_unreliablePatchRate;
211
168
  #_unreliablePatchInterval;
212
169
  #_state;
170
+ #_ready;
213
171
  #_messages;
214
172
  // Re-expose the registry for @colyseus/playground introspection and
215
173
  // @colyseus/testing handler-swapping (both reach in via bracket access).
@@ -222,21 +180,19 @@ var Room = class _Room {
222
180
  #pendingFrameClients;
223
181
  #afterPatchBroadcasts;
224
182
  #_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, {
183
+ static {
184
+ Object.defineProperties(this.prototype, {
236
185
  state: {
237
186
  enumerable: true,
238
- get: () => this.#_state,
239
- set: (newState) => {
187
+ configurable: true,
188
+ get() {
189
+ return this.#_state;
190
+ },
191
+ set(newState) {
192
+ if (!this.#_ready) {
193
+ this.#_state = newState;
194
+ return;
195
+ }
240
196
  if (newState?.constructor[Symbol.metadata] !== void 0 || newState[$changes] !== void 0) {
241
197
  this.setSerializer(new SchemaSerializer());
242
198
  } else if ("_definition" in newState) {
@@ -251,15 +207,29 @@ var Room = class _Room {
251
207
  },
252
208
  maxClients: {
253
209
  enumerable: true,
254
- get: () => this.#_maxClients,
255
- set: (value) => {
210
+ configurable: true,
211
+ get() {
212
+ return this.#_maxClients;
213
+ },
214
+ set(value) {
215
+ if (!this.#_ready) {
216
+ this.#_maxClients = value;
217
+ return;
218
+ }
256
219
  this.setMatchmaking({ maxClients: value });
257
220
  }
258
221
  },
259
222
  autoDispose: {
260
223
  enumerable: true,
261
- get: () => this.#_autoDispose,
262
- set: (value) => {
224
+ configurable: true,
225
+ get() {
226
+ return this.#_autoDispose;
227
+ },
228
+ set(value) {
229
+ if (!this.#_ready) {
230
+ this.#_autoDispose = value;
231
+ return;
232
+ }
263
233
  if (value !== this.#_autoDispose && this._internalState !== RoomInternalState.DISPOSING) {
264
234
  this.#_autoDispose = value;
265
235
  this.resetAutoDisposeTimeout();
@@ -268,9 +238,15 @@ var Room = class _Room {
268
238
  },
269
239
  patchRate: {
270
240
  enumerable: true,
271
- get: () => this.#_patchRate,
272
- set: (milliseconds) => {
241
+ configurable: true,
242
+ get() {
243
+ return this.#_patchRate;
244
+ },
245
+ set(milliseconds) {
273
246
  this.#_patchRate = milliseconds;
247
+ if (!this.#_ready) {
248
+ return;
249
+ }
274
250
  if (this.#_patchInterval) {
275
251
  clearInterval(this.#_patchInterval);
276
252
  this.#_patchInterval = void 0;
@@ -284,13 +260,35 @@ var Room = class _Room {
284
260
  },
285
261
  unreliablePatchRate: {
286
262
  enumerable: true,
287
- get: () => this.#_unreliablePatchRate,
288
- set: (milliseconds) => {
263
+ configurable: true,
264
+ get() {
265
+ return this.#_unreliablePatchRate;
266
+ },
267
+ set(milliseconds) {
289
268
  this.#_unreliablePatchRate = milliseconds;
269
+ if (!this.#_ready) {
270
+ return;
271
+ }
290
272
  this._armUnreliablePatches();
291
273
  }
292
274
  }
293
275
  });
276
+ }
277
+ /**
278
+ * This method is called by the MatchMaker before onCreate()
279
+ * @internal
280
+ */
281
+ __init() {
282
+ for (let i = 0; i < ROOM_ACCESSORS.length; i++) {
283
+ const key = ROOM_ACCESSORS[i];
284
+ if (!Object.hasOwn(this, key)) {
285
+ continue;
286
+ }
287
+ const value = this[key];
288
+ Object.defineProperty(this, key, Object.getOwnPropertyDescriptor(_Room.prototype, key));
289
+ this[key] = value;
290
+ }
291
+ this.#_ready = true;
294
292
  this.patchRate = this.#_patchRate;
295
293
  if (this.#_state) {
296
294
  this.state = this.#_state;
@@ -298,6 +296,9 @@ var Room = class _Room {
298
296
  if (this.plugins !== void 0) {
299
297
  setupRoomPlugins(this);
300
298
  }
299
+ if (this.onUncaughtException !== void 0) {
300
+ this.#registerUncaughtExceptionHandlers();
301
+ }
301
302
  if (this.messages !== void 0) {
302
303
  if (this.messages["_"]) {
303
304
  this.onMessage("*", this.messages["_"].bind(this));
@@ -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) {
@@ -1521,7 +1522,7 @@ var Room = class _Room {
1521
1522
  if (this.onCreate !== void 0) {
1522
1523
  this.onCreate = wrapTryCatch(this.onCreate.bind(this), onUncaughtException, OnCreateException, "onCreate", true);
1523
1524
  }
1524
- if (this.onAuth !== void 0) {
1525
+ if (this.onAuth !== _Room.prototype.onAuth) {
1525
1526
  this.onAuth = wrapTryCatch(this.onAuth.bind(this), onUncaughtException, OnAuthException, "onAuth", true);
1526
1527
  }
1527
1528
  if (this.onJoin !== void 0) {