Raxol.Animation.StateManager (Raxol v2.0.1)

View Source

Refactored StateManager that delegates to GenServer implementation.

This module provides the same API as the original Animation.StateManager but uses a supervised GenServer instead of the Process dictionary for state management.

Migration Notice

This module is a drop-in replacement for Raxol.Animation.StateManager. All functions maintain backward compatibility while providing improved fault tolerance and functional programming patterns.

Benefits over Process Dictionary

  • Supervised state management with fault tolerance
  • Pure functional transformations
  • Better debugging and testing capabilities
  • Clear separation of concerns
  • No global state pollution
  • Support for batch operations for better performance

Summary

Functions

Batch updates multiple active animations at once.

Clears all animation state (used primarily for testing or reset).

Gets the count of active animations across all elements.

Ensures the Animation StateServer is started. Called automatically when using any function.

Retrieves a specific active animation instance for a given element and animation name.

Retrieves all active animations.

Retrieves an animation definition by name.

Gets all animations for a specific element.

Retrieves the animation framework settings.

Checks if an element has any active animations.

Initializes the animation state storage.

Lists all element IDs that have active animations.

Stores an active animation instance for a given element.

Stores an animation definition.

Removes a completed or stopped animation instance for a specific element.

Removes all animations for a specific element.

Functions

batch_update_active_animations(updates)

Batch updates multiple active animations at once.

This is more efficient than multiple individual calls when updating many animations in a single frame.

Parameters

  • updates: A list of update operations, each being either:
    • {:put, element_id, animation_name, instance} to add/update an animation
    • {:remove, element_id, animation_name} to remove an animation

Example

StateManager.batch_update_active_animations([
  {:put, "elem1", "fade", %{opacity: 0.5}},
  {:put, "elem2", "slide", %{x: 100}},
  {:remove, "elem3", "rotate"}
])

clear_all()

Clears all animation state (used primarily for testing or reset).

WARNING: This will remove all animation definitions and active animations. Use with caution in production environments.

count_active_animations()

Gets the count of active animations across all elements.

ensure_started()

Ensures the Animation StateServer is started. Called automatically when using any function.

get_active_animation(element_id, animation_name)

Retrieves a specific active animation instance for a given element and animation name.

Returns nil if no matching animation is found.

get_active_animations()

Retrieves all active animations.

Returns a map of {element_id, %{animation_name => instance}}.

get_animation(animation_name)

Retrieves an animation definition by name.

Returns nil if no animation with the given name exists.

get_element_animations(element_id)

Gets all animations for a specific element.

Returns a map of animation_name => instance for the given element, or an empty map if the element has no active animations.

get_settings()

Retrieves the animation framework settings.

Returns the current animation settings from the GenServer state.

has_active_animations?(element_id)

Checks if an element has any active animations.

init(settings)

Initializes the animation state storage.

This function provides backward compatibility with the original StateManager. It delegates to the GenServer implementation.

list_animated_elements()

Lists all element IDs that have active animations.

put_active_animation(element_id, animation_name, instance)

Stores an active animation instance for a given element.

Parameters

  • element_id: The ID of the element being animated
  • animation_name: The name of the animation
  • instance: The animation instance data

put_animation(animation)

Stores an animation definition.

The animation must have a name field which will be used as the key.

remove_active_animation(element_id, animation_name)

Removes a completed or stopped animation instance for a specific element.

This is typically called when an animation completes or is cancelled.

remove_element_animations(element_id)

Removes all animations for a specific element.

This is useful when an element is being destroyed or reset.