init
This commit is contained in:
commit
ec53fcbe95
1905 changed files with 513762 additions and 0 deletions
21
hooks/useMailboxBridge.ts
Normal file
21
hooks/useMailboxBridge.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import { useCallback, useEffect, useMemo, useSyncExternalStore } from 'react'
|
||||
import { useMailbox } from '../context/mailbox.js'
|
||||
|
||||
type Props = {
|
||||
isLoading: boolean
|
||||
onSubmitMessage: (content: string) => boolean
|
||||
}
|
||||
|
||||
export function useMailboxBridge({ isLoading, onSubmitMessage }: Props): void {
|
||||
const mailbox = useMailbox()
|
||||
|
||||
const subscribe = useMemo(() => mailbox.subscribe.bind(mailbox), [mailbox])
|
||||
const getSnapshot = useCallback(() => mailbox.revision, [mailbox])
|
||||
const revision = useSyncExternalStore(subscribe, getSnapshot)
|
||||
|
||||
useEffect(() => {
|
||||
if (isLoading) return
|
||||
const msg = mailbox.poll()
|
||||
if (msg) onSubmitMessage(msg.content)
|
||||
}, [isLoading, revision, mailbox, onSubmitMessage])
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue