From 34fab8aa47b67585479a8438c5724f54cb75325c Mon Sep 17 00:00:00 2001 From: Miron Pawlik Date: Mon, 17 Jun 2024 10:32:08 +0200 Subject: [PATCH] Extract types --- src/Client.ts | 276 +------------------------------------------ src/Client.types.ts | 280 ++++++++++++++++++++++++++++++++++++++++++++ src/create.tsx | 2 +- src/index.ts | 2 +- 4 files changed, 285 insertions(+), 275 deletions(-) create mode 100644 src/Client.types.ts diff --git a/src/Client.ts b/src/Client.ts index e86e2fc..ec6842e 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -1,13 +1,8 @@ import EventEmitter from "events"; import type TypedEmitter from "typed-emitter"; import type { - AuthErrorReason, BandwidthLimit, - Component, ConnectConfig, - CreateConfig, - MessageEvents, - Peer, SimulcastConfig, TrackBandwidthLimit, TrackContext, @@ -15,277 +10,12 @@ import type { } from "@fishjam-dev/ts-client"; import { FishjamClient } from "@fishjam-dev/ts-client"; import type { PeerId, PeerState, PeerStatus, Track, TrackId, TrackWithOrigin } from "./state.types"; -import type { DeviceManagerEvents } from "./DeviceManager"; import { DeviceManager } from "./DeviceManager"; -import type { MediaDeviceType, ScreenShareManagerConfig } from "./ScreenShareManager"; +import type { ScreenShareManagerConfig } from "./ScreenShareManager"; import { ScreenShareManager } from "./ScreenShareManager"; -import type { DeviceManagerConfig, DeviceManagerInitConfig, Devices, DeviceState, MediaState } from "./types"; - -export type ClientApi = { - local: PeerState | null; - - peers: Record>; - peersTracks: Record>; - - components: Record>; - componentsTracks: Record>; - - bandwidthEstimation: bigint; - status: PeerStatus; - media: MediaState | null; - devices: Devices; - deviceManager: DeviceManager; - screenShareManager: ScreenShareManager; -}; - -export interface ClientEvents { - /** - * Emitted when the websocket connection is closed - * - * @param {CloseEvent} event - Close event object from the websocket - */ - socketClose: (event: CloseEvent, client: ClientApi) => void; - - /** - * Emitted when occurs an error in the websocket connection - * - * @param {Event} event - Event object from the websocket - */ - socketError: (event: Event, client: ClientApi) => void; - - /** - * Emitted when the websocket connection is opened - * - * @param {Event} event - Event object from the websocket - */ - socketOpen: (event: Event, client: ClientApi) => void; - - /** Emitted when authentication is successful */ - authSuccess: (client: ClientApi) => void; - - /** Emitted when authentication fails */ - authError: (reason: AuthErrorReason, client: ClientApi) => void; - - /** Emitted when the connection is closed */ - disconnected: (client: ClientApi) => void; - - /** - * Called when peer was accepted. - */ - joined: ( - event: { - peerId: string; - peers: Peer[]; - components: Component[]; - }, - client: ClientApi, - ) => void; - - /** - * Called when peer was not accepted - * @param metadata - Pass through for client application to communicate further actions to frontend - */ - joinError: (metadata: any, client: ClientApi) => void; // eslint-disable-line @typescript-eslint/no-explicit-any - - /** - * Called when data in a new track arrives. - * - * This callback is always called after {@link MessageEvents.trackAdded}. - * It informs user that data related to the given track arrives and can be played or displayed. - */ - trackReady: (ctx: TrackContext, client: ClientApi) => void; - - /** - * Called each time the peer which was already in the room, adds new track. Fields track and stream will be set to null. - * These fields will be set to non-null value in {@link MessageEvents.trackReady} - */ - trackAdded: (ctx: TrackContext, client: ClientApi) => void; - - /** - * Called when some track will no longer be sent. - * - * It will also be called before {@link MessageEvents.peerLeft} for each track of this peer. - */ - trackRemoved: ( - ctx: TrackContext, - client: ClientApi, - ) => void; - - /** - * Called each time peer has its track metadata updated. - */ - trackUpdated: ( - ctx: TrackContext, - client: ClientApi, - ) => void; - - /** - * Called each time new peer joins the room. - */ - peerJoined: (peer: Peer, client: ClientApi) => void; - - /** - * Called each time peer leaves the room. - */ - peerLeft: (peer: Peer, client: ClientApi) => void; - - /** - * Called each time peer has its metadata updated. - */ - peerUpdated: (peer: Peer, client: ClientApi) => void; - - /** - * Called each time new Component is added to the room. - */ - componentAdded: ( - peer: Component, - client: ClientApi, - ) => void; - - /** - * Called each time Component is removed from the room. - */ - componentRemoved: ( - peer: Component, - client: ClientApi, - ) => void; - - /** - * Called each time Component has its metadata updated. - */ - componentUpdated: ( - peer: Component, - client: ClientApi, - ) => void; - - /** - * Called in case of errors related to multimedia session e.g. ICE connection. - */ - connectionError: (message: string, client: ClientApi) => void; - - /** - * Called every time the server estimates client's bandiwdth. - * - * @param {bigint} estimation - client's available incoming bitrate estimated - * by the server. It's measured in bits per second. - */ - bandwidthEstimationChanged: (estimation: bigint, client: ClientApi) => void; - - // track context events - encodingChanged: ( - context: TrackContext, - client: ClientApi, - ) => void; - - /** - * Emitted every time an update about voice activity is received from the server. - */ - voiceActivityChanged: ( - context: TrackContext, - client: ClientApi, - ) => void; - - // device manager events - managerStarted: ( - event: Parameters[0] & { - mediaDeviceType: MediaDeviceType; - }, - client: ClientApi, - ) => void; - managerInitialized: ( - event: { audio?: DeviceState; video?: DeviceState; mediaDeviceType: MediaDeviceType }, - client: ClientApi, - ) => void; - deviceReady: ( - event: Parameters[0] & { mediaDeviceType: MediaDeviceType }, - client: ClientApi, - ) => void; - devicesStarted: ( - event: Parameters[0] & { mediaDeviceType: MediaDeviceType }, - client: ClientApi, - ) => void; - devicesReady: ( - event: Parameters[0] & { mediaDeviceType: MediaDeviceType }, - client: ClientApi, - ) => void; - deviceStopped: ( - event: Parameters[0] & { mediaDeviceType: MediaDeviceType }, - client: ClientApi, - ) => void; - deviceEnabled: ( - event: Parameters[0] & { - mediaDeviceType: MediaDeviceType; - }, - client: ClientApi, - ) => void; - deviceDisabled: ( - event: Parameters[0] & { - mediaDeviceType: MediaDeviceType; - }, - client: ClientApi, - ) => void; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - error: (arg: any, client: ClientApi) => void; - - targetTrackEncodingRequested: ( - event: Parameters["targetTrackEncodingRequested"]>[0], - client: ClientApi, - ) => void; - localTrackAdded: ( - event: Parameters["localTrackAdded"]>[0], - client: ClientApi, - ) => void; - localTrackRemoved: ( - event: Parameters["localTrackRemoved"]>[0], - client: ClientApi, - ) => void; - localTrackReplaced: ( - event: Parameters["localTrackReplaced"]>[0], - client: ClientApi, - ) => void; - localTrackMuted: ( - event: Parameters["localTrackMuted"]>[0], - client: ClientApi, - ) => void; - localTrackUnmuted: ( - event: Parameters["localTrackUnmuted"]>[0], - client: ClientApi, - ) => void; - localTrackBandwidthSet: ( - event: Parameters["localTrackBandwidthSet"]>[0], - client: ClientApi, - ) => void; - localTrackEncodingBandwidthSet: ( - event: Parameters["localTrackEncodingBandwidthSet"]>[0], - client: ClientApi, - ) => void; - localTrackEncodingEnabled: ( - event: Parameters["localTrackEncodingEnabled"]>[0], - client: ClientApi, - ) => void; - localTrackEncodingDisabled: ( - event: Parameters["localTrackEncodingDisabled"]>[0], - client: ClientApi, - ) => void; - localEndpointMetadataChanged: ( - event: Parameters["localEndpointMetadataChanged"]>[0], - client: ClientApi, - ) => void; - localTrackMetadataChanged: ( - event: Parameters["localTrackMetadataChanged"]>[0], - client: ClientApi, - ) => void; - disconnectRequested: ( - event: Parameters["disconnectRequested"]>[0], - client: ClientApi, - ) => void; -} +import type { DeviceManagerConfig, DeviceManagerInitConfig, Devices, MediaState } from "./types"; -export type ReactClientCreteConfig = { - clientConfig?: CreateConfig; - deviceManagerDefaultConfig?: DeviceManagerConfig; - screenShareManagerDefaultConfig?: ScreenShareManagerConfig; -}; +import type { ClientEvents, ReactClientCreteConfig } from "./Client.types"; const NOOP = () => {}; diff --git a/src/Client.types.ts b/src/Client.types.ts new file mode 100644 index 0000000..70292df --- /dev/null +++ b/src/Client.types.ts @@ -0,0 +1,280 @@ +import type { + AuthErrorReason, + Component, + CreateConfig, + MessageEvents, + Peer, + TrackContext, +} from "@fishjam-dev/ts-client"; +import type { PeerId, PeerState, PeerStatus, TrackId, TrackWithOrigin } from "./state.types"; +import type { DeviceManagerEvents } from "./DeviceManager"; +import type { DeviceManager } from "./DeviceManager"; +import type { MediaDeviceType, ScreenShareManagerConfig } from "./ScreenShareManager"; +import type { ScreenShareManager } from "./ScreenShareManager"; +import type { DeviceManagerConfig, Devices, DeviceState, MediaState } from "./types"; + +export type ClientApi = { + local: PeerState | null; + + peers: Record>; + peersTracks: Record>; + + components: Record>; + componentsTracks: Record>; + + bandwidthEstimation: bigint; + status: PeerStatus; + media: MediaState | null; + devices: Devices; + deviceManager: DeviceManager; + screenShareManager: ScreenShareManager; +}; + +export interface ClientEvents { + /** + * Emitted when the websocket connection is closed + * + * @param {CloseEvent} event - Close event object from the websocket + */ + socketClose: (event: CloseEvent, client: ClientApi) => void; + + /** + * Emitted when occurs an error in the websocket connection + * + * @param {Event} event - Event object from the websocket + */ + socketError: (event: Event, client: ClientApi) => void; + + /** + * Emitted when the websocket connection is opened + * + * @param {Event} event - Event object from the websocket + */ + socketOpen: (event: Event, client: ClientApi) => void; + + /** Emitted when authentication is successful */ + authSuccess: (client: ClientApi) => void; + + /** Emitted when authentication fails */ + authError: (reason: AuthErrorReason, client: ClientApi) => void; + + /** Emitted when the connection is closed */ + disconnected: (client: ClientApi) => void; + + /** + * Called when peer was accepted. + */ + joined: ( + event: { + peerId: string; + peers: Peer[]; + components: Component[]; + }, + client: ClientApi, + ) => void; + + /** + * Called when peer was not accepted + * @param metadata - Pass through for client application to communicate further actions to frontend + */ + joinError: (metadata: any, client: ClientApi) => void; // eslint-disable-line @typescript-eslint/no-explicit-any + + /** + * Called when data in a new track arrives. + * + * This callback is always called after {@link MessageEvents.trackAdded}. + * It informs user that data related to the given track arrives and can be played or displayed. + */ + trackReady: (ctx: TrackContext, client: ClientApi) => void; + + /** + * Called each time the peer which was already in the room, adds new track. Fields track and stream will be set to null. + * These fields will be set to non-null value in {@link MessageEvents.trackReady} + */ + trackAdded: (ctx: TrackContext, client: ClientApi) => void; + + /** + * Called when some track will no longer be sent. + * + * It will also be called before {@link MessageEvents.peerLeft} for each track of this peer. + */ + trackRemoved: ( + ctx: TrackContext, + client: ClientApi, + ) => void; + + /** + * Called each time peer has its track metadata updated. + */ + trackUpdated: ( + ctx: TrackContext, + client: ClientApi, + ) => void; + + /** + * Called each time new peer joins the room. + */ + peerJoined: (peer: Peer, client: ClientApi) => void; + + /** + * Called each time peer leaves the room. + */ + peerLeft: (peer: Peer, client: ClientApi) => void; + + /** + * Called each time peer has its metadata updated. + */ + peerUpdated: (peer: Peer, client: ClientApi) => void; + + /** + * Called each time new Component is added to the room. + */ + componentAdded: ( + peer: Component, + client: ClientApi, + ) => void; + + /** + * Called each time Component is removed from the room. + */ + componentRemoved: ( + peer: Component, + client: ClientApi, + ) => void; + + /** + * Called each time Component has its metadata updated. + */ + componentUpdated: ( + peer: Component, + client: ClientApi, + ) => void; + + /** + * Called in case of errors related to multimedia session e.g. ICE connection. + */ + connectionError: (message: string, client: ClientApi) => void; + + /** + * Called every time the server estimates client's bandiwdth. + * + * @param {bigint} estimation - client's available incoming bitrate estimated + * by the server. It's measured in bits per second. + */ + bandwidthEstimationChanged: (estimation: bigint, client: ClientApi) => void; + + // track context events + encodingChanged: ( + context: TrackContext, + client: ClientApi, + ) => void; + + /** + * Emitted every time an update about voice activity is received from the server. + */ + voiceActivityChanged: ( + context: TrackContext, + client: ClientApi, + ) => void; + + // device manager events + managerStarted: ( + event: Parameters[0] & { + mediaDeviceType: MediaDeviceType; + }, + client: ClientApi, + ) => void; + managerInitialized: ( + event: { audio?: DeviceState; video?: DeviceState; mediaDeviceType: MediaDeviceType }, + client: ClientApi, + ) => void; + deviceReady: ( + event: Parameters[0] & { mediaDeviceType: MediaDeviceType }, + client: ClientApi, + ) => void; + devicesStarted: ( + event: Parameters[0] & { mediaDeviceType: MediaDeviceType }, + client: ClientApi, + ) => void; + devicesReady: ( + event: Parameters[0] & { mediaDeviceType: MediaDeviceType }, + client: ClientApi, + ) => void; + deviceStopped: ( + event: Parameters[0] & { mediaDeviceType: MediaDeviceType }, + client: ClientApi, + ) => void; + deviceEnabled: ( + event: Parameters[0] & { + mediaDeviceType: MediaDeviceType; + }, + client: ClientApi, + ) => void; + deviceDisabled: ( + event: Parameters[0] & { + mediaDeviceType: MediaDeviceType; + }, + client: ClientApi, + ) => void; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + error: (arg: any, client: ClientApi) => void; + + targetTrackEncodingRequested: ( + event: Parameters["targetTrackEncodingRequested"]>[0], + client: ClientApi, + ) => void; + localTrackAdded: ( + event: Parameters["localTrackAdded"]>[0], + client: ClientApi, + ) => void; + localTrackRemoved: ( + event: Parameters["localTrackRemoved"]>[0], + client: ClientApi, + ) => void; + localTrackReplaced: ( + event: Parameters["localTrackReplaced"]>[0], + client: ClientApi, + ) => void; + localTrackMuted: ( + event: Parameters["localTrackMuted"]>[0], + client: ClientApi, + ) => void; + localTrackUnmuted: ( + event: Parameters["localTrackUnmuted"]>[0], + client: ClientApi, + ) => void; + localTrackBandwidthSet: ( + event: Parameters["localTrackBandwidthSet"]>[0], + client: ClientApi, + ) => void; + localTrackEncodingBandwidthSet: ( + event: Parameters["localTrackEncodingBandwidthSet"]>[0], + client: ClientApi, + ) => void; + localTrackEncodingEnabled: ( + event: Parameters["localTrackEncodingEnabled"]>[0], + client: ClientApi, + ) => void; + localTrackEncodingDisabled: ( + event: Parameters["localTrackEncodingDisabled"]>[0], + client: ClientApi, + ) => void; + localEndpointMetadataChanged: ( + event: Parameters["localEndpointMetadataChanged"]>[0], + client: ClientApi, + ) => void; + localTrackMetadataChanged: ( + event: Parameters["localTrackMetadataChanged"]>[0], + client: ClientApi, + ) => void; + disconnectRequested: ( + event: Parameters["disconnectRequested"]>[0], + client: ClientApi, + ) => void; +} + +export type ReactClientCreteConfig = { + clientConfig?: CreateConfig; + deviceManagerDefaultConfig?: DeviceManagerConfig; + screenShareManagerDefaultConfig?: ScreenShareManagerConfig; +}; diff --git a/src/create.tsx b/src/create.tsx index 0748b22..b9ce221 100644 --- a/src/create.tsx +++ b/src/create.tsx @@ -12,7 +12,7 @@ import type { UseSetupMediaConfig, UseSetupMediaResult, } from "./types"; -import type { ClientApi, ClientEvents } from "./Client"; +import type { ClientApi, ClientEvents } from "./Client.types"; import { Client } from "./Client"; import type { MediaDeviceType, ScreenShareManagerConfig } from "./ScreenShareManager"; diff --git a/src/index.ts b/src/index.ts index bef4957..3df77e4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ export { create } from "./create"; export type { CreateFishjamClient, UseConnect } from "./create"; export { Client } from "./Client"; -export type { ClientEvents } from "./Client"; +export type { ClientEvents } from "./Client.types"; export type { PeerState,