Normalises the event-handler props that components accept.
MapLibre events reach Elixir through JavaScript's Hologram.dispatchAction(), which needs
three things: an action name, a target component id, and a params map. Event
props therefore accept the same shapes Hologram's own longhand event bindings
do, so there is one syntax to remember:
# Bare action name, dispatched to the page.
on_click={:parcel_clicked}
# Longhand, when the handler lives on a component rather than the page.
on_click={%{action: :parcel_clicked, target: "sidebar"}}
# With static params merged into the payload.
on_click={%{action: :parcel_clicked, target: "sidebar", params: %{layer: "parcels"}}}A keyword list works wherever a map does.
Throttling and debouncing
Some events come many times a second: on_mouse_move, and the camera
events MapLibre fires on every frame of a move, such as on_move. A
handler map can limit how often its action is dispatched, in
milliseconds:
# At most one action every 100 ms, and the last event always.
on_move={%{action: :camera_moving, throttle: 100}}
# One action, once the events have stopped for 250 ms.
on_mouse_move={%{action: :pointer_rested, debounce: 250}}throttle: ms dispatches the first event at once and opens an interval of
ms. The events that come during it are held, each replacing the one
before, and the one held when the interval ends is dispatched then, which
opens the next interval. So at most one action is dispatched per interval,
and the last event of a burst always is, at most ms after it came: the
application always receives where the camera, the pointer or the marker
ended up. debounce: ms dispatches nothing while events keep coming, and
the last one once none has come for ms.
Either works on every event prop, with the same meaning, and the payload is
the one the dispatched event carried when it came. 0 dispatches every
event, and a handler can carry one of the two, not both. A held event is
dropped when its handler is taken away or replaced, and when the map goes.
An event the runtime filters out, such as a camera move it made itself,
is neither dispatched nor counted.
HoloMap.Map's camera events that MapLibre fires on every frame of a
move, on_move, on_zoom, on_rotate, on_pitch, on_roll and
on_drag, and on_render, which it fires on every frame it draws, are
throttled to 100 ms unless the handler names a throttle or a debounce
of its own. Every other event prop dispatches every event unless its
handler asks otherwise.
Why the default target is the page
Hologram defaults an event binding to the closest enclosing stateful
component. HoloMap's children are deliberately stateless, with no
cid of their own, so there is no such component to fall back on, and
silently targeting the map container would send the action to HoloMap.Map
rather than to the caller's handler. "page" is both the common case and the
one target that always exists.
Event payload
Whatever params you supply is merged under the runtime payload, so the
data MapLibre reports always wins over a static value with the same key. The
payload keys a handler can expect are documented per component, and the full
list is in the Events guide.
Summary
Functions
Normalises a handler prop into the map the browser reconciler consumes.
Types
Functions
Normalises a handler prop into the map the browser reconciler consumes.
Returns nil for an unset handler, which is how a component signals that no
MapLibre listener should be attached at all.
iex> HoloMap.Event.normalize(:parcel_clicked)
%{"action" => "parcel_clicked", "target" => "page", "params" => %{}}
iex> HoloMap.Event.normalize(%{action: :zoomed, target: "sidebar"})
%{"action" => "zoomed", "target" => "sidebar", "params" => %{}}
iex> HoloMap.Event.normalize(%{action: :picked, params: %{layer: "parcels"}})
%{"action" => "picked", "target" => "page", "params" => %{"layer" => "parcels"}}
iex> HoloMap.Event.normalize(%{action: :camera_moving, throttle: 100})
%{"action" => "camera_moving", "target" => "page", "params" => %{}, "throttle" => 100}
iex> HoloMap.Event.normalize(nil)
nil