Last active
May 6, 2016 14:54
-
-
Save paolino/e6eabe5c439844049a48870f1bd6affb to your computer and use it in GitHub Desktop.
caching reflex widget event creation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ------------- Spider synonims | |
| type ES = Event Spider | |
| type DS = Dynamic Spider | |
| -------------- Dom + spider synonyms | |
| type MS = MonadWidget Spider | |
| data WidgetEvents m b where | |
| GetEvent :: MonadIO m => (forall a. EventName a -> (ES (EventResultType a) -> ES b) -> m (ES b)) -> WidgetEvents m b | |
| -- extract an "unique event" dispatcher for the widget, where results are collapsed in type b | |
| eventsOf :: forall m w b. (MS m, HasDomEvent Spider w) => w -> m (WidgetEvents m b) | |
| eventsOf w = do | |
| mm <- liftIO $ newIORef Nothing | |
| return $ GetEvent (match mm w) | |
| -- dont't export, hide the intricacy of the dispatcher here, as an example we imagine there are procedures to init Mousemove | |
| match :: (MS m, HasDomEvent Spider w) => IORef (Maybe (ES (EventResultType 'MousemoveTag))) -> w -> EventName a -> (ES (EventResultType a) -> ES b) -> m (ES b) | |
| match _ w Mousedown f = return . f $ domEvent Mousedown w -- dumb solution | |
| match _ w Mouseup f = return . f $ domEvent Mouseup w -- again | |
| match mm w Mousemove f = do | |
| m <- liftIO $ readIORef mm | |
| e <- case m of | |
| Nothing -> do | |
| --- monadically initialize 'e' instead of what I do next | |
| let e = domEvent Mousemove w | |
| liftIO $ writeIORef mm (Just e) | |
| return e | |
| Just e -> return e | |
| return $ f e | |
| -- export, a box opener for GetEvent | |
| getEvent :: WidgetEvents m b | |
| -> EventName a -- event to match | |
| -> (ES (EventResultType a) -> ES b) -- collapser | |
| -> m (ES b) -- the safe event | |
| getEvent (GetEvent g) x f = g x f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment