Skip to content

Commit

Permalink
Better syntax for useDyn (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Solomon authored Jun 14, 2023
1 parent e383ffc commit 778a6ee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
18 changes: 6 additions & 12 deletions src/Deku/Hooks.purs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ module Deku.Hooks
, useDyn
, useDynAtBeginning
, useDynAtEnd
, Dyn(..)
, (//\\)
) where

import Prelude
Expand All @@ -43,7 +41,7 @@ import Data.Array as Array
import Data.Either (Either(..))
import Data.Foldable (for_)
import Data.Maybe (Maybe(..), fromMaybe)
import Data.Tuple (curry)
import Data.Tuple (Tuple(..), curry)
import Data.Tuple.Nested (type (/\), (/\))
import Deku.Core (Nut(..), NutF(..), Node, Child, bus, bussedUncurried, insert, remove, sendToPos, dyn)
import Deku.Do as Deku
Expand Down Expand Up @@ -162,20 +160,16 @@ useMailboxed f = bussedUncurried fx
ee :: Event Nut
ee = mailboxed b \c -> f (a /\ c)

data Dyn a = Dyn (a -> Int) a

infixl 4 Dyn as //\\

useDyn
:: forall value
. Event (Dyn value)
. Event (Tuple Int value)
-> ( { value :: value, remove :: Effect Unit, sendTo :: Int -> Effect Unit }
-> Nut
)
-> Nut
useDyn e f = dyn $ map
( \(Dyn fi value) -> Deku.do
{ remove, sendTo } <- useDyn' (fi value)
( \(Tuple pos value) -> Deku.do
{ remove, sendTo } <- useDyn' pos
f { remove, sendTo, value }
)
e
Expand All @@ -187,7 +181,7 @@ useDynAtBeginning
-> Nut
)
-> Nut
useDynAtBeginning e = useDyn ((const 0 //\\ _) <$> e)
useDynAtBeginning e = useDyn ((0 /\ _) <$> e)

useDynAtEnd
:: forall value
Expand All @@ -196,7 +190,7 @@ useDynAtEnd
-> Nut
)
-> Nut
useDynAtEnd e = useDyn (mapAccum (\a b -> (a + 1) /\ (const a //\\ b)) 0 e)
useDynAtEnd e = useDyn (mapAccum (\a b -> (a + 1) /\ (a /\ b)) 0 e)

useDyn'
:: Int
Expand Down
4 changes: 2 additions & 2 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Deku.Control (blank, globalPortal1, portal1, switcher, text, text_, (<#~>
import Deku.Core (Hook, Nut, fixed)
import Deku.DOM as D
import Deku.Do as Deku
import Deku.Hooks (useDyn, useDynAtBeginning, useDynAtEnd, useEffect, useMemoized, useRef, useState, useState', (//\\))
import Deku.Hooks (useDyn, useDynAtBeginning, useDynAtEnd, useEffect, useMemoized, useRef, useState, useState')
import Deku.Lifecycle (onDidMount, onDismount, onWillMount)
import Deku.Listeners (click, click_)
import Deku.Pursx ((~~))
Expand Down Expand Up @@ -163,7 +163,7 @@ insertsAtCorrectPositions = D.div [ id_ "div0" ]
-- are inserted in the scrambled order so that they read
-- 0-1-2-3-4 from top to bottom
{ value: i } <- useDyn
((identity //\\ _) <$> oneOfMap pure [ 3, 0, 4, 2, 1 ])
((Tuple <*> identity) <$> oneOfMap pure [ 3, 0, 4, 2, 1 ])
D.span [ id_ ("dyn" <> show i) ] [ text_ (show i) ]
]

Expand Down

0 comments on commit 778a6ee

Please sign in to comment.