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

View File

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