Skip to content

Commit

Permalink
Merge pull request #1099 from obsidiansystems/better-pair-routes
Browse files Browse the repository at this point in the history
Add `pairRoute`
  • Loading branch information
Ericson2314 authored Oct 2, 2024
2 parents c9af1a8 + e22125e commit a294a75
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
3 changes: 2 additions & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ This project's release branch is `master`. This log is written from the perspect

## Unreleased

* Obelisk.Route: add pathQueryEncoder and generalizeIdentity
* [#1038](https://github.com/obsidiansystems/obelisk/pull/1038): `Obelisk.Route`: Add `pathQueryEncoder` and `generalizeIdentity`
* [#1071](https://github.com/obsidiansystems/obelisk/pull/1071): Support deployment information repository sub-directories
* [#1086](https://github.com/obsidiansystems/obelisk/pull/1086): Delete extraneous config files during deploy
* [#1099](https://github.com/obsidiansystems/obelisk/pull/1099): `Obelisk.Route`: Add `pairRoute` and deprecate `subPairRoute` and `subPairRoute_`

## v1.3.0.0
* [#1047](https://github.com/obsidiansystems/obelisk/pull/1047): Update default ios sdk to 15
Expand Down
26 changes: 22 additions & 4 deletions lib/route/src/Obelisk/Route/Frontend.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Obelisk.Route.Frontend
, mapRoutedT
, subRoute
, subRoute_
, pairRoute
, subPairRoute
, subPairRoute_
, maybeRoute
Expand Down Expand Up @@ -216,14 +217,31 @@ subRoute_ :: (MonadFix m, MonadHold t m, GEq r, Adjustable t m) => (forall a. r
subRoute_ f = factorRouted $ strictDynWidget_ $ \(c :=> r') -> do
runRoutedT (f c) r'

-- | Like 'subRoute_', but with a pair rather than an R
subPairRoute_ :: (MonadFix m, MonadHold t m, Eq a, Adjustable t m) => (a -> RoutedT t b m ()) -> RoutedT t (a, b) m ()
subPairRoute_ f = withRoutedT (fmap (\(a, b) -> Const2 a :/ b)) $ subRoute_ (\(Const2 a) -> f a)

subRoute :: (MonadFix m, MonadHold t m, GEq r, Adjustable t m) => (forall a. r a -> RoutedT t a m b) -> RoutedT t (R r) m (Dynamic t b)
subRoute f = factorRouted $ strictDynWidget $ \(c :=> r') -> do
runRoutedT (f c) r'

-- | Pass the left side of the dynamic pair to the function, which then
-- routes over the right side.
pairRoute
:: (Monad m, Reflex t)
=> (Dynamic t rl -> RoutedT t rr m a)
-> RoutedT t (rl, rr) m a
pairRoute f = do
r <- askRoute
withRoutedT
(fmap snd)
(f $ fmap fst r)

{-# DEPRECATED
subPairRoute, subPairRoute_
"Use 'pairRoute' instead. This function unnecessarily eliminates the 'Dynamic' for the left side, which is poor taste. In general, we want to eliminate dynamics as little as possible as we case on the route, so when the router changes DOM is not unnecessarily recomputed."
#-}

-- | Like 'subRoute_', but with a pair rather than an R
subPairRoute_ :: (MonadFix m, MonadHold t m, Eq a, Adjustable t m) => (a -> RoutedT t b m ()) -> RoutedT t (a, b) m ()
subPairRoute_ f = withRoutedT (fmap (\(a, b) -> Const2 a :/ b)) $ subRoute_ (\(Const2 a) -> f a)

-- | Like 'subRoute_', but with a pair rather than an R
subPairRoute :: (MonadFix m, MonadHold t m, Eq a, Adjustable t m) => (a -> RoutedT t b m c) -> RoutedT t (a, b) m (Dynamic t c)
subPairRoute f = withRoutedT (fmap (\(a, b) -> Const2 a :/ b)) $ subRoute (\(Const2 a) -> f a)
Expand Down

0 comments on commit a294a75

Please sign in to comment.