rewriting user online status checks with peer is_connected api

This commit is contained in:
Pavel Murygin 2021-03-18 21:10:19 +03:00
parent 314847738a
commit 87cc957580
2 changed files with 40 additions and 62 deletions

View File

@ -2,12 +2,12 @@ import { FluenceClient, Particle, sendParticle, sendParticleAsFetch } from '@flu
import {
fluentPadServiceId,
notifyOnlineFnName,
notifyTextUpdateFnName,
notifyUserAddedFnName,
notifyUserRemovedFnName,
history,
userList,
notifyOnlineFnName,
} from './constants';
export interface ServiceResult {
@ -50,16 +50,10 @@ export const updateOnlineStatuses = async (client: FluenceClient) => {
(fold allUsers.$.users! u
(par
(seq
(call u.$.relay_id! ("op" "identity") [])
(seq
(call u.$.peer_id! ("op" "identity") [])
(seq
(call u.$.relay_id! ("op" "identity") [])
(call u.$.relay_id! ("peer" "is_connected") [u.$.peer_id!] isOnline)
(seq
(call myRelay ("op" "identity") [])
(call myPeerId (fluentPadServiceId notifyOnline) [u.$.peer_id!])
)
)
(call myPeerId (fluentPadServiceId notifyOnline) [u.$.peer_id! isOnline])
)
)
(next u)
@ -92,7 +86,7 @@ export const notifySelfAdded = (client: FluenceClient, name: string) => {
(par
(seq
(call u.$.relay_id! ("op" "identity") [])
(call u.$.peer_id! (fluentPadServiceId notifyUserAdded) [myUser setOnline])
(call u.$.peer_id! (fluentPadServiceId notifyUserAdded) [myUser true])
)
(next u)
)
@ -107,14 +101,11 @@ export const notifySelfAdded = (client: FluenceClient, name: string) => {
myPeerId: client.selfPeerId,
fluentPadServiceId: fluentPadServiceId,
notifyUserAdded: notifyUserAddedFnName,
myUser: [
{
myUser: {
name: name,
peer_id: client.selfPeerId,
relay_id: client.relayPeerId,
},
],
setOnline: true,
},
);
@ -128,9 +119,20 @@ export const getUserList = async (client: FluenceClient) => {
(call myRelay ("op" "identity") [])
(seq
(call userlistNode (userlist "get_users") [] allUsers)
(fold allUsers.$.users! u
(par
(seq
(call u.$.relay_id! ("op" "identity") [])
(seq
(call u.$.relay_id! ("peer" "is_connected") [u.$.peer_id!] isOnline)
(seq
(call myRelay ("op" "identity") [])
(call myPeerId (fluentPadServiceId notifyUserAdded) [allUsers.$.users!])
(call myPeerId (fluentPadServiceId notifyUserAdded) [u isOnline])
)
)
)
(next u)
)
)
)
)
@ -142,7 +144,6 @@ export const getUserList = async (client: FluenceClient) => {
myPeerId: client.selfPeerId,
fluentPadServiceId: fluentPadServiceId,
notifyUserAdded: notifyUserAddedFnName,
immediately: true,
},
);

View File

@ -14,17 +14,8 @@ interface User {
id: PeerIdB58;
name: string;
isOnline: boolean;
shouldBecomeOnline: boolean;
}
const turnUserAsOfflineCandidate = (u: User): User => {
return {
...u,
isOnline: u.shouldBecomeOnline,
shouldBecomeOnline: false,
};
};
const refreshTimeoutMs = 2000;
export const UserList = (props: { selfName: string }) => {
@ -33,13 +24,6 @@ export const UserList = (props: { selfName: string }) => {
useEffect(() => {
const listRefreshTimer = setInterval(() => {
setUsers((prev) => {
const newUsers = Array.from(prev).map(
([key, user]) => [key, turnUserAsOfflineCandidate(user)] as const,
);
return new Map(newUsers);
});
// don't block
withErrorHandlingAsync(async () => {
await api.updateOnlineStatuses(client);
@ -47,23 +31,21 @@ export const UserList = (props: { selfName: string }) => {
}, refreshTimeoutMs);
const unsub1 = subscribeToEvent(client, fluentPadServiceId, notifyUserAddedFnName, (args, _) => {
const [users, setOnline] = args as [api.User[], boolean];
const [user, isOnline] = args as [api.User, boolean];
console.log(user, isOnline);
setUsers((prev) => {
const u = user;
const result = new Map(prev);
for (let u of users) {
if (result.has(u.peer_id)) {
continue;
return result;
}
const isCurrentUser = u.peer_id === client.selfPeerId;
result.set(u.peer_id, {
name: u.name,
id: u.peer_id,
isOnline: isCurrentUser || setOnline,
shouldBecomeOnline: isCurrentUser || setOnline,
isOnline: isOnline,
});
}
return result;
});
});
@ -78,18 +60,13 @@ export const UserList = (props: { selfName: string }) => {
});
const unsub3 = subscribeToEvent(client, fluentPadServiceId, notifyOnlineFnName, (args, _) => {
const [userOnline] = args as [PeerIdB58[]];
const [user, onlineStatus] = args as [PeerIdB58, boolean];
setUsers((prev) => {
const result = new Map(prev);
for (let u of userOnline) {
const toSetOnline = result.get(u);
if (toSetOnline) {
toSetOnline.shouldBecomeOnline = true;
toSetOnline.isOnline = true;
const u = result.get(user);
if (u) {
result.set(user, { ...u, isOnline: onlineStatus });
}
}
return result;
});
});