{-# LANGUAGE RecordWildCards #-}
module Miso.Subscription.History
(
uriSub
, getURI
, pushURI
, replaceURI
, back
, forward
, go
, URI (..)
) where
import Control.Monad
import Control.Monad.IO.Class
import Language.Javascript.JSaddle
import Network.URI hiding (path)
import System.IO.Unsafe
import Miso.Concurrent
import qualified Miso.FFI.Internal as FFI
import Miso.String
import Miso.Effect (Sub)
getURI :: JSM URI
{-# INLINE getURI #-}
getURI :: JSM URI
getURI = do
href <- MisoString -> String
forall a. FromMisoString a => MisoString -> a
fromMisoString (MisoString -> String) -> JSM MisoString -> JSM String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSM MisoString
getWindowLocationHref
case parseURI href of
Maybe URI
Nothing -> String -> JSM URI
forall a. String -> JSM a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> JSM URI) -> String -> JSM URI
forall a b. (a -> b) -> a -> b
$ String
"Could not parse URI from window.location: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
href
Just URI
uri ->
URI -> JSM URI
forall a. a -> JSM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (URI -> URI
dropPrefix URI
uri)
where
dropPrefix :: URI -> URI
dropPrefix u :: URI
u@URI{String
Maybe URIAuth
uriScheme :: String
uriAuthority :: Maybe URIAuth
uriPath :: String
uriQuery :: String
uriFragment :: String
uriAuthority :: URI -> Maybe URIAuth
uriFragment :: URI -> String
uriPath :: URI -> String
uriQuery :: URI -> String
uriScheme :: URI -> String
..}
| Char
'/' : String
xs <- String
uriPath = URI
u { uriPath = xs }
| Bool
otherwise = URI
u
pushURI :: URI -> JSM ()
{-# INLINE pushURI #-}
pushURI :: URI -> JSM ()
pushURI URI
uri = URI -> JSM ()
pushStateNoModel URI
uri { uriPath = toPath uri }
toPath :: URI -> String
toPath :: URI -> String
toPath URI
uri =
case URI -> String
uriPath URI
uri of
String
"" -> String
"/"
String
"/" -> String
"/"
xs :: String
xs@(Char
'/' : String
_) -> String
xs
String
xs -> Char
'/' Char -> String -> String
forall a. a -> [a] -> [a]
: String
xs
replaceURI :: URI -> JSM ()
{-# INLINE replaceURI #-}
replaceURI :: URI -> JSM ()
replaceURI URI
uri = URI -> JSM ()
replaceTo' URI
uri { uriPath = toPath uri }
back :: JSM ()
{-# INLINE back #-}
back :: JSM ()
back = JSM JSVal -> JSM ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (JSM JSVal -> JSM ()) -> JSM JSVal -> JSM ()
forall a b. (a -> b) -> a -> b
$ JSM JSVal
getHistory JSM JSVal -> String -> () -> JSM JSVal
forall this name args.
(MakeObject this, ToJSString name, MakeArgs args) =>
this -> name -> args -> JSM JSVal
# String
"back" (() -> JSM JSVal) -> () -> JSM JSVal
forall a b. (a -> b) -> a -> b
$ ()
forward :: JSM ()
{-# INLINE forward #-}
forward :: JSM ()
forward = JSM JSVal -> JSM ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (JSM JSVal -> JSM ()) -> JSM JSVal -> JSM ()
forall a b. (a -> b) -> a -> b
$ JSM JSVal
getHistory JSM JSVal -> String -> () -> JSM JSVal
forall this name args.
(MakeObject this, ToJSString name, MakeArgs args) =>
this -> name -> args -> JSM JSVal
# String
"forward" (() -> JSM JSVal) -> () -> JSM JSVal
forall a b. (a -> b) -> a -> b
$ ()
go :: Int -> JSM ()
{-# INLINE go #-}
go :: Int -> JSM ()
go Int
n = JSM JSVal -> JSM ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (JSM JSVal -> JSM ()) -> JSM JSVal -> JSM ()
forall a b. (a -> b) -> a -> b
$ JSM JSVal
getHistory JSM JSVal -> String -> [Int] -> JSM JSVal
forall this name args.
(MakeObject this, ToJSString name, MakeArgs args) =>
this -> name -> args -> JSM JSVal
# String
"go" ([Int] -> JSM JSVal) -> [Int] -> JSM JSVal
forall a b. (a -> b) -> a -> b
$ [Int
n]
chan :: Waiter
{-# NOINLINE chan #-}
chan :: Waiter
chan = IO Waiter -> Waiter
forall a. IO a -> a
unsafePerformIO IO Waiter
waiter
uriSub :: (URI -> action) -> Sub action
uriSub :: forall action. (URI -> action) -> Sub action
uriSub = \URI -> action
f Sink action
sink -> do
JSM ThreadId -> JSM ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (JSM ThreadId -> JSM ())
-> (JSM () -> JSM ThreadId) -> JSM () -> JSM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSM () -> JSM ThreadId
FFI.forkJSM (JSM () -> JSM ThreadId)
-> (JSM () -> JSM ()) -> JSM () -> JSM ThreadId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSM () -> JSM ()
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (JSM () -> JSM ()) -> JSM () -> JSM ()
forall a b. (a -> b) -> a -> b
$ do
IO () -> JSM ()
forall a. IO a -> JSM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Waiter -> IO ()
wait Waiter
chan)
Sink action
sink Sink action -> (URI -> action) -> URI -> JSM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. URI -> action
f (URI -> JSM ()) -> JSM URI -> JSM ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< JSM URI
getURI
MisoString -> (JSVal -> JSM ()) -> JSM ()
FFI.windowAddEventListener (String -> MisoString
forall str. ToMisoString str => str -> MisoString
ms String
"popstate") ((JSVal -> JSM ()) -> JSM ()) -> (JSVal -> JSM ()) -> JSM ()
forall a b. (a -> b) -> a -> b
$ \JSVal
_ ->
Sink action
sink Sink action -> (URI -> action) -> URI -> JSM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. URI -> action
f (URI -> JSM ()) -> JSM URI -> JSM ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< JSM URI
getURI
pushStateNoModel :: URI -> JSM ()
{-# INLINE pushStateNoModel #-}
pushStateNoModel :: URI -> JSM ()
pushStateNoModel URI
u = do
MisoString -> JSM ()
pushState (MisoString -> JSM ()) -> (URI -> MisoString) -> URI -> JSM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> MisoString
pack (String -> MisoString) -> (URI -> String) -> URI -> MisoString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. URI -> String
forall a. Show a => a -> String
show (URI -> JSM ()) -> URI -> JSM ()
forall a b. (a -> b) -> a -> b
$ URI
u
IO () -> JSM ()
forall a. IO a -> JSM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Waiter -> IO ()
serve Waiter
chan)
replaceTo' :: URI -> JSM ()
{-# INLINE replaceTo' #-}
replaceTo' :: URI -> JSM ()
replaceTo' URI
u = do
MisoString -> JSM ()
replaceState (MisoString -> JSM ()) -> (URI -> MisoString) -> URI -> JSM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> MisoString
pack (String -> MisoString) -> (URI -> String) -> URI -> MisoString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. URI -> String
forall a. Show a => a -> String
show (URI -> JSM ()) -> URI -> JSM ()
forall a b. (a -> b) -> a -> b
$ URI
u
IO () -> JSM ()
forall a. IO a -> JSM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Waiter -> IO ()
serve Waiter
chan)
getWindowLocationHref :: JSM MisoString
getWindowLocationHref :: JSM MisoString
getWindowLocationHref = do
href <- JSVal -> JSM (Maybe (Maybe MisoString))
forall a. FromJSVal a => JSVal -> JSM (Maybe a)
fromJSVal (JSVal -> JSM (Maybe (Maybe MisoString)))
-> JSM JSVal -> JSM (Maybe (Maybe MisoString))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< String -> JSM JSVal
forall a. ToJSString a => a -> JSM JSVal
jsg String
"window" JSM JSVal -> String -> JSM JSVal
forall this name.
(MakeObject this, ToJSString name) =>
this -> name -> JSM JSVal
! String
"location" JSM JSVal -> String -> JSM JSVal
forall this name.
(MakeObject this, ToJSString name) =>
this -> name -> JSM JSVal
! String
"href"
case join href of
Maybe MisoString
Nothing -> MisoString -> JSM MisoString
forall a. a -> JSM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure MisoString
forall a. Monoid a => a
mempty
Just MisoString
uri -> MisoString -> JSM MisoString
forall a. a -> JSM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure MisoString
uri
getHistory :: JSM JSVal
getHistory :: JSM JSVal
getHistory = String -> JSM JSVal
forall a. ToJSString a => a -> JSM JSVal
jsg String
"window" JSM JSVal -> String -> JSM JSVal
forall this name.
(MakeObject this, ToJSString name) =>
this -> name -> JSM JSVal
! String
"history"
pushState :: MisoString -> JSM ()
pushState :: MisoString -> JSM ()
pushState MisoString
url = do
_ <- JSM JSVal
getHistory JSM JSVal -> String -> (JSVal, JSVal, MisoString) -> JSM JSVal
forall this name args.
(MakeObject this, ToJSString name, MakeArgs args) =>
this -> name -> args -> JSM JSVal
# String
"pushState" ((JSVal, JSVal, MisoString) -> JSM JSVal)
-> (JSVal, JSVal, MisoString) -> JSM JSVal
forall a b. (a -> b) -> a -> b
$ (JSVal
jsNull, JSVal
jsNull, MisoString
url)
pure ()
replaceState :: MisoString -> JSM ()
replaceState :: MisoString -> JSM ()
replaceState MisoString
url = do
_ <- JSM JSVal
getHistory JSM JSVal -> String -> (JSVal, JSVal, MisoString) -> JSM JSVal
forall this name args.
(MakeObject this, ToJSString name, MakeArgs args) =>
this -> name -> args -> JSM JSVal
# String
"replaceState" ((JSVal, JSVal, MisoString) -> JSM JSVal)
-> (JSVal, JSVal, MisoString) -> JSM JSVal
forall a b. (a -> b) -> a -> b
$ (JSVal
jsNull, JSVal
jsNull, MisoString
url)
pure ()