Skip to content

Commit

Permalink
feat: add remove event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
tyohan committed Aug 28, 2024
1 parent f055f64 commit 611eb3e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
44 changes: 44 additions & 0 deletions packages/room/event/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -60,6 +102,8 @@ export const createEvent = () => {
return {
emit: event.emit,
on: event.on,
addEventListener: event.addEventListener,
removeEventListener: event.removeEventListener,
}
},
}
Expand Down
2 changes: 2 additions & 0 deletions packages/room/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
8 changes: 4 additions & 4 deletions packages/room/peer/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 611eb3e

Please sign in to comment.