Skip to content

Instantly share code, notes, and snippets.

@eric-corumdigital
Created March 13, 2018 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric-corumdigital/60746399669e6321b3850735860b8302 to your computer and use it in GitHub Desktop.
Save eric-corumdigital/60746399669e6321b3850735860b8302 to your computer and use it in GitHub Desktop.
purescript-behaviors subscribeWithLast
subscribeWithLast :: forall a b eff.
(b -> a -> Eff (frp :: FRP | eff) b)
-> b
-> Event a
-> Eff (frp :: FRP | eff) (Eff (frp :: FRP | eff) b)
subscribeWithLast f z evt = dropRefEff $ do
r <- newRef z
unsub <-
FRP.subscribe evt
(\e -> readRef r >>= \a -> addRefEff (f a e) >>= writeRef r)
pure (dropRefEff (unsub *> readRef r))
where
dropRefEff :: forall e x. Eff (ref :: REF | e) x -> Eff e x
dropRefEff = unsafeCoerceEff
addRefEff :: forall e x. Eff e x -> Eff (ref :: REF | e) x
addRefEff = unsafeCoerceEff
--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment