Skip to content

Commit

Permalink
Expose global sink
Browse files Browse the repository at this point in the history
  • Loading branch information
dmjio committed Oct 31, 2024
1 parent 0305b26 commit 002eab7
Showing 1 changed file with 19 additions and 5 deletions.
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 globalSinkRef)

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

0 comments on commit 002eab7

Please sign in to comment.