All files / src/stores reaction.ts

100% Statements 14/14
100% Branches 7/7
100% Functions 3/3
100% Lines 14/14

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            1x 10x 10x   10x   10x 6x 6x 6x   10x 4x 4x 4x   10x 1x  
type ReactionData = Record<string, { likes: number; dislikes: number }>;
type ReactionHandler = (
  id: string,
  value: { likes: number; dislikes: number },
) => void;
 
export const reactionStore = (() => {
  const data: ReactionData = {};
  const listeners = new Set<ReactionHandler>();
 
  const getData = (id: string) => data[id] || null;
 
  const setData = (id: string, value: { likes: number; dislikes: number }) => {
    data[id] = value;
    listeners.forEach((handler) => handler(id, value));
  };
 
  const subscribe = (handler: ReactionHandler): (() => void) => {
    listeners.add(handler);
    return () => listeners.delete(handler);
  };
 
  return { getData, setData, subscribe };
})();