subs client

This commit is contained in:
tonyrewin 2022-12-07 10:39:09 +03:00
parent 80577c2cd3
commit 199e9cf93b
4 changed files with 30 additions and 8 deletions

View File

@ -4,7 +4,6 @@ import { Icon } from '../_shared/Icon'
import DialogCard from '../Inbox/DialogCard' import DialogCard from '../Inbox/DialogCard'
import Search from '../Inbox/Search' import Search from '../Inbox/Search'
import { useSession } from '../../context/session' import { useSession } from '../../context/session'
import type { Client } from '@urql/core'
import Message from '../Inbox/Message' import Message from '../Inbox/Message'
import { loadRecipients, loadMessages } from '../../stores/inbox' import { loadRecipients, loadMessages } from '../../stores/inbox'
import { t } from '../../utils/intl' import { t } from '../../utils/intl'
@ -16,7 +15,6 @@ import '../../styles/Inbox.scss'
import { useInbox } from '../../context/inbox' import { useInbox } from '../../context/inbox'
import DialogHeader from '../Inbox/DialogHeader' import DialogHeader from '../Inbox/DialogHeader'
import { apiClient } from '../../utils/apiClient' import { apiClient } from '../../utils/apiClient'
import { createChatClient } from '../../graphql/privateGraphQLClient'
import MessagesFallback from '../Inbox/MessagesFallback' import MessagesFallback from '../Inbox/MessagesFallback'
const userSearch = (array: Author[], keyword: string) => { const userSearch = (array: Author[], keyword: string) => {
@ -29,7 +27,7 @@ const userSearch = (array: Author[], keyword: string) => {
export const InboxView = () => { export const InboxView = () => {
const { const {
chats, chats,
actions: { loadChats } actions: { loadChats, setListener }
} = useInbox() } = useInbox()
const [messages, setMessages] = createSignal<MessageType[]>([]) const [messages, setMessages] = createSignal<MessageType[]>([])
const [recipients, setRecipients] = createSignal<Author[]>([]) const [recipients, setRecipients] = createSignal<Author[]>([])
@ -39,7 +37,6 @@ export const InboxView = () => {
const [currentDialog, setCurrentDialog] = createSignal<Chat>() const [currentDialog, setCurrentDialog] = createSignal<Chat>()
const { session } = useSession() const { session } = useSession()
const currentUserId = createMemo(() => session()?.user.id) const currentUserId = createMemo(() => session()?.user.id)
const [subClient, setSubClient] = createSignal<Client>()
// Поиск по диалогам // Поиск по диалогам
const getQuery = (query) => { const getQuery = (query) => {
// if (query().length >= 2) { // if (query().length >= 2) {
@ -59,7 +56,6 @@ export const InboxView = () => {
try { try {
const response = await loadMessages({ chat: chat.id }) const response = await loadMessages({ chat: chat.id })
setMessages(response as unknown as MessageType[]) setMessages(response as unknown as MessageType[])
setSubClient((_) => createChatClient(onMessage))
// TODO: one client recreating // TODO: one client recreating
} catch (error) { } catch (error) {
console.error('[loadMessages]', error) console.error('[loadMessages]', error)
@ -72,6 +68,7 @@ export const InboxView = () => {
try { try {
const response = await loadRecipients({ days: 365 }) const response = await loadRecipients({ days: 365 })
setRecipients(response as unknown as Author[]) setRecipients(response as unknown as Author[])
setListener(onMessage)
} catch (error) { } catch (error) {
console.log(error) console.log(error)
} }

View File

@ -1,13 +1,16 @@
import type { Accessor, JSX } from 'solid-js' import { Accessor, createMemo, JSX, onMount } from 'solid-js'
import { createContext, createSignal, useContext } from 'solid-js' import { createContext, createSignal, useContext } from 'solid-js'
import { createChatClient } from '../graphql/privateGraphQLClient'
import type { Chat } from '../graphql/types.gen' import type { Chat } from '../graphql/types.gen'
import { apiClient } from '../utils/apiClient' import { apiClient } from '../utils/apiClient'
import newMessages from '../graphql/subs/new-messages'
type InboxContextType = { type InboxContextType = {
chats: Accessor<Chat[]> chats: Accessor<Chat[]>
actions: { actions: {
createChat: (members: number[], title: string) => Promise<void> createChat: (members: number[], title: string) => Promise<void>
loadChats: () => Promise<void> loadChats: () => Promise<void>
setListener: (listener: (ev) => void) => void
} }
} }
@ -19,6 +22,8 @@ export function useInbox() {
export const InboxProvider = (props: { children: JSX.Element }) => { export const InboxProvider = (props: { children: JSX.Element }) => {
const [chats, setChats] = createSignal<Chat[]>([]) const [chats, setChats] = createSignal<Chat[]>([])
const [listener, setListener] = createSignal(console.debug)
const subclient = createMemo(() => createChatClient(listener()))
const loadChats = async () => { const loadChats = async () => {
try { try {
const newChats = await apiClient.getChats({ limit: 50, offset: 0 }) const newChats = await apiClient.getChats({ limit: 50, offset: 0 })
@ -42,9 +47,13 @@ export const InboxProvider = (props: { children: JSX.Element }) => {
const actions = { const actions = {
createChat, createChat,
loadChats loadChats,
setListener // setting listening handler
} }
onMount(() => {
const resp = subclient().subscription(newMessages, {})
console.debug(resp)
})
const value: InboxContextType = { chats, actions } const value: InboxContextType = { chats, actions }
return <InboxContext.Provider value={value}>{props.children}</InboxContext.Provider> return <InboxContext.Provider value={value}>{props.children}</InboxContext.Provider>
} }

View File

@ -67,6 +67,7 @@ export const createChatClient = (onMessage) => {
} }
} }
}) })
options.exchanges.unshift(sseExchange) options.exchanges.unshift(sseExchange)
return createClient(options) return createClient(options)
} }

View File

@ -0,0 +1,15 @@
import { gql } from '@urql/core'
export default gql`
subscription {
newMessages {
id
chatId
author
body
replyTo
createdAt
updatedAt
}
}
`