reflex icon indicating copy to clipboard operation
reflex copied to clipboard

EventWriterT could be more general

Open mpickering opened this issue 4 years ago • 0 comments

I found myself wanting a different event combination strategy than using merge when using EventWriterT.

Therefore I generalised the type of tellEvent to

class (Monad m, Semigroup w) => EventWriter t (f :: * -> *) w m | m -> t w where
   tellEventF :: f (Event t w) -> m ()

and implementing a running function which returned a list of the wrapped events rather than always combining them with merge.

-- | Run a 'EventWriterT' action.
runEventWriterTWithComb :: forall t f m a r. (Reflex t, Monad m)
    => EventWriterT t f r m a
    -> m (a, [f (Event t r)])
runEventWriterTWithComb (EventWriterT a)  = do
  (result, requests) <- runStateT a $ EventWriterState (-1) []
  let combineResults :: DMap (TellId r) (Compose f (Event t)) -> [f (Event t r)]
      combineResults =
        DMap.foldlWithKey (\vs tid (Compose v) -> withTellIdRefl tid $ v : vs) [] -- This is where we finally reverse the DMap to get things in the correct order
  let e =  combineResults $ DMap.fromDistinctAscList $ _eventWriterState_told requests
  return (result, e)

https://github.com/mpickering/reflex/commit/cf216f37f98abedbaa9f0946a45674a139db59a0

mpickering avatar Mar 16 '20 10:03 mpickering