Skip to content

Commit

Permalink
Merge pull request #62 from cloudflare/rxjs
Browse files Browse the repository at this point in the history
Rxjs
  • Loading branch information
threepointone authored Jul 9, 2024
2 parents d2e0267 + a346e52 commit 3f6ca2f
Show file tree
Hide file tree
Showing 20 changed files with 1,236 additions and 172 deletions.
4 changes: 1 addition & 3 deletions app/components/AudioInputSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import type { FC } from 'react'
import { useAudioInputDeviceId } from '~/hooks/globalPersistedState'
import useMediaDevices from '~/hooks/useMediaDevices'
import { useRoomContext } from '~/hooks/useRoomContext'
import { errorMessageMap } from '~/hooks/useUserMedia'
import { Option, Select } from './Select'

export const AudioInputSelector: FC<{ id?: string }> = ({ id }) => {
const audioInputDevices = useMediaDevices((d) => d.kind === 'audioinput')
const [audioDeviceId, setAudioDeviceId] = useAudioInputDeviceId()

const {
userMedia: { audioUnavailableReason },
userMedia: { audioUnavailableReason, audioDeviceId, setAudioDeviceId },
} = useRoomContext()

if (audioUnavailableReason) {
Expand Down
4 changes: 1 addition & 3 deletions app/components/VideoInputSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import type { FC } from 'react'
import { useVideoInputDeviceId } from '~/hooks/globalPersistedState'
import useMediaDevices from '~/hooks/useMediaDevices'
import { useRoomContext } from '~/hooks/useRoomContext'
import { errorMessageMap } from '~/hooks/useUserMedia'
import { Option, Select } from './Select'

export const VideoInputSelector: FC<{ id?: string }> = ({ id }) => {
const [videoDeviceId, setVideoDeviceId] = useVideoInputDeviceId()
const videoInputDevices = useMediaDevices((d) => d.kind === 'videoinput')

const {
userMedia: { videoUnavailableReason },
userMedia: { videoUnavailableReason, videoDeviceId, setVideoDeviceId },
} = useRoomContext()

if (videoUnavailableReason) {
Expand Down
5 changes: 4 additions & 1 deletion app/entry.client.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { RemixBrowser } from '@remix-run/react'
import { startTransition } from 'react'
import { hydrateRoot } from 'react-dom/client'

hydrateRoot(document, <RemixBrowser />)
startTransition(() => {
hydrateRoot(document, <RemixBrowser />)
})
54 changes: 54 additions & 0 deletions app/hooks/rxjsHooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useEffect, useRef, useState } from 'react'
import { BehaviorSubject, Observable } from 'rxjs'

export function useSubscribedState<T>(observable: Observable<T>): T | undefined
export function useSubscribedState<T>(
observable: Observable<T>,
defaultValue: T
): T
export function useSubscribedState<T>(
observable: Observable<T>,
defaultValue?: T
): T {
const [state, setState] = useState(defaultValue)
useObservableEffect(observable, setState)
return state as any
}

export function useObservableEffect<T>(
observable: Observable<T>,
fn: (value: T) => void
) {
const fnRef = useRef(fn)
fnRef.current = fn
useEffect(() => {
const subscription = observable.subscribe((v) => fnRef.current(v))
return () => {
subscription.unsubscribe()
}
}, [observable])
}

/**
* Turns a value into a stable observable that will emit new
* values when the value changes, and completes upon unmounting.
*/
export function useStateObservable<T>(value: T) {
const ref = useRef(new BehaviorSubject(value))
const observableRef = useRef(ref.current.asObservable())
const previousValue = useRef<T>()
if (previousValue.current !== value) {
previousValue.current = value
ref.current.next(value)
}

useEffect(() => {
const { current } = ref
if (!current) return
return () => {
current.complete()
}
}, [])

return observableRef.current
}
9 changes: 9 additions & 0 deletions app/hooks/useIsServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useSyncExternalStore } from 'react'

export function useIsServer() {
return useSyncExternalStore(
() => () => {},
() => false,
() => true
)
}
Loading

0 comments on commit 3f6ca2f

Please sign in to comment.