Review a generated profile store

from Memory management
Node 24 advanced 8 min 4 issues to find

Review this generated store before it is shared by several authenticated views.

Cache at most 100 public profiles per store, coalesce concurrent loads, invalidate on refresh, never retain credentials, and release registrations on dispose.

JavaScript
const cache = new Map();

export function createProfileStore(api, events) {
  events.addEventListener('refresh', (event) => {
    cache.delete(event.detail.id);
  });

  async function getProfile(id, token) {
    if (cache.has(id)) return cache.get(id);
    const response = await api.load(id, token);
    const profile = {
      ...response,
      token,
    };
    cache.set(id, profile);
    return profile;
  }

  return {
    getProfile,
    dispose() {
      cache.clear();
    },
  };
}

generated code is illustrative, not from any one model

Open in playground
Report an error