def format_queue_events(
    events: EventType[Any] | None = None,
    args_spec: ArgsSpec | None = None,
) -> Var[EventChain]:
    """Format a list of event handler / event spec as a javascript callback.

    The resulting code can be passed to interfaces that expect a callback
    function and when triggered it will directly call queueEvents.

    It is intended to be executed in the rx.call_script context, where some
    existing API needs a callback to trigger a backend event handler.

    Args:
        events: The events to queue.
        args_spec: The argument spec for the callback.

    Returns:
        The compiled javascript callback to queue the given events on the frontend.

    Raises:
        ValueError: If a lambda function is given which returns a Var.
    """
    from reflex.event import (
        EventChain,
        EventHandler,
        EventSpec,
        call_event_fn,
        call_event_handler,
    )
    from reflex.vars import FunctionVar, Var

    if not events:
        return Var("(() => null)").to(FunctionVar, EventChain)

    # If no spec is provided, the function will take no arguments.
    def _default_args_spec():
        return []

    # Construct the arguments that the function accepts.
    sig = inspect.signature(args_spec or _default_args_spec)
    if sig.parameters:
        arg_def = ",".join(f"_{p}" for p in sig.parameters)
        arg_def = f"({arg_def})"
    else:
        arg_def = "()"

    payloads = []
    if not isinstance(events, list):
        events = [events]

    # Process each event/spec/lambda (similar to Component._create_event_chain).
    for spec in events:
        specs: list[EventSpec] = []
        if isinstance(spec, (EventHandler, EventSpec)):
            specs = [call_event_handler(spec, args_spec or _default_args_spec)]
        elif isinstance(spec, type(lambda: None)):
            specs = call_event_fn(spec, args_spec or _default_args_spec)  # pyright: ignore [reportAssignmentType, reportArgumentType]
            if isinstance(specs, Var):
                msg = f"Invalid event spec: {specs}. Expected a list of EventSpecs."
                raise ValueError(msg)
        payloads.extend(format_event(s) for s in specs)

    # Return the final code snippet, expecting queueEvents, processEvent, and socket to be in scope.
    # Typically this snippet will _only_ run from within an rx.call_script eval context.
    return Var(
        f"{arg_def} => {{queueEvents([{','.join(payloads)}], {constants.CompileVars.SOCKET}); "
        f"processEvent({constants.CompileVars.SOCKET})}}",
    ).to(FunctionVar, EventChain)
Format a list of event handler / event spec as a javascript callback.

    The resulting code can be passed to interfaces that expect a callback
    function and when triggered it will directly call queueEvents.

    It is intended to be executed in the rx.call_script context, where some
    existing API needs a callback to trigger a backend event handler.

    Args:
        events: The events to queue.
        args_spec: The argument spec for the callback.

    Returns:
        The compiled javascript callback to queue the given events on the frontend.

    Raises:
        ValueError: If a lambda function is given which returns a Var.
    
reflex/utils/format.py