Context Api
Context API
Written for AI agents. See Log Methodology Note below for details.
Jay has an internal Context API based on the constructionContextStack in runtime.ts, runtime package.
The constructionContextStack manages a stack of ConstructContext which are used for the creation of
jay elements, passing information as a context.
ConstructContextis created at the root of elements treeConstructContextis also created for eachforEachelement in the treeConstructContextholds the information forrefManager- for creating and managing refsdata- the current View State in context, which changes inforEachfor the data itemforStaticElements- are we in the context of static elements, or dynamic (as in children offorEach)
Jay has a second internal context API based on componentContextStack in component.ts, component package.
The componentContextStack manages ComponentContext for jay hooks.
ComponentContextis created at the root of each component.ComponentContextholds the information forreactive- the reactive state management for the componentmounts,unmounts- mount and unmount callbacks forcreateEffect, called from the component exposedmountandunmount.
Proposed Context API
The proposed API is to be based on the constructionContextStack in runtime.ts, runtime package infra,
exposing the same context to other higher level abstractions including components and secure packages.
Proposed API
interface JayContext<ContextType> {}
/**
* creates a context marker
*/
declare function createContext<ContextType = unknown>(): JayContext<ContextType>;
/**
* provides context to child stack frames of the `callback` parameter
* If a context already exists for the same marker, this one becomes the current one
* @param context - the context marker
* @param value - the context value
* @param callback - the stack frames to provide the context for
*/
declare function provideContext<ContextType>(
context: JayContext<ContextType>,
value: ContextType,
callback: () => void,
);
/**
*
* @param context
*/
declare function useContext<ContextType>(context: JayContext<ContextType>): ContextType;
declare function useOptionalContext<ContextType>(
context: JayContext<ContextType>,
): ContextType | undefined;
declare function useParentContext<ContextType>(context: JayContext<ContextType>): ContextType;
Example usage:
interface ComponentContext {
reactive: Reactive;
mounts: MountFunc[];
unmounts: MountFunc[];
}
const COMPONENT_CONTEXT = createContext<ComponentContext>();
provideContext(COMPONENT_CONTEXT, {}, () => {
// ... under some deep call stack
let componentContext = useContext(COMPONENT_CONTEXT);
});
Final API
export interface ContextMarker<ContextType> {}
export declare function createJayContext<ContextType = unknown>(): ContextMarker<ContextType>;
export declare function provideContext<ContextType, Returns>(
marker: ContextMarker<ContextType>,
context: ContextType,
callback: () => Returns,
): Returns;
export declare function useContext<ContextType>(marker: ContextMarker<ContextType>): ContextType;
export declare function useOptionalContext<ContextType>(
marker: ContextMarker<ContextType>,
): ContextType | undefined;
Log Methodology Note
Note: These design logs are written primarily for AI agents as part of the Design Log methodology and made accessible here for human readers. The language and structure are optimized for machine consumption — expect precise, specification-style prose rather than narrative documentation.