mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import type { AppStore } from './store';
|
|
import type { AppSocket } from '../api/socket';
|
|
import { userActions } from './slices';
|
|
import type { FriendRequest } from '../model/friend';
|
|
|
|
/**
|
|
* 初始化Redux 上下文
|
|
*/
|
|
export function setupRedux(socket: AppSocket, store: AppStore) {
|
|
console.log('初始化Redux上下文...');
|
|
|
|
// 获取好友列表
|
|
socket.request<string[]>('friend.getAllFriends').then((data) => {
|
|
store.dispatch(userActions.setFriendList(data));
|
|
});
|
|
|
|
// 获取好友邀请列表
|
|
socket.request<FriendRequest[]>('friend.request.allRelated').then((data) => {
|
|
store.dispatch(userActions.setFriendRequests(data));
|
|
});
|
|
|
|
socket.listen<{ userId: string }>('friend.add', ({ userId }) => {
|
|
if (typeof userId !== 'string') {
|
|
console.error('错误的信息', userId);
|
|
return;
|
|
}
|
|
store.dispatch(userActions.appendFriend(userId));
|
|
});
|
|
|
|
socket.listen<FriendRequest>('friend.request.add', (request) => {
|
|
store.dispatch(userActions.appendFriendRequest(request));
|
|
});
|
|
|
|
socket.listen<{ requestId: string }>(
|
|
'friend.request.remove',
|
|
({ requestId }) => {
|
|
store.dispatch(userActions.removeFriendRequest(requestId));
|
|
}
|
|
);
|
|
}
|