Skip to content

Commit 4148128

Browse files
committed
wip
1 parent 6fbfa06 commit 4148128

File tree

4 files changed

+42
-39
lines changed

4 files changed

+42
-39
lines changed

ghcjs/delivery-calculator/src/App/Types.hs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ module App.Types
1313
newFieldPairId,
1414
newTotal,
1515
inspectExchangeRate,
16-
unShareUri,
17-
stShortUri,
18-
stLongUri,
16+
mkShortUri,
17+
unShortUri,
18+
mkLongUri,
19+
unLongUri,
1920
emitter,
2021
icon,
2122
vsn,
@@ -415,17 +416,19 @@ foldFieldPair :: Rational -> FieldPair DynamicField f -> Rational
415416
foldFieldPair acc =
416417
foldField acc . fieldPairValue
417418

418-
stShortUri :: (MonadThrow m) => Model -> m URI
419-
stShortUri st = do
419+
mkShortUri :: (MonadThrow m) => Model -> m URI
420+
mkShortUri st = do
420421
uri <- mkURI $ from @Unicode @Prelude.Text baseUri
421422
let qxs = toQuery . uniqueToIdentity $ modelState st
422423
pure $ uri {URI.uriQuery = qxs}
423424

424-
stLongUri :: (MonadThrow m) => Model -> m URI
425-
stLongUri = stUri
425+
unShortUri :: (MonadIO m, MonadThrow m) => URI -> m (St Unique)
426+
unShortUri uri = do
427+
st <- either throw pure . fromQuery $ URI.uriQuery uri
428+
identityToUnique st
426429

427-
stUri :: (MonadThrow m) => Model -> m URI
428-
stUri st = do
430+
mkLongUri :: (MonadThrow m) => Model -> m URI
431+
mkLongUri st = do
429432
uri <- mkURI $ from @Unicode @Prelude.Text baseUri
430433
qxs <-
431434
stQuery
@@ -452,26 +455,13 @@ stUri st = do
452455
{ URI.uriQuery = qxs
453456
}
454457

455-
stQuery :: (MonadThrow m) => St Identity -> m [URI.QueryParam]
456-
stQuery st = do
457-
kSt <- URI.mkQueryKey "d"
458-
vSt <- URI.mkQueryValue <=< encode $ encodeBinary st
459-
pure [URI.QueryParam kSt vSt]
460-
where
461-
encode :: (MonadThrow m) => BL.ByteString -> m Text
462-
encode =
463-
either throw pure
464-
. decodeUtf8Strict
465-
. B64URL.encode
466-
. from @BL.ByteString @ByteString
467-
468-
unShareUri ::
458+
unLongUri ::
469459
( MonadIO m,
470460
MonadThrow m
471461
) =>
472462
URI ->
473463
m (Maybe (St Unique))
474-
unShareUri uri = do
464+
unLongUri uri = do
475465
kSt <- URI.mkQueryKey "d"
476466
case qsGet kSt $ URI.uriQuery uri of
477467
Nothing -> pure Nothing
@@ -484,6 +474,19 @@ unShareUri uri = do
484474
pure
485475
$ Just uSt
486476

477+
stQuery :: (MonadThrow m) => St Identity -> m [URI.QueryParam]
478+
stQuery st = do
479+
kSt <- URI.mkQueryKey "d"
480+
vSt <- URI.mkQueryValue <=< encode $ encodeBinary st
481+
pure [URI.QueryParam kSt vSt]
482+
where
483+
encode :: (MonadThrow m) => BL.ByteString -> m Text
484+
encode =
485+
either throw pure
486+
. decodeUtf8Strict
487+
. B64URL.encode
488+
. from @BL.ByteString @ByteString
489+
487490
baseUri :: Unicode
488491
#ifdef GHCID
489492
baseUri =

ghcjs/delivery-calculator/src/App/Widgets/Templates.hs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ newModel ::
1515
Web.Opts ->
1616
MVar (Action -> IO ()) ->
1717
Maybe Model ->
18-
URI ->
18+
Maybe (St Unique) ->
1919
m Model
20-
newModel webOpts sink mSt uri = do
21-
defSt <- maybe (liftIO newSt) pure $ mSt ^? _Just . #modelState
22-
mApp <- unShareUri uri
20+
newModel webOpts sink mMod mApp = do
21+
defSt <- maybe (liftIO newSt) pure $ mMod ^? _Just . #modelState
2322
donate <- newDonateViewer
24-
market <- maybe Rates.newMarket pure $ mSt ^? _Just . #modelMarket
23+
market <- maybe Rates.newMarket pure $ mMod ^? _Just . #modelMarket
2524
ct <- getCurrentTime
2625
pure
2726
Model
@@ -38,7 +37,7 @@ newModel webOpts sink mSt uri = do
3837
modelUriViewer = mempty,
3938
modelDonateViewer = donate,
4039
modelCurrencies =
41-
fromMaybe [btc, usd, rub, cny] (mSt ^? _Just . #modelCurrencies),
40+
fromMaybe [btc, usd, rub, cny] (mMod ^? _Just . #modelCurrencies),
4241
modelWebOpts = webOpts,
4342
modelMarket = market,
4443
modelTime = ct

ghcjs/delivery-calculator/src/Main.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ main =
4747
)
4848
$ do
4949
uri <- URI.mkURI . inspect =<< getCurrentURI
50-
mSt <- unShareUri uri
50+
mSt <- handleAny (const $ pure Nothing) . fmap Just $ unShortUri uri
5151
web <- getWebOpts
5252
sink <- newEmptyMVar
53-
st <- newModel web sink Nothing uri
53+
st <- newModel web sink Nothing mSt
5454
startApp
5555
App
5656
{ model = st,
@@ -148,7 +148,8 @@ updateModel (InitUpdate ext) prevSt = do
148148
.~ False
149149
opfsSync sink nextSt
150150
Just uri -> do
151-
finSt <- newModel (nextSt ^. #modelWebOpts) mvSink (Just nextSt) uri
151+
mSt <- unLongUri uri
152+
finSt <- newModel (nextSt ^. #modelWebOpts) mvSink (Just nextSt) mSt
152153
liftIO
153154
. sink
154155
. PushUpdate
@@ -189,9 +190,9 @@ updateModel (EvalUpdate f) st = do
189190
. PushUpdate
190191
. PureUpdate
191192
$ unload
192-
longUri <- stLongUri next
193+
longUri <- mkLongUri next
193194
Jsm.insertStorage ("current-" <> vsn) longUri
194-
shortUri <- stShortUri next
195+
shortUri <- mkShortUri next
195196
uriViewer <-
196197
newFieldPair mempty
197198
. DynamicFieldText

ghcjs/delivery-calculator/test/App/TypesSpec.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import qualified Optics.Setter as Ops
1111
import Test.Hspec
1212
import Test.Hspec.QuickCheck
1313
import Test.QuickCheck.Instances ()
14-
import qualified Text.URI as URI
1514

1615
data Expr
1716
= Lit Int
@@ -57,8 +56,9 @@ spec = do
5756
-- `shouldBe` Mul (Sub (Lit 2) (Lit 3)) (Lit 4)
5857
it "serialization" $ do
5958
var <- newEmptyMVar
60-
st0 <- newModel Web.defOpts var Nothing =<< URI.mkURI "http://localhost"
61-
uri <- stLongUri st0
62-
st1 <- newModel Web.defOpts var Nothing uri
59+
st0 <- newModel Web.defOpts var Nothing Nothing
60+
uri <- mkLongUri st0
61+
mSt <- unLongUri uri
62+
st1 <- newModel Web.defOpts var Nothing mSt
6363
(st0 ^. #modelState . to uniqueToIdentity)
6464
`shouldBe` (st1 ^. #modelState . to uniqueToIdentity)

0 commit comments

Comments
 (0)