/** * Module definition of {@link Context} property decorators. * * The {@link Context} decorators provides functions to read/write the context * shared among the binary objects during the reading phase. * * @module Context */ import { DecoratorType } from '../types.ts'; import { PropertyMetaDescriptor } from './common.ts'; export declare const CtxSymbol: unique symbol; export type GlobalCtx = object; export type CtxKeyFunction = (targetInstance: This) => string; /** * @category Options */ export declare enum CtxType { CtxGetter = 0, CtxSetter = 1 } /** * @category Options */ export interface CtxOptions { /** * Ensures that a relation exists before defining the Transformer decorator. */ base_type: 'array' | undefined; } /** * @category Options */ export declare const CtxOptionsDefault: { base_type: undefined; }; /** * Ctx metadata type definition. * * @extends {PropertyMetaDescriptor} */ export interface Ctx extends PropertyMetaDescriptor { options: CtxOptions; /** * Function that retrieve the key to access the context */ keyGetter: CtxKeyFunction; /** * Context type: retrieve a value or set a value */ func_type: CtxType; /** * Context */ default_value: Value | undefined; } /** * `@CtxGet` decorator retrieve a value based on the key passed as argument * from a 'context' shared during the reading phase. * * @example * * In the following example, a streaming protocol that receives records * of arbitrary length is defined. * Records have two different type a 'definition' or a 'data' both use an * 'id' to identify themself. The definition defines the size of the data * message they define by using `CtxSet` to store that size into the * context. The data message uses `CtxGet` to fetch its size defined * previously by the definition. * * ```typescript * class RecordDefinition { * @Relation(PrimitiveSymbol.u8) * id: number * * @CtxSet(_ => `Definition.${_.id}`) * @Relation(PrimitiveSymbol.u8) * size: number * } * * class RecordMessage { * @Relation(PrimitiveSymbol.u8) * id: number * * @CtxGet(_ => `Definition.${_.id}`) * _size: number * * @Count('_size') * @Relation(PrimitiveSymbol.u8) * data * } * * class Record { * @Relation(PrimitiveSymbol.u8) * type: number * * @Choice('type', { * 0x00: RecordDefinition, * 0x01: RecordMessage, * }) * message: RecordDefinition | RecordMessage * } * * class Protocol { * @Until(EOF) * records: Record[] * } * ``` * * @param {CtxKeyFunction} keyGetter Either a string formatted as * recursive key or a function that returns that string based on the * instance value. * @param {Value} [defaultValue] Default value to retrieve if no value was * found for the key passed as argument. If no default value is passed an * error will be thrown. * @param {Partial} [opt] Optional configuration. * @returns {DecoratorType} The property decorator function. * * @category Decorators */ export declare function CtxGet(keyGetter: CtxKeyFunction | string, defaultValue?: Value, opt?: Partial): DecoratorType; /** * `@CtxSet` decorator set the value of the decorated property into a * shared 'context' during the reading phase. * * @example * * In the following example, a streaming protocol that receives records * of arbitrary length is defined. * Records have two different type a 'definition' or a 'data' both use an * 'id' to identify themself. The definition defines the size of the data * message they define by using `CtxSet` to store that size into the * context. The data message uses `CtxGet` to fetch its size defined * previously by the definition. * * ```typescript * class RecordDefinition { * @Relation(PrimitiveSymbol.u8) * id: number * * @CtxSet(_ => `Definition.${_.id}`) * @Relation(PrimitiveSymbol.u8) * size: number * } * * class RecordMessage { * @Relation(PrimitiveSymbol.u8) * id: number * * @CtxGet(_ => `Definition.${_.id}`) * _size: number * * @Count('_size') * @Relation(PrimitiveSymbol.u8) * data * } * * class Record { * @Relation(PrimitiveSymbol.u8) * type: number * * @Choice('type', { * 0x00: RecordDefinition, * 0x01: RecordMessage, * }) * message: RecordDefinition | RecordMessage * } * * class Protocol { * @Until(EOF) * records: Record[] * } * ``` * * @param {CtxKeyFunction} keyGetter Either a string formatted as * recursive key or a function that returns that string based on the * instance value. * @param {Partial} [opt] Optional configuration. * @returns {DecoratorType} The property decorator function. * * @category Decorators */ export declare function CtxSet(keyGetter: CtxKeyFunction | string, opt?: Partial): DecoratorType; /** * `@CtxAppend` decorator append the value of the decorated property into a * shared 'context' during the reading phase. * * It works the same way as `@CtxSet` but will append the data to an array * instead of an object property. * * @example * * * ```typescript * class Record { * @CtxAppend('Content') * @Relation(PrimitiveSymbol.u8) * data: number * } * * class Protocol { * @Until(EOF) * records: Record[] * } * ``` * * @param {CtxKeyFunction} keyGetter Either a string formatted as * recursive key or a function that returns that string based on the * instance value. * @param {Partial} [opt] Optional configuration. * @returns {DecoratorType} The property decorator function. * * @category Decorators */ export declare function CtxAppend(keyGetter: CtxKeyFunction | string, opt?: Partial): DecoratorType; /** * useContextGet execute an array of `Ctx` decorator metadata. * * @typeParam This The type of the class the decorator is applied to. * * @param {Array>} metaCtx An array of context function to apply. * @param {This} targetInstance The target class instance containing the property. * @param {GlobalCtx} ctx The shared context reference. * @returns {any} The context retrieved. * * @category Advanced Use */ export declare function useContextGet(metaCtx: Array>, targetInstance: This, ctx: GlobalCtx): any; /** * useContextSet execute an array of `Ctx` decorator metadata. * * @typeParam This The type of the class the decorator is applied to. * * @param {Array>} metaCtx An array of context function to apply. * @param {This} targetInstance The target class instance containing the property. * @param {GlobalCtx} ctx The shared context reference. * @returns {any} The context retrieved. * * @category Advanced Use */ export declare function useContextSet(metaCtx: Array>, propertyValue: any, targetInstance: This, ctx: GlobalCtx): void;