diff --git a/packages/room/event/event.js b/packages/room/event/event.js index 902ad07e..e1626157 100644 --- a/packages/room/event/event.js +++ b/packages/room/event/event.js @@ -51,6 +51,48 @@ export const createEvent = () => { this._events[eventName].add(callback) } + + /** + * @param {string} eventName - The name of the event + * @param {(data: any) => void} callback - A callback function + */ + addEventListener = (eventName, callback) => { + if (typeof eventName !== 'string' || eventName.trim().length === 0) { + throw new Error('Valid string for event name is required') + } + + if (typeof callback !== 'function') { + throw new TypeError('Valid callback function is required') + } + + const event = this._events[eventName] + + if (!(event instanceof Set)) { + this._events[eventName] = new Set() + } + + this._events[eventName].add(callback) + } + + /** + * @param {string} eventName - The name of the event + * @param {(data: any) => void} callback - A callback function + */ + removeEventListener = (eventName, callback) => { + if (typeof eventName !== 'string' || eventName.trim().length === 0) { + throw new Error('Valid string for event name is required') + } + + if (typeof callback !== 'function') { + throw new TypeError('Valid callback function is required') + } + + const event = this._events[eventName] + + if (event instanceof Set) { + event.delete(callback) + } + } } return { @@ -60,6 +102,8 @@ export const createEvent = () => { return { emit: event.emit, on: event.on, + addEventListener: event.addEventListener, + removeEventListener: event.removeEventListener, } }, } diff --git a/packages/room/index.js b/packages/room/index.js index 2c70c363..d433f207 100644 --- a/packages/room/index.js +++ b/packages/room/index.js @@ -62,6 +62,8 @@ export const Room = (userConfig = config) => { createDataChannel: api.createDataChannel, setAuth: api.setAuth, on: event.on, + addEventListener: event.addEventListener, + removeEventListener: event.removeEventListener, leaveRoom: api.leaveRoom, endRoom: api.endRoom, } diff --git a/packages/room/peer/peer.js b/packages/room/peer/peer.js index 1d42c2eb..e01d0d29 100644 --- a/packages/room/peer/peer.js +++ b/packages/room/peer/peer.js @@ -227,7 +227,7 @@ export const createPeer = ({ api, createStream, event, streams, config }) => { const videoTrack = localStream.mediaStream.getVideoTracks()[0] if (!newTrack && !videoTrack) { - throw new Error('Cannot find any video track which can be processed') + return } if (newTrack) { @@ -278,7 +278,7 @@ export const createPeer = ({ api, createStream, event, streams, config }) => { const audioTrack = localStream.mediaStream.getAudioTracks()[0] if (!newTrack && !audioTrack) { - throw new Error('Cannot find any audio track which can be processed') + return } if (newTrack) { @@ -329,7 +329,7 @@ export const createPeer = ({ api, createStream, event, streams, config }) => { const videoTrack = localStream?.mediaStream.getVideoTracks()[0] if (!videoTrack || videoTrack.readyState === 'ended') { - throw new Error(`No running video track available to be processed.`) + return } if (stop) { @@ -358,7 +358,7 @@ export const createPeer = ({ api, createStream, event, streams, config }) => { const audioTrack = localStream?.mediaStream.getAudioTracks()[0] if (!audioTrack || audioTrack.readyState === 'ended') { - throw new Error(`No running audio track available to be processed.`) + return } if (stop) {