Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move runWidegeAsAff function to Discharge module #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/Concur/Core/Discharge.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ module Concur.Core.Discharge where
import Prelude

import Concur.Core.Types (Widget(..), WidgetStep(..), unWidget)
import Control.Monad.Free (resume, wrap)
import Control.Monad.Free (resume, runFreeM, wrap)
import Control.Monad.Writer (runWriterT, tell)
import Data.Array (singleton)
import Data.Either (Either(..))
import Data.Tuple (Tuple(..))
import Effect (Effect)
import Effect.Aff (runAff_)
import Effect.Aff (Aff, runAff_)
import Effect.Aff.Class (liftAff)
import Effect.Class (liftEffect)
import Effect.Exception (Error)

-- Widget discharge strategies
Expand Down Expand Up @@ -44,6 +48,19 @@ dischargePartialEffect w = case resume (unWidget w) of
dischargePartialEffect (Widget w')
Left (WidgetStepView ws) -> pure (Tuple (Widget (wrap (WidgetStepView ws))) ws.view)

-- | Dischage all effects and recieve the result and viewss as Array.
-- | Mainly for testing.
-- | Be carefull that never ending Widget will convert to never ending Aff.
dischargeAll :: forall v a. Widget v a -> Aff { result :: a, views :: Array v }
dischargeAll widget = do
Tuple result views <- runWriterT $ runFreeM interpret (unWidget widget)
pure { result, views }
where
interpret (WidgetStepEff eff) =
liftEffect eff
interpret (WidgetStepView rec) = do
tell $ singleton rec.view
liftAff rec.cont
{-
-- | Discharge a widget, forces async resolution of the continuation.
-- | 1. Runs the Effect action
Expand Down
28 changes: 0 additions & 28 deletions test/Test/Utils.purs

This file was deleted.

6 changes: 3 additions & 3 deletions test/Test/WidgetSpec.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Test.WidgetSpec where

import Prelude

import Concur.Core.Discharge (dischargeAll)
import Concur.Core.Types (affAction)
import Control.MultiAlternative (orr)
import Data.Time.Duration (Milliseconds(..))
Expand All @@ -10,15 +11,14 @@ import Effect.Class (liftEffect)
import Effect.Ref as Ref
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual, shouldReturn)
import Test.Utils (runWidgetAsAff)

widgetSpec :: Spec Unit
widgetSpec =
describe "Widget" do
describe "orr" do
it "should cancel running effects when the widget returns a value" do
ref <- liftEffect $ Ref.new ""
{ views } <- runWidgetAsAff $ orr
{ views } <- dischargeAll $ orr
[ affAction "a" do
delay (Milliseconds 100.0)
liftEffect $ Ref.write "a" ref
Expand All @@ -33,7 +33,7 @@ widgetSpec =

it "should start all the widgets only once" do
ref <- liftEffect (Ref.new 0)
{ result, views } <- runWidgetAsAff $ orr
{ result, views } <- dischargeAll $ orr
[ do
affAction "a0" $ delay (Milliseconds 100.0)
affAction "a1" $ delay (Milliseconds 100.0)
Expand Down