From cf36e9cd4e0b470b8ac80e969c933b560efbec16 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Thu, 17 Oct 2024 14:56:10 +0300 Subject: [PATCH] cache clients --- src/lib/bskyClient.ts | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/lib/bskyClient.ts b/src/lib/bskyClient.ts index 1b0da47..e55f2bf 100644 --- a/src/lib/bskyClient.ts +++ b/src/lib/bskyClient.ts @@ -1,5 +1,8 @@ import { AtUri, type AtpSessionData, BskyAgent } from "@atproto/api"; +// try and cut down the amount of session resumes by caching the clients +const clientCache = new Map() + export type BskyLoginParams = { identifier: string; password: string; @@ -25,15 +28,21 @@ export class BskyClient { } public static createAgentFromSession(session: AtpSessionData): BskyClient { - const client = new BskyClient(); - client.agent.resumeSession(session); - client.me = { - did: session.did, - handle: session.handle, - email: session.email, - }; + if (clientCache.has(session.did)) { + return clientCache.get(session.did) + } else { + const client = new BskyClient(); + client.agent.resumeSession(session); + client.me = { + did: session.did, + handle: session.handle, + email: session.email, + }; - return client; + clientCache.set(session.did, client); + + return client; + } } public static async createAgent({ @@ -50,6 +59,9 @@ export class BskyClient { handle: data.handle, email: data.email, }; + + clientCache.set(data.did, client); + return client; }