A utility for logically grouping the performance and destruction of side effects. Inspired by React's
Why the name you ask? Modum is Latin for effect.
componentWillUnmount(); and similarly Angular has
The problem with these functions is that often times the work that you have to do on "init" closely mirrors the work that you have to do on "destroy". So you end up with this highly coupled code split between two different functions.
Modum addresses that problem by letting you declare both the "init" and "destroy" logic at the same time in the same place. It's just not all run at the same time. The function that you pass to
perform() (the effect) is run immediately, and the function that you return from the effect is not run until you tell Modum to destroy that side effect.
let modum =modummodummodum
Perform a side effect. You can optionally return a function from the
effect that cleans up the effect. This ensures that these highly related functions are logically grouped in your code.
Clean up a particular side effect that has been performed.
let modum =let id = modummodum
Clean up all side effects that have been performed since the last time this function was called.
addEvent(el, eventType, handler, options)
Perform the specific effect of adding an event listener to an element. This event is then automatically removed when the side effect is destroyed.
||The element to add the event listener to|
||The type of event to add|
||The event handler|
||Optional. These options get passed directly through to the