Skip to content

Commit

Permalink
Missing constructors (#574)
Browse files Browse the repository at this point in the history
* add replacement for the Event constructor

* add early return when Audio is not defined

* add warn message

* add changeset
  • Loading branch information
framini authored Jun 21, 2022
1 parent eaed370 commit 4e35e0a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/chilled-seahorses-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@signalwire/js': patch
'@signalwire/webrtc': patch
---

Fix issue with missing constructors on react-native
5 changes: 5 additions & 0 deletions packages/js/src/features/mediaElements/mediaElementsSagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ export const makeAudioElementSaga = ({ speakerId }: { speakerId?: string }) => {
instance: room,
runSaga,
}: CustomSagaParams<RoomSessionConnection>): SagaIterator {
if (typeof Audio === 'undefined') {
getLogger().warn('`Audio` is not supported on this environment.')
return
}

try {
const audioEl = new Audio()
let audioTask: Task | undefined
Expand Down
21 changes: 20 additions & 1 deletion packages/webrtc/src/utils/webrtcHelpers.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,28 @@ export const stopStream = (stream: RNMediaStream) => {
stream = null
}

/**
* This class in implemented by `react-native-webrtc` but
* it's not exported directly. To avoid dealing with manual
* file imports and having (potential) issues of mixing
* commonjs/esm we ported it here since it's just a few
* lines of code.
*/
class MediaStreamTrackEvent {
type: string
track: any
constructor(type: string, eventInitDict: { track: any }) {
this.type = type.toString()
this.track = eventInitDict.track
}
}

export const stopTrack = (track: MediaStreamTrack) => {
if (track && track.readyState === 'live') {
track.stop()
track.dispatchEvent(new Event('ended'))
track.dispatchEvent(
// @ts-expect-error
new MediaStreamTrackEvent('ended', { track })
)
}
}

0 comments on commit 4e35e0a

Please sign in to comment.