/** * @fileoverview Centralized reactive state store with pub/sub. * * Wraps a plain data object in a Proxy so that `BB.state.foo = bar` triggers * subscribers. The Proxy approach (vs. explicit getters/setters) keeps the API * concise: any property access reads directly from `data`, any assignment calls * `set()` which notifies listeners. */ (function() { 'use strict'; const subscribers = {}; const data = { sources: [], items: [], currentSource: '', currentOrder: 'chronological', currentSearch: '', currentPage: 0, hasMore: false, selectedItemId: null, selectedPluginId: null, allTags: [], currentTag: '', queryFeeds: [], currentQueryFeed: null, sessionArticlesRead: 0, sessionArticlesStarred: 0, }; /** * Subscribe to changes on a state key. Returns an unsubscribe function. * @param {string} key - State property name to watch. * @param {function(any, any): void} callback - Called with (newValue, oldValue). * @returns {function(): void} Unsubscribe function. */ function subscribe(key, callback) { if (!subscribers[key]) subscribers[key] = []; subscribers[key].push(callback); return () => { subscribers[key] = subscribers[key].filter(cb => cb !== callback); }; } /** * Update a state value and notify all subscribers for that key. * @param {string} key - State property name. * @param {any} value - New value. */ function set(key, value) { const old = data[key]; data[key] = value; if (subscribers[key]) { subscribers[key].forEach(cb => cb(value, old)); } } /** * Read a state value. * @param {string} key - State property name. * @returns {any} Current value. */ function get(key) { return data[key]; } /** * Reset pagination to the first page, optionally clearing selection. * @param {boolean} [clearSelection=false] - Also clear selectedItemId. */ function resetPagination(clearSelection) { set('currentPage', 0); if (clearSelection) set('selectedItemId', null); } // Proxy for direct access: BB.state.sources etc. BB.state = new Proxy(data, { get(target, prop) { if (prop === 'subscribe') return subscribe; if (prop === 'set') return set; if (prop === 'get') return get; if (prop === 'resetPagination') return resetPagination; return target[prop]; }, set(target, prop, value) { set(prop, value); return true; }, }); })();