Add FriendListUpdateComposer interval

This commit is contained in:
Bill 2022-07-19 11:50:48 -04:00
parent daf761e145
commit 33dd28f089

View File

@ -1,5 +1,5 @@
import { AcceptFriendMessageComposer, DeclineFriendMessageComposer, FollowFriendMessageComposer, FriendListFragmentEvent, FriendListUpdateEvent, FriendParser, FriendRequestsEvent, GetFriendRequestsComposer, MessengerInitComposer, MessengerInitEvent, NewFriendRequestEvent, RequestFriendComposer, SetRelationshipStatusComposer } from '@nitrots/nitro-renderer'; import { AcceptFriendMessageComposer, DeclineFriendMessageComposer, FollowFriendMessageComposer, FriendListFragmentEvent, FriendListUpdateComposer, FriendListUpdateEvent, FriendParser, FriendRequestsEvent, GetFriendRequestsComposer, MessengerInitComposer, MessengerInitEvent, NewFriendRequestEvent, RequestFriendComposer, SetRelationshipStatusComposer } from '@nitrots/nitro-renderer';
import { useCallback, useEffect, useMemo, useState } from 'react'; import { useEffect, useMemo, useState } from 'react';
import { useBetween } from 'use-between'; import { useBetween } from 'use-between';
import { CloneObject, MessengerFriend, MessengerRequest, MessengerSettings, SendMessageComposer } from '../../api'; import { CloneObject, MessengerFriend, MessengerRequest, MessengerSettings, SendMessageComposer } from '../../api';
import { useMessageEvent } from '../events'; import { useMessageEvent } from '../events';
@ -43,10 +43,11 @@ const useFriendsState = () =>
return offlineFriends; return offlineFriends;
}, [ friends ]); }, [ friends ]);
const followFriend = useCallback((friend: MessengerFriend) => SendMessageComposer(new FollowFriendMessageComposer(friend.id)), []); const followFriend = (friend: MessengerFriend) => SendMessageComposer(new FollowFriendMessageComposer(friend.id));
const updateRelationship = useCallback((friend: MessengerFriend, type: number) => ((type !== friend.relationshipStatus) && SendMessageComposer(new SetRelationshipStatusComposer(friend.id, type))), []);
const getFriend = useCallback((userId: number) => const updateRelationship = (friend: MessengerFriend, type: number) => ((type !== friend.relationshipStatus) && SendMessageComposer(new SetRelationshipStatusComposer(friend.id, type)));
const getFriend = (userId: number) =>
{ {
for(const friend of friends) for(const friend of friends)
{ {
@ -54,9 +55,9 @@ const useFriendsState = () =>
} }
return null; return null;
}, [ friends ]); }
const canRequestFriend = useCallback((userId: number) => const canRequestFriend = (userId: number) =>
{ {
if(getFriend(userId)) return false; if(getFriend(userId)) return false;
@ -65,9 +66,9 @@ const useFriendsState = () =>
if(sentRequests.indexOf(userId) >= 0) return false; if(sentRequests.indexOf(userId) >= 0) return false;
return true; return true;
}, [ requests, sentRequests, getFriend ]); }
const requestFriend = useCallback((userId: number, userName: string) => const requestFriend = (userId: number, userName: string) =>
{ {
if(!canRequestFriend(userId)) return false; if(!canRequestFriend(userId)) return false;
@ -81,9 +82,9 @@ const useFriendsState = () =>
}); });
SendMessageComposer(new RequestFriendComposer(userName)); SendMessageComposer(new RequestFriendComposer(userName));
}, [ canRequestFriend ]); }
const requestResponse = useCallback((requestId: number, flag: boolean) => const requestResponse = (requestId: number, flag: boolean) =>
{ {
if(requestId === -1 && !flag) if(requestId === -1 && !flag)
{ {
@ -114,9 +115,9 @@ const useFriendsState = () =>
return newRequests; return newRequests;
}); });
} }
}, []); }
const onMessengerInitEvent = useCallback((event: MessengerInitEvent) => useMessageEvent<MessengerInitEvent>(MessengerInitEvent, event =>
{ {
const parser = event.getParser(); const parser = event.getParser();
@ -127,11 +128,9 @@ const useFriendsState = () =>
parser.categories)); parser.categories));
SendMessageComposer(new GetFriendRequestsComposer()); SendMessageComposer(new GetFriendRequestsComposer());
}, []); });
useMessageEvent(MessengerInitEvent, onMessengerInitEvent); useMessageEvent<FriendListFragmentEvent>(FriendListFragmentEvent, event =>
const onFriendsFragmentEvent = useCallback((event: FriendListFragmentEvent) =>
{ {
const parser = event.getParser(); const parser = event.getParser();
@ -151,11 +150,9 @@ const useFriendsState = () =>
return newValue; return newValue;
}); });
}, []); });
useMessageEvent(FriendListFragmentEvent, onFriendsFragmentEvent); useMessageEvent<FriendListUpdateEvent>(FriendListUpdateEvent, event =>
const onFriendsUpdateEvent = useCallback((event: FriendListUpdateEvent) =>
{ {
const parser = event.getParser(); const parser = event.getParser();
@ -193,11 +190,9 @@ const useFriendsState = () =>
return newValue; return newValue;
}); });
}, []); });
useMessageEvent(FriendListUpdateEvent, onFriendsUpdateEvent); useMessageEvent<FriendRequestsEvent>(FriendRequestsEvent, event =>
const onFriendRequestsEvent = useCallback((event: FriendRequestsEvent) =>
{ {
const parser = event.getParser(); const parser = event.getParser();
@ -225,11 +220,9 @@ const useFriendsState = () =>
return newValue; return newValue;
}); });
}, []); });
useMessageEvent(FriendRequestsEvent, onFriendRequestsEvent); useMessageEvent<NewFriendRequestEvent>(NewFriendRequestEvent, event =>
const onNewFriendRequestEvent = useCallback((event: NewFriendRequestEvent) =>
{ {
const parser = event.getParser(); const parser = event.getParser();
const request = parser.request; const request = parser.request;
@ -250,13 +243,18 @@ const useFriendsState = () =>
return newRequests; return newRequests;
}); });
}, []); });
useMessageEvent(NewFriendRequestEvent, onNewFriendRequestEvent);
useEffect(() => useEffect(() =>
{ {
SendMessageComposer(new MessengerInitComposer()); SendMessageComposer(new MessengerInitComposer());
const interval = setInterval(() => SendMessageComposer(new FriendListUpdateComposer()), 120000);
return () =>
{
clearInterval(interval);
}
}, []); }, []);
return { friends, requests, sentRequests, settings, onlineFriends, offlineFriends, getFriend, canRequestFriend, requestFriend, requestResponse, followFriend, updateRelationship }; return { friends, requests, sentRequests, settings, onlineFriends, offlineFriends, getFriend, canRequestFriend, requestFriend, requestResponse, followFriend, updateRelationship };