PageSourceSearch

https://smrtbld.ai/js/utils/pubsub.js

js smrtbld.ai collected 2026-10-05 15:08:23 UTC 1,398 bytes, 59 lines download raw bytes

1/**
2 * PubSub (Event Bus)
3 * Simple publish/subscribe pattern for loose coupling.
4 */
5
6const handlers = new Map();
7
8export const pubsub = {
9    /**
10     * Subscribe to an event
11     * @param {string} eventName 
12     * @param {Function} fn 
13     * @returns {Function} unsubscribe function
14     */
15    subscribe(eventName, fn) {
16        if (!handlers.has(eventName)) {
17            handlers.set(eventName, new Set());
18        }
19        handlers.get(eventName).add(fn);
20
21        // Return unsubscribe function
22        return () => {
23            if (handlers.has(eventName)) {
24                handlers.get(eventName).delete(fn);
25            }
26        };
27    },
28
29    /**
30     * Publish an event
31     * @param {string} eventName 
32     * @param {any} data 
33     */
34    publish(eventName, data) {
35        if (!handlers.has(eventName)) return;
36        handlers.get(eventName).forEach(fn => {
37            try {
38                fn(data);
39            } catch (err) {
40                console.error(`[PubSub] Error in handler for ${eventName}:`, err);
41            }
42        });
43    },
44
45    /**
46     * Clear all subscribers for an event (or all events)
47     * @param {string} [eventName] 
48     */
49    clear(eventName) {
50        if (eventName) {
51            handlers.delete(eventName);
52        } else {
53            handlers.clear();
54        }
55    }
56};
57
58// Global access for legacy/debugging
59window.pubsub = pubsub;

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.