generated from graasp/graasp-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor websocket client, add new real-time behaviours
- Loading branch information
1 parent
35b96c4
commit e836e7c
Showing
20 changed files
with
717 additions
and
651 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { PartialChatMessage, QueryClientConfig, UUID } from '../types'; | ||
import { buildGetItemChatRoute, buildPostItemChatMessageRoute } from './routes'; | ||
import { DEFAULT_GET, DEFAULT_POST, failOnError } from './utils'; | ||
|
||
export const getItemChat = async ( | ||
id: UUID, | ||
{ API_HOST }: QueryClientConfig, | ||
) => { | ||
const res = await fetch( | ||
`${API_HOST}/${buildGetItemChatRoute(id)}`, | ||
DEFAULT_GET, | ||
).then(failOnError); | ||
const itemChat = await res.json(); | ||
return itemChat; | ||
}; | ||
|
||
export const postItemChatMessage = async ( | ||
{ chatId, body }: PartialChatMessage, | ||
{ API_HOST }: QueryClientConfig, | ||
) => { | ||
const res = await fetch( | ||
`${API_HOST}/${buildPostItemChatMessageRoute(chatId)}`, | ||
{ | ||
...DEFAULT_POST, | ||
body: JSON.stringify({ body }), | ||
}, | ||
).then(failOnError); | ||
const publishedMessage = await res.json(); | ||
return publishedMessage; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useQuery } from 'react-query'; | ||
import * as Api from '../api'; | ||
import { buildItemChatKey } from '../config/keys'; | ||
import { QueryClientConfig, UUID } from '../types'; | ||
|
||
export default (queryConfig: QueryClientConfig) => { | ||
const { retry, cacheTime, staleTime } = queryConfig; | ||
const defaultOptions = { | ||
retry, | ||
cacheTime, | ||
staleTime, | ||
}; | ||
|
||
return { | ||
useItemChat: (itemId: UUID) => | ||
useQuery({ | ||
queryKey: buildItemChatKey(itemId), | ||
queryFn: () => | ||
Api.getItemChat(itemId, queryConfig).then((data) => data), | ||
...defaultOptions, | ||
enabled: Boolean(itemId), | ||
}), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { QueryClient } from 'react-query'; | ||
import * as Api from '../api' | ||
import { MUTATION_KEYS } from '../config/keys'; | ||
import { QueryClientConfig } from '../types'; | ||
|
||
const { POST_ITEM_CHAT_MESSAGE } = MUTATION_KEYS; | ||
|
||
export default (queryClient: QueryClient, queryConfig: QueryClientConfig) => { | ||
queryClient.setMutationDefaults(POST_ITEM_CHAT_MESSAGE, { | ||
mutationFn: (chatMsg) => Api.postItemChatMessage(chatMsg, queryConfig), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* TODO: use types from graasp-websockets | ||
*/ | ||
|
||
/** Namespace for notifications realm */ | ||
export const REALM_NOTIF = 'notif'; | ||
|
||
/** Client actions */ | ||
export const CLIENT_ACTION_SUBSCRIBE = 'subscribe'; | ||
export const CLIENT_ACTION_UNSUBSCRIBE = 'unsubscribe'; | ||
export const CLIENT_ACTION_SUBSCRIBE_ONLY = 'subscribeOnly'; | ||
export const CLIENT_ACTION_DISCONNECT = 'disconnect'; | ||
|
||
/** Server message types */ | ||
export const SERVER_TYPE_RESPONSE = 'response'; | ||
export const SERVER_TYPE_UPDATE = 'update'; | ||
export const SERVER_TYPE_INFO = 'info'; | ||
|
||
/** Server response status */ | ||
export const RESPONSE_STATUS_SUCCESS = 'success'; | ||
export const RESPONSE_STATUS_ERROR = 'error'; | ||
|
||
/** Error names */ | ||
export const ERROR_ACCESS_DENIED = 'ACCESS_DENIED'; | ||
export const ERROR_BAD_REQUEST = 'BAD_REQUEST'; | ||
export const ERROR_NOT_FOUND = 'NOT_FOUND'; | ||
export const ERROR_SERVER_ERROR = 'SERVER_ERROR'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { QueryClient } from 'react-query'; | ||
import { buildItemChatKey } from '../../config/keys'; | ||
import { Chat, ChatMessage, UUID } from '../../types'; | ||
import { Channel, GraaspWebsocketClient } from '../ws-client'; | ||
|
||
// todo: use graasp-types? | ||
interface ChatEvent { | ||
kind: string; | ||
op: string; | ||
message: ChatMessage; | ||
} | ||
|
||
export default ( | ||
websocketClient: GraaspWebsocketClient, | ||
queryClient: QueryClient, | ||
) => ({ | ||
/** | ||
* React hook to subscribe to the updates of the given chat ID | ||
* @param chatId The ID of the chat of which to observves updates | ||
*/ | ||
useItemChatUpdates: (chatId: UUID) => { | ||
if (!chatId) { | ||
return; | ||
} | ||
|
||
const channel: Channel = { name: chatId, topic: 'chat/item' }; | ||
|
||
const handler = (event: ChatEvent) => { | ||
if (event.kind === 'item') { | ||
const chatKey = buildItemChatKey(chatId); | ||
const current: Chat | undefined = queryClient.getQueryData(chatKey); | ||
|
||
if (current) { | ||
switch (event.op) { | ||
case 'publish': { | ||
const msg = event.message; | ||
const newChat = Object.assign({}, current); | ||
newChat.messages = [...current.messages]; | ||
newChat.messages.push(msg); | ||
queryClient.setQueryData(chatKey, newChat); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
websocketClient.subscribe(channel, handler); | ||
|
||
return function cleanup() { | ||
websocketClient.unsubscribe(channel, handler); | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { QueryClient } from 'react-query'; | ||
import { GraaspWebsocketClient } from '../ws-client'; | ||
import configureChatHooks from './chat'; | ||
import configureItemHooks from './item'; | ||
import configureMembershipHooks from './membership'; | ||
|
||
export default ( | ||
websocketClient: GraaspWebsocketClient, | ||
queryClient: QueryClient, | ||
) => { | ||
return { | ||
...configureItemHooks(websocketClient, queryClient), | ||
...configureMembershipHooks(websocketClient, queryClient), | ||
...configureChatHooks(websocketClient, queryClient), | ||
}; | ||
}; |
Oops, something went wrong.