I'm trying to throttle a search field in purescript-halogen. What I have so far:
eval (Search search next) = do
State st <- get
-- clear last timeout
liftEff' $ maybe (return unit) T.clearTimeout st.searchTimeout
-- new timeout
t <- liftEff' $ T.timeout 1000 $ return unit -- how to send action from here???
modify (\(State s) -> State $ s { searchTimeout = Just t })
pure next
I thought about saving the UI driver in a global Var
and send new actions from there, but this seems very hacky to me.
Or maybe there's another way to do that?
This is the kind of thing you'll probably need to create an EventSource
for. An EventSource
allows you to subscribe to something somewhat like a signal/stream/event listener and then raise actions.
This isn't quite what you want, but is an example of using an EventSource
to run an interval based timer: https://github.com/slamdata/slamdata/blob/2ab704302292406e838e1a6e5541aa06ad47e952/src/Notebook/Cell/Component.purs#L213-L217