File size: 870 Bytes
d0e4d6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
import { createPubSub } from "create-pubsub";

/**
 * Represents a single log entry with timestamp and message
 */
type LogEntry = {
  /** ISO timestamp of when the log entry was created */
  timestamp: string;
  /** The log message content */
  message: string;
  /** Unique identifier for the log entry */
  id: string;
};

/**
 * PubSub instance for managing log entries across the application
 */
export const logEntriesPubSub = createPubSub<LogEntry[]>([]);

const [updateLogEntries, , getLogEntries] = logEntriesPubSub;

/**
 * Adds a new log entry with the current timestamp
 * @param message - The log message to add
 */
export function addLogEntry(message: string) {
  updateLogEntries([
    ...getLogEntries(),
    {
      timestamp: new Date().toISOString(),
      message,
      id: `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
    },
  ]);
}