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

Use microlens instead of lens #421

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ For example:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}

import Control.Lens ((&), (%~), (.~))
import Lens.Micro ((&), (%~), (.~))
import Data.Proxy
import Reflex.Dom

Expand All @@ -28,4 +28,4 @@ linkPreventDefault c = do
& elementConfig_eventSpec %~ addEventSpecFlags (Proxy :: Proxy (DomBuilderSpace m)) Click (const preventDefault)
(link, a) <- element "a" cfg c
return (domEvent Click link, a)
```
```
8 changes: 5 additions & 3 deletions reflex-dom-core/reflex-dom-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ library
jsaddle >= 0.9.0.0 && < 0.10,
-- keycode-0.2 has a bug on firefox
keycode >= 0.2.1 && < 0.3,
lens >= 4.7 && < 5,
microlens-ghc >= 0.4 && < 0.5,
microlens >= 0.4 && < 0.5,
monad-control >= 1.0.1 && < 1.1,
mtl >= 2.1 && < 2.3,
primitive >= 0.5 && < 0.8,
Expand Down Expand Up @@ -169,7 +170,8 @@ library
if flag(use-template-haskell)
build-depends:
dependent-sum-template >= 0.1 && < 0.2,
template-haskell >= 2.12.0 && < 2.17
template-haskell >= 2.12.0 && < 2.17,
microlens-th >= 0.4 && < 0.5
other-extensions: TemplateHaskell
cpp-options: -DUSE_TEMPLATE_HASKELL
other-modules:
Expand Down Expand Up @@ -207,7 +209,7 @@ test-suite hydration
, HUnit
, jsaddle
, jsaddle-warp
, lens
, microlens
, lifted-base
, network
, random
Expand Down
2 changes: 1 addition & 1 deletion reflex-dom-core/src/Foreign/JavaScript/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import Foreign.C.Types
import Foreign.Ptr
import Text.Encoding.Z
#else
import Control.Lens.Operators ((^.))
import Lens.Micro.GHC ((^.))
import Data.Word (Word8)
import GHCJS.DOM.Types (JSVal, MonadJSM (..), liftJSM, runJSM, toJSString, toJSVal)
import Language.Javascript.JSaddle (Function (..), array, eval, freeFunction, function, js, js1, jss, valBool,
Expand Down
2 changes: 1 addition & 1 deletion reflex-dom-core/src/Foreign/JavaScript/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Foreign.JavaScript.Utils
, js_jsonParse
) where

import Control.Lens
import Lens.Micro.GHC
import Data.Aeson
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
Expand Down
4 changes: 2 additions & 2 deletions reflex-dom-core/src/Reflex/Dom/Builder/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import Reflex.Query.Class
import Reflex.Requester.Base

import qualified Control.Category
import Control.Lens hiding (element)
import Lens.Micro.GHC
import Control.Monad.Reader
import qualified Control.Monad.State as Lazy
import Control.Monad.State.Strict
Expand Down Expand Up @@ -749,7 +749,7 @@ instance HasDocument m => HasDocument (QueryT t q m)
class HasSetValue a where
type SetValue a :: *
setValue :: Lens' a (SetValue a)

instance Reflex t => HasSetValue (TextAreaElementConfig er t m) where
type SetValue (TextAreaElementConfig er t m) = Event t Text
setValue = textAreaElementConfig_setValue
14 changes: 11 additions & 3 deletions reflex-dom-core/src/Reflex/Dom/Builder/Class/TH.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
module Reflex.Dom.Builder.Class.TH where

import Control.Lens
import Language.Haskell.TH.Lib (DecsQ)
import Language.Haskell.TH.Syntax (Name, nameBase)
import Lens.Micro.GHC
import Lens.Micro.TH
import Language.Haskell.TH ( mkName, Name, DecsQ, nameBase )
import Data.Char (toLower)

namer :: [String] -> Name -> [Name] -> Name -> [DefName]
namer s n ks t | nameBase t `elem` s = []
| otherwise = underscoreNoPrefixNamer n ks t

makeLensesWithoutField :: [String] -> Name -> DecsQ
makeLensesWithoutField s = makeLensesWith (lensRules & lensField .~ namer s)

-- copied from lens
underscoreNoPrefixNamer :: Name -> [Name] -> Name -> [DefName]
underscoreNoPrefixNamer _ _ n =
case nameBase n of
'_':x:xs -> [TopName (mkName (toLower x:xs))]
_ -> []
10 changes: 6 additions & 4 deletions reflex-dom-core/src/Reflex/Dom/Builder/Immediate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module Reflex.Dom.Builder.Immediate

import Control.Concurrent
import Control.Exception (bracketOnError)
import Control.Lens (Identity(..), imapM_, iforM_, (^.), makeLenses)
import Lens.Micro.GHC ( (^.) )
import Control.Monad.Exception
import Control.Monad.Primitive
import Control.Monad.Reader
Expand All @@ -133,6 +133,7 @@ import Data.FastMutableIntMap (PatchIntMap (..))
import Data.Foldable (for_, traverse_)
import Data.Functor.Compose
import Data.Functor.Constant
import Data.Functor.Identity
import Data.Functor.Misc
import Data.Functor.Product
import Data.GADT.Compare (GCompare)
Expand Down Expand Up @@ -208,7 +209,8 @@ import qualified Reflex.TriggerEvent.Base as TriggerEventT (askEvents)

#ifndef USE_TEMPLATE_HASKELL
import Data.Functor.Contravariant (phantom)
import Control.Lens (Lens', Getter)
#else
import Lens.Micro.TH
#endif

#ifndef ghcjs_HOST_OS
Expand Down Expand Up @@ -527,7 +529,7 @@ wrap
-> RawElementConfig er t s
-> m (DMap EventName (EventFilterTriggerRef t er))
wrap events e cfg = do
forM_ (_rawElementConfig_modifyAttributes cfg) $ \modifyAttrs -> requestDomAction_ $ ffor modifyAttrs $ imapM_ $ \(AttributeName mAttrNamespace n) mv -> case mAttrNamespace of
forM_ (_rawElementConfig_modifyAttributes cfg) $ \modifyAttrs -> requestDomAction_ $ ffor (Map.toList <$> modifyAttrs) $ mapM_ $ \(AttributeName mAttrNamespace n, mv) -> case mAttrNamespace of
Nothing -> maybe (removeAttribute e n) (setAttribute e n) mv
Just ns -> maybe (removeAttributeNS e (Just ns) n) (setAttributeNS e (Just ns) n) mv
eventTriggerRefs :: DMap EventName (EventFilterTriggerRef t er) <- liftJSM $ fmap DMap.fromList $ forM (DMap.toList $ _ghcjsEventSpec_filters $ _rawElementConfig_eventSpec cfg) $ \(en :=> GhcjsEventFilter f) -> do
Expand Down Expand Up @@ -680,7 +682,7 @@ makeElement doc elementTag cfg = do
e <- uncheckedCastTo DOM.Element <$> case cfg ^. namespace of
Nothing -> createElement doc elementTag
Just ens -> createElementNS doc (Just ens) elementTag
iforM_ (cfg ^. initialAttributes) $ \(AttributeName mAttrNamespace n) v -> case mAttrNamespace of
forM_ (Map.toList $ cfg ^. initialAttributes) $ \(AttributeName mAttrNamespace n, v) -> case mAttrNamespace of
Nothing -> setAttribute e n v
Just ans -> setAttributeNS e (Just ans) n v
pure e
Expand Down
2 changes: 1 addition & 1 deletion reflex-dom-core/src/Reflex/Dom/Builder/Static.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Reflex.Dom.Builder.Static where

import Data.IORef (IORef)
import Blaze.ByteString.Builder.Html.Utf8
import Control.Lens hiding (element)
import Lens.Micro.GHC
import Control.Monad.Exception
import Control.Monad.Identity
import Control.Monad.Primitive
Expand Down
3 changes: 2 additions & 1 deletion reflex-dom-core/src/Reflex/Dom/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module Reflex.Dom.Class ( module Reflex.Dom.Class
, module Web.KeyCode
) where

import Control.Lens
import Lens.Micro
import Lens.Micro.Internal
import Reflex.Class
import Web.KeyCode

Expand Down
2 changes: 1 addition & 1 deletion reflex-dom-core/src/Reflex/Dom/Location.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Reflex.Dom.Location
import Reflex
import Reflex.Dom.Builder.Immediate (wrapDomEvent)

import Control.Lens ((^.))
import Lens.Micro.GHC ((^.))
import Control.Monad ((>=>))
import Control.Monad.Fix (MonadFix)
import Data.Align (align)
Expand Down
2 changes: 1 addition & 1 deletion reflex-dom-core/src/Reflex/Dom/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ import Reflex.Profiled
#endif

import Control.Concurrent
import Control.Lens
import Control.Monad
import Control.Monad.Reader hiding (forM, forM_, mapM, mapM_, sequence, sequence_)
import Control.Monad.Ref
import Data.ByteString (ByteString)
import Data.Dependent.Sum (DSum (..))
import Data.Foldable (for_)
import Data.Functor.Identity
import Data.IORef
import Data.Maybe
import Data.Monoid ((<>))
Expand Down
5 changes: 2 additions & 3 deletions reflex-dom-core/src/Reflex/Dom/Old.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ module Reflex.Dom.Old

import Control.Arrow (first)
#ifdef USE_TEMPLATE_HASKELL
import Control.Lens (makeLenses, (%~), (&), (.~), (^.))
#else
import Control.Lens (Lens, Lens', (%~), (&), (.~), (^.))
import Lens.Micro.TH (makeLenses)
#endif
import Lens.Micro.GHC ((%~), (&), (.~), (^.))
import Control.Monad
import Control.Monad.Fix
import Control.Monad.IO.Class
Expand Down
5 changes: 4 additions & 1 deletion reflex-dom-core/src/Reflex/Dom/WebSocket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import Reflex.TriggerEvent.Class
import Control.Concurrent
import Control.Concurrent.STM
import Control.Exception
import Control.Lens
import Lens.Micro.GHC
import Control.Monad hiding (forM, forM_, mapM, mapM_, sequence)
import Control.Monad.IO.Class
import Control.Monad.State
Expand All @@ -58,6 +58,9 @@ import GHCJS.DOM.Types (runJSM, askJSM, MonadJSM, liftJSM, JSM)
import GHCJS.DOM.WebSocket (getReadyState)
import GHCJS.Marshal
import qualified Language.Javascript.JSaddle.Monad as JS (catch)
#ifdef USE_TEMPLATE_HASKELL
import Lens.Micro.TH
#endif

data WebSocketConfig t a
= WebSocketConfig { _webSocketConfig_send :: Event t [a]
Expand Down
8 changes: 4 additions & 4 deletions reflex-dom-core/src/Reflex/Dom/Widget/Basic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ import Reflex.PostBuild.Class
import Reflex.Workflow

import Control.Arrow
import Control.Lens hiding (children, element)
import Lens.Micro.GHC hiding (children, element)
import Control.Monad.Reader hiding (forM, forM_, mapM, mapM_, sequence, sequence_)
import Data.Align
import Data.Default
Expand All @@ -89,6 +89,7 @@ import qualified Data.Text as T
import Data.These
import Data.Traversable
import Prelude hiding (mapM, mapM_, sequence, sequence_)
import qualified Data.Map as M

-- | Breaks the given Map into pieces based on the given Set. Each piece will contain only keys that are less than the key of the piece, and greater than or equal to the key of the piece with the next-smaller key. There will be one additional piece containing all keys from the original Map that are larger or equal to the largest key in the Set.
-- Either k () is used instead of Maybe k so that the resulting map of pieces is sorted so that the additional piece has the largest key.
Expand Down Expand Up @@ -323,15 +324,14 @@ tabDisplay :: forall t m k. (MonadFix m, DomBuilder t m, MonadHold t m, PostBuil
tabDisplay ulClass activeClass tabItems = do
let t0 = listToMaybe $ Map.keys tabItems
rec currentTab :: Demux t (Maybe k) <- elAttr "ul" ("class" =: ulClass) $ do
tabClicksList :: [Event t k] <- Map.elems <$> imapM (\k (s,_) -> headerBarLink s k $ demuxed currentTab (Just k)) tabItems
tabClicksList :: [Event t k] <- mapM (\(k, (s,_)) -> headerBarLink s k $ demuxed currentTab (Just k)) (M.toList tabItems)
let eTabClicks :: Event t k = leftmost tabClicksList
fmap demux $ holdDyn t0 $ fmap Just eTabClicks
el "div" $ do
iforM_ tabItems $ \k (_, w) -> do
forM_ (M.toList tabItems) $ \(k, (_, w)) -> do
let isSelected = demuxed currentTab $ Just k
attrs = ffor isSelected $ \s -> if s then Map.empty else Map.singleton "style" "display:none;"
elDynAttr "div" attrs w
return ()
where
headerBarLink :: Text -> k -> Dynamic t Bool -> m (Event t k)
headerBarLink x k isSelected = do
Expand Down
6 changes: 4 additions & 2 deletions reflex-dom-core/src/Reflex/Dom/Widget/Input.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Reflex.Dom.Widget.Input (module Reflex.Dom.Widget.Input, def, (&), (.~))

import Prelude

import Control.Lens hiding (element, ix)
import Lens.Micro.GHC ( (&), (.~) )
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Reader
Expand Down Expand Up @@ -50,7 +50,9 @@ import Reflex.Dynamic
import Reflex.PostBuild.Class
import Reflex.TriggerEvent.Class
import qualified Text.Read as T

#ifdef USE_TEMPLATE_HASKELL
import Lens.Micro.TH
#endif
import qualified GHCJS.DOM.Event as Event
import qualified GHCJS.DOM.HTMLInputElement as Input

Expand Down
20 changes: 12 additions & 8 deletions reflex-dom-core/src/Reflex/Dom/Xhr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
#ifdef USE_TEMPLATE_HASKELL

{-# LANGUAGE TemplateHaskell #-}
#endif


-- | A module for performing asynchronous HTTP calls from JavaScript
-- using the
Expand Down Expand Up @@ -149,15 +149,15 @@ import Reflex.Dom.Xhr.ResponseType

import Control.Concurrent
import Control.Exception (handle)
import Control.Lens
import Lens.Micro.GHC
import Control.Monad hiding (forM)
import Control.Monad.IO.Class
import Data.Aeson
#if MIN_VERSION_aeson(1,0,0)

import Data.Aeson.Text
#else
import Data.Aeson.Encode
#endif



import qualified Data.ByteString.Lazy as BL
import Data.CaseInsensitive (CI)
import qualified Data.CaseInsensitive as CI
Expand All @@ -175,7 +175,11 @@ import qualified Data.Text.Lazy.Builder as B
import Data.Traversable
import Data.Typeable

import Lens.Micro.TH


import Language.Javascript.JSaddle.Monad (JSM, askJSM, runJSM, MonadJSM, liftJSM)
import qualified Data.Map as M

data XhrRequest a
= XhrRequest { _xhrRequest_method :: Text
Expand Down Expand Up @@ -261,7 +265,7 @@ newXMLHttpRequestWithError req cb = do
True
(fromMaybe "" $ _xhrRequestConfig_user c)
(fromMaybe "" $ _xhrRequestConfig_password c)
iforM_ (_xhrRequestConfig_headers c) $ xmlHttpRequestSetRequestHeader xhr
forM_ (M.toList $ _xhrRequestConfig_headers c) $ uncurry (xmlHttpRequestSetRequestHeader xhr)
maybe (return ()) (xmlHttpRequestSetResponseType xhr . fromResponseType) rt
xmlHttpRequestSetWithCredentials xhr creds
_ <- xmlHttpRequestOnreadystatechange xhr $ do
Expand Down
6 changes: 4 additions & 2 deletions reflex-dom-core/src/Reflex/Dom/Xhr/FormData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Reflex.Dom.Xhr.FormData
)
where

import Control.Lens
import Lens.Micro.GHC
import Data.Default
import Data.Map (Map)
import Data.Text (Text)
Expand All @@ -19,6 +19,8 @@ import GHCJS.DOM.Types (File, IsBlob)
import Language.Javascript.JSaddle.Monad (MonadJSM, liftJSM)
import Reflex
import Reflex.Dom.Xhr
import qualified Data.Map as M
import Control.Monad

-- | A FormData value may be a blob/file or a string. The file can optionally be provided with filename.
data FormValue blob = FormValue_Text Text
Expand All @@ -35,7 +37,7 @@ postForms
postForms url payload = do
performMkRequestsAsync $ ffor payload $ \fs -> for fs $ \u -> liftJSM $ do
fd <- FD.newFormData Nothing
iforM_ u $ \k v -> case v of
forM_ (M.toList u) $ \(k, v) -> case v of
FormValue_Text t -> FD.append fd k t
FormValue_File b fn -> FD.appendBlob fd k b fn
return $ xhrRequest "POST" url $ def & xhrRequestConfig_sendData .~ fd
Expand Down
2 changes: 1 addition & 1 deletion reflex-dom-core/test/hydration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import Prelude hiding (fail)
import Control.Concurrent
import qualified Control.Concurrent.Async as Async
import Control.Lens.Operators
import Lens.Micro ((^.), (?~))
import Control.Monad hiding (fail)
import Control.Monad.Catch
import Control.Monad.Fail
Expand Down
2 changes: 1 addition & 1 deletion reflex-dom/examples/sortableList.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecursiveDo #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Lens
import Lens.Micro.GHC
import Control.Monad.Identity
import Control.Monad.IO.Class
import Data.Dependent.Map (DMap)
Expand Down