Skip to content

Commit 72ce559

Browse files
committed
wip
1 parent 98727c5 commit 72ce559

File tree

2 files changed

+76
-65
lines changed

2 files changed

+76
-65
lines changed

ghcjs/delivery-calculator/src/Main.hs

Lines changed: 67 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -170,68 +170,79 @@ updateModel SyncInputs st = do
170170
syncInputs st
171171
pure Noop
172172
]
173-
updateModel (ChanUpdate f) st = do
174-
let prevSt = f st
173+
updateModel (ChanUpdate update0) st0 = do
174+
let st1 = update0 st0
175175
batchEff
176-
prevSt
176+
st1
177177
[ do
178178
--
179179
-- NOTE : Workaround to fix slow rendering after screen switch.
180180
--
181181
sleepMilliSeconds 300
182-
pure SyncInputs
183-
-- do
184-
-- actions <-
185-
-- drainTChan $ prevSt ^. #modelConsumerQueue
186-
-- nextSt <-
187-
-- handleAny
188-
-- ( \e -> do
189-
-- consoleLog e
190-
-- pure $ prevSt & #modelLoading .~ False
191-
-- )
192-
-- $ evalModel
193-
-- =<< foldlM evalUpdate prevSt actions
194-
-- uri <- stUri nextSt
195-
-- Jsm.insertStorage ("favorite-" <> vsn) (nextSt ^. #modelFavMap)
196-
-- Jsm.insertStorage ("current-" <> vsn) uri
197-
-- syncUri uri
198-
-- nextUri <- stUri $ nextSt & #modelState . #stScreen %~ unQrCode
199-
-- uriViewer <-
200-
-- newFieldPair mempty
201-
-- . DynamicFieldText
202-
-- . from @Prelude.String @Unicode
203-
-- $ URI.renderStr nextUri
204-
-- let finSt =
205-
-- nextSt
206-
-- & #modelUriViewer
207-
-- %~ mergeFieldPairs
208-
-- [ uriViewer
209-
-- & #fieldPairValue
210-
-- . #fieldOpts
211-
-- . #fieldOptsQrState
212-
-- .~ Just Opened
213-
-- ]
214-
-- if finSt ^. #modelLoading
215-
-- then do
216-
-- void
217-
-- . spawnLink
218-
-- . deepseq (viewModel finSt)
219-
-- . pushActionQueue prevSt
220-
-- . Instant
221-
-- . PureUpdate
222-
-- . const
223-
-- $ finSt
224-
-- & #modelLoading
225-
-- .~ False
226-
-- pure
227-
-- . ChanUpdate
228-
-- $ #modelLoading
229-
-- .~ True
230-
-- else
231-
-- pure
232-
-- . ChanUpdate
233-
-- $ #modelLoading
234-
-- .~ False
182+
pure SyncInputs,
183+
do
184+
actions <-
185+
drainTChan $ st1 ^. #modelConsumerQueue
186+
update1 <-
187+
foldlM
188+
( \acc upd -> do
189+
fun <- unUpdate upd
190+
pure $ fun . acc
191+
)
192+
id
193+
actions
194+
let st2 = update1 st1
195+
update2 <-
196+
handleAny
197+
( \e -> do
198+
consoleLog e
199+
pure $ #modelLoading .~ False
200+
)
201+
$ evalModel st2
202+
let st3 = update2 st2
203+
uri <- stUri st3
204+
Jsm.insertStorage ("favorite-" <> vsn) $ st3 ^. #modelFavMap
205+
Jsm.insertStorage ("current-" <> vsn) uri
206+
syncUri uri
207+
nextUri <- stUri $ st3 & #modelState . #stScreen %~ unQrCode
208+
uriViewer <-
209+
newFieldPair mempty
210+
. DynamicFieldText
211+
. from @Prelude.String @Unicode
212+
$ URI.renderStr nextUri
213+
let update3 =
214+
#modelUriViewer
215+
%~ mergeFieldPairs
216+
[ uriViewer
217+
& #fieldPairValue
218+
. #fieldOpts
219+
. #fieldOptsQrState
220+
.~ Just Opened
221+
]
222+
let st4 = update3 st3
223+
if st4 ^. #modelLoading
224+
then do
225+
void
226+
. spawnLink
227+
. deepseq (viewModel st4)
228+
. pushActionQueue st4
229+
. Instant
230+
. PureUpdate
231+
$ #modelLoading
232+
.~ False
233+
pure
234+
. ChanUpdate
235+
$ (#modelLoading .~ True)
236+
. update3
237+
. update2
238+
. update1
239+
else
240+
pure
241+
. ChanUpdate
242+
$ (#modelLoading .~ False)
243+
. update3
244+
. update2
245+
. update1
235246
]
236247
updateModel (PushUpdate value) st = do
237248
case instantOrDelayedValue value of

ghcjs/miso-functora/src/Functora/Miso/Types.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ module Functora.Miso.Types
5050
LeadingOrTrailing (..),
5151
OpenedOrClosed (..),
5252
Update (..),
53+
unUpdate,
5354
themeCssFile,
54-
evalUpdate,
5555
module X,
5656
)
5757
where
@@ -546,17 +546,17 @@ data Update model
546546
| PureAndEffectUpdate (model -> model) (JSM ())
547547
deriving stock (Generic)
548548

549+
unUpdate :: Update model -> JSM (model -> model)
550+
unUpdate = \case
551+
PureUpdate f -> pure f
552+
ImpureUpdate g -> g >>= pure
553+
EffectUpdate e -> e >> pure id
554+
PureAndImpureUpdate f g -> g >>= pure . (f .)
555+
PureAndEffectUpdate f e -> e >> pure f
556+
549557
themeCssFile :: Theme -> Unicode
550558
themeCssFile =
551559
(<> ".min.css")
552560
. from @String @Unicode
553561
. Casing.kebab
554562
. inspect @String
555-
556-
evalUpdate :: model -> Update model -> JSM model
557-
evalUpdate x = \case
558-
PureUpdate f -> pure $ f x
559-
ImpureUpdate g -> g >>= pure . ($ x)
560-
EffectUpdate e -> e >> pure x
561-
PureAndImpureUpdate f g -> g >>= pure . ($ f x)
562-
PureAndEffectUpdate f e -> e >> pure (f x)

0 commit comments

Comments
 (0)