UNPKG

react-antd-admin-panel

Version:

Modern TypeScript-first React admin panel builder with Ant Design 6

95 lines 2.54 kB
/** * Global Store - Reactive key-value store for application state * Supports subscriptions for reactive updates * * @example * const store = new GlobalStore(); * store.set('theme', 'dark'); * store.subscribe('theme', (theme) => console.log('Theme changed:', theme)); * store.get<string>('theme'); // 'dark' */ export class GlobalStore { _data = new Map(); _subscribers = new Map(); /** * Get a value from the store */ get(key) { return this._data.get(key); } /** * Set a value in the store * Notifies all subscribers of the change */ set(key, value) { const prevValue = this._data.get(key); const newValue = typeof value === 'function' ? value(prevValue) : value; this._data.set(key, newValue); this._notifySubscribers(key, newValue); } /** * Remove a value from the store */ remove(key) { this._data.delete(key); this._notifySubscribers(key, undefined); } /** * Clear all values from the store */ clear() { const keys = Array.from(this._data.keys()); this._data.clear(); keys.forEach(key => this._notifySubscribers(key, undefined)); } /** * Check if a key exists in the store */ has(key) { return this._data.has(key); } /** * Get all keys in the store */ keys() { return Array.from(this._data.keys()); } /** * Subscribe to changes for a specific key * Returns an unsubscribe function */ subscribe(key, callback) { if (!this._subscribers.has(key)) { this._subscribers.set(key, new Set()); } this._subscribers.get(key).add(callback); // Return unsubscribe function return () => { const subscribers = this._subscribers.get(key); if (subscribers) { subscribers.delete(callback); if (subscribers.size === 0) { this._subscribers.delete(key); } } }; } /** * Notify all subscribers of a value change */ _notifySubscribers(key, value) { const subscribers = this._subscribers.get(key); if (subscribers) { subscribers.forEach(callback => callback(value)); } } /** * Get the store as a plain object */ toObject() { return Object.fromEntries(this._data); } } //# sourceMappingURL=Store.js.map