fix: reading the columns emits data

This commit is contained in:
orion 2024-05-03 13:45:27 -05:00
parent 3a5553fd29
commit 8201ede7c4
Signed by: orion
GPG Key ID: 6D4165AE4C928719

View File

@ -3,7 +3,7 @@ module Node.Stream.CSV.Parse where
import Prelude hiding (join)
import Control.Alt ((<|>))
import Control.Monad.Error.Class (liftEither, liftMaybe)
import Control.Monad.Error.Class (liftEither)
import Control.Monad.Except (runExcept)
import Control.Monad.Except.Trans (catchError)
import Control.Monad.Maybe.Trans (MaybeT(..), runMaybeT)
@ -15,6 +15,7 @@ import Data.Bifunctor (lmap)
import Data.CSV.Record (class ReadCSVRecord, readCSVRecord)
import Data.Either (Either(..))
import Data.Filterable (filter)
import Data.Foldable (for_)
import Data.Map (Map)
import Data.Map as Map
import Data.Maybe (Maybe(..))
@ -117,8 +118,9 @@ foreach stream cb = do
UnliftAff unlift <- askUnliftAff
liftAff $ makeAff \res -> do
removeDataListener <- flip (Event.on dataH) stream \row -> do
cols <- liftMaybe (error "did not read header column") =<< getOrInitColumnsMap stream
record <- liftEither $ lmap (error <<< show) $ runExcept $ readCSVRecord @r @rl cols row
cols <- getOrInitColumnsMap stream
for_ cols \cols' -> do
record <- liftEither $ lmap (error <<< show) $ runExcept $ readCSVRecord @r @rl cols' row
launchAff_ $ flip catchError (liftEffect <<< res <<< Left) (unlift $ cb record)
removeEndListener <- flip (Event.once Stream.endH) stream (res $ Right unit)
removeErrorListener <- flip (Event.on Stream.errorH) stream (res <<< Left)