~alcinnz/haskell-stylist

9da41d4e978f1a1ba403884088c55f3e22f856f4 — Adrian Cochrane 4 years ago 7d1e885
Request @import'd styles concurrently.
1 files changed, 7 insertions(+), 9 deletions(-)

M src/Data/CSS/Preprocessor/Conditions.hs
M src/Data/CSS/Preprocessor/Conditions.hs => src/Data/CSS/Preprocessor/Conditions.hs +7 -9
@@ 126,22 126,20 @@ resolveImports self responses = self {rules = map resolveImport $ rules self}
            Internal cond body
        resolveImport x = x

-- | Evaluates a given "loader" to resolve any `@import` rules.
loadImports :: PropertyParser p => (URI -> IO Text) -> (Text -> Query.Datum) -> (Token -> Query.Datum) ->
        ConditionalStyles p -> [URI] -> IO (ConditionalStyles p)
loadImports loader vars evalToken self blocklist = do
        let imports = extractImports vars evalToken self
        imported <- loadAll [url | url <- imports, url `notElem` blocklist] Nothing
        return $ resolveImports self imported
    where
        loadAll urls Nothing = loadAll urls $ Just urls
        loadAll (url:urls) (Just blocklist') = do
        let urls = [url | url <- imports, url `notElem` blocklist]
        imported <- forConcurrently urls $ \url -> do
            source <- loader url
            let parsed = parse self {rules = []} source
            styles <- loadImports loader vars evalToken parsed (blocklist ++ blocklist')
            rest <- loadAll urls $ Just blocklist'
            return ((url, styles):rest)
        loadAll [] _ = return []
            styles <- loadImports loader vars evalToken parsed (blocklist ++ urls)
            return (url, styles)
        return $ resolveImports self imported

-- | Evaluates any media queries, returning a new StyleSheet with the queued operations.
resolve :: StyleSheet s => (Text -> Query.Datum) -> (Token -> Query.Datum) ->
        s -> ConditionalStyles p -> s
resolve v t styles self = resolve' v t (reverse $ rules self) styles