|
1 | 1 | module Concur.Core.Patterns where |
2 | 2 |
|
3 | 3 | import Concur.Core (Result(..), Widget, mkWidget) |
| 4 | +import Concur.Core.Types (runWidget) |
4 | 5 | import Control.Alt (class Alt, (<|>)) |
5 | 6 | import Control.Applicative (pure) |
6 | 7 | import Control.Bind (bind, discard, (>>=)) |
7 | 8 | import Control.Monad (class Monad) |
| 9 | +import Control.MonadFix (fixEffect) |
8 | 10 | import Control.Plus (class Plus) |
9 | 11 | import Data.Array as Array |
10 | 12 | import Data.Either (Either(..), either) |
11 | | -import Data.Function (flip, ($), (<<<), (>>>)) |
| 13 | +import Data.Foldable (fold) |
| 14 | +import Data.Function (applyFlipped, flip, (#), ($), (<<<), (>>>)) |
12 | 15 | import Data.Functor ((<$>)) |
13 | 16 | import Data.Lens (Lens') |
14 | 17 | import Data.Lens as L |
15 | | -import Data.Monoid (class Monoid) |
| 18 | +import Data.Maybe (fromMaybe) |
| 19 | +import Data.Monoid (class Monoid, mempty) |
16 | 20 | import Data.Newtype (class Newtype, un) |
17 | 21 | import Data.Semigroup ((<>)) |
18 | 22 | import Data.Traversable (traverse_) |
| 23 | +import Data.TraversableWithIndex (forWithIndex) |
19 | 24 | import Data.Unit (Unit, unit) |
20 | 25 | import Effect (Effect) |
21 | 26 | import Effect.Class (class MonadEffect, liftEffect) |
| 27 | +import Effect.Ref (Ref) |
22 | 28 | import Effect.Ref as Ref |
23 | 29 | import Unsafe.Reference (unsafeRefEq) |
24 | 30 |
|
@@ -151,3 +157,50 @@ with wire f = do |
151 | 157 | res <- (Left <$> f a) <|> (Right <$> receive wire) |
152 | 158 | either pure go res |
153 | 159 |
|
| 160 | +--- |
| 161 | +-- RESUMABLE WIDGETS (WIP) |
| 162 | + |
| 163 | +type ResumableWidget v a = |
| 164 | + { handlerRef :: Ref (Result v a -> Effect Unit) |
| 165 | + , view :: Ref v |
| 166 | + , canceler :: Effect Unit |
| 167 | + } |
| 168 | + |
| 169 | +resumeResumable :: forall v a. ResumableWidget v a -> Widget v a |
| 170 | +resumeResumable { handlerRef, view, canceler } = mkWidget \cb -> do |
| 171 | + Ref.write cb handlerRef |
| 172 | + v <- Ref.read view |
| 173 | + cb (View v) |
| 174 | + pure canceler |
| 175 | + |
| 176 | +suspendResumable :: forall v a. ResumableWidget v a -> Effect Unit |
| 177 | +suspendResumable { handlerRef } = Ref.write mempty handlerRef |
| 178 | + |
| 179 | +cancelResumable :: forall v a. ResumableWidget v a -> Effect Unit |
| 180 | +cancelResumable { canceler } = canceler |
| 181 | + |
| 182 | +runToResumable :: forall v a. Widget v a -> Ref v -> (Result v a -> Effect Unit) -> Effect (ResumableWidget v a) |
| 183 | +runToResumable w view handler = do |
| 184 | + handlerRef <- Ref.new handler |
| 185 | + canceler <- runWidget w \res -> Ref.read handlerRef >>= applyFlipped res |
| 186 | + pure { handlerRef, view, canceler } |
| 187 | + |
| 188 | +resumableOrr :: forall v a. Monoid v => Array (Widget v a) -> Widget v { val :: a, others :: Array (ResumableWidget v a) } |
| 189 | +resumableOrr widgets = mkWidget \cb -> do |
| 190 | + viewsRef <- Ref.new (Array.replicate (Array.length widgets) mempty) |
| 191 | + resumables <- fixEffect \resumables -> forWithIndex widgets \i w -> do |
| 192 | + viewRef <- Ref.new mempty |
| 193 | + fixEffect \s -> runToResumable w viewRef case _ of |
| 194 | + View v -> do |
| 195 | + Ref.write v viewRef |
| 196 | + views <- Ref.read viewsRef |
| 197 | + Array.updateAt i v views # traverse_ \newViews -> do |
| 198 | + Ref.write newViews viewsRef |
| 199 | + cb (View (fold newViews)) |
| 200 | + Completed val -> do |
| 201 | + cancelResumable (s unit) |
| 202 | + let others = fromMaybe (resumables unit) $ Array.deleteAt i (resumables unit) |
| 203 | + traverse_ suspendResumable others |
| 204 | + cb (Completed { val, others }) |
| 205 | + pure do traverse_ cancelResumable resumables |
| 206 | + |
0 commit comments