Documentation / @cashconnect-js/templates-dev / storage/store-inmemory
StoreInMemory
Defined in: packages/templates-dev/src/storage/store-inmemory.ts:9
Implementation of BaseStore using Memory as the storage backend.
Remarks
This implementation can be used testing and caching of expensive operations.
Extends
Constructors
Constructor
new StoreInMemory(): StoreInMemory;Defined in: packages/templates-dev/src/storage/store-inmemory.ts:17
Returns
Overrides
Methods
from()
static from(): StoreInMemory;Defined in: packages/templates-dev/src/storage/store-inmemory.ts:11
Returns
all()
all<T>(): Promise<{
[key: string]: T;
}>;Defined in: packages/templates-dev/src/storage/store-inmemory.ts:23
Retrieves all key-value pairs from the store.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the values in the store |
Returns
Promise<{ [key: string]: T; }>
Overrides
keys()
keys(): Promise<string[]>;Defined in: packages/templates-dev/src/storage/store-inmemory.ts:31
Returns an array of all keys in the store.
Returns
Promise<string[]>
Overrides
get()
get<T>(key): Promise<T>;Defined in: packages/templates-dev/src/storage/store-inmemory.ts:35
Retrieves a value from the store by its key.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the value to retrieve |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key to lookup |
Returns
Promise<T>
Overrides
set()
set<T>(key, value): Promise<void>;Defined in: packages/templates-dev/src/storage/store-inmemory.ts:39
Stores a value in the store with the specified key.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the value to store |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key under which to store the value |
value | T | The value to store |
Returns
Promise<void>
Overrides
delete()
delete(key): Promise<void>;Defined in: packages/templates-dev/src/storage/store-inmemory.ts:44
Removes a value from the store by its key.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key to remove |
Returns
Promise<void>
Overrides
clear()
clear(): Promise<void>;Defined in: packages/templates-dev/src/storage/store-inmemory.ts:49
Removes all key-value pairs from the store.
Returns
Promise<void>
Overrides
has()
has(key): Promise<boolean>;Defined in: packages/templates-dev/src/storage/store-inmemory.ts:54
Checks if a key exists in the store.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key to check |
Returns
Promise<boolean>
Overrides
batchGet()
batchGet<T>(keys): Promise<Map<string, T>>;Defined in: packages/templates-dev/src/storage/base-store.ts:153
Retrieves multiple values from the store by their keys. Filters out undefined values from the results.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the values to retrieve |
Parameters
| Parameter | Type | Description |
|---|---|---|
keys | string[] | Array of keys to retrieve |
Returns
Promise<Map<string, T>>
Inherited from
batchSet()
batchSet<T>(items): Promise<void>;Defined in: packages/templates-dev/src/storage/base-store.ts:173
Stores multiple key-value pairs in the store. Emits 'set' events for each key-value pair.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the values to store |
Parameters
| Parameter | Type | Description |
|---|---|---|
items | Map<string, T> | Map of key-value pairs to store |
Returns
Promise<void>
Inherited from
close()
close(): Promise<void>;Defined in: packages/templates-dev/src/storage/base-store.ts:188
Closes the store connection. Override this method in implementing classes if needed.
Returns
Promise<void>
Inherited from
getOrFail()
getOrFail<T>(key): Promise<T>;Defined in: packages/templates-dev/src/storage/base-store.ts:196
Retrieves a value from the store by its key, throwing an error if the value doesn't exist.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the value to retrieve |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key to lookup |
Returns
Promise<T>
Inherited from
getOrSet()
getOrSet<T>(key, setter): Promise<T>;Defined in: packages/templates-dev/src/storage/base-store.ts:216
Retrieves a value from the store by its key, or sets it using the provided setter function if it doesn't exist.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the value to retrieve or set |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key to lookup |
setter | () => T | Promise<T> | Function to generate the value if it doesn't exist |
Returns
Promise<T>
Inherited from
proxy()
proxy<T>(key): StoreValueProxy<T>;Defined in: packages/templates-dev/src/storage/base-store.ts:245
Creates a wrapper object that provides convenient access to a stored value. The wrapper allows getting and setting the value using async property access.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the value to wrap |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key under which the value is stored |
Returns
An object with a getter and setter for the stored value
Example
const numValue = store.proxy<number>('myNumber');
const value = await numValue.get(); // gets the stored value
await numValue.set(42); // sets the stored valueInherited from
on()
on<K>(
type,
listener,
debounceMilliseconds?): OffCallback;Defined in: packages/core/dist/primitives.d.ts:593
Registers a listener for the specified event type.
Type Parameters
| Type Parameter | Description |
|---|---|
K extends keyof StoreEvents | The event type key |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | K | The event type to listen for |
listener | Listener<StoreEvents[K]> | The function to call when the event is emitted |
debounceMilliseconds? | number | Optional debounce time in milliseconds |
Returns
OffCallback
A callback function that can be used to unregister this listener
Example
const off = emitter.on('data-change', (data) => {
console.log('Data updated:', data);
}, 300); // Debounce for 300ms
// Later, unregister the listener
off();Inherited from
once()
once<K>(
type,
listener,
debounceMilliseconds?): OffCallback;Defined in: packages/core/dist/primitives.d.ts:611
Registers a one-time listener for the specified event type. The listener will be automatically removed after being called once.
Type Parameters
| Type Parameter | Description |
|---|---|
K extends keyof StoreEvents | The event type key |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | K | The event type to listen for |
listener | Listener<StoreEvents[K]> | The function to call when the event is emitted |
debounceMilliseconds? | number | Optional debounce time in milliseconds |
Returns
OffCallback
A callback function that can be used to unregister this listener before it fires
Example
emitter.once('user-login', ({ userId }) => {
console.log(`Welcome ${userId}! This will only show once.`);
});Inherited from
off()
off<K>(type, listener): void;Defined in: packages/core/dist/primitives.d.ts:626
Removes a specific listener for the given event type.
Type Parameters
| Type Parameter | Description |
|---|---|
K extends keyof StoreEvents | The event type key |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | K | The event type to remove the listener from |
listener | Listener<StoreEvents[K]> | The listener function to remove |
Returns
void
Example
const handler = (data) => console.log(data);
emitter.on('test', handler);
emitter.off('test', handler); // Removes the listenerInherited from
emit()
emit<K>(type, payload): boolean;Defined in: packages/core/dist/primitives.d.ts:648
Emits an event of the specified type with the given payload. All registered listeners for this event type will be called.
Type Parameters
| Type Parameter | Description |
|---|---|
K extends keyof StoreEvents | The event type key |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | K | The event type to emit |
payload | StoreEvents[K] | The data to pass to all listeners |
Returns
boolean
true if there were listeners for this event, false otherwise
Example
const hasListeners = emitter.emit('user-login', {
userId: '123',
timestamp: Date.now()
});
if (!hasListeners) {
console.log('No one was listening for user-login events');
}Inherited from
removeAllListeners()
removeAllListeners(): void;Defined in: packages/core/dist/primitives.d.ts:659
Removes all listeners for all events. This effectively resets the EventEmitter to its initial state.
Returns
void
Example
emitter.removeAllListeners();
// All previously registered listeners are now goneInherited from
waitFor()
waitFor<K>(
type,
predicate,
timeoutMs?): Promise<StoreEvents[K]>;Defined in: packages/core/dist/primitives.d.ts:685
Returns a Promise that resolves when an event of the specified type is emitted and matches the given predicate.
Type Parameters
| Type Parameter | Description |
|---|---|
K extends keyof StoreEvents | The event type key |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | K | The event type to wait for |
predicate | (payload) => boolean | A function that determines if the event payload matches what we're waiting for |
timeoutMs? | number | Optional timeout in milliseconds. If specified, the promise will reject after this time |
Returns
Promise<StoreEvents[K]>
A Promise that resolves with the event payload when the condition is met
Throws
If the timeout is reached before a matching event occurs
Example
try {
const loginData = await emitter.waitFor(
'user-login',
({ userId }) => userId === 'admin',
5000 // Wait up to 5 seconds
);
console.log('Admin logged in:', loginData);
} catch (error) {
console.log('Timeout: Admin never logged in');
}