Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose global sink #751

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/Miso.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
module Miso
( miso
, startApp
, sink
, module Miso.Effect
, module Miso.Event
, module Miso.Html
Expand All @@ -41,17 +42,18 @@ import Control.Monad
import Control.Monad.IO.Class
import Data.IORef
import Data.List
import Data.Sequence ((|>))
import Data.Sequence ((|>))
import qualified Data.Sequence as S
import qualified JavaScript.Object.Internal as OI
import System.IO.Unsafe
import System.Mem.StableName
import qualified Data.Sequence as S
import qualified JavaScript.Object.Internal as OI

#ifndef ghcjs_HOST_OS
import Language.Javascript.JSaddle (eval, waitForAnimationFrame)
import Language.Javascript.JSaddle (eval, waitForAnimationFrame)
#ifdef IOS
import Miso.JSBits
#else
import GHCJS.Types (JSString)
import GHCJS.Types (JSString)
import Data.FileEmbed
#endif
#else
Expand Down Expand Up @@ -99,6 +101,8 @@ common App {..} m getView = do
let writeEvent a = void . liftIO . forkIO $ do
atomicModifyIORef' actionsRef $ \as -> (as |> a, ())
notify
-- init global sink
liftIO (writeIORef sinkRef writeEvent)
-- init Subs
forM_ subs $ \sub ->
sub writeEvent
Expand Down Expand Up @@ -152,6 +156,16 @@ miso f = do
-- Create virtual dom, perform initial diff
liftIO (newIORef initialVTree)

sinkRef :: IORef (Sink action)
{-# NOINLINE sinkRef #-}
sinkRef = unsafePerformIO $ newIORef (\_ -> pure ())

-- | Global sink exposed as a backdoor
-- Meant for usage in long running IO actions, or custom callbacks
-- Good for integrating with third-party components.
sink :: Sink action
sink = unsafePerformIO (readIORef sinkRef)

-- | Runs a miso application
startApp :: Eq model => App model action -> JSM ()
startApp app@App {..} =
Expand Down
Loading