|
6 | 6 | ---------------------------------------------------------------------------- |
7 | 7 | module Main where |
8 | 8 | ---------------------------------------------------------------------------- |
9 | | -import Control.Monad (void, when) |
| 9 | +import Control.Concurrent (threadDelay) |
| 10 | +import Control.Monad (forever, when) |
10 | 11 | import Data.Foldable (toList) |
11 | | -import Data.IORef |
12 | 12 | import Data.Sequence (Seq, ViewL(..), ViewR(..)) |
13 | 13 | import qualified Data.Sequence as Seq |
14 | 14 | import Data.Set (Set) |
15 | 15 | import qualified Data.Set as Set |
16 | 16 | ---------------------------------------------------------------------------- |
17 | 17 | import Miso hiding (Phase) |
18 | 18 | import Miso.CSS hiding (ms, background, Phase) |
19 | | -import Miso.DSL (syncCallback1, requestAnimationFrame, fromJSValUnchecked, freeFunction, Function(..)) |
20 | 19 | import qualified Miso.Html as H |
21 | 20 | import qualified Miso.Html.Property as HP |
22 | 21 | import qualified Miso.Svg as S |
23 | 22 | import qualified Miso.Svg.Property as SP |
24 | 23 | import Miso.Lens |
25 | 24 | import Miso.Random (replicateRM) |
26 | 25 | import Miso.Reload |
27 | | -import Miso.Subscription.Util (createSub) |
28 | 26 | ---------------------------------------------------------------------------- |
29 | 27 |
|
30 | 28 | gridSize :: Int |
@@ -129,29 +127,10 @@ emptyModel = Model |
129 | 127 | , _prevSnake = initSnake |
130 | 128 | } |
131 | 129 |
|
132 | | -rAFSubElapsed :: Double -> action -> Sub action |
133 | | -rAFSubElapsed interval action sink = createSub acquire release sink |
134 | | - where |
135 | | - acquire = do |
136 | | - cbRef <- newIORef (error "rAFSubElapsed: uninitialized, impossible") |
137 | | - let go lastT elap = do |
138 | | - cb <- syncCallback1 $ \jsval -> do |
139 | | - t <- fromJSValUnchecked jsval |
140 | | - let dt = if lastT == 0 then 0 else min interval (t - lastT) |
141 | | - newElap = elap + dt |
142 | | - if newElap >= interval |
143 | | - then sink action *> go t (newElap - interval) |
144 | | - else go t newElap |
145 | | - writeIORef cbRef cb |
146 | | - void (requestAnimationFrame cb) |
147 | | - go 0 0 |
148 | | - pure cbRef |
149 | | - release cbRef = freeFunction . Function =<< readIORef cbRef |
150 | | - |
151 | 130 | app :: App Model Action |
152 | 131 | app = (component emptyModel updateModel viewModel) |
153 | 132 | { subs = |
154 | | - [ rAFSubElapsed tickInterval Tick |
| 133 | + [ \sink -> forever (threadDelay (round (tickInterval * 1000)) >> sink Tick) |
155 | 134 | , \sink -> windowSub "keydown" keycodeDecoder (\case |
156 | 135 | KeyCode 37 -> Turn DLeft |
157 | 136 | KeyCode 38 -> Turn DUp |
|
0 commit comments