Registry
This file implements a global Registry
class for storing and retrieving values by key. The registry is globalized, ensuring all packages using this library share the same instance, enabling cross-package communication and data sharing.
Features
- Global access: All packages share the same registry instance.
- CRUD operations: Set, get, check, remove, and clear values by key.
- Debugging: Optional debug logging for registry operations.
- Search and export/import: Search for keys, export registry data, and import data into the registry.
Usage
Registry.set('myKey', 'myValue');
const value = Registry.get<string>('myKey');
const exists = Registry.has('myKey');
Registry.remove('myKey');
Registry.clear();
API
set(key: string, value: any, overwrite = false): void
Stores a value by key. Throws an error if the key exists and overwrite
is false
.
get<T = any>(key: string): T | undefined
Retrieves a value by key. Returns undefined
if the key does not exist.
has(key: string): boolean
Checks if a key exists in the registry.
remove(key: string): void
Removes a key and its value from the registry.
keys(): string[]
Returns an array of all keys in the registry.
clear(): void
Removes all entries from the registry.
search(query?: string): string[]
Returns an array of keys that include the optional query
string.
all(): any[]
Returns an array of all values in the registry.
export(): string
Exports the registry as a JSON string. Note: Only serializable values are exported; modules and injection references are lost.
import(data: Record<string, any>): void
Imports a plain object into the registry, overwriting any existing keys with the same name.
setDebug(debug: boolean): void
Enables or disables debug logging for registry operations.
Globalization
The registry is attached to globalThis['@sodacore:registry']
to ensure a single shared instance across all packages.