f-o-a-m / purescript-web3

a purescript library for the web3 api
Apache License 2.0
127 stars 24 forks source link

wrap eth_newFilter call with bracket #104

Closed safareli closed 6 years ago

safareli commented 6 years ago

fixes #97

see https://github.com/slamdata/purescript-aff#3-bracketing

safareli commented 6 years ago

Also something like this can do the trick too:

p <- ask
resVar <- makeEmptyVar
let
  acquire = do
    runWeb3 p $ eth_newFilter $ fltr # _fromBlock .~ BN pollingFromBlock
  release = case _ of
    Left err -> putVar (Left err) resVar
    Right filterId -> void $ runWeb3 p $ eth_uninstallFilter filterId
  action = case _ of
    Left err -> putVar (Left err) resVar
    Right filterId -> do
      res <- runWeb3 p $ void $ runProcess $ reduceEventStream (pollFilter filterId (fltr ^. _toBlock)) handler)
      putVar res resVar
liftAff $ bracket acquire release action
res <- liftAff $ takeVar resVar
either throwError pure res

but if we don't add the MonadBracket instance then everyone would have to solve this issue over and over again.