Misha Moroshko
Misha Moroshko

Reputation: 171351

How to sampleOn signal and keep the sampled value in Elm?

Consider a model:

model : Signal Model

and the following 2 signals:

clickPosition = Mouse.position
  |> Signal.sampleOn Mouse.clicks

and:

dimensions = Window.dimensions

I'd like to get the following desiredSignal:

            (0,0)       (30,20)               (60,70)
clickPosition +------------+---------------------+---------

              M0   M1                  M2     M3
        model +----+-------------------+------+------------

          (600,800)         (400,300)  (200,800)
   dimensions +-----------------+----------+---------------

              D1           D2                    D3
desiredSignal +------------+---------------------+---------


where:

  D1 = ((0,0), M0, (600,800))
  D2 = ((30,20), M1, (600,800))
  D3 = ((60,70), M3, (200,800))

i.e. I'd like to sampleOn clickPosition, but keep the sampled value.

How could I do that?

Upvotes: 1

Views: 53

Answers (1)

Apanatshka
Apanatshka

Reputation: 5958

It looks like your desiredSignal is the latest value of the three signals, tupled, but only changing on a change from the clickPosition signal. You can do this by first combining them, and then sampling on the clickPosition signal:

desiredSignal =
  Signal.map3 (,,) clickPosition model dimensions
  |> Signal.sampleOn clickPosition

Upvotes: 3

Related Questions