Skip to content

Commit

Permalink
feat: create a particular type to the room state
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossantos74 committed Jan 9, 2025
1 parent 21e5076 commit 42f5fc9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
6 changes: 3 additions & 3 deletions packages/room/src/core/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Logger } from '../common/utils/logger';
import { IOC } from '../services/io';
import { IOCState } from '../services/io/types';

import { ParticipantEvent, RoomParams } from './types';
import { ParticipantEvent, RoomParams, RoomState } from './types';

import { Room } from './index';

Expand Down Expand Up @@ -212,7 +212,7 @@ describe('Room', () => {
});

it('should get participants when room is connected', async () => {
room['state'] = IOCState.CONNECTED;
room['state'] = RoomState.CONNECTED;

const date = Date.now();

Expand Down Expand Up @@ -249,7 +249,7 @@ describe('Room', () => {
});

it('should return empty array when room is not connected', async () => {
room['state'] = IOCState.DISCONNECTED;
room['state'] = RoomState.DISCONNECTED;

const participants = await room.getParticipants();

Expand Down
22 changes: 11 additions & 11 deletions packages/room/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SlotService } from '../services/slot';
import { StoreType } from '../stores/common/types';
import { useStore } from '../stores/common/use-store';

import { GeneralEvent, ParticipantEvent, RoomEventPayload, RoomParams, Callback, EventOptions, RoomEvent } from './types';
import { GeneralEvent, ParticipantEvent, RoomEventPayload, RoomParams, Callback, EventOptions, RoomEvent, RoomState } from './types';

export class Room {
private participant: Participant;
Expand All @@ -20,7 +20,7 @@ export class Room {
private slotService: SlotService;
private useStore = useStore.bind(this) as typeof useStore;
private participants: Record<string, Participant>;
private state: IOCState = IOCState.DISCONNECTED;
private state: RoomState = RoomState.DISCONNECTED;
private logger: Logger;

private subscriptions: Map<Callback<GeneralEvent>, Subscription> = new Map();
Expand All @@ -39,7 +39,7 @@ export class Room {
* @description leave the room, destroy the socket connnection and all attached components
*/
public leave() {
this.state = IOCState.DISCONNECTED;
this.state = RoomState.DISCONNECTED;
this.unsubscribeFromRoomEvents();

this.emit(ParticipantEvent.PARTICIPANT_LEFT, this.participant);
Expand Down Expand Up @@ -123,7 +123,7 @@ export class Room {
an empty array is returned.
*/
public async getParticipants(): Promise<Participant[]> {
if (!this.room || this.state !== IOCState.CONNECTED) {
if (!this.room || this.state !== RoomState.CONNECTED) {
return [];
}

Expand Down Expand Up @@ -396,18 +396,18 @@ export class Room {
private onConnectionStateChange = (state: IOCState): void => {
this.logger.log('connection state changed', state);

const common = () => {
const common = (state: RoomState) => {
this.emit(RoomEvent.UPDATE, { status: state });
this.state = state;
};

const map = {
[IOCState.CONNECTING]: () => common(),
[IOCState.CONNECTION_ERROR]: () => common(),
[IOCState.CONNECTED]: () => common(),
[IOCState.DISCONNECTED]: () => common(),
[IOCState.RECONNECTING]: () => common(),
[IOCState.RECONNECT_ERROR]: () => common(),
[IOCState.CONNECTING]: () => common(RoomState.CONNECTING),
[IOCState.CONNECTION_ERROR]: () => common(RoomState.CONNECTION_ERROR),
[IOCState.CONNECTED]: () => common(RoomState.CONNECTED),
[IOCState.DISCONNECTED]: () => common(RoomState.DISCONNECTED),
[IOCState.RECONNECTING]: () => common(RoomState.RECONNECTING),
[IOCState.RECONNECT_ERROR]: () => common(RoomState.RECONNECT_ERROR),

// error
[IOCState.AUTH_ERROR]: () => this.onAuthError(),
Expand Down
11 changes: 10 additions & 1 deletion packages/room/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ type RoomError = {
}

type RoomUpdate = {
status: IOCState,
status: RoomState | `${RoomState}`
}

export enum RoomState {
CONNECTED = 'CONNECTED',
CONNECTING = 'CONNECTING',
DISCONNECTED = 'DISCONNECTED',
CONNECTION_ERROR = 'CONNECTION_ERROR',
RECONNECTING = 'RECONNECTING',
RECONNECT_ERROR = 'RECONNECT_ERROR',
}

export enum ParticipantEvent {
Expand Down
4 changes: 3 additions & 1 deletion packages/room/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from 'zod';

import { InitialParticipant, Participant } from './common/types/participant.types';
import { Room } from './core';
import { Callback, ParticipantEvent, RoomEvent } from './core/types';
import { Callback, ParticipantEvent, RoomEvent, RoomState } from './core/types';
import { ApiService } from './services/api';
import config from './services/config';
import { InitializeRoomParams, InitializeRoomSchema } from './types';
Expand Down Expand Up @@ -147,12 +147,14 @@ export {
RoomEvent,
ParticipantEvent,
Callback,
RoomState,
};

if (typeof window !== 'undefined') {
window.SuperVizRoom = {
createRoom,
RoomEvent,
ParticipantEvent,
RoomState,
};
}
2 changes: 1 addition & 1 deletion packages/room/src/shims.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Room } from './core';

declare global {
interface Window {
SuperVizRoom: {};
SuperVizRoom: Record<string, unknown>;
SUPERVIZ_ROOM: Room;
}
}

0 comments on commit 42f5fc9

Please sign in to comment.