From 583430dd02da0e8af7636d05083c191fb3cd1f99 Mon Sep 17 00:00:00 2001 From: Thomas Honeyman Date: Sat, 7 Aug 2021 18:05:51 -0400 Subject: [PATCH] Migrate UseSelector module and run purs-tidy (#5) --- README.md | 21 ++++++++++++------- example/basic-hooks/Basic/Counter.purs | 13 ++++++------ example/basic-no-action/NoAction/Counter.purs | 2 +- example/basic/Basic/Counter.purs | 2 +- .../ReduxTodo/Component/AddTodo.purs | 2 +- .../ReduxTodo/Component/FilterLink.purs | 2 +- .../ReduxTodo/Component/TodoList.purs | 2 +- example/redux-todo/ReduxTodo/Store.purs | 1 - example/redux-todo/ReduxTodo/Store/Todos.purs | 2 +- .../ReduxTodo/Store/Visibility.purs | 2 +- packages.dhall | 2 +- src/Halogen/Store/Connect.purs | 2 +- .../Store/{Hooks => }/UseSelector.purs | 20 +++++++++--------- 13 files changed, 39 insertions(+), 34 deletions(-) rename src/Halogen/Store/{Hooks => }/UseSelector.purs (71%) diff --git a/README.md b/README.md index 2320c30..a730a4d 100644 --- a/README.md +++ b/README.md @@ -199,22 +199,27 @@ main = launchAff_ do runUI root unit body ``` -### Using store with hooks +### Using `halogen-store` with `halogen-hooks` -If you want to write your component with [Halogen Hooks library](https://github.com/thomashoneyman/purescript-halogen-hooks) ,then you can use `useSelector` hook to access store. It takes selector and return the part of current store retrieved via given selector. +If you want to write your component with [Halogen Hooks](https://github.com/thomashoneyman/purescript-halogen-hooks) ,then you can use the `useSelector` hook to access the store. ```purs +module Main where + +import Prelude + import Halogen.Hooks as Hooks -import Halogen.Store.Hooks (useSelector) import Halogen.Store.Select (selectAll) +import Halogen.Store.UseSelector (useSelector) -component :: forall q i o m - . MonadStore BS.Action BS.Store m - => H.Component q i o m +component + :: forall q i o m + . MonadStore BS.Action BS.Store m + => H.Component q i o m component = Hooks.component \_ _ -> Hooks.do - ctx <- useSelector selectAll + context <- useSelector selectAll Hooks.pure do ... ``` -Unlike the case with connect, though, context returned by `useSelector` hook has type `Maybe store`, because the hook does not have access to store before it has been initialized. \ No newline at end of file +Unlike `connect`, the context returned by `useSelector` has the type `Maybe store` because the hook does not have access to the store before it is initialized. diff --git a/example/basic-hooks/Basic/Counter.purs b/example/basic-hooks/Basic/Counter.purs index 90acf51..e2a45f4 100644 --- a/example/basic-hooks/Basic/Counter.purs +++ b/example/basic-hooks/Basic/Counter.purs @@ -8,13 +8,14 @@ import Halogen as H import Halogen.HTML as HH import Halogen.HTML.Events as HE import Halogen.Hooks as Hooks -import Halogen.Store.Hooks.UseSelector (useSelector) import Halogen.Store.Monad (class MonadStore, updateStore) import Halogen.Store.Select (selectEq) +import Halogen.Store.UseSelector (useSelector) -component :: forall q i o m - . MonadStore BS.Action BS.Store m - => H.Component q i o m +component + :: forall q i o m + . MonadStore BS.Action BS.Store m + => H.Component q i o m component = Hooks.component \_ _ -> Hooks.do count <- useSelector $ selectEq _.count Hooks.pure do @@ -22,9 +23,9 @@ component = Hooks.component \_ _ -> Hooks.do HH.div_ [ HH.button [ HE.onClick \_ -> updateStore BS.Increment ] - [ HH.text "Increment"] + [ HH.text "Increment" ] , HH.text $ " Count: " <> show cnt <> " " , HH.button [ HE.onClick \_ -> updateStore BS.Decrement ] [ HH.text "Decrement" ] - ] \ No newline at end of file + ] diff --git a/example/basic-no-action/NoAction/Counter.purs b/example/basic-no-action/NoAction/Counter.purs index b2fa98e..c4a48bb 100644 --- a/example/basic-no-action/NoAction/Counter.purs +++ b/example/basic-no-action/NoAction/Counter.purs @@ -44,7 +44,7 @@ component = connect selectState $ H.mkComponent HH.div_ [ HH.button [ HE.onClick \_ -> Increment ] - [ HH.text "Increment"] + [ HH.text "Increment" ] , HH.text $ " Count: " <> show count <> " " , HH.button [ HE.onClick \_ -> Decrement ] diff --git a/example/basic/Basic/Counter.purs b/example/basic/Basic/Counter.purs index 50eb6fd..8af3ee9 100644 --- a/example/basic/Basic/Counter.purs +++ b/example/basic/Basic/Counter.purs @@ -41,7 +41,7 @@ component = connect selectCount $ H.mkComponent HH.div_ [ HH.button [ HE.onClick \_ -> Increment ] - [ HH.text "Increment"] + [ HH.text "Increment" ] , HH.text $ " Count: " <> show count <> " " , HH.button [ HE.onClick \_ -> Decrement ] diff --git a/example/redux-todo/ReduxTodo/Component/AddTodo.purs b/example/redux-todo/ReduxTodo/Component/AddTodo.purs index 3db8fb7..3e1bc77 100644 --- a/example/redux-todo/ReduxTodo/Component/AddTodo.purs +++ b/example/redux-todo/ReduxTodo/Component/AddTodo.purs @@ -14,7 +14,7 @@ import ReduxTodo.Store.Todos (createTodo) import Type.Proxy (Proxy(..)) import Web.Event.Event (Event, preventDefault) -type Slot id slots = ( addTodo :: H.Slot (Const Void) Void id | slots ) +type Slot id slots = (addTodo :: H.Slot (Const Void) Void id | slots) addTodo :: forall act slots m diff --git a/example/redux-todo/ReduxTodo/Component/FilterLink.purs b/example/redux-todo/ReduxTodo/Component/FilterLink.purs index de7f59c..f5e8568 100644 --- a/example/redux-todo/ReduxTodo/Component/FilterLink.purs +++ b/example/redux-todo/ReduxTodo/Component/FilterLink.purs @@ -15,7 +15,7 @@ import ReduxTodo.Store as Store import ReduxTodo.Store.Visibility (Visibility, setVisibility) import Type.Proxy (Proxy(..)) -type Slot id slots = ( filterLink :: H.Slot (Const Void) Void id | slots ) +type Slot id slots = (filterLink :: H.Slot (Const Void) Void id | slots) filterLink :: forall action id slots m diff --git a/example/redux-todo/ReduxTodo/Component/TodoList.purs b/example/redux-todo/ReduxTodo/Component/TodoList.purs index 860aea4..540f384 100644 --- a/example/redux-todo/ReduxTodo/Component/TodoList.purs +++ b/example/redux-todo/ReduxTodo/Component/TodoList.purs @@ -19,7 +19,7 @@ import ReduxTodo.Store.Todos (Todo, toggleTodo) import ReduxTodo.Store.Visibility (Visibility(..)) import Type.Proxy (Proxy(..)) -type Slot id slots = ( todoList :: H.Slot (Const Void) Void id | slots ) +type Slot id slots = (todoList :: H.Slot (Const Void) Void id | slots) todoList :: forall action slots m diff --git a/example/redux-todo/ReduxTodo/Store.purs b/example/redux-todo/ReduxTodo/Store.purs index 862b92f..3dfe371 100644 --- a/example/redux-todo/ReduxTodo/Store.purs +++ b/example/redux-todo/ReduxTodo/Store.purs @@ -1,6 +1,5 @@ module ReduxTodo.Store where - import Data.Variant (Variant) import Data.Variant as Variant import ReduxTodo.Store.Todos as Todos diff --git a/example/redux-todo/ReduxTodo/Store/Todos.purs b/example/redux-todo/ReduxTodo/Store/Todos.purs index 3dbfeda..7c3df39 100644 --- a/example/redux-todo/ReduxTodo/Store/Todos.purs +++ b/example/redux-todo/ReduxTodo/Store/Todos.purs @@ -51,7 +51,7 @@ reduce store = case _ of store { todos = newTodos } -type Action' v = ( todos :: Action | v ) +type Action' v = (todos :: Action | v) injAction :: forall v. Action -> Variant (Action' v) injAction = Variant.inj (Proxy :: Proxy "todos") diff --git a/example/redux-todo/ReduxTodo/Store/Visibility.purs b/example/redux-todo/ReduxTodo/Store/Visibility.purs index dc7d0d3..8be35f3 100644 --- a/example/redux-todo/ReduxTodo/Store/Visibility.purs +++ b/example/redux-todo/ReduxTodo/Store/Visibility.purs @@ -41,7 +41,7 @@ reduce store = case _ of SetVisibility visibility -> store { visibility = visibility } -type Action' v = ( visibility :: Action | v ) +type Action' v = (visibility :: Action | v) injAction :: forall v. Action -> Variant (Action' v) injAction = Variant.inj (Proxy :: Proxy "visibility") diff --git a/packages.dhall b/packages.dhall index 4f1a337..7a6905b 100644 --- a/packages.dhall +++ b/packages.dhall @@ -1,4 +1,4 @@ let upstream = - https://github.com/purescript/package-sets/releases/download/psc-0.14.1-20210506/packages.dhall sha256:d199e142515f9cc15838d8e6d724a98cd0ca776ceb426b7b36e841311643e3ef + https://github.com/purescript/package-sets/releases/download/psc-0.14.3-20210722/packages.dhall sha256:1ceb43aa59436bf5601bac45f6f3781c4e1f0e4c2b8458105b018e5ed8c30f8c in upstream diff --git a/src/Halogen/Store/Connect.purs b/src/Halogen/Store/Connect.purs index 1610795..a62b51e 100644 --- a/src/Halogen/Store/Connect.purs +++ b/src/Halogen/Store/Connect.purs @@ -85,7 +85,7 @@ connect (Selector selector) component = Update newContext -> H.gets _.context >>= case _ of Just oldContext | unsafeRefEq oldContext newContext -> pure unit - _ -> H.modify_ _ { context = Just newContext} + _ -> H.modify_ _ { context = Just newContext } Raise output -> H.raise output diff --git a/src/Halogen/Store/Hooks/UseSelector.purs b/src/Halogen/Store/UseSelector.purs similarity index 71% rename from src/Halogen/Store/Hooks/UseSelector.purs rename to src/Halogen/Store/UseSelector.purs index 0f7a871..5ad4fb0 100644 --- a/src/Halogen/Store/Hooks/UseSelector.purs +++ b/src/Halogen/Store/UseSelector.purs @@ -1,4 +1,4 @@ -module Halogen.Store.Hooks.UseSelector where +module Halogen.Store.UseSelector where import Prelude @@ -14,13 +14,13 @@ foreign import data UseSelector :: Type -> Type -> Type -> Hooks.HookType type UseSelector' :: Type -> Type -> Type -> Hooks.HookType type UseSelector' act store ctx = UseState (Maybe ctx) <> UseEffect <> Hooks.Pure -instance newtypeUseSelector - :: HookNewtype (UseSelector act store ctx) (UseSelector' act store ctx) +instance HookNewtype (UseSelector act store ctx) (UseSelector' act store ctx) -useSelector :: forall m act store ctx - . MonadStore act store m - => Selector store ctx - -> Hook m (UseSelector act store ctx) (Maybe ctx) +useSelector + :: forall m act store ctx + . MonadStore act store m + => Selector store ctx + -> Hook m (UseSelector act store ctx) (Maybe ctx) useSelector (Selector selector) = Hooks.wrap hook where hook :: Hook m (UseSelector' act store ctx) (Maybe ctx) @@ -29,7 +29,7 @@ useSelector (Selector selector) = Hooks.wrap hook Hooks.useLifecycleEffect do emitter <- emitSelected (Selector selector) - subscriptionId <- Hooks.subscribe $ map (Hooks.put ctxId <<< Just) emitter + subscriptionId <- Hooks.subscribe $ map (Hooks.put ctxId <<< Just) emitter pure $ Just $ Hooks.unsubscribe subscriptionId - - Hooks.pure ctx \ No newline at end of file + + Hooks.pure ctx