fix: timeslice shenanigans

This commit is contained in:
orion 2024-05-03 10:58:47 -05:00
parent a3625ab1b7
commit 07c86f096f
Signed by: orion
GPG Key ID: 6D4165AE4C928719

View File

@ -3,6 +3,7 @@ module Node.Stream.CSV.Parse where
import Prelude hiding (join)
import Control.Alt ((<|>))
import Control.Alternative (guard)
import Control.Monad.Error.Class (liftEither)
import Control.Monad.Except (runExcept)
import Control.Monad.Maybe.Trans (MaybeT(..), runMaybeT)
@ -20,12 +21,13 @@ import Data.Filterable (filter)
import Data.Map (Map)
import Data.Map as Map
import Data.Maybe (Maybe(..), isNothing)
import Data.Newtype (wrap)
import Data.Nullable (Nullable)
import Data.Nullable as Nullable
import Data.Traversable (for)
import Effect (Effect)
import Effect as Effect
import Effect.Aff (Canceler(..), makeAff)
import Effect.Aff (Canceler(..), delay, makeAff)
import Effect.Aff.Class (class MonadAff, liftAff)
import Effect.Class (liftEffect)
import Effect.Exception (error)
@ -120,7 +122,14 @@ foreach
=> CSVParser r x
-> ({ | r } -> m Unit)
-> m Unit
foreach stream cb = whileJust do
foreach stream cb =
whileJust
$ runMaybeT
$ do
liftAff $ delay $ wrap 0.0
guard =<< not <$> liftEffect (Stream.closed stream)
isReadable <- liftEffect $ Stream.readable stream
liftAff $ when (not isReadable) $ makeAff \res -> do
stop <- flip (Event.once Stream.readableH) stream $ res $ Right unit
@ -133,9 +142,9 @@ foreach stream cb = whileJust do
pure $ isNothing r
records <- liftEffect $ ST.toEffect $ Array.ST.unsafeFreeze recordsST
parTraverse_ cb records
isClosed <- liftEffect $ Stream.closed stream
pure $ if isClosed then Nothing else Just unit
lift $ parTraverse_ cb records
guard =<< not <$> liftEffect (Stream.closed stream)
pure unit
-- | Reads a parsed record from the stream.
-- |