3

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?

Philip Kamenarsky
  • 2,757
  • 2
  • 24
  • 30

1 Answers1

3

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

gb.
  • 4,629
  • 1
  • 20
  • 19
  • Thanks! Just wanted to say - I'll check this out in a couple days and accept then if it helps :) – Philip Kamenarsky Dec 17 '15 at 18:51
  • No problem! Sorry I couldn't give you an exact answer, I'm a little busy right now. If it doesn't help and I have time next week I'll try and work up a proper solution. – gb. Dec 18 '15 at 15:34
  • Worked, thanks! I defined `doLaterAff = subscribe <<< EventSource <<< producerToStallingProducer <<< produce`, which I can then use like this: `doLater \emit -> ...`, it's pretty convenient. – Philip Kamenarsky Jan 14 '16 at 21:52