{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeOperators #-}
module Shpadoinkle.Run (
runJSorWarp
, runJSorWarpWithIndex
, Env(..), Port
, liveWithBackend
, liveWithBackendAndIndex
, liveWithStatic
, liveWithStaticAndIndex
, live
, liveWithIndex
, fullPage
, fullPageJSM
, simple
, entrypoint
) where
import Data.Text (Text)
import Data.ByteString.Lazy (ByteString)
import GHCJS.DOM.Types (JSM)
import Shpadoinkle (Backend, Html, RawNode,
TVar, newTVarIO,
shpadoinkle, type (~>))
#ifndef ghcjs_HOST_OS
import Language.Javascript.JSaddle.Warp (run, runWithIndex)
import Language.Javascript.JSaddle.WebSockets (debug, debugWithIndex, debugOr, debugWithIndexOr)
import Network.Wai (Application)
import Network.Wai.Application.Static (defaultFileServerSettings,
staticApp)
liveWithBackend
:: Port
-> JSM ()
-> IO Application
-> IO ()
liveWithBackend port frontend server = debugOr port frontend =<< server
liveWithBackendAndIndex
:: ByteString
-> Port
-> JSM ()
-> IO Application
-> IO ()
liveWithBackendAndIndex idx port frontend server = debugWithIndexOr idx port frontend =<< server
live
:: Port
-> JSM ()
-> IO ()
live = debug
liveWithIndex
:: ByteString
-> Port
-> JSM ()
-> IO ()
liveWithIndex = debugWithIndex
liveWithStatic
:: Port
-> JSM ()
-> FilePath
-> IO ()
liveWithStatic port frontend =
liveWithBackend port frontend . pure . staticApp . defaultFileServerSettings
liveWithStaticAndIndex
:: ByteString
-> Port
-> JSM ()
-> FilePath
-> IO ()
liveWithStaticAndIndex idx port frontend =
liveWithBackendAndIndex idx port frontend . pure . staticApp . defaultFileServerSettings
#else
data Application
live :: Port -> JSM () -> IO ()
live = error "Live reloads require GHC"
liveWithIndex :: ByteString -> Port -> JSM () -> IO ()
liveWithIndex = error "Live reloads require GHC"
liveWithStatic :: Port -> JSM () -> FilePath -> IO ()
liveWithStatic = error "Live reloads require GHC"
liveWithStaticAndIndex :: ByteString -> Port -> JSM () -> FilePath -> IO ()
liveWithStaticAndIndex = error "Live reloads require GHC"
liveWithBackend :: Port -> JSM () -> IO Application -> IO ()
liveWithBackend = error "Live reloads require GHC"
liveWithBackendAndIndex :: ByteString -> Port -> JSM () -> IO Application -> IO ()
liveWithBackendAndIndex = error "Live reloads require GHC"
#endif
data Env = Dev | Prod
type Port = Int
fullPage
:: Backend b m a => Monad (b m) => Eq a
=> (m ~> JSM)
-> (TVar a -> b m ~> m)
-> a
-> (a -> Html (b m) a)
-> b m RawNode
-> JSM ()
fullPage g f i view getStage = do
model <- newTVarIO i
shpadoinkle g f model view getStage
{-# INLINE fullPage #-}
fullPageJSM
:: Backend b JSM a => Monad (b JSM) => Eq a
=> (TVar a -> b JSM ~> JSM)
-> a
-> (a -> Html (b JSM) a)
-> b JSM RawNode
-> JSM ()
fullPageJSM = fullPage id
{-# INLINE fullPageJSM #-}
runJSorWarp :: Int -> JSM () -> IO ()
#ifdef ghcjs_HOST_OS
runJSorWarp _ = id
{-# INLINE runJSorWarp #-}
#else
runJSorWarp = run
{-# INLINE runJSorWarp #-}
#endif
runJSorWarpWithIndex :: ByteString -> Int -> JSM () -> IO ()
#ifdef ghcjs_HOST_OS
runJSorWarpWithIndex _ _ = id
{-# INLINE runJSorWarpWithIndex #-}
#else
runJSorWarpWithIndex = runWithIndex
{-# INLINE runJSorWarpWithIndex #-}
#endif
simple
:: Backend b JSM a => Monad (b JSM) => Eq a
=> (TVar a -> b JSM ~> JSM)
-> a
-> (a -> Html (b JSM) a)
-> b JSM RawNode
-> JSM ()
simple = fullPageJSM
{-# INLINE simple #-}
entrypoint :: Env -> Text
entrypoint Dev = "/jsaddle.js"
entrypoint Prod = "/all.min.js"