Search code examples
elmelm-signal

How to Debug.log a signal in Elm?


I'm trying to introduce a new signal into my program which main function looks like this:

main : Signal Html
main =
  Signal.map2 view Window.dimensions model

(more context here)

To design my new signal I'd like to start from this one:

clicks : Signal (Int, Int)
clicks =
  Signal.sampleOn Mouse.clicks Mouse.position

I can get this signal:

Signal.map (Debug.log "click") clicks

But, I'm not sure how to integrate this signal into my program such that it will print a debug message on every click.

What's the simplest way to Debug.log a new signal in an existing Elm program?


Solution

  • Wherever you want to use this clicks signal, you can use (Signal.map (Debug.log "click") clicks) instead.

    Since this seems to be another input, you can add a MouseClick action, and Signal.merge the mouse click actions into the general input signal that you have. If that's the place where you want to add your mouse clicks, then that's the place where you add the logging.