From a20c8dbbf44666606c202891d481166f0cae5eba Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Mon, 8 Jan 2024 14:44:44 +0200 Subject: [PATCH] add requestAnimationFrame to socialgraph handleevent --- .../system/src/SocialGraph/SocialGraph.ts | 60 ++++++++++--------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/packages/system/src/SocialGraph/SocialGraph.ts b/packages/system/src/SocialGraph/SocialGraph.ts index 0c6cfae8..82e75862 100644 --- a/packages/system/src/SocialGraph/SocialGraph.ts +++ b/packages/system/src/SocialGraph/SocialGraph.ts @@ -51,43 +51,45 @@ export default class SocialGraph { if (event.kind !== 3) { return; } - try { - const author = ID(event.pubkey); - const timestamp = event.created_at; - const existingTimestamp = this.latestFollowEventTimestamps.get(author); - if (existingTimestamp && timestamp <= existingTimestamp) { - return; - } - this.latestFollowEventTimestamps.set(author, timestamp); + requestAnimationFrame(() => { + try { + const author = ID(event.pubkey); + const timestamp = event.created_at; + const existingTimestamp = this.latestFollowEventTimestamps.get(author); + if (existingTimestamp && timestamp <= existingTimestamp) { + return; + } + this.latestFollowEventTimestamps.set(author, timestamp); - // Collect all users followed in the new event. - const followedInEvent = new Set(); - for (const tag of event.tags) { - if (tag[0] === "p") { - const followedUser = ID(tag[1]); - if (followedUser !== author) { - followedInEvent.add(followedUser); + // Collect all users followed in the new event. + const followedInEvent = new Set(); + for (const tag of event.tags) { + if (tag[0] === "p") { + const followedUser = ID(tag[1]); + if (followedUser !== author) { + followedInEvent.add(followedUser); + } } } - } - // Get the set of users currently followed by the author. - const currentlyFollowed = this.followedByUser.get(author) || new Set(); + // Get the set of users currently followed by the author. + const currentlyFollowed = this.followedByUser.get(author) || new Set(); - // Find users that need to be removed. - for (const user of currentlyFollowed) { - if (!followedInEvent.has(user)) { - this.removeFollower(user, author); + // Find users that need to be removed. + for (const user of currentlyFollowed) { + if (!followedInEvent.has(user)) { + this.removeFollower(user, author); + } } - } - // Add or update the followers based on the new event. - for (const user of followedInEvent) { - this.addFollower(user, author); + // Add or update the followers based on the new event. + for (const user of followedInEvent) { + this.addFollower(user, author); + } + } catch (e) { + // might not be logged in or sth } - } catch (e) { - // might not be logged in or sth - } + }); } isFollowing(follower: HexKey, followedUser: HexKey): boolean {