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
fixIO
Fill
_eventm
with yourEventM
, and you will be able to access the event listener that will eventually be created through the namerec
.rec
will be the result of thenewListener
call, but it can be "used" before that is executed. I say "used," because trying to force it withseq
or anything stronger will cause an infinite loop, but you should be fine doing what you're doing.fixIO
is a generalization offix
:The idea of
fix
is making a function's eventual result available to it before it is actually created.The idea of
fixIO
is making anIO
function's eventual result available to it before it is actually created while also carrying out someIO
actions. Also,fixIO
only carries out these actions once, which is why the first definition offix
(that only callsf
once) is more relevant than the second.fixIO
, in turn, is a specialization ofmfix :: MonadFix m => (a -> m a) -> m a
, whereMonadFix
is the class of monads (includingIO
, withmfix = fixIO
) that admit such knot-tying semantics. GHC supports "recursivedo
" notation for anyMonadFix
: