Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
NickSeagull committed Aug 2, 2024
1 parent 27db778 commit e52e256
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion core/nhcore.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ library
Command,
Html,
Platform,
Subscription,
Trigger,

-- Concurrency
AsyncIO,
Expand Down
4 changes: 2 additions & 2 deletions core/platform/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ map f (Command commands) =
|> Array.push (ANON {name = MapCommand, payload = Unknown.fromValue f})
|> Command

-- FIXME: Rather than applying this complex mapping, we should just setup a subscription for each command and apply the mapping
-- when the command is triggered, handled, and passed through that subscription
-- FIXME: Rather than applying this complex mapping, we should just setup a trigger for each command and apply the mapping
-- when the command is triggered, handled, and passed through that trigger
processBatch ::
forall (value :: Type).
(Unknown.Convertible value) =>
Expand Down
17 changes: 17 additions & 0 deletions core/platform/Platform.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module Platform
where

import Appendable ((++))
import Array (Array)
import Array qualified
import AsyncIO qualified
import Basics
import Brick qualified
Expand All @@ -26,6 +28,8 @@ import Map qualified
import Maybe (Maybe (..))
import Text (Text)
import ToText (Show (..), ToText, toText)
import Trigger (Trigger (..))
import Trigger qualified
import Unknown qualified
import Var (Var)
import Var qualified
Expand All @@ -42,6 +46,7 @@ type UserApp (model :: Type) (msg :: Type) =
Record
'[ "init" := (model, Command msg),
"view" := (model -> View),
"triggers" := Array (Trigger msg),
"update" := (msg -> model -> (model, Command msg))
]

Expand Down Expand Up @@ -118,6 +123,18 @@ init userApp = do

-- PRIVATE

runTriggers ::
forall (msg :: Type).
Array (Trigger msg) ->
Channel msg ->
IO ()
runTriggers triggers eventsQueue = do
let triggerDispatch (Trigger process) =
process (Channel.write eventsQueue)
triggers
|> Array.map triggerDispatch -- FIXME: Make Async
|> Array.iterate

getState ::
forall (msg :: Type).
RuntimeState msg ->
Expand Down
18 changes: 9 additions & 9 deletions core/platform/Subscription.hs → core/platform/Trigger.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Subscription
( Subscription,
module Trigger
( Trigger,
new,
everyMilliseconds,
)
Expand All @@ -9,28 +9,28 @@ import AsyncIO qualified
import Basics

-- |
-- A subscription is a way to listen to a process that's running in the background and
-- A trigger is a way to listen to a process that's running in the background and
-- generates events. When you tell NeoHaskell to subscribe to a process, NeoHaskell will
-- pass a callback function that posts an event to the event queue whenever the process
-- generates an event. The callback function is called a `dispatch` function.
--
-- An example Subscription is `Time.every`, which generates an event every specified
-- An example Trigger is `Time.every`, which generates an event every specified
-- milliseconds. When you subscribe to `Time.every`, you pass a callback function that
-- posts an event to the event queue whenever the process ticks.
newtype Subscription (message :: Type) = Subscription ((message -> IO ()) -> IO ())
newtype Trigger (message :: Type) = Trigger ((message -> IO ()) -> IO ())

new ::
forall (message :: Type).
((message -> IO ()) -> IO ()) ->
Subscription message
new processConstructor = Subscription processConstructor
Trigger message
new processConstructor = Trigger processConstructor

-- | Create a subscription that generates an event every specified milliseconds.
-- | Create a trigger that generates an event every specified milliseconds.
everyMilliseconds ::
forall (message :: Type).
Int ->
(Int -> message) ->
Subscription message
Trigger message
everyMilliseconds milliseconds messageConstructor =
-- TODO: Move to Time module
new \dispatch -> forever do
Expand Down

0 comments on commit e52e256

Please sign in to comment.