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