SIAF / src /lib /offline-store.ts
GUI-STUDIO
Add FFmpeg v2 routers, libs and offline store
6f9647d
Raw
History Blame Contribute Delete
1.54 kB
// Simple Store using Map
class OfflineStore {
state: Map<any, any>;
subscribers: Set<(state: any) => void>;
constructor() {
this.state = new Map();
this.subscribers = new Set();
}
// Get a value by key
get(key: string) {
return this.state.get(key);
}
// Set a value by key and notify subscribers
set(key: string, value: any) {
this.state.set(key, value);
this.notifySubscribers();
}
// Delete a value by key and notify subscribers
delete(key: string) {
this.state.delete(key);
this.notifySubscribers();
}
// Clear the entire store and notify subscribers
clear() {
this.state.clear();
this.notifySubscribers();
}
// Subscribe to store changes
subscribe(callback: (state: any) => void) {
this.subscribers.add(callback);
return () => this.subscribers.delete(callback);
}
// Notify all subscribers
notifySubscribers() {
this.subscribers.forEach((callback) => {
return callback(this.state);
});
}
}
const offline_store = new OfflineStore();
export { offline_store };
// Example usage
// Subscribe to changes
// const unsubscribe = store.subscribe((state: any[]) => {
// console.log("Store updated:", Array.from(state.entries()));
// });
// Set values
// store.set("user", { name: "Alice", age: 30 });
// store.set("theme", "dark");
// Get a value
// console.log(store.get("user")); // { name: "Alice", age: 30 }
// Delete a value
// store.delete("theme");
// Clear the store
// store.clear();
// Unsubscribe
// unsubscribe();