I recently read Jasper Woudenberg’s article about drag-and-drop and found it really insightful.
To accomplish better drag and drop he needed some external javascript to keep track of where elements currently are.
I had the idea to only check where elements are on the start of a drag action, which has the additional benefit of not having weird side-cases where drag actions impact the width of elements which impacts the drag action again, etc.
Since I would only measure the elements once, it would be possible to use Browser.Dom.getElement
(side note: would measureElement
be a better name than getElement
?).
This turned out to be problematic: It seems tasks are executed in a requestAnimationFrame
, and if I Task.map2
, the tasks are executed in order, but in individual animation frames.
There was a discussion about something similar previously:
Since it is unfortunately not possible to execute tasks in parallel, measuring e.g. 60 elements for drag-and-drop in a list takes 60 animation frames: in most cases 1 second.
In the same topic, Evan writes this:
My current feeling is that it might want an abstraction of its own, and the starting point there is to get a couple real examples where people feel this might be the best path.
So with the above description, I hoped to have contributed a real-world example, and I asynchronous Task.map2
or similar would be the best solution.
Current alternatives include:
- Using javascript.
As I currently only want to get information from the javascript side to the elm side, I need to use a subscription port. But I also want it to only send information, when I start dragging a specific DOM element, but communicating this to a subscription port is difficult (am I correct?). - Using a
Cmd.batch
.
If I understood this correctly,Cmd.batch
would run both tasks in a single animation frame (am I correct?). However, this would mean a lot of additional complexity in myMsg
andModel
datatype, which would be very hard to abstract out into a library or similar and feels like accidental complexity.
I realize I don’t really have a call-to-action. Maybe you had similar issues in the past? Do you agree with me? Did I miss something?
Thank you!