From e1f57e9f97b672de6f3d54b5c23757fe83571638 Mon Sep 17 00:00:00 2001 From: Ian Silva Date: Wed, 14 Aug 2024 09:52:27 -0300 Subject: [PATCH] chore: import socket-client from modules instead of lib folder --- __mocks__/io.mock.ts | 2 +- jest.setup.js | 2 +- package.json | 2 +- src/common/types/cdn.types.ts | 2 +- src/components/base/index.ts | 2 +- src/components/form-elements/index.ts | 2 +- src/components/form-elements/types.ts | 2 +- src/components/presence-mouse/canvas/index.ts | 2 +- src/components/presence-mouse/html/index.ts | 2 +- src/components/realtime/channel.ts | 2 +- src/components/realtime/presence.test.ts | 2 +- src/components/realtime/presence.ts | 10 +- src/components/video/index.test.ts | 2 +- src/components/video/index.ts | 2 +- src/components/who-is-online/index.ts | 2 +- src/core/launcher/index.ts | 2 +- src/index.ts | 2 +- .../socket/common/types/callbacks.types.ts | 1 - src/lib/socket/common/types/event.types.ts | 46 ---- src/lib/socket/common/types/presence.types.ts | 8 - src/lib/socket/connection/index.ts | 133 ----------- src/lib/socket/connection/types.ts | 45 ---- src/lib/socket/index.ts | 23 -- src/lib/socket/presence/index.ts | 202 ----------------- src/lib/socket/presence/types.ts | 14 -- src/lib/socket/realtime/index.ts | 62 ------ src/lib/socket/room/index.ts | 208 ------------------ src/lib/socket/room/types.ts | 39 ---- src/services/io/index.ts | 2 +- .../presence-3d-manager/index.test.ts | 4 +- src/services/presence-3d-manager/index.ts | 2 +- src/services/room-state/index.test.ts | 2 +- src/services/room-state/index.ts | 2 +- src/services/room-state/type.ts | 2 +- src/services/slot/index.ts | 2 +- yarn.lock | 32 +++ 36 files changed, 62 insertions(+), 809 deletions(-) delete mode 100644 src/lib/socket/common/types/callbacks.types.ts delete mode 100644 src/lib/socket/common/types/event.types.ts delete mode 100644 src/lib/socket/common/types/presence.types.ts delete mode 100644 src/lib/socket/connection/index.ts delete mode 100644 src/lib/socket/connection/types.ts delete mode 100644 src/lib/socket/index.ts delete mode 100644 src/lib/socket/presence/index.ts delete mode 100644 src/lib/socket/presence/types.ts delete mode 100644 src/lib/socket/realtime/index.ts delete mode 100644 src/lib/socket/room/index.ts delete mode 100644 src/lib/socket/room/types.ts diff --git a/__mocks__/io.mock.ts b/__mocks__/io.mock.ts index 78795ee2..e38ffb8a 100644 --- a/__mocks__/io.mock.ts +++ b/__mocks__/io.mock.ts @@ -1,5 +1,5 @@ import { jest } from '@jest/globals'; -import { Room } from '../src/lib/socket'; +import { Room } from '@superviz/socket-client'; export const MOCK_IO = { ClientState: { diff --git a/jest.setup.js b/jest.setup.js index e631e1b8..ff4045e5 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -36,4 +36,4 @@ global.DOMPoint = class { } }; -jest.mock('./src/lib/socket', () => MOCK_IO); +jest.mock('@superviz/socket-client', () => MOCK_IO); diff --git a/package.json b/package.json index e8ceb3bd..d930dc8b 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,7 @@ "luxon": "^3.4.4", "rxjs": "^7.8.1", "semantic-release-version-file": "^1.0.2", - "socket.io-client": "^4.7.5", + "@superviz/socket-client": "^1.10.0", "zod": "^3.23.8" }, "config": { diff --git a/src/common/types/cdn.types.ts b/src/common/types/cdn.types.ts index 1a2f6dec..5fbf41ae 100644 --- a/src/common/types/cdn.types.ts +++ b/src/common/types/cdn.types.ts @@ -35,7 +35,7 @@ import type { import { ParticipantType } from './participant.types'; import { SuperVizSdkOptions } from './sdk-options.types'; import { StoreType } from '../types/stores.types'; -import { PresenceEvents } from '../../lib/socket/common/types/event.types'; +import { PresenceEvents } from '@superviz/socket-client'; import { FieldEvents } from '../../components/form-elements/types'; import { PinMode } from '../../web-components/comments/components/types'; diff --git a/src/components/base/index.ts b/src/components/base/index.ts index cbcae7de..6d72e47e 100644 --- a/src/components/base/index.ts +++ b/src/components/base/index.ts @@ -1,4 +1,4 @@ -import * as Socket from '../../lib/socket'; +import * as Socket from '@superviz/socket-client'; import { ComponentLifeCycleEvent } from '../../common/types/events.types'; import { Group } from '../../common/types/participant.types'; diff --git a/src/components/form-elements/index.ts b/src/components/form-elements/index.ts index be3e5b44..8b14a522 100644 --- a/src/components/form-elements/index.ts +++ b/src/components/form-elements/index.ts @@ -1,4 +1,4 @@ -import type { SocketEvent } from '../../lib/socket'; +import type { SocketEvent } from '@superviz/socket-client'; import { Participant } from '../../common/types/participant.types'; import { StoreType } from '../../common/types/stores.types'; diff --git a/src/components/form-elements/types.ts b/src/components/form-elements/types.ts index eacd4527..5d07277f 100644 --- a/src/components/form-elements/types.ts +++ b/src/components/form-elements/types.ts @@ -1,4 +1,4 @@ -import { SocketEvent } from '../../lib/socket'; +import { SocketEvent } from '@superviz/socket-client'; export type FormElementsProps = { fields?: string[] | string; diff --git a/src/components/presence-mouse/canvas/index.ts b/src/components/presence-mouse/canvas/index.ts index 3d04b265..d8f328c5 100644 --- a/src/components/presence-mouse/canvas/index.ts +++ b/src/components/presence-mouse/canvas/index.ts @@ -1,4 +1,4 @@ -import * as Socket from '../../../lib/socket'; +import * as Socket from '@superviz/socket-client'; import { throttle } from 'lodash'; import { RealtimeEvent } from '../../../common/types/events.types'; diff --git a/src/components/presence-mouse/html/index.ts b/src/components/presence-mouse/html/index.ts index 0b47e02e..4bffb602 100644 --- a/src/components/presence-mouse/html/index.ts +++ b/src/components/presence-mouse/html/index.ts @@ -1,4 +1,4 @@ -import * as Socket from '../../../lib/socket'; +import * as Socket from '@superviz/socket-client'; import { isEqual } from 'lodash'; import { Subscription, fromEvent, throttleTime } from 'rxjs'; diff --git a/src/components/realtime/channel.ts b/src/components/realtime/channel.ts index 1939d930..9a30e783 100644 --- a/src/components/realtime/channel.ts +++ b/src/components/realtime/channel.ts @@ -1,4 +1,4 @@ -import * as Socket from '../../lib/socket'; +import * as Socket from '@superviz/socket-client'; import throttle from 'lodash/throttle'; import { Participant } from '../../common/types/participant.types'; diff --git a/src/components/realtime/presence.test.ts b/src/components/realtime/presence.test.ts index 3166e5eb..e1baa777 100644 --- a/src/components/realtime/presence.test.ts +++ b/src/components/realtime/presence.test.ts @@ -1,4 +1,4 @@ -import { PresenceEvent, PresenceEvents, Room } from '../../lib/socket'; +import { PresenceEvent, PresenceEvents, Room } from '@superviz/socket-client'; import { RealtimePresence } from './presence'; import { MOCK_IO } from '../../../__mocks__/io.mock'; diff --git a/src/components/realtime/presence.ts b/src/components/realtime/presence.ts index ce98feb6..f3fe65c7 100644 --- a/src/components/realtime/presence.ts +++ b/src/components/realtime/presence.ts @@ -1,6 +1,5 @@ import { Logger } from '../../common/utils'; -import * as Socket from '../../lib/socket'; -import { PresenceEventsArg } from '../../lib/socket/common/types/event.types'; +import * as Socket from '@superviz/socket-client'; export class RealtimePresence { private logger: Logger; @@ -14,12 +13,15 @@ export class RealtimePresence { this.room.presence.update(data); } - public subscribe(event: PresenceEventsArg, callback: Socket.PresenceCallback) { + public subscribe( + event: Socket.PresenceEventsArg, + callback: Socket.PresenceCallback, + ) { this.logger.log('Realtime Presence @ subscribe', event); this.room.presence.on(event, callback); } - public unsubscribe(event: PresenceEventsArg) { + public unsubscribe(event: Socket.PresenceEventsArg) { this.logger.log('Realtime Presence @ unsubscribe', event); this.room.presence.off(event); } diff --git a/src/components/video/index.test.ts b/src/components/video/index.test.ts index 1e51df9c..fcc23042 100644 --- a/src/components/video/index.test.ts +++ b/src/components/video/index.test.ts @@ -1,6 +1,6 @@ import { TextEncoder, TextDecoder } from 'util'; -import { PresenceEvent } from '../../lib/socket'; +import { PresenceEvent } from '@superviz/socket-client'; import { MOCK_CONFIG } from '../../../__mocks__/config.mock'; import { EVENT_BUS_MOCK } from '../../../__mocks__/event-bus.mock'; diff --git a/src/components/video/index.ts b/src/components/video/index.ts index 63d68bba..01793ad3 100644 --- a/src/components/video/index.ts +++ b/src/components/video/index.ts @@ -1,4 +1,4 @@ -import { PresenceEvent, PresenceEvents, Room } from '../../lib/socket'; +import { PresenceEvent, PresenceEvents, Room } from '@superviz/socket-client'; import { ColorsVariables } from '../../common/types/colors.types'; import { diff --git a/src/components/who-is-online/index.ts b/src/components/who-is-online/index.ts index 97b7bafb..ad66c0ed 100644 --- a/src/components/who-is-online/index.ts +++ b/src/components/who-is-online/index.ts @@ -1,4 +1,4 @@ -import { PresenceEvent, PresenceEvents } from '../../lib/socket'; +import { PresenceEvent, PresenceEvents } from '@superviz/socket-client'; import { RealtimeEvent, WhoIsOnlineEvent } from '../../common/types/events.types'; import { Participant, Avatar } from '../../common/types/participant.types'; diff --git a/src/core/launcher/index.ts b/src/core/launcher/index.ts index 95ee1099..48f90e15 100644 --- a/src/core/launcher/index.ts +++ b/src/core/launcher/index.ts @@ -1,4 +1,4 @@ -import * as Socket from '../../lib/socket'; +import * as Socket from '@superviz/socket-client'; import { isEqual } from 'lodash'; import { ParticipantEvent } from '../../common/types/events.types'; diff --git a/src/index.ts b/src/index.ts index 4f14b822..6612ded5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,7 +24,7 @@ import { import { ParticipantType } from './common/types/participant.types'; import { RealtimeComponentEvent, RealtimeComponentState } from './components/realtime/types'; import { StoreType } from './common/types/stores.types'; -import { PresenceEvents } from './lib/socket/common/types/event.types'; +import { PresenceEvents } from '@superviz/socket-client'; import { FieldEvents } from './components/form-elements/types'; import { PinMode } from './web-components/comments/components/types'; diff --git a/src/lib/socket/common/types/callbacks.types.ts b/src/lib/socket/common/types/callbacks.types.ts deleted file mode 100644 index a97e7e2d..00000000 --- a/src/lib/socket/common/types/callbacks.types.ts +++ /dev/null @@ -1 +0,0 @@ -export type ErrorCallback = (error: Error) => void; diff --git a/src/lib/socket/common/types/event.types.ts b/src/lib/socket/common/types/event.types.ts deleted file mode 100644 index 2db2c012..00000000 --- a/src/lib/socket/common/types/event.types.ts +++ /dev/null @@ -1,46 +0,0 @@ -/** - * @enum RoomEvents - * @description events that the server listens to in the room module - * @property JOIN_ROOM - event to join a room - * @property LEAVE_ROOM - event to leave a room - * @property UPDATE - event to update a room - * @property JOINED_ROOM - event to indicate a user has joined a room - * @property ERROR - event to indicate an error in the room module - */ -export enum RoomEvents { - JOIN_ROOM = 'room.join', - JOINED_ROOM = 'room.joined', - LEAVE_ROOM = 'room.leave', - UPDATE = 'room.update', - ERROR = 'room.error', -} - -export enum InternalRoomEvents { - GET = 'room.get', -} - -/** - * @enum PresenceEvents - * @description events that the server listens to in the presence module - * @property JOINED_ROOM - event to indicate a user has joined a room - * @property LEAVE - event to indicate a user has left a room - * @property UPDATE - event to indicate a user has updated their presence - * @property ERROR - event to indicate an error in the presence module - */ -export enum PresenceEvents { - JOINED_ROOM = 'presence.joined-room', - LEAVE = 'presence.leave', - ERROR = 'presence.error', - UPDATE = 'presence.update', -} - -export type PresenceEventsArg = PresenceEvents | `${PresenceEvents}`; - -/** - * @enum InternalPresenceEvents - * @description events that the server listens to in the presence module - * @property GET - event to get the presence list - */ -export enum InternalPresenceEvents { - GET = 'presence.get', -} diff --git a/src/lib/socket/common/types/presence.types.ts b/src/lib/socket/common/types/presence.types.ts deleted file mode 100644 index 2b600164..00000000 --- a/src/lib/socket/common/types/presence.types.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { z } from 'zod'; - -export const PresenceSchema = z.object({ - id: z.string(), - name: z.string(), -}); - -export type Presence = z.infer; diff --git a/src/lib/socket/connection/index.ts b/src/lib/socket/connection/index.ts deleted file mode 100644 index 9affaa34..00000000 --- a/src/lib/socket/connection/index.ts +++ /dev/null @@ -1,133 +0,0 @@ -import { Subject } from 'rxjs'; -import type { Socket } from 'socket.io-client'; - -import { ErrorCallback } from '../common/types/callbacks.types'; - -import { ClientState, ConnectionState, SocketErrorEvent, SocketEvent } from './types'; -import { Logger } from '../../../common/utils'; - -export class ClientConnection { - private logger: Logger; - private stateObserver: Subject; - - public state: ClientState; - - constructor(private socket: Socket) { - this.logger = new Logger('@superviz/sdk/socket-client/connection'); - this.subscribeToManagerEvents(); - this.stateObserver = new Subject(); - } - - public on(next: (state: ConnectionState) => void, error?: ErrorCallback) { - if (this.stateObserver.closed) { - this.stateObserver = new Subject(); - } - - this.stateObserver.subscribe({ - next, - error, - }); - } - - public off() { - if (this.stateObserver.closed) return; - - this.stateObserver.unsubscribe(); - } - - /** - * @function subscribeToManagerEvents - * @description Subscribe to the manager events - * @returns {void} - */ - private subscribeToManagerEvents(): void { - this.socket.on('connect', this.onConnect); - this.socket.on('disconnect', this.onDisconnect); - this.socket.on('connect_error', this.onConnectError); - this.socket.io.on('error', this.onConnectionError); - this.socket.io.on('reconnect', this.onReconnect); - this.socket.io.on('reconnect_attempt', this.onReconnecAttempt); - this.socket.io.on('reconnect_error', this.onReconnectError); - this.socket.io.on('reconnect_failed', this.onReconnectFailed); - - // custom validations listener - this.socket.on(SocketEvent.ERROR, this.onCustomError); - } - - /** - * @function changeState - * @description Change the state of the connection - * @returns {void} - */ - private changeState(state: ClientState, reason?: string): void { - this.state = state; - - if (this.stateObserver.closed) return; - - this.stateObserver.next({ - state, - reason, - }); - } - - /** Manager events handlers */ - - private onConnect = () => { - this.logger.log('connection @ on connect', 'Connected to the socket'); - this.changeState(ClientState.CONNECTED); - }; - - private onDisconnect = (reason: Socket.DisconnectReason) => { - this.logger.log('connection @ on disconnect', 'Disconnected from the socket'); - this.changeState(ClientState.DISCONNECTED, reason); - }; - - private onConnectError = (error: Error) => { - this.logger.log('connection @ on connect error', 'Connection error', error); - this.changeState(ClientState.CONNECTION_ERROR, error.message); - }; - - private onConnectionError = (error: Error) => { - this.logger.log('connection @ on connection error', 'Connection error', error); - this.changeState(ClientState.CONNECTION_ERROR, error.message); - }; - - private onReconnect = () => { - this.logger.log('connection @ on reconnect', 'Reconnected to the socket'); - this.changeState(ClientState.CONNECTED); - }; - - private onReconnectError = (error: Error) => { - this.logger.log('connection @ on reconnect error', 'Reconnect error', error); - this.changeState(ClientState.RECONNECT_ERROR, error.message); - }; - - private onReconnectFailed = () => { - this.logger.log('connection @ on reconnect failed', 'Failed to reconnect to the socket'); - this.changeState(ClientState.RECONNECT_ERROR); - }; - - private onReconnecAttempt = (attempt: number) => { - this.logger.log('connection @ on reconnect attempt', `Reconnect attempt #${attempt}`); - this.changeState(ClientState.RECONNECTING, `Reconnect attempt #${attempt}`); - }; - - private onCustomError = (error: SocketErrorEvent) => { - if (error.needsToDisconnect) { - this.socket.disconnect(); - this.changeState(ClientState.DISCONNECTED, error.errorType); - } - - const logMessage = `[SuperViz] - - Error: ${error.errorType} - - Message: ${error.message} - `; - - if (error.level === 'error') { - console.error(logMessage); - return; - } - - console.warn(logMessage); - }; -} diff --git a/src/lib/socket/connection/types.ts b/src/lib/socket/connection/types.ts deleted file mode 100644 index 1fdde25e..00000000 --- a/src/lib/socket/connection/types.ts +++ /dev/null @@ -1,45 +0,0 @@ -/** - * @enum ClientState - * @description the state of the client - * @property CONNECTED - the client is connected - * @property CONNECTING - the client is connecting - * @property DISCONNECTED - the client is disconnected - * @property CONNECTION_ERROR - the client has a connection error - * @property RECONNECTING - the client is reconnecting - * @property RECONNECT_ERROR - the client has a reconnect error - */ -export enum ClientState { - CONNECTED = 'CONNECTED', - CONNECTING = 'CONNECTING', - DISCONNECTED = 'DISCONNECTED', - CONNECTION_ERROR = 'CONNECTION_ERROR', - RECONNECTING = 'RECONNECTING', - RECONNECT_ERROR = 'RECONNECT_ERROR', -} - -/** - * @interface ConnectionState - * @description the state of the connection - * @property state - the state of the connection - * @property reason - the reason for the state change - */ -export interface ConnectionState { - state: ClientState; - reason?: string; -} - -export type SocketErrorEvent = { - errorType: - | 'message-size-limit' - | 'rate-limit' - | 'room-connections-limit' - | 'user-already-in-room'; - message: string; - connectionId: string; - needsToDisconnect: boolean; - level: 'error' | 'warn'; -}; - -export enum SocketEvent { - ERROR = 'socket-event.error', -} diff --git a/src/lib/socket/index.ts b/src/lib/socket/index.ts deleted file mode 100644 index 6a37879e..00000000 --- a/src/lib/socket/index.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { PresenceEvents, RoomEvents } from './common/types/event.types'; -import { ClientState, ConnectionState } from './connection/types'; -import { PresenceRoom } from './presence'; -import { PresenceCallback, PresenceEvent } from './presence/types'; -import { Realtime } from './realtime'; -import { Room } from './room'; -import { Callback, SocketEvent, JoinRoomPayload, RoomHistory } from './room/types'; - -export { - Realtime, - PresenceEvents, - RoomEvents, - ClientState, - ConnectionState, - Callback, - SocketEvent, - JoinRoomPayload, - RoomHistory, - PresenceCallback, - PresenceEvent, - Room, - PresenceRoom, -}; diff --git a/src/lib/socket/presence/index.ts b/src/lib/socket/presence/index.ts deleted file mode 100644 index d82fc66f..00000000 --- a/src/lib/socket/presence/index.ts +++ /dev/null @@ -1,202 +0,0 @@ -import { Subject } from 'rxjs'; -import type { Socket } from 'socket.io-client'; - -import { ErrorCallback } from '../common/types/callbacks.types'; -import { - InternalPresenceEvents, - PresenceEvents, - PresenceEventsArg, -} from '../common/types/event.types'; -import type { Presence } from '../common/types/presence.types'; - -import { PresenceCallback, PresenceEvent, PresenceEventFromServer } from './types'; -import { Logger } from '../../../common/utils'; - -export class PresenceRoom { - private logger: Logger; - private presences: Set = new Set(); - private observers: Map> = new Map(); - - constructor(private io: Socket, private presence: Presence, private roomId: string) { - this.logger = new Logger('@superviz/sdk/socket-client/presence'); - - this.registerSubjects(); - this.subscribeToPresenceEvents(); - } - - public static register(io: Socket, presence: Presence, roomId: string) { - return new PresenceRoom(io, presence, roomId); - } - - /** - * @function get - * @description Get the presences in the room - * @returns {void} - */ - public get(next: (data: PresenceEvent[]) => void, error?: ErrorCallback): void { - const subject = new Subject(); - - subject.subscribe({ - next, - error, - }); - - const callback = (event: { - presences: PresenceEventFromServer[]; - connectionId: string; - timestamp: number; - roomId: string; - }) => { - const presences = event.presences.map((presence) => ({ - connectionId: presence.connectionId, - data: presence.data, - id: presence.id, - name: presence.name, - timestamp: presence.timestamp, - })); - - this.logger.log('presence room @ get', event); - this.io.off(InternalPresenceEvents.GET, callback); - subject.next(presences); - subject.complete(); - }; - - this.io.on(InternalPresenceEvents.GET, callback); - this.io.emit(InternalPresenceEvents.GET, this.roomId); - } - - /** - * @function update - * @description update the presence data in the room - * @param payload - The data to update - * @returns {void} - */ - public update(payload: T): void { - const body: PresenceEvent = { - connectionId: this.io.id, - data: payload, - id: this.presence.id, - name: this.presence.name, - timestamp: Date.now(), - }; - - this.io.emit(PresenceEvents.UPDATE, this.roomId, body); - this.logger.log('presence room @ update', this.roomId, body); - } - - public destroy(): void { - this.io.off(PresenceEvents.JOINED_ROOM, this.onPresenceJoin); - this.io.off(PresenceEvents.LEAVE, this.onPresenceLeave); - this.io.off(PresenceEvents.UPDATE, this.onPresenceUpdate); - - this.observers.forEach((observer) => observer.unsubscribe()); - this.observers.clear(); - } - - /** - * @function registerSubjects - * @description Register the subjects for the presence events - * @returns {void} - */ - private registerSubjects(): void { - this.observers.set(PresenceEvents.JOINED_ROOM, new Subject()); - this.observers.set(PresenceEvents.LEAVE, new Subject()); - this.observers.set(PresenceEvents.UPDATE, new Subject()); - } - - /** - * @function on - * @description Listen to an event - * @param event - The event to listen to - * @param callback - The callback to execute when the event is emitted - * @returns {void} - */ - public on( - event: PresenceEventsArg, - callback: PresenceCallback, - error?: ErrorCallback, - ): void { - this.observers.get(event).subscribe({ - error, - next: callback, - }); - } - - /** - * @function off - * @description Stop listening to an event - * @param event - The event to stop listening to - * @param callback - The callback to remove from the event - * @returns {void} - */ - public off(event: PresenceEventsArg): void { - this.observers.get(event).unsubscribe(); - this.observers.delete(event); - this.observers.set(event, new Subject()); - } - - /** - * @function subscribeToPresenceEvents - * @description Subscribe to the presence events - * @returns {void} - */ - private subscribeToPresenceEvents(): void { - this.io.on(PresenceEvents.JOINED_ROOM, this.onPresenceJoin); - this.io.on(PresenceEvents.LEAVE, this.onPresenceLeave); - this.io.on(PresenceEvents.UPDATE, this.onPresenceUpdate); - } - - /** - * @function onPresenceJoin - * @description Handle the presence join event - * @param event - The presence event - * @returns {void} - */ - private onPresenceJoin = (event: PresenceEventFromServer): void => { - if (event?.roomId !== this.roomId) return; - - this.logger.log('presence room @ presence join', event); - this.presences.add(event); - this.observers.get(PresenceEvents.JOINED_ROOM).next({ - connectionId: event.connectionId, - data: event.data, - id: event.id, - name: event.name, - timestamp: event.timestamp, - }); - }; - - /** - * @function onPresenceLeave - * @description Handle the presence leave event - * @param event - The presence event - * @returns {void} - */ - private onPresenceLeave = (event: PresenceEventFromServer): void => { - if (event?.roomId !== this.roomId) return; - - this.logger.log('presence room @ presence leave', event); - this.presences.delete(event); - this.observers.get(PresenceEvents.LEAVE).next(event); - }; - - /** - * @function onPresenceUpdate - * @description Handle the presence update event - * @param event - The presence event - * @returns {void} - */ - private onPresenceUpdate = (event: PresenceEventFromServer): void => { - if (event?.roomId !== this.roomId) return; - - this.logger.log('presence room @ presence update', event); - this.observers.get(PresenceEvents.UPDATE).next({ - connectionId: event.connectionId, - data: event.data, - id: event.id, - name: event.name, - roomId: event?.roomId, - timestamp: event.timestamp, - } as PresenceEvent); - }; -} diff --git a/src/lib/socket/presence/types.ts b/src/lib/socket/presence/types.ts deleted file mode 100644 index 89b986e3..00000000 --- a/src/lib/socket/presence/types.ts +++ /dev/null @@ -1,14 +0,0 @@ -export type PresenceEvent = { - id: string; - name: string; - connectionId: string; - data: T; - timestamp: number; -}; - -export interface PresenceEventFromServer extends PresenceEvent { - roomKey: string; - roomId: string; -} - -export type PresenceCallback = (event: PresenceEvent) => void; diff --git a/src/lib/socket/realtime/index.ts b/src/lib/socket/realtime/index.ts deleted file mode 100644 index f45d54de..00000000 --- a/src/lib/socket/realtime/index.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { type Socket, Manager } from 'socket.io-client'; - -import { Presence } from '../common/types/presence.types'; -import { ClientConnection } from '../connection'; -import { ClientState } from '../connection/types'; -import { Room } from '../room'; - -export class Realtime { - private socket: Socket; - private manager: Manager; - public connection: ClientConnection; - - constructor( - private apiKey: string, - private environment: 'dev' | 'prod', - private presence: Presence, - ) { - this.manager = new Manager('https://io.superviz.com', { - addTrailingSlash: false, - secure: true, - withCredentials: true, - reconnection: true, - reconnectionDelay: 1000, - reconnectionDelayMax: 5000, - reconnectionAttempts: 5, - extraHeaders: { - 'sv-api-key': this.apiKey, - }, - }); - - // use the default namespace to handle the connections - const { origin } = window.location; - this.socket = this.manager.socket(`/${environment}`, { - auth: { - apiKey: this.apiKey, - origin, - envirioment: this.environment, - }, - }); - - this.connection = new ClientConnection(this.socket); - } - - public get state(): ClientState { - return this.connection.state; - } - - /** - * @function connect - * @param room - The room name - * @param maxConnections - The maximum number of connections allowed in the room - * @returns {Room} - The room instance - */ - public connect(room: string, maxConnections?: number | 'unlimited'): Room { - return Room.register(this.socket, this.presence, room, this.apiKey, maxConnections); - } - - public destroy() { - this.socket.disconnect(); - this.connection.off(); - } -} diff --git a/src/lib/socket/room/index.ts b/src/lib/socket/room/index.ts deleted file mode 100644 index 7c205a02..00000000 --- a/src/lib/socket/room/index.ts +++ /dev/null @@ -1,208 +0,0 @@ -import { Subject, Subscription } from 'rxjs'; -import type { Socket } from 'socket.io-client'; - -import { ErrorCallback } from '../common/types/callbacks.types'; -import { InternalRoomEvents, RoomEvents } from '../common/types/event.types'; -import { Presence } from '../common/types/presence.types'; -import { PresenceRoom } from '../presence'; - -import { Callback, SocketEvent, JoinRoomPayload, RoomHistory } from './types'; -import { Logger } from '../../../common/utils'; - -export class Room { - private logger: Logger; - private isJoined: boolean = false; - private subscriptions: Map, Subscription> = new Map(); - private observers: Map> = new Map(); - - public presence: PresenceRoom; - - constructor( - private io: Socket, - private user: Presence, - private roomId: string, - private apiKey: string, - private maxConnections: number | 'unlimited' = 100, - ) { - this.logger = new Logger('@superviz/sdk/socket-client/room'); - - const payload: JoinRoomPayload = { - name: roomId, - user, - maxConnections, - }; - - this.presence = PresenceRoom.register(io, user, roomId); - this.io.emit(RoomEvents.JOIN_ROOM, payload); - this.subscribeToRoomEvents(); - } - - public static register( - io: Socket, - presence: Presence, - roomId: string, - apiKey: string, - maxConnections: number | 'unlimited', - ): Room { - return new Room(io, presence, roomId, apiKey, maxConnections); - } - - /** - * @function on - * @description Listen to an event - * @param event - The event to listen to - * @param callback - The callback to execute when the event is emitted - * @returns {void} - */ - public on(event: string, callback: Callback): void { - this.logger.log('room @ on', event); - - let subject = this.observers.get(event); - - if (!subject) { - subject = new Subject(); - this.observers.set(event, subject); - - this.io.on(event, (data: SocketEvent) => { - this.publishEventToClient(event, data); - }); - } - - this.subscriptions.set(callback, subject.subscribe(callback)); - } - - /** - * @function off - * @description Stop listening to an event - * @param event - The event to stop listening to - * @param callback - The callback to remove from the event - * @returns {void} - */ - public off(event: string, callback?: Callback): void { - this.logger.log('room @ off', event); - - if (!callback) { - this.observers.delete(event); - this.io.off(event); - return; - } - - this.subscriptions.get(callback)?.unsubscribe(); - } - - /** - * @function emit - * @description Emit an event - * @param event - The event to emit - * @param payload - The payload to send with the event - * @returns {void} - */ - public emit(event: string, payload: T): void { - if (!this.isJoined) { - this.logger.log('Cannot emit event. Not joined to room'); - return; - } - - const body: SocketEvent = { - name: event, - roomId: this.roomId, - presence: this.user, - connectionId: this.io.id, - data: payload, - timestamp: Date.now(), - }; - - this.io.emit(RoomEvents.UPDATE, this.roomId, body); - this.logger.log('room @ emit', event, payload); - } - - /** - * @function get - * @description Get the presences in the room - * @returns {void} - */ - public history(next: (data: RoomHistory) => void, error?: ErrorCallback): void { - const subject = new Subject(); - - subject.subscribe({ - next, - error, - }); - - const callback = (event: RoomHistory) => { - this.logger.log('room @ history', event); - this.io.off(InternalRoomEvents.GET, callback); - subject.next(event); - subject.complete(); - subject.unsubscribe(); - }; - - this.io.on(InternalRoomEvents.GET, callback); - this.io.emit(InternalRoomEvents.GET, this.roomId); - } - - /** - * @function disconnect - * @description Disconnect from the room - * @returns {void} - */ - public disconnect(): void { - this.logger.log('room @ disconnect', 'Leaving room...', this.roomId); - this.io.emit(RoomEvents.LEAVE_ROOM, this.roomId); - - // unsubscribe from all events - this.subscriptions.forEach((subscription) => subscription.unsubscribe()); - this.subscriptions.clear(); - this.observers.forEach((subject) => subject.unsubscribe()); - this.observers.clear(); - - this.presence.destroy(); - } - - /** - * @function publishEventToClient - * @description Publish an event to the client - * @param event - The event to publish - * @param data - The data to publish - * @returns {void} - */ - private publishEventToClient(event: string, data: SocketEvent): void { - const subject = this.observers.get(event); - - if (!subject || data.roomId !== this.roomId) return; - - subject.next(data); - } - - /* - * @function subscribeToRoomEvents - * @description Subscribe to room events - * @returns {void} - */ - private subscribeToRoomEvents(): void { - this.io.on(RoomEvents.JOINED_ROOM, this.onJoinedRoom); - this.io.on(RoomEvents.ERROR, (event: SocketEvent) => { - this.logger.log('Error:', event.data); - }); - - this.io.on(`http:${this.roomId}:${this.apiKey}`, this.onHttpEvent); - } - - /** - * @function onJoinedRoom - * @description handles the event when a user joins a room. - * @param event The socket event containing presence data. - * @returns {void} - */ - private onJoinedRoom = (event: SocketEvent<{ name: string }>): void => { - if (this.roomId !== event?.data?.name) return; - - this.isJoined = true; - this.io.emit(RoomEvents.JOINED_ROOM, this.roomId, event.data); - this.logger.log('room @ joined', event); - }; - - private onHttpEvent = (event: SocketEvent) => { - this.publishEventToClient(event.name, event); - }; -} diff --git a/src/lib/socket/room/types.ts b/src/lib/socket/room/types.ts deleted file mode 100644 index e40f3c0b..00000000 --- a/src/lib/socket/room/types.ts +++ /dev/null @@ -1,39 +0,0 @@ -import z from 'zod'; - -import { PresenceSchema } from '../common/types/presence.types'; - -export type SocketEvent = { - name: string; - roomId: string; - connectionId: string; - presence?: z.infer; - data: T; - timestamp: number; -}; - -export type RoomHistory = { - roomId: string; - room: { - id: string; - name: string; - userId: string; - apiKey: string; - createdAt: Date; - }; - events: SocketEvent[]; - connectionId: string; - timestamp: Date; -}; - -export type Callback = (event: SocketEvent) => void; - -export const JoinRoomSchema = z.object({ - name: z.string(), - user: z.object({ - id: z.string(), - name: z.string(), - }), - maxConnections: z.union([z.number(), z.literal('unlimited'), z.undefined()]), -}); - -export type JoinRoomPayload = z.infer; diff --git a/src/services/io/index.ts b/src/services/io/index.ts index d85989d4..bbbc4697 100644 --- a/src/services/io/index.ts +++ b/src/services/io/index.ts @@ -1,4 +1,4 @@ -import * as Socket from '../../lib/socket'; +import * as Socket from '@superviz/socket-client'; import { Subject } from 'rxjs'; import { Participant } from '../../common/types/participant.types'; diff --git a/src/services/presence-3d-manager/index.test.ts b/src/services/presence-3d-manager/index.test.ts index 01fb912a..b3e987f5 100644 --- a/src/services/presence-3d-manager/index.test.ts +++ b/src/services/presence-3d-manager/index.test.ts @@ -1,4 +1,4 @@ -import { PresenceEvents } from '../../lib/socket'; +import { PresenceEvents } from '@superviz/socket-client'; import { MOCK_IO } from '../../../__mocks__/io.mock'; import { MOCK_LOCAL_PARTICIPANT } from '../../../__mocks__/participants.mock'; @@ -259,7 +259,7 @@ describe('Presence3DManager', () => { ...MOCK_LOCAL_PARTICIPANT, name: 'new name', id: undefined, - }; + } as unknown as Participant; presence3DManager['unthrottledUpdatePresence3D'](modifiedLocalParticipant); diff --git a/src/services/presence-3d-manager/index.ts b/src/services/presence-3d-manager/index.ts index bc687123..269e1d4b 100644 --- a/src/services/presence-3d-manager/index.ts +++ b/src/services/presence-3d-manager/index.ts @@ -1,4 +1,4 @@ -import { PresenceEvent, PresenceEvents, Room, SocketEvent } from '../../lib/socket'; +import { PresenceEvent, PresenceEvents, Room, SocketEvent } from '@superviz/socket-client'; import { throttle } from 'lodash'; import { Participant } from '../../common/types/participant.types'; diff --git a/src/services/room-state/index.test.ts b/src/services/room-state/index.test.ts index a3c064a6..f8492d08 100644 --- a/src/services/room-state/index.test.ts +++ b/src/services/room-state/index.test.ts @@ -1,6 +1,6 @@ import { TextEncoder, TextDecoder } from 'util'; -import { PresenceEvent, PresenceEvents } from '../../lib/socket'; +import { PresenceEvent, PresenceEvents } from '@superviz/socket-client'; import { MOCK_LOCAL_PARTICIPANT } from '../../../__mocks__/participants.mock'; import { TranscriptState } from '../../common/types/events.types'; diff --git a/src/services/room-state/index.ts b/src/services/room-state/index.ts index 5eb81a8b..a1c5cde7 100644 --- a/src/services/room-state/index.ts +++ b/src/services/room-state/index.ts @@ -1,4 +1,4 @@ -import { PresenceEvent, PresenceEvents, Room, SocketEvent } from '../../lib/socket'; +import { PresenceEvent, PresenceEvents, Room, SocketEvent } from '@superviz/socket-client'; import { TranscriptState } from '../../common/types/events.types'; import { ParticipantType, VideoParticipant } from '../../common/types/participant.types'; diff --git a/src/services/room-state/type.ts b/src/services/room-state/type.ts index 71d70aed..8dbc4ddf 100644 --- a/src/services/room-state/type.ts +++ b/src/services/room-state/type.ts @@ -1,4 +1,4 @@ -import { PresenceEvent } from '../../lib/socket'; +import { PresenceEvent } from '@superviz/socket-client'; import { TranscriptState } from '../../common/types/events.types'; import { Participant } from '../../common/types/participant.types'; diff --git a/src/services/slot/index.ts b/src/services/slot/index.ts index 7f592a0c..a178b4a6 100644 --- a/src/services/slot/index.ts +++ b/src/services/slot/index.ts @@ -1,4 +1,4 @@ -import * as Socket from '../../lib/socket'; +import * as Socket from '@superviz/socket-client'; import { NAME_IS_WHITE_TEXT, diff --git a/yarn.lock b/yarn.lock index bcdb9811..affa3bec 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2472,6 +2472,13 @@ unbzip2-stream "1.4.3" yargs "17.7.1" +"@reactivex/rxjs@^6.6.7": + version "6.6.7" + resolved "https://registry.yarnpkg.com/@reactivex/rxjs/-/rxjs-6.6.7.tgz#52ab48f989aba9cda2b995acc904a43e6e1b3b40" + integrity sha512-xZIV2JgHhWoVPm3uVcFbZDRVJfx2hgqmuTX7J4MuKaZ+j5jN29agniCPBwrlCmpA15/zLKcPi7/bogt0ZwOFyA== + dependencies: + tslib "^1.9.0" + "@rollup/plugin-node-resolve@^15.0.1": version "15.1.0" resolved "https://registry.yarnpkg.com/@rollup/plugin-node-resolve/-/plugin-node-resolve-15.1.0.tgz#9ffcd8e8c457080dba89bb9fcb583a6778dc757e" @@ -2659,6 +2666,19 @@ resolved "https://registry.yarnpkg.com/@socket.io/component-emitter/-/component-emitter-3.1.2.tgz#821f8442f4175d8f0467b9daf26e3a18e2d02af2" integrity sha512-9BCxFwvbGg/RsZK9tjXd8s4UcwR0MWeFQ1XEKIQVVvAGJyINdrqKMcTRyLoK8Rse1GjzLV9cwjWV1olXRWEXVA== +"@superviz/socket-client@^1.10.0": + version "1.10.0" + resolved "https://registry.yarnpkg.com/@superviz/socket-client/-/socket-client-1.10.0.tgz#98773e2a30ecdea6971cbca4f89ba3cf8a14ec18" + integrity sha512-7cbeFynVdILFiEV4UE7XyPrVHgRNnlNwtc+13YPPpn1JMufO7D9AwUHU6cUd9FOZAy3nCQG4BW9GFpGSMmDtWA== + dependencies: + "@reactivex/rxjs" "^6.6.7" + debug "^4.3.5" + lodash "^4.17.21" + rxjs "^7.8.1" + semantic-release-version-file "^1.0.2" + socket.io-client "^4.7.5" + zod "^3.23.8" + "@tootallnate/once@2": version "2.0.0" resolved "https://registry.npmjs.org/@tootallnate/once/-/once-2.0.0.tgz" @@ -4763,6 +4783,13 @@ debug@^4.3.1, debug@~4.3.1, debug@~4.3.2: dependencies: ms "2.1.2" +debug@^4.3.5: + version "4.3.6" + resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.6.tgz#2ab2c38fbaffebf8aa95fdfe6d88438c7a13c52b" + integrity sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg== + dependencies: + ms "2.1.2" + decimal.js@^10.4.2: version "10.4.3" resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.4.3.tgz#1044092884d245d1b7f65725fa4ad4c6f781cc23" @@ -10578,6 +10605,11 @@ tsconfig-paths@^3.15.0: minimist "^1.2.6" strip-bom "^3.0.0" +tslib@^1.9.0: + version "1.14.1" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00" + integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg== + tslib@^2.0.1, tslib@^2.4.0: version "2.6.1" resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.1.tgz#fd8c9a0ff42590b25703c0acb3de3d3f4ede0410"