@@ -7,7 +7,8 @@ module Language.Javascript.JSaddle.WKWebView.Internal
9import Control.Monad (void, join)
9import Control.Monad (void, join)
10−import Control.Concurrent (forkIO)
10+import Control.Concurrent (forkIO, forkOS)
11+import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
12import Data.Monoid ((<>))
13import Data.Monoid ((<>))
13import Data.ByteString (useAsCString, packCString)
14import Data.ByteString (useAsCString, packCString)
@@ -37,9 +38,8 @@ foreign import ccall mainBundleResourcePathC :: IO CString
37-- | Run JSaddle in WKWebView
38-- | Run JSaddle in WKWebView
38jsaddleMain :: JSM () -> WKWebView -> IO ()
39jsaddleMain :: JSM () -> WKWebView -> IO ()
39jsaddleMain f webView = do
40jsaddleMain f webView = do
40− jsaddleMain' f webView
41+ jsaddleMain' f webView $
42− useAsCString (toStrict indexHtml) $ loadHTMLString webView
42+ useAsCString (toStrict indexHtml) $ loadHTMLString webView
44-- | Run JSaddle in a WKWebView first loading the specified file
44-- | Run JSaddle in a WKWebView first loading the specified file
45-- from the mainBundle (relative to the resourcePath).
45-- from the mainBundle (relative to the resourcePath).
@@ -47,23 +47,27 @@ jsaddleMainFile :: BS.ByteString -- ^ The file to navigate to.
47 -> BS.ByteString -- ^ The path to allow read access to.
47 -> BS.ByteString -- ^ The path to allow read access to.
48 -> JSM () -> WKWebView -> IO ()
48 -> JSM () -> WKWebView -> IO ()
49jsaddleMainFile url allowing f webView = do
49jsaddleMainFile url allowing f webView = do
50− jsaddleMain' f webView
50+ jsaddleMain' f webView $
51+ useAsCString url $ \u ->
52+ useAsCString allowing $ \a ->
53+ loadBundleFile webView u a
52− useAsCString url $ \u ->
55+jsaddleMain' :: JSM () -> WKWebView -> IO () -> IO ()
53− useAsCString allowing $ \a ->
56+jsaddleMain' f webView loadHtml = do
54− loadBundleFile webView u a
57+ ready <- newEmptyMVar
56−jsaddleMain' :: JSM () -> WKWebView -> IO ()
57−jsaddleMain' f webView = do
58 (processResult, start) <- runJavaScript (\batch ->
59 (processResult, start) <- runJavaScript (\batch ->
59 useAsCString (toStrict $ "runJSaddleBatch(" <> encode batch <> ");") $
60 useAsCString (toStrict $ "runJSaddleBatch(" <> encode batch <> ");") $
60 evaluateJavaScript webView)
61 evaluateJavaScript webView)
63− startHandler <- newStablePtr (
64+ startHandler <- newStablePtr (putMVar ready ())
64− useAsCString (toStrict jsaddleJs) (evaluateJavaScript webView) >> void (forkIO start))
65 resultHandler <- newStablePtr processResult
65 resultHandler <- newStablePtr processResult
66 addJSaddleHandler webView startHandler resultHandler
66 addJSaddleHandler webView startHandler resultHandler
70+ useAsCString (toStrict jsaddleJs) (evaluateJavaScript webView) >> void (forkIO start)
68jsaddleStart :: StablePtr (IO ()) -> IO ()
72jsaddleStart :: StablePtr (IO ()) -> IO ()
69jsaddleStart ptrHandler = join (deRefStablePtr ptrHandler)
73jsaddleStart ptrHandler = join (deRefStablePtr ptrHandler)