createContextStoreWithLoader()
createContextStoreWithLoader<
Result,Context,Args>(key,loader,options?): readonly [(ctx) =>Promise<Result>, (ctx, ...args) =>void]
Creates a context store for managing asynchronous values within the context object. See https://baeta.io/docs/guides/context-store
Type Parameters
| Type Parameter | Default type |
|---|---|
|
|
‐ |
|
|
|
|
|
|
Parameters
| Parameter | Type | Description |
|---|---|---|
|
|
|
A unique symbol to identify the stored value in the context |
|
|
( |
A function that returns the value for the store |
|
|
Configuration options for the store |
Returns
readonly [(ctx) => Promise<Result>, (ctx, ...args) => void]
A tuple containing get and load functions for managing the stored value
Example
// Create a store for user data
const userStoreKey = Symbol('userStore');
const [getUser, initUserStore] = createContextStoreWithLoader(userStoreKey, (userId: string) => { return fetchUser(userId); }, { lazy: true });
// Initialize the store when you create the context
initUserStore(ctx, userId);
// Later, retrieve the user in a resolver
const user = await getUser(ctx);