Suppose I want to create, using ghcjs-dom, an event listener which responds to a click, and then removes itself.
I have
addListener :: (IsEventTarget t, IsEvent e)
=> t -> EventName t e -> SaferEventListener t e -> Bool -> IO ()
removeListener :: (IsEventTarget t, IsEvent e)
=> t -> EventName t e -> SaferEventListener t e -> Bool -> IO ()
to add and remove, and
newListener :: (IsEvent e) => EventM t e () -> IO (SaferEventListener t e)
to construct the listener out of an EventM. How can I access the SaferEventListener (which I will only later construct) from within the EventM, in order to remove it when the event occurs?
In JavaScript, you use a named function expression as your callback to addEventListener, and then apply removeEventListener to that name from within the callback. But nothing analogus seems to be possible here. Or am I missing something?
Use
fixIOFill
_eventmwith yourEventM, and you will be able to access the event listener that will eventually be created through the namerec.recwill be the result of thenewListenercall, but it can be "used" before that is executed. I say "used," because trying to force it withseqor anything stronger will cause an infinite loop, but you should be fine doing what you're doing.fixIOis a generalization offix:The idea of
fixis making a function's eventual result available to it before it is actually created.The idea of
fixIOis making anIOfunction's eventual result available to it before it is actually created while also carrying out someIOactions. Also,fixIOonly carries out these actions once, which is why the first definition offix(that only callsfonce) is more relevant than the second.fixIO, in turn, is a specialization ofmfix :: MonadFix m => (a -> m a) -> m a, whereMonadFixis the class of monads (includingIO, withmfix = fixIO) that admit such knot-tying semantics. GHC supports "recursivedo" notation for anyMonadFix: