Sending a message from spawned process

67 views
Skip to first unread message

Brian J. Cardiff

unread,
Sep 21, 2016, 1:09:30 AM9/21/16
to elm-d...@googlegroups.com
Hi,

In elm 0.17.1, is it possible to send a message to the app from a spawned process? If so, How? :-) 

Some days ago I failed to do that. I wanted to implement a debounce module. The rough idea was that from a message I want to debounce a spawned process would start. First it will do a sleep and then it should message the app.

Spawning the process is required so in the next message of the user, the previous "setTimeout" process can be killed. The Process.Id is needed.

I was forced to take a different approach. Basically, filtering the outdated "setTimeouts". But I would really like to change that.

In case you want to see more source, the package is: http://package.elm-lang.org/packages/bcardiff/elm-debounce/latest/Debounce . And the spawn branch is as far as I got: spawning the process, getting the Id, but unable to receive a message after the sleep.

Thanks in advance,
--
Brian J. Cardiff - Manas Technology Solutions
[ar.phone] 4796.0232 #BCR(227)
[us.phone] 312.612.1050  #BCR(227)
[email] bcar...@manas.com.ar
[web] www.manas.com.ar
[weblog] http://weblogs.manas.com.ar/bcardiff/

Brian Cardiff

unread,
Sep 21, 2016, 1:33:49 AM9/21/16
to Elm Discuss
On Wednesday, September 21, 2016 at 2:09:30 AM UTC-3, Brian Cardiff wrote:
Hi,

In elm 0.17.1, is it possible to send a message to the app from a spawned process? If so, How? :-) 

Some days ago I failed to do that. I wanted to implement a debounce module. The rough idea was that from a message I want to debounce a spawned process would start. First it will do a sleep and then it should message the app.

Spawning the process is required so in the next message of the user, the previous "setTimeout" process can be killed. The Process.Id is needed.

I was forced to take a different approach. Basically, filtering the outdated "setTimeouts". But I would really like to change that.

In case you want to see more source, the package is: http://package.elm-lang.org/packages/bcardiff/elm-debounce/latest/Debounce . And the spawn branch is as far as I got: spawning the process, getting the Id, but unable to receive a message after the sleep.

Thanks in advance,

For sure any feedback is welcome. If other approach should/could have been taken to debounce events I am all ears, well ... eyes.

Max Goldstein

unread,
Sep 21, 2016, 2:06:25 AM9/21/16
to Elm Discuss
Well for starters you can search for "debounce" on the package catalog and look at what other people have done.

Let's say I'm debouncing the many Msgs that can come in from moving the mouse. I would expect that when the mouse stays put for a supplied duration, I'm given that mouse position.

But after a few minutes of thought, I can't come up with a good non-native API. If I can dream of additions to core, I might want

debounce : Time -> Sub msg -> Sub msg

Which would work great for mouse positions and keep Model and update completely uncluttered, but I'm not sure if it would help all use-cases since it's only for Subs and would require an addition to core.

Brian J. Cardiff

unread,
Sep 21, 2016, 8:29:32 AM9/21/16
to Elm Discuss
Hi Max, 

Yes, I started there :-). There are two other debounce packages registered.
http://package.elm-lang.org/packages/fredcy/elm-debouncer/latest

Basically, all three (counting mine), perform several Process.sleep that are never cancelled, but filtered.
As sometimes happens, the interface of each package matches the vision of the author.

In my case, I wanted the minimum idiomatic expression to go from:

type Msg = UserInput String | ...
view model = ... input [ onInput UserInput ] [] ...

to a debounced event: notify the model, but not too often :-). So

view model = ... input [ onInput (deb1 UserInput) ] [] ...

where deb1 : (a -> Msg) -> (a -> Msg). 

or even debounce from the update instead of the view. These scenarios are supported and I am happy with the interface, but not the implementation.

Going to the outside world, with subscription, on this loop of UserInput seems overkill.
Since all debounce packages use the same underlying technique either that's it (for now?), or there might be another way to address this thing.
Debouncing is important so pages can feel comfortable to the user, but nice to the server also ;-).



--
You received this message because you are subscribed to a topic in the Google Groups "Elm Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elm-discuss/TUEqLPpHVCE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages