Avataaars API Documentation - v3.1.0
    Preparing search index...

    Class OptionContext

    OptionContext manages the state of all avatar customization options.

    This class provides:

    • Centralized state management for all avatar options
    • Publish-subscribe pattern for reactive updates
    • Default value handling
    • Option availability tracking
    const context = new OptionContext(allOptions);
    context.setValue("topType", "LongHairStraight");
    context.getValue("topType"); // "LongHairStraight"
    Index

    Constructors

    Accessors

    Methods

    • Add a listener for state changes. Called whenever any option state is updated.

      Parameters

      • listener: () => void

        Callback function to invoke on state changes

      Returns void

    • Remove a state change listener.

      Parameters

      • listener: () => void

        Callback function to remove

      Returns void

    • Add a listener for value changes. Called whenever a specific option value is updated.

      Parameters

      • listener: (key: OptionKey, value: string) => void

        Callback function receiving (key, value) on changes

      Returns void

    • Remove a value change listener.

      Parameters

      • listener: (key: OptionKey, value: string) => void

        Callback function to remove

      Returns void

    • Increment the availability counter for an option. Used to track how many components are using this option.

      Parameters

      • key: OptionKey

        The option key to increment availability for

      Returns void

    • Decrement the availability counter for an option. Used when a component stops using this option.

      Parameters

      • key: OptionKey

        The option key to decrement availability for

      Returns void

    • Get the current value for a specific option. Returns the set value, or the default value if no value is set.

      Parameters

      • key: OptionKey

        The option key to get value for

      Returns string | null

      Current value, default value, or null

    • Set the value for a specific option. Triggers value change listeners and state change notification.

      Parameters

      • key: OptionKey

        The option key to set value for

      • value: string

        The value to set

      Returns void

    • Set multiple option values at once. Useful for bulk updates or initialization.

      Parameters

      • data: Partial<Record<OptionKey, string>>

        Object mapping option keys to values

      Returns void

    • Set the default value for an option. Used when no explicit value is set.

      Parameters

      • key: OptionKey

        The option key to set default for

      • defaultValue: string

        The default value to use

      Returns void

    • Set the available option values for a specific option key. Used by Selector components to register their available options.

      Parameters

      • key: OptionKey

        The option key to set available options for

      • options: readonly string[]

        Array of available option values

      Returns void