Skip to content

Instantly share code, notes, and snippets.

@harrysarson
Last active March 3, 2020 09:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harrysarson/6ffeb44925197f961572b61825b97106 to your computer and use it in GitHub Desktop.
Save harrysarson/6ffeb44925197f961572b61825b97106 to your computer and use it in GitHub Desktop.
Effect managers in elm

The best guide on event managers in elm

Not because this guide is any good, but because it is the only one. This is (to the best of my knowledge) correct as of version 19.0 of the elm compiler and version 1.0.2 of elm/core.

Types of event managed things in elm

  1. Cmds
  2. Port Cmds (I believe these to be fundamentally different from normal Cmds).
  3. Subs.
  4. Taskss (although maybe these are just a precursor to normal Cmds).

Event manager

An event manager is a collection of specially named elm functions (helped by some special syntax) contained within an event manager elm module. These functions have access to some state which they can modify (via a pure update function). An effect manager elm module starts with effect module ModuleName where { command = MyCmd, subscription = MySub } exposing (..) if the module does not contain any function that create Cmds then the command = MyCmd, is ommitted (likewise for the , subscription = MySub part).

javascript generated for an event manager

  1. A Cmd event manager is a javascript object. init, onEffects, onSelfMsg and cmdMap are elm functions defined in the effect manager elm module.
{
  __init: init,
  __onEffects: onEffects,
  __onSelfMsg: onSelfMsg,
  __cmdMap: cmdMap,
  __subMap: undefined
};
  1. A port Cmd event manager (created by the elm compiler for every port x : SomeType -> Cmd never) is different from a normal Cmd event manager.
{
  	__cmdMap: _Platform_outgoingPortMap,
  	__converter: converter,
  	__portSetup: _Platform_setupOutgoingPort
  }

See the definition of _Platform_outgoingPortMap and _Platform_setupOutgoingPort. converter is an elm function with type annotation: converter: SomeType -> Json.Encode.Value. 3. A Sub event manager is very similar to a Cmd event manager. init, onEffects, onSelfMsg and subMap are elm functions defined in the effect manager elm module.

{
  __init: init,
  __onEffects: onEffects,
  __onSelfMsg: onSelfMsg,
  __cmdMap: 0,
  __subMap: subMap
};

I believe the __cmdMap is set to 0 rather than undefined to save bits.

Resources/references

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment