@antha/multiplayer-core 0.0.3 → 0.0.5

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.
@@ -1,7 +1,7 @@
1
1
  import { type JsonCompatibleValue, type MaybePromise, type PartialWithUndefined } from '@augment-vir/common';
2
2
  import { type FindPortOptions } from '@rest-vir/api';
3
3
  import { type AnyDuration } from 'date-vir';
4
- import { ListenTarget } from 'typed-event-target';
4
+ import { ListenTarget, type RemoveListenerCallback } from 'typed-event-target';
5
5
  import { type ClientId, type RoomId } from '../multiplayer-id.js';
6
6
  import { type RoomInput } from '../webrtc/webrtc-multiplayer-controller.js';
7
7
  import { RoomRejectionError } from './errors.js';
@@ -397,7 +397,9 @@ export declare class MultiplayerRoomController<Message extends JsonCompatibleVal
397
397
  *
398
398
  * If a callback is provided, it is called each time the room list is updated.
399
399
  */
400
- startRoomUpdates(callback?: ControllerRoomListListener | undefined): import("typed-event-target").RemoveListenerCallback;
400
+ startRoomUpdates(callback: ControllerRoomListListener): RemoveListenerCallback;
401
+ startRoomUpdates(callback?: undefined): undefined;
402
+ startRoomUpdates(callback?: ControllerRoomListListener | undefined): RemoveListenerCallback | undefined;
401
403
  /** Turn off room list updates and remove callbacks added via `startRoomUpdates`. */
402
404
  stopRoomUpdates(): void;
403
405
  /** Send a generic message to the current room. */
@@ -408,11 +410,7 @@ export declare class MultiplayerRoomController<Message extends JsonCompatibleVal
408
410
  isConnected(): boolean;
409
411
  /** Cleanup everything. */
410
412
  destroy(): void;
411
- /**
412
- * Join or create a room.
413
- *
414
- * @throws `Error` if this controller is already connected to a room.
415
- */
413
+ /** Join or create a room. */
416
414
  joinOrCreateRoom(room: Readonly<RoomInput>): Promise<void>;
417
415
  /** Leave the current room or single player connection. */
418
416
  leaveRoom(): void;
@@ -1,7 +1,7 @@
1
1
  import { waitUntil } from '@augment-vir/assert';
2
2
  import { ensureError, makeWritable, } from '@augment-vir/common';
3
3
  import { convertDuration } from 'date-vir';
4
- import { defineTypedCustomEvent, ListenTarget } from 'typed-event-target';
4
+ import { defineTypedCustomEvent, ListenTarget, } from 'typed-event-target';
5
5
  import { WebrtcMultiplayerConnectionUpdateEvent, WebrtcMultiplayerController, WebrtcMultiplayerMessageEvent, } from '../webrtc/webrtc-multiplayer-controller.js';
6
6
  import { RoomRejectionError } from './errors.js';
7
7
  import { multiplayerHealthEndpoint, multiplayerRoomsEndpoint, } from './multiplayer-api.js';
@@ -178,17 +178,17 @@ export class MultiplayerRoomController extends ListenTarget {
178
178
  throw error;
179
179
  }
180
180
  }
181
- /**
182
- * Listen for room list updates, including while connected to a room.
183
- *
184
- * If a callback is provided, it is called each time the room list is updated.
185
- */
186
181
  startRoomUpdates(callback) {
187
182
  this.isListeningToRoomUpdates = true;
188
183
  this.startRoomInterval();
189
- return this.listen(ControllerRoomListEvent, async (event) => {
190
- await callback?.(event.detail);
191
- });
184
+ if (callback) {
185
+ return this.listen(ControllerRoomListEvent, async (event) => {
186
+ await callback(event.detail);
187
+ });
188
+ }
189
+ else {
190
+ return undefined;
191
+ }
192
192
  }
193
193
  /** Turn off room list updates and remove callbacks added via `startRoomUpdates`. */
194
194
  stopRoomUpdates() {
@@ -220,21 +220,16 @@ export class MultiplayerRoomController extends ListenTarget {
220
220
  this.currentConnection?.destroy();
221
221
  this.stopRoomUpdates();
222
222
  }
223
- /**
224
- * Join or create a room.
225
- *
226
- * @throws `Error` if this controller is already connected to a room.
227
- */
223
+ /** Join or create a room. */
228
224
  async joinOrCreateRoom(room) {
229
- if (this.currentConnection) {
230
- throw new Error('Cannot join room: connection already established.');
231
- }
232
- else if (!this.multiplayerApiClient || !this.multiplayerParams) {
225
+ if (!this.multiplayerApiClient || !this.multiplayerParams) {
233
226
  throw new Error('Cannot join room. Please start this controller in multiplayer mode to join rooms.');
234
227
  }
235
228
  else if (this.rejectedRoomIds.has(room.roomId)) {
236
229
  throw new RoomRejectionError(room);
237
230
  }
231
+ const previousConnection = this.currentConnection;
232
+ const previousRoomId = this.roomId;
238
233
  this.updateConnectionState({
239
234
  room: MultiplayerConnectionState.Connecting,
240
235
  });
@@ -243,7 +238,6 @@ export class MultiplayerRoomController extends ListenTarget {
243
238
  return this.params.acceptConnection?.(data.connectingClientId, this) ?? true;
244
239
  }
245
240
  : undefined);
246
- this.currentConnection = currentConnection;
247
241
  currentConnection.listen((WebrtcMultiplayerMessageEvent), (event) => {
248
242
  this.dispatch(new ControllerMessageEvent(event.sourceClientId, event.detail));
249
243
  });
@@ -252,32 +246,47 @@ export class MultiplayerRoomController extends ListenTarget {
252
246
  detail: event.detail,
253
247
  }));
254
248
  });
255
- await currentConnection.initConnection();
256
- const connectionResult = await waitUntil.isDefined(() => {
257
- const connected = currentConnection.isConnected();
258
- const destroyed = currentConnection.isDestroyed;
259
- return !connected && !destroyed
260
- ? undefined
261
- : {
262
- connected,
263
- destroyed,
264
- };
265
- });
266
- if (connectionResult.connected) {
267
- makeWritable(this).roomId = room.roomId;
268
- this.stopRoomUpdates();
269
- this.updateConnectionState({
270
- room: MultiplayerConnectionState.Connected,
249
+ try {
250
+ await currentConnection.initConnection();
251
+ const connectionResult = await waitUntil.isDefined(() => {
252
+ const connected = currentConnection.isConnected();
253
+ const destroyed = currentConnection.isDestroyed;
254
+ return !connected && !destroyed
255
+ ? undefined
256
+ : {
257
+ connected,
258
+ destroyed,
259
+ };
271
260
  });
261
+ if (connectionResult.connected) {
262
+ this.currentConnection = currentConnection;
263
+ previousConnection?.destroy();
264
+ makeWritable(this).roomId = room.roomId;
265
+ this.stopRoomUpdates();
266
+ this.updateConnectionState({
267
+ room: MultiplayerConnectionState.Connected,
268
+ });
269
+ }
270
+ else {
271
+ this.rejectedRoomIds.add(room.roomId);
272
+ throw new RoomRejectionError(room);
273
+ }
272
274
  }
273
- else {
275
+ catch (error) {
274
276
  currentConnection.destroy();
275
- this.rejectedRoomIds.add(room.roomId);
276
- this.currentConnection = undefined;
277
- const error = new RoomRejectionError(room);
278
- this.updateConnectionState({
279
- room: error,
280
- });
277
+ if (previousConnection) {
278
+ this.currentConnection = previousConnection;
279
+ makeWritable(this).roomId = previousRoomId;
280
+ this.updateConnectionState({
281
+ room: MultiplayerConnectionState.Connected,
282
+ });
283
+ }
284
+ else {
285
+ this.currentConnection = undefined;
286
+ this.updateConnectionState({
287
+ room: ensureError(error),
288
+ });
289
+ }
281
290
  throw error;
282
291
  }
283
292
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antha/multiplayer-core",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Core functionalities for Antha multiplayer mods.",
5
5
  "keywords": [
6
6
  "vir",