import type { EvalContext, FeatureMap, FlagEvents } from './types.ts'; /** * FeatureFlagManager * * A lightweight feature flag evaluator with: * - global enable/disable switch per flag * - ordered gate evaluation by priority (desc) * - fail-safe behavior (any unexpected error => disabled) * - evaluation event emission for observability * * @example * const ff = new FeatureFlagManager({ * newUI: { enabled: true, gates: [{ type: 'env', allow: ['prod'] }] } * }); * * ff.onEvaluate(e => console.log(e.feature, e.result)); * * if (ff.isEnabled('newUI', { env: 'prod', version: '1.2.3', userId: 'u1' })) { * // ... * } * * @author dafengzhen */ export declare class FeatureFlagManager { /** * Event bus for emitting evaluation results. */ private readonly bus; /** * Current flags snapshot. * Use {@link setFlags} to update partially. */ private flags; /** * Create a FeatureFlagManager. * * @param flags - Initial feature flags configuration. */ constructor(flags: F); /** * Evaluate whether a feature is enabled for the given context. * * Evaluation rules: * 1) If flag is missing or `enabled === false`, returns false. * 2) Gates are evaluated in descending `priority` order (higher first). * 3) All gates must pass (AND). First failure short-circuits to false. * 4) Any unexpected error => returns false (fail-safe). * 5) Emits an `evaluated` event with the final result. * * @typeParam K - Feature key type. * @param key - Feature name key. * @param ctx - Evaluation context (env/version/userId/experiments...). * @returns `true` if enabled, otherwise `false`. */ isEnabled(key: K, ctx: EvalContext): boolean; /** * Subscribe to evaluation events. * * @param fn - Callback invoked after each evaluation. */ onEvaluate(fn: (e: FlagEvents['evaluated']) => void): void; /** * Run a callback only when the feature is enabled in the given context. * * @param key - Feature name key. * @param ctx - Evaluation context. * @param fn - Callback to execute if enabled. */ run(key: K, ctx: EvalContext, fn: () => void): void; /** * Merge-in new flags config (partial update). * * @param next - Partial feature flags to merge into current config. */ setFlags(next: Partial): void; /** * Emit an evaluation event. * * @param feature - Feature name. * @param result - Evaluation result. * @param ctx - Evaluation context. */ private emitEvaluated; /** * Evaluate a single gate against the context. * * Notes: * - Any unrecognized gate type returns false. * - Keep this function side-effect free. * * @param gate - Gate configuration. * @param ctx - Evaluation context. * @returns Whether the gate passes. */ private evaluateGate; }