{-# LANGUAGE RecursiveDo #-}
module Reflex.Potato.Helpers
( leftmostwarn
, fanDSum
, foldDynMergeWith
, getLeft
, getRight
, getHere
, getThere
)
where
import Relude
import Reflex
import Control.Monad.Fix
import qualified Data.Dependent.Map as DM
import qualified Data.Dependent.Sum as DS
import Data.Wedge
leftmostwarn :: (Reflex t) => String -> [Event t a] -> Event t a
leftmostwarn label evs = r where
combine = mergeList evs
nowarn =
fmapMaybe (\x -> if length x == 1 then Just (head x) else Nothing) combine
warn =
traceEventWith
(const ("WARNING: multiple " <> label <> " events triggered"))
$ fmapMaybe (\x -> if length x > 1 then Just (head x) else Nothing)
combine
r = leftmost [nowarn, warn]
fanDSum
:: forall t k
. (Reflex t, DM.GCompare k)
=> Event t (DS.DSum k Identity)
-> EventSelector t k
fanDSum ds = fan $ DM.fromAscList . (: []) <$> ds
foldDynMergeWith
:: (Reflex t, MonadHold t m, MonadFix m)
=> b
-> [Event t (b -> b)]
-> m (Dynamic t b)
foldDynMergeWith acc = foldDyn ($) acc . mergeWith (.)
getLeft :: Either a b -> Maybe a
getLeft (Left x) = Just x
getLeft _ = Nothing
getRight :: Either a b -> Maybe b
getRight (Right x) = Just x
getRight _ = Nothing
getHere :: Wedge a b -> Maybe a
getHere c = case c of
Here x -> Just x
_ -> Nothing
getThere :: Wedge a b -> Maybe b
getThere c = case c of
There x -> Just x
_ -> Nothing