@webinex/chatify 1.0.1-rc3 → 1.0.2-rc3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,33 +1,30 @@
1
- import { useEffect, useMemo } from 'react';
1
+ import { useEffect, useRef } from 'react';
2
2
  import { useClient } from '../ChatifyContext';
3
3
  import { useDispatch, useSelect, useStateRef } from './reducer';
4
+ import { FETCH_QUERY_STATE, QueryState } from './state';
5
+ import { uniqId } from '../util';
6
+ import { Account } from './models';
4
7
 
5
- export function useGetAccountsQuery() {
8
+ export function useGetAccountsQuery(): QueryState<Account[]> {
9
+ const id = useRef(uniqId());
6
10
  const client = useClient();
7
11
  const stateRef = useStateRef();
8
- useSelect((x) => x.query.accounts, []);
12
+ const queryState = useSelect((x) => x.query.accounts, []) ?? FETCH_QUERY_STATE;
9
13
  const dispatch = useDispatch();
10
14
 
11
- const uninitialized = useMemo(() => {
12
- const uninitialized = stateRef.current.query.accounts.uninitialized !== false;
13
- if (uninitialized) dispatch({ type: 'accounts_fetch', data: undefined });
14
- return uninitialized;
15
-
16
- // eslint-disable-next-line react-hooks/exhaustive-deps
17
- }, []);
18
-
19
15
  useEffect(() => {
20
- if (!uninitialized) {
16
+ if (stateRef.current.query.accounts.uninitialized === false) {
21
17
  return;
22
18
  }
23
19
 
20
+ dispatch({ type: 'accounts_fetch', data: { requestId: id.current } });
24
21
  client
25
22
  .accounts()
26
23
  .then((accounts) => dispatch({ type: 'accounts_fulfilled', data: { accounts } }))
27
24
  .catch((error) => dispatch({ type: 'accounts_rejected', data: { error } }));
28
25
 
29
26
  // eslint-disable-next-line react-hooks/exhaustive-deps
30
- }, [uninitialized]);
27
+ }, []);
31
28
 
32
- return stateRef.current.query.accounts;
29
+ return queryState ?? FETCH_QUERY_STATE;
33
30
  }
@@ -1,26 +1,23 @@
1
- import { useEffect, useMemo } from 'react';
1
+ import { useEffect, useRef } from 'react';
2
2
  import { useClient } from '../ChatifyContext';
3
3
  import { useDispatch, useSelect, useStateRef } from './reducer';
4
+ import { uniqId } from '../util';
5
+ import { FETCH_QUERY_STATE, QueryState } from './state';
6
+ import { ChatListItem } from './models';
4
7
 
5
- export function useGetChatListQuery() {
8
+ export function useGetChatListQuery(): QueryState<ChatListItem[]> {
9
+ const id = useRef(uniqId());
6
10
  const stateRef = useStateRef();
7
11
  const client = useClient();
8
- useSelect((x) => x.query.chatList, []);
12
+ const queryState = useSelect((x) => x.query.chatList, []) ?? FETCH_QUERY_STATE;
9
13
  const dispatch = useDispatch();
10
14
 
11
- const uninitialized = useMemo(() => {
12
- const uninitialized = stateRef.current.query.chatList.uninitialized !== false;
13
- if (uninitialized) dispatch({ type: 'chatList_fetch', data: undefined });
14
- return uninitialized;
15
-
16
- // eslint-disable-next-line react-hooks/exhaustive-deps
17
- }, []);
18
-
19
15
  useEffect(() => {
20
- if (!uninitialized) {
16
+ if (stateRef.current.query.chatList.uninitialized === false) {
21
17
  return;
22
18
  }
23
19
 
20
+ dispatch({ type: 'chatList_fetch', data: { requestId: id.current } });
24
21
  client
25
22
  .chats()
26
23
  .then((chats) => dispatch({ type: 'chatList_fulfilled', data: { chats } }))
@@ -29,5 +26,5 @@ export function useGetChatListQuery() {
29
26
  // eslint-disable-next-line react-hooks/exhaustive-deps
30
27
  }, []);
31
28
 
32
- return stateRef.current.query.chatList;
29
+ return queryState ?? FETCH_QUERY_STATE;
33
30
  }
@@ -1,34 +1,31 @@
1
- import { useEffect, useMemo } from 'react';
1
+ import { useEffect, useRef } from 'react';
2
2
  import { useClient } from '../ChatifyContext';
3
3
  import { useDispatch, useSelect, useStateRef } from './reducer';
4
+ import { uniqId } from '../util';
5
+ import { FETCH_QUERY_STATE, QueryState } from './state';
6
+ import { Chat } from './models';
4
7
 
5
- export function useGetChatQuery(args: { chatId: string }) {
8
+ export function useGetChatQuery(args: { chatId: string }): QueryState<Chat> {
9
+ const id = useRef(uniqId());
6
10
  const { chatId } = args;
7
11
  const client = useClient();
8
12
  const stateRef = useStateRef();
9
- useSelect((x) => x.query.chats[chatId], [chatId]);
13
+ const queryState = useSelect((x) => x.query.chats[chatId], [chatId]) ?? FETCH_QUERY_STATE;
10
14
  const dispatch = useDispatch();
11
15
 
12
- const uninitialized = useMemo(() => {
13
- const uninitialized = stateRef.current.query.chats[chatId]?.uninitialized !== false;
14
- if (uninitialized) dispatch({ type: 'chat_fetch', data: { chatId } });
15
- return uninitialized;
16
-
17
- // eslint-disable-next-line react-hooks/exhaustive-deps
18
- }, [chatId]);
19
-
20
16
  useEffect(() => {
21
- if (!uninitialized) {
17
+ if (stateRef.current.query.chats[chatId]?.uninitialized === true) {
22
18
  return;
23
19
  }
24
20
 
21
+ dispatch({ type: 'chat_fetch', data: { chatId, requestId: id.current } });
25
22
  client
26
23
  .chat(chatId)
27
24
  .then((chat) => dispatch({ type: 'chat_fulfilled', data: { chat } }))
28
25
  .catch((error) => dispatch({ type: 'chat_rejected', data: { chatId, error } }));
29
26
 
30
27
  // eslint-disable-next-line react-hooks/exhaustive-deps
31
- }, [uninitialized, chatId]);
28
+ }, [chatId]);
32
29
 
33
- return stateRef.current.query.chats[chatId];
30
+ return queryState ?? FETCH_QUERY_STATE;
34
31
  }
@@ -1,35 +1,35 @@
1
- import { useEffect, useMemo } from 'react';
1
+ import { useEffect, useRef } from 'react';
2
2
  import { useClient } from '../ChatifyContext';
3
- import { MessageState, QueryState } from './state';
3
+ import { FETCH_QUERY_STATE, MessageState, QueryState } from './state';
4
4
  import { useDispatch, useSelect, useStateRef } from './reducer';
5
+ import { uniqId } from '../util';
5
6
 
6
7
  export function useGetMessagesQuery(args: { chatId: string }): QueryState<MessageState[]> {
7
8
  const { chatId } = args;
9
+ const id = useRef(uniqId());
8
10
  const stateRef = useStateRef();
9
11
  const dispatch = useDispatch();
10
12
  useSelect((x) => x.query.messages[chatId], [chatId]);
11
13
  const client = useClient();
12
14
 
13
- const uninitialized = useMemo(() => {
14
- const uninitialized = stateRef.current.query.messages[chatId]?.uninitialized !== false;
15
- if (uninitialized) dispatch({ type: 'messages_fetch', data: { chatId } });
16
- return uninitialized;
17
- }, [chatId, dispatch, stateRef]);
18
-
19
15
  useEffect(() => {
20
- if (!uninitialized) {
16
+ if (stateRef.current.query.messages[chatId]?.uninitialized === false) {
21
17
  return;
22
18
  }
23
19
 
24
- client.messages({ chatId, skip: 0, take: 16 }).then((messages) =>
25
- dispatch({
26
- type: 'messages_fulfilled',
27
- data: { chatId, messages: messages.slice(0, 15), hasMore: messages.length === 16 },
28
- }),
29
- );
20
+ dispatch({ type: 'messages_fetch', data: { requestId: id.current, chatId } });
21
+ client
22
+ .messages({ chatId, skip: 0, take: 16 })
23
+ .then((messages) =>
24
+ dispatch({
25
+ type: 'messages_fulfilled',
26
+ data: { chatId, messages: messages.slice(0, 15), hasMore: messages.length === 16 },
27
+ }),
28
+ )
29
+ .catch((error) => dispatch({ type: 'messages_rejected', data: { chatId, error } }));
30
30
 
31
31
  // eslint-disable-next-line react-hooks/exhaustive-deps
32
32
  }, [chatId]);
33
33
 
34
- return stateRef.current.query.messages[chatId];
34
+ return stateRef.current.query.messages[chatId] ?? FETCH_QUERY_STATE;
35
35
  }