[🐴] Do not init event bus if no session (#4193)
* Do not init event bus if no session * Be explicit * Simplify, fix logzio/stable
parent
17e0cb62a8
commit
3d1ed04a70
|
@ -2,7 +2,7 @@ import React from 'react'
|
||||||
import {AppState} from 'react-native'
|
import {AppState} from 'react-native'
|
||||||
|
|
||||||
import {MessagesEventBus} from '#/state/messages/events/agent'
|
import {MessagesEventBus} from '#/state/messages/events/agent'
|
||||||
import {useAgent} from '#/state/session'
|
import {useAgent, useSession} from '#/state/session'
|
||||||
|
|
||||||
const MessagesEventBusContext = React.createContext<MessagesEventBus | null>(
|
const MessagesEventBusContext = React.createContext<MessagesEventBus | null>(
|
||||||
null,
|
null,
|
||||||
|
@ -11,7 +11,9 @@ const MessagesEventBusContext = React.createContext<MessagesEventBus | null>(
|
||||||
export function useMessagesEventBus() {
|
export function useMessagesEventBus() {
|
||||||
const ctx = React.useContext(MessagesEventBusContext)
|
const ctx = React.useContext(MessagesEventBusContext)
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
throw new Error('useChat must be used within a ChatProvider')
|
throw new Error(
|
||||||
|
'useMessagesEventBus must be used within a MessagesEventBusProvider',
|
||||||
|
)
|
||||||
}
|
}
|
||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
@ -20,6 +22,26 @@ export function MessagesEventBusProvider({
|
||||||
children,
|
children,
|
||||||
}: {
|
}: {
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
|
}) {
|
||||||
|
const {currentAccount} = useSession()
|
||||||
|
|
||||||
|
if (!currentAccount) {
|
||||||
|
return (
|
||||||
|
<MessagesEventBusContext.Provider value={null}>
|
||||||
|
{children}
|
||||||
|
</MessagesEventBusContext.Provider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<MessagesEventBusProviderInner>{children}</MessagesEventBusProviderInner>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function MessagesEventBusProviderInner({
|
||||||
|
children,
|
||||||
|
}: {
|
||||||
|
children: React.ReactNode
|
||||||
}) {
|
}) {
|
||||||
const {getAgent} = useAgent()
|
const {getAgent} = useAgent()
|
||||||
const [bus] = React.useState(
|
const [bus] = React.useState(
|
||||||
|
|
Loading…
Reference in New Issue