Interface IKosTopicHandlerParams<Response, Model, TransformedResponse>

Configuration parameters for defining a Kos topic handler function within a Kos Data Model class.

Typeparam

Response - The type of the raw response payload received from the topic.

Typeparam

Model - The type of the Kos Data Model to which the topic handler belongs.

Typeparam

TransformedResponse - The type of the transformed response payload after applying the transformation function.

interface IKosTopicHandlerParams<Response, Model, TransformedResponse> {
    topic: string | string[];
    condition?: ((payload, model, raw) => boolean);
    transform?: ((payload) => TransformedResponse);
    websocket?: boolean;
    debounce?: number | {
        delay: number;
        discardIntermediate?: boolean;
    };
    throttle?: number | {
        interval: number;
        discardIntermediate?: boolean;
    };
    buffer?: {
        time: number;
        maxSize?: number;
    };
    filter?: ((payload) => boolean);
    once?: boolean;
    replay?: {
        bufferSize: number;
    };
    wildcardName?: string;
    flow?: FlowControlOptions<Response, TransformedResponse>;
    requiresBaseline?: BaselineDependency;
}

Type Parameters

  • Response = any
  • Model extends IKosDataModel = any
  • TransformedResponse = Response

Properties

topic: string | string[]

The topic or array of topics to which the handler should respond.

condition?: ((payload, model, raw) => boolean)

A conditional function that determines whether the handler should be invoked based on the received payload and the Kos Data Model instance. If not provided, the handler will always be invoked when a matching topic is received.

Type declaration

    • (payload, model, raw): boolean
    • Parameters

      • payload: TransformedResponse

        The transformed payload received from the topic.

      • model: Model

        The Kos Data Model instance to which the topic handler belongs.

      • raw: Response

      Returns boolean

Returns

true if the handler should be invoked, false otherwise.

transform?: ((payload) => TransformedResponse)

A transformation function that converts the raw response payload into a desired format. If not provided, the payload will be used as is.

Type declaration

Returns

The transformed payload in the desired format.

websocket?: boolean

Indicates whether the topic handler should listen for WebSocket messages. If set to true, the handler will be invoked when WebSocket messages with matching topics are received. Default is false.

debounce?: number | {
    delay: number;
    discardIntermediate?: boolean;
}

Debounce events - only emit after a period of inactivity

Type declaration

  • delay: number
  • Optional discardIntermediate?: boolean

    If true, only keeps the last value. If false (default), accumulates all values

Example

debounce: 1000 // Wait 1 second after last event

Example

debounce: { delay: 1000, discardIntermediate: true } // Only keep last value
throttle?: number | {
    interval: number;
    discardIntermediate?: boolean;
}

Throttle events - limit emissions to once per interval

Type declaration

  • interval: number
  • Optional discardIntermediate?: boolean

    If true, only keeps the first value in window. If false (default), accumulates all values

Example

throttle: 500 // At most once every 500ms

Example

throttle: { interval: 500, discardIntermediate: true } // Only keep first value in window
buffer?: {
    time: number;
    maxSize?: number;
}

Buffer events and emit them as an array after time window or max size

Type declaration

  • time: number

    Time window to collect events (ms)

  • Optional maxSize?: number

    Maximum number of events to buffer

filter?: ((payload) => boolean)

Filter events based on a predicate function Note: This operates on the raw payload before transformation

Type declaration

    • (payload): boolean
    • Parameters

      Returns boolean

once?: boolean

Only handle the event once, then automatically unsubscribe

replay?: {
    bufferSize: number;
}

Replay the last N events to new handlers

Type declaration

  • bufferSize: number

    Number of events to remember and replay

wildcardName?: string

Named wildcard capture for trailing /* patterns. When enabled, the portion after the /* is parsed and passed to the handler. You can optionally specify a name for the captured segment.

Example

@kosTopicHandler({
topic: "/config/system/*",
websocket: true,
wildcardName: "configPath"
})
handleConfigUpdate(payload: ConfigData, raw: ApiCallbackWithWildcard<any>) {
// raw.wildcardCapture.configPath contains everything after "/config/system/"
}
flow?: FlowControlOptions<Response, TransformedResponse>

Advanced flow control options using async iterators internally. Enables sophisticated event processing patterns like batching, backpressure, on-the-fly transforms, and rate limiting.

Example

@kosTopicHandler({
topic: 'sensor.data',
websocket: true,
flow: {
batch: { size: 50, window: 1000 },
transform: (events) => aggregateEvents(events),
backpressure: { maxConcurrent: 3 }
}
})
handleProcessedEvents(aggregatedData) {
// Receives transformed, batched data with backpressure control
}
requiresBaseline?: BaselineDependency

Baseline dependency configuration for baseline-delta race condition handling. When specified, this handler will wait for the baseline service request to complete before processing events, and will intelligently replay queued events based on temporal comparison with the baseline request/response timing.

Example

@kosTopicHandler({
topic: TOPIC_INGREDIENT_UPDATE,
websocket: true,
lifecycle: DependencyLifecycle.READY,
requiresBaseline: {
path: PATH_INGREDIENTS,
method: 'get',
replayStrategy: 'after-request'
}
})
handleIngredientUpdate(payload: IngredientUpdatePayload) {
// Framework guarantees:
// 1. Baseline loaded before this handler executes
// 2. Queued events replayed with temporal filtering
// 3. Only events newer than baseline are processed
}