@fjell/lib
Version:
Server-side Library for Fjell
81 lines (80 loc) • 2.62 kB
TypeScript
import { ComKey, Item, PriKey } from "@fjell/types";
export type ItemKey = PriKey<any> | ComKey<any, any, any, any, any, any>;
/**
* OperationContext for managing state across asynchronous operations.
* Used to:
* - Detect circular dependencies during reference/aggregation loading
* - Cache loaded items to avoid duplicate work
* - Share state across nested operations
*/
export interface OperationContext {
/**
* Set of serialized keys that are currently being processed.
* Used to detect circular dependencies.
*/
inProgress: Set<string>;
/**
* Cache of fully loaded objects keyed by serialized key.
* Used to avoid duplicate work and provide already-loaded objects.
* Can store individual items, arrays, or any other values.
*/
cache: Map<string, any>;
/**
* Add a key to the in-progress set
*/
markInProgress(key: ItemKey): void;
/**
* Remove a key from the in-progress set
*/
markComplete(key: ItemKey): void;
/**
* Check if a key is currently being processed (cycle detection)
*/
isInProgress(key: ItemKey): boolean;
/**
* Get a cached object by key
*/
getCached(key: ItemKey): Item<any, any, any, any, any, any> | undefined;
/**
* Cache a loaded object by key
*/
setCached(key: ItemKey, item: Item<any, any, any, any, any, any>): void;
/**
* Check if an object is cached
*/
isCached(key: ItemKey): boolean;
}
/**
* @deprecated Use OperationContext instead
*/
export type ProcessingContext = OperationContext;
/**
* Serialize an ItemKey to a string for use in sets and maps
*/
export declare const serializeKey: (key: ItemKey) => string;
/**
* Create a new OperationContext
*/
export declare const createOperationContext: () => OperationContext;
/**
* @deprecated Use createOperationContext instead
*/
export declare const createProcessingContext: () => OperationContext;
/**
* Context Manager for sharing context across operations without changing public interfaces
* Uses AsyncLocalStorage to properly maintain context across async boundaries
*/
declare class ContextManager {
private asyncLocalStorage;
/**
* Get the current context if one is set
*/
getCurrentContext(): OperationContext | undefined;
/**
* Execute a function with a specific context set as current
* The context will be available to all async operations within the function
*/
withContext<T>(context: OperationContext, fn: () => Promise<T>): Promise<T>;
}
export declare const contextManager: ContextManager;
export {};