@2toad/reflex
Version:
A simple approach to state management
95 lines • 4.11 kB
TypeScript
import { Reflex, BackpressureOptions, BackpressureCapable } from "./types";
/**
* Creates a reflex with backpressure handling capabilities.
* The returned object combines both Reflex<T> functionality and backpressure control methods.
*
* Backpressure is a mechanism to handle situations where values are being produced faster
* than they can be consumed. This function provides several strategies to handle such situations:
*
* - Drop: Simply drops values when backpressure is applied
* - Buffer: Stores values in a buffer up to a specified size
* - Sliding: Maintains a buffer of the most recent values, dropping older ones
* - Error: Throws an error when backpressure limit is exceeded
*
* Example usage:
* ```typescript
* const source = reflex({ initialValue: 0 });
* const controlled = withBackpressure(source, {
* strategy: BackpressureStrategy.Buffer,
* bufferSize: 100,
* shouldApplyBackpressure: () => customCondition
* });
*
* // Control flow with pause/resume
* controlled.pause(); // Stop processing values
* controlled.resume(); // Resume and process buffered values
* ```
*
* @param source The source reflex to add backpressure handling to
* @param options Configuration options for backpressure handling:
* - strategy: The backpressure strategy to use (Drop, Buffer, Sliding, Error)
* - bufferSize: Maximum number of values to buffer (default: 1000)
* - shouldApplyBackpressure: Optional function to determine when to apply backpressure
* @returns A Reflex that includes backpressure control methods
*/
export declare function withBackpressure<T>(source: Reflex<T>, options: BackpressureOptions): Reflex<T> & BackpressureCapable;
/**
* Creates a reflex that buffers values for a specified duration.
* Values received during the duration window are collected into an array
* and emitted together when the window closes.
*
* Example usage:
* ```typescript
* const source = reflex({ initialValue: 0 });
* const buffered = buffer(source, 1000); // Buffer for 1 second
*
* // If source emits: 1, 2, 3 within 1 second
* // buffered will emit: [1, 2, 3] after 1 second
* ```
*
* @param source The source reflex to buffer
* @param duration The duration in milliseconds to buffer values
* @returns A Reflex that emits arrays of buffered values
*/
export declare function buffer<T>(source: Reflex<T>, duration: number): Reflex<T[]>;
/**
* Creates a reflex that samples the source reflex at the specified interval.
* The resulting reflex will emit the most recent value from the source
* at each interval, regardless of how many values the source has emitted.
*
* Example usage:
* ```typescript
* const source = reflex({ initialValue: 0 });
* const sampled = sample(source, 1000); // Sample every second
*
* // If source emits rapidly: 1, 2, 3, 4, 5
* // sampled might emit: 1, 3, 5 (at 1-second intervals)
* ```
*
* @param source The source reflex to sample
* @param interval The interval in milliseconds between samples
* @returns A Reflex that emits sampled values at the specified interval
*/
export declare function sample<T>(source: Reflex<T>, interval: number): Reflex<T>;
/**
* Creates a reflex that throttles the source reflex to emit at most once per specified duration.
* The throttled reflex will emit:
* 1. The initial value immediately
* 2. The first value in a new throttle window if it arrives early in the window
* 3. The last value received during the throttle window when the window ends
*
* Example usage:
* ```typescript
* const source = reflex({ initialValue: 0 });
* const throttled = throttle(source, 1000); // Throttle to at most one value per second
*
* // If source emits rapidly: 0, 1, 2, 3, 4, 5
* // throttled might emit: 0 (initial), 1 (first in window), 3 (last in window)
* ```
*
* @param source The source reflex to throttle
* @param duration The minimum time between emissions in milliseconds
* @returns A Reflex that emits throttled values
*/
export declare function throttle<T>(source: Reflex<T>, duration: number): Reflex<T>;
//# sourceMappingURL=backpressure.d.ts.map