Skip to content

Commit

Permalink
Add useSteppingAff hook (#72)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Shipman <jason.shipman@narvar.com>
  • Loading branch information
jship and Jason Shipman authored May 30, 2023
1 parent d637dd6 commit 2a0b598
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/React/Basic/Hooks/Aff.purs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module React.Basic.Hooks.Aff
( useAff
, useSteppingAff
, UseAff(..)
, useAffReducer
, AffReducer
Expand Down Expand Up @@ -37,11 +38,33 @@ useAff ::
deps ->
Aff a ->
Hook (UseAff deps a) (Maybe a)
useAff deps aff =
useAff = useAff' (const Nothing)

--| A variant of `useAff` where the asynchronous effect's result is preserved up
--| until the next run of the asynchronous effect _completes_.
--|
--| Contrast this with `useAff`, where the asynchronous effect's result is
--| preserved only up until the next run of the asynchronous effect _starts_.
useSteppingAff ::
forall deps a.
Eq deps =>
deps ->
Aff a ->
Hook (UseAff deps a) (Maybe a)
useSteppingAff = useAff' identity

useAff' ::
forall deps a.
Eq deps =>
(Maybe (Either Error a) -> Maybe (Either Error a)) ->
deps ->
Aff a ->
Hook (UseAff deps a) (Maybe a)
useAff' initUpdater deps aff =
coerceHook React.do
result /\ setResult <- useState Nothing
useEffect deps do
setResult (const Nothing)
setResult initUpdater
fiber <-
launchAff do
r <- try aff
Expand Down

0 comments on commit 2a0b598

Please sign in to comment.