achirkin / qua-kit

Quick Urban Analysis Kit
MIT License
17 stars 8 forks source link

Make a library with datatypes compatible between GHC and GHCJS #60

Closed mb21 closed 6 years ago

mb21 commented 7 years ago
  1. Make conditionals in .cabal file, e.g.:

    library
      if impl(ghcjs)
        build-depends:
          ghcjs-base >= 0.2.0.0
      else
        build-depends:
          aeson >= 1.x.x.x
  2. Use CPP to select proper imports in each implementation, e.g.:

    {-# LANGUAGE CPP #-}
    
    #ifdef ghcjs_HOST_OS
    import JavaScript.JSON.Types.Internal
    import JavaScript.JSON.Types.Instances
    import JavaScript.JSON.Types.Generic ()
    #else
    import Data.Aeson
    #endif
  3. Use DeriveGeneric extension to automate generation of from/to json:

    {-# LANGUAGE DeriveGeneric #-}
    ...
    import GHC.Generics
    ...
    data AAA = AAA
      { fieldAAAN1       :: !Int
      , fieldBBBJStringT :: !JSString
      , anotherThingy    :: !Double
      } deriving Generic
    
    instance ToJSON AAA
    instance FromJSON AAA
  4. Hope for good luck!

achirkin commented 7 years ago

Good! Let's make it into a small cross-platform library libs/hs/qua-web-api. Also try to keep a bare minimum of dependencies - this is important for qua-view (atm, qua-view.js can be up to 15 mb without optimizations already).

Another, connected task is to track all necessary api calls. What we remember so far:

NorfairKing commented 7 years ago

Because you're using ghcjs and reflex, you could use servant and reflex-servant to take care of most of the boiler plate.

achirkin commented 7 years ago

Yes, we could. Though, it is debatable if the benefit of using servant API outweights the costs of integrating servant into yesod, porting existing yesod handlers, and, most importantly, having more deps in GHCJS output. Do you know how much servant-reflex adds up to the output size? If around 100KB then I would agree, if couple megabytes then it would be too much.

mb21 commented 6 years ago

This has been done on the reflex branch...