我正在尝试实现一个滴答事件,并在下面进行了一个小测试,证明它不起作用。我很欣赏关于为什么它不起作用的洞察力。
gameloop :: TChan UAC ->
IO ()
gameloop commandChannel = do
(tickHandler, tickSink) <- newAddHandler
networkDescr <- compile $ makeNetworkDescription commandChannel tickHandler
actuate networkDescr
forkIO $ forever $ (timer 10) >>= tickSink
return ()
makeNetworkDescription :: forall t . Frameworks t =>
TChan UAC ->
AddHandler () ->
Moment t ()
makeNetworkDescription commandChannel tickHandler = do
eTick <- fromAddHandler tickHandler
bCChannel <- fromPoll $ grabCommands commandChannel
-- test fromPoll
test <- initial bCChannel
liftIO $ appendFile "testPoll.txt" $ show test
-- end fromPoll test
let eCChannel = bCChannel <@ eTick
liftIO $ print "hi there\n"
reactimate $ (\n -> appendFile "Commands.txt" (show n)) <$> eCChannel
grabCommands :: TChan UAC -> IO [UAC]
grabCommands unval = do
result <- (atomically $ readTChan unval) `untilM` (atomically $ isEmptyTChan unval)
liftIO $ print $ show result
return result
timer :: TimeOut -> IO ()
timer ms = do
threadDelay ms
这是一些测试数据。
main :: IO ()
main = do
commandChan <- atomically $ newTChan :: IO (TChan UAC)
forkIO $ gameloop commandChan
liftIO $ print "back from fork\n"
atomically $ populateCC commandChan playerCommands
return ()
populateCC :: TChan UAC -> [UAC] -> STM ()
populateCC pChannel pCommands = mapM_ (writeTChan pChannel) pCommands
playerCommands =
[UAC (PlayerCommand (CommandData (T.pack "1" :: AID) Look) (T.pack "1")),
UAC (PlayerCommand (CommandData (T.pack "2" :: AID) (Move Mongo)) (T.pack "2"))
]
当我执行上述操作时,Main
我得到了这个输出。
"back from fork\n"
"[UAC (PlayerCommand (CommandData \"1\" Look) \"1\"),UAC (PlayerCommand (CommandData \"2\" (Move Mongo)) \"2\")]"
"hi there\n"
该文件Commands.txt
永远不会存在。我将此问题归因于错误的滴答事件。
我从中得到了定时器实现的想法,但想知道我是否以错误的方式思考这个问题。有任何想法吗?
编辑:我想要一些保证fromPoll
是做正确的事。我添加了上面的测试,它是。