@antha/multiplayer-core 0.0.4 → 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.
@@ -410,11 +410,7 @@ export declare class MultiplayerRoomController<Message extends JsonCompatibleVal
410
410
  isConnected(): boolean;
411
411
  /** Cleanup everything. */
412
412
  destroy(): void;
413
- /**
414
- * Join or create a room.
415
- *
416
- * @throws `Error` if this controller is already connected to a room.
417
- */
413
+ /** Join or create a room. */
418
414
  joinOrCreateRoom(room: Readonly<RoomInput>): Promise<void>;
419
415
  /** Leave the current room or single player connection. */
420
416
  leaveRoom(): void;
@@ -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.4",
3
+ "version": "0.0.5",
4
4
  "description": "Core functionalities for Antha multiplayer mods.",
5
5
  "keywords": [
6
6
  "vir",