UtilitiesStoresUsage

Usage

Using the Store class

index.ts
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
}

Using the NamedStore class

index.ts
import { NamedStores } from "@aezen/stores";

// Create a new NamedStores instance
const namedStores = new NamedStores();

// Create a new store with the name 'numbers'
const numbersStore = namedStores.createStore<number>("numbers");

// Set and get values in the named store
numbersStore.set("key1", 100);
console.log(numbersStore.get("key1")); // Output: 100

// Update the value in the named store
numbersStore.set("key1", 101);
console.log(numbersStore.get("key1")); // Output: 101

// Delete the value from the named store
numbersStore.delete("key1");
console.log(numbersStore.get("key1")); // Output: undefined

// Add and notify a listener in the named store
const namedStoreListener = (newValue: number, oldValue: number | undefined) => {
  console.log(`numbers:key1 changed from ${oldValue} to ${newValue}`);
};
numbersStore.addListener("key1", namedStoreListener);
numbersStore.set("key1", 200); // Output: numbers:key1 changed from undefined to 200

// Remove the listener from the named store
numbersStore.removeListener("key1", namedStoreListener);
numbersStore.set("key1", 201); // No output since the listener has been removed

// Handling non-existing keys in the named store
console.log(numbersStore.get("nonExistingKey")); // Output: undefined

// Preventing empty keys in the named store
try {
  numbersStore.set("", 42);
} catch (error) {
  console.error(error.message); // Output: Key must be non-empty
}

// Preventing duplicate store names
try {
  namedStores.createStore<number>("numbers");
} catch (error) {
  console.error(error.message); // Output: Store with name 'numbers' already exists
}

// Deleting a store
namedStores.deleteStore("numbers");
try {
  namedStores.getStore<number>("numbers");
} catch (error) {
  console.error(error.message); // Output: Store with name 'numbers' does not exist
}

Last updated on