Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventWriterT could be more general #406

Open
mpickering opened this issue Mar 16, 2020 · 0 comments
Open

EventWriterT could be more general #406

mpickering opened this issue Mar 16, 2020 · 0 comments

Comments

@mpickering
Copy link

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)

mpickering@cf216f3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant