All files / src/stores sentiment.ts

100% Statements 18/18
100% Branches 9/9
100% Functions 4/4
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32                1x 45x 45x   45x   45x 10x 10x 10x   45x 3x 3x 3x   45x 5x 5x 5x   45x 1x  
// Live comment sentiment pushed over the lunch-menu socket after the backend
// finishes async classification. Keyed by commentId (NOT menuId): a privileged
// subscriber receives classify events for every comment in a menu, so keying by
// menuId would overwrite the wrong comment. Matching by commentId keeps each
// subscriber's own comment correct regardless of privilege.
type SentimentData = Record<string, string>;
type SentimentHandler = (commentId: string, sentiment: string | null) => void;
 
export const sentimentStore = (() => {
  const data: SentimentData = {};
  const listeners = new Set<SentimentHandler>();
 
  const getData = (commentId: string) => data[commentId] || null;
 
  const setData = (commentId: string, sentiment: string) => {
    data[commentId] = sentiment;
    listeners.forEach((handler) => handler(commentId, sentiment));
  };
 
  const clearData = (commentId: string) => {
    delete data[commentId];
    listeners.forEach((handler) => handler(commentId, null));
  };
 
  const subscribe = (handler: SentimentHandler): (() => void) => {
    listeners.add(handler);
    return () => listeners.delete(handler);
  };
 
  return { getData, setData, clearData, subscribe };
})();