diff --git a/packages/sdk/src/social/page.ts b/packages/sdk/src/social/page.ts index 0049a1b..f7f2933 100644 --- a/packages/sdk/src/social/page.ts +++ b/packages/sdk/src/social/page.ts @@ -47,12 +47,26 @@ export async function actorPage(identifier: string): Promise { return { actor, profile, projects, tangledRepos }; } -/** Just an account's identity and profile, for showing who someone is without loading their page. */ +/** + * Just an account's identity and profile, for showing who someone is without loading their page. + * `describe()` (sign-in, restore, the account switcher) and the actor page both call this for the + * same DID on a typical navigation, so it is cached here, per DID, for as long as this module stays + * loaded (effectively the page's lifetime in a browser): concurrent callers for the same DID share + * one in-flight lookup, a failed lookup is dropped so the next call retries, and `setProfile` forgets + * the session's own entry so the next read after a save sees what it just wrote. + */ export async function actorProfile(identifier: string): Promise> { const actor = await resolveActor(identifier); - return { actor, profile: await profileOf(repoClient(actor.did, actor.pds), actor.did) }; + const cached = profileCache.get(actor.did); + if (cached) return cached; + const lookup = profileOf(repoClient(actor.did, actor.pds), actor.did).then((profile) => ({ actor, profile })); + profileCache.set(actor.did, lookup); + lookup.catch(() => profileCache.delete(actor.did)); + return lookup; } +const profileCache = new Map>>(); + /** `dev.nibling.actor.profile`, with each field falling back to `app.bsky.actor.profile`. */ async function profileOf(client: Client, did: Did): Promise { const [nibling, bsky] = await Promise.all([ @@ -83,6 +97,8 @@ export async function setProfile( input: { repo: session.did, collection: PROFILE, rkey: 'self', record }, }); if (!res.ok) throw new SdkError(`setProfile failed: ${res.data.error}`, 'ERR_NIB_RECORD_INVALID'); + // The next actorProfile() for this account should see what was just written, not a stale cache entry. + profileCache.delete(session.did); } async function readSelfRecord( diff --git a/packages/sdk/tests/social-page.test.ts b/packages/sdk/tests/social-page.test.ts index b50bade..f3c263a 100644 --- a/packages/sdk/tests/social-page.test.ts +++ b/packages/sdk/tests/social-page.test.ts @@ -54,3 +54,50 @@ test('actorPage works for a tangled org, an ordinary account with repo declarati const page = await actorPage(org); assert.deepEqual(page.tangledRepos, [{ name: 'widget', repoDid: 'did:plc:widgetrepo' }]); }); + +test('actorProfile caches per DID: concurrent calls share one lookup, a later call reuses it', async () => { + const bob = pds.createAccount('bob-cache'); + pds.resetCounts(); + + const [a, b] = await Promise.all([actorProfile(bob), actorProfile(bob)]); + assert.equal(pds.count('com.atproto.repo.listRecords'), 2); // nibling profile + bsky profile, once + assert.deepEqual(a, b); + + const c = await actorProfile(bob); + assert.equal(pds.count('com.atproto.repo.listRecords'), 2); // no new request on a later call + assert.deepEqual(c, a); +}); + +test('actorProfile drops a failed lookup so the next call retries', async () => { + const flaky = pds.createAccount('flaky'); + const session = pds.session(flaky); + // A record to list, so listRecords' response actually goes through the tampered URI below. + await session.client.post('com.atproto.repo.createRecord', { + input: { repo: flaky, collection: 'dev.nibling.actor.profile', rkey: 'self', record: { $type: 'dev.nibling.actor.profile', displayName: 'Flaky' } }, + }); + + pds.tamper.uri = () => { + throw new Error('simulated PDS failure'); + }; + try { + await assert.rejects(actorProfile(flaky)); + } finally { + pds.tamper.uri = undefined; + } + + const page = await actorProfile(flaky); + assert.equal(page.actor.did, flaky); + assert.equal(page.profile.displayName, 'Flaky'); +}); + +test('setProfile invalidates the cache so a later actorProfile sees the write', async () => { + const carol = pds.createAccount('carol-cache'); + const session = pds.session(carol); + + const before = await actorProfile(carol); + assert.equal(before.profile.displayName, undefined); + + await setProfile(session, { displayName: 'Carol' }); + const after = await actorProfile(carol); + assert.equal(after.profile.displayName, 'Carol'); +});