@@ -41,33 +41,16 @@ import Control.Lens
4141import Control.Monad hiding (forM, forM_, mapM, mapM_, sequence)
4242import Control.Monad.IO.Class
4343import Control.Monad.State
44−import Data.Aeson hiding (json)
4544import Data.ByteString (ByteString)
4645import Data.Default
4746import Data.IORef
48−import Data.JSString (JSString)
4947import Data.Maybe (isJust)
5048import Data.Text
5149import Data.Text.Encoding
5250import GHCJS.DOM.Types (runJSM, askJSM, MonadJSM, liftJSM, JSM)
5351import GHCJS.Marshal
54−import qualified Language.Javascript.JSaddle as JS (jsg, js1)
5552import qualified Language.Javascript.JSaddle.Monad as JS (catch)
5653
57−#ifdef ghcjs_HOST_OS
58−import GHCJS.DOM.Types ()
59−import System.IO.Unsafe
60−#else
61−import Data.JSString.Text
62−import qualified Data.ByteString.Lazy as LBS
63−#endif
64−-- When on ghcjs and using packages with jsstring patches it removes the FromJSVal
65−-- instance for Value from ghcjs-base. This makes it so that if the patches
66−-- are used then the instance comes from ghcjs-json
67−#if defined(ghcjs_HOST_OS) && defined(USE_TEXT_JSSTRING)
68−import JavaScript.JSON.Types.FromJSVal ()
69−#endif
70−
7154data WebSocketConfig t a
7255 = WebSocketConfig { _webSocketConfig_send :: Event t [a]
7356 , _webSocketConfig_close :: Event t (Word, Text)
@@ -145,29 +128,6 @@ webSocket' url config onRawMessage = do
145128textWebSocket :: (MonadJSM m, MonadJSM (Performable m), HasJSContext m, PostBuild t m, TriggerEvent t m, PerformEvent t m, MonadHold t m, Reflex t) => Text -> WebSocketConfig t Text -> m (RawWebSocket t Text)
146129textWebSocket url cfg = webSocket' url cfg (either (return . decodeUtf8) fromJSValUnchecked)
147130
148−-- | Deserialize a JSON value from a JSString.
149−-- When on ghcjs it uses JSON.parse which is usually faster than using aeson's decode to get a Value
150−-- When on ghc the JSString is converted to Text and then to ByteString in order to use aeson's decode
151−jsonDecode :: FromJSON a => JSString -> Maybe a
152−jsonDecode t = do
153−#ifdef ghcjs_HOST_OS
154− result <- unsafePerformIO $ handle (\(_ :: SomeException) -> pure Nothing) $ do
155− safeJsonParse t >>= \case
156− Nothing -> pure Nothing
157− Just a -> fromJSVal a
158− case fromJSON result of
159− Success a -> pure a
160− Error _ -> Nothing
161−#else
162− decode $ LBS.fromStrict $ encodeUtf8 $ textFromJSString t
163−#endif
164−
165−safeJsonParse :: JSString -> JSM (Maybe JSVal)
166−safeJsonParse a = (Just <$> jsonParse a) `JS.catch` \(_ :: SomeException) -> return Nothing
167−
168−jsonParse :: JSString -> JSM JSVal
169−jsonParse a = JS.jsg "JSON" ^. JS.js1 "parse" a
170−
171131#ifdef USE_TEMPLATE_HASKELL
172132makeLensesWith (lensRules & simpleLenses .~ True) ''WebSocketConfig
173133makeLensesWith (lensRules & simpleLenses .~ True) ''RawWebSocket