import { Store } from "@aezen/stores";
// Create a new Store instance
const globalStore = new Store();
// Set a value in the store
globalStore.set("key1", 42);
console.log(globalStore.get("key1")); // Output: 42
// Update the value in the store
globalStore.set("key1", 43);
console.log(globalStore.get("key1")); // Output: 43
// Delete the value from the store
globalStore.delete("key1");
console.log(globalStore.get("key1")); // Output: undefined
// Add a listener to a key
const listener = (newValue: number, oldValue: number | undefined) => {
console.log(`key1 changed from ${oldValue} to ${newValue}`);
};
globalStore.addListener("key1", listener);
// Setting a value triggers the listener
globalStore.set("key1", 42); // Output: key1 changed from undefined to 42
// Remove the listener
globalStore.removeListener("key1", listener);
globalStore.set("key1", 43); // No output since the listener has been removed
// Handling non-existing keys
console.log(globalStore.get("nonExistingKey")); // Output: undefined
// Preventing empty keys
try {
globalStore.set("", 42);
} catch (error) {
console.error(error.message); // Output: Key must be non-empty
}