-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
32 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,48 @@ | ||
{-# LANGUAGE BangPatterns #-} | ||
|
||
module Main where | ||
|
||
import Control.Concurrent.Async (race_) | ||
import Control.Concurrent.Async (concurrently) | ||
import Control.Concurrent.BoundedChan ( BoundedChan | ||
, readChan | ||
, writeChan | ||
, newBoundedChan | ||
) | ||
import Control.Monad (forM_) | ||
import GHC.Conc.Sync (atomically, STM) | ||
import Options.Applicative (execParser) | ||
import Args | ||
import Itunes | ||
import Actions (getAction) | ||
import Channel | ||
import Debug.Trace | ||
|
||
main :: IO () | ||
main = execParser app >>= run | ||
|
||
run :: Args -> IO () | ||
run args = let | ||
action = getAction (actionType args) | ||
channel = newChannel 500 -- wild ass guess | ||
channel = newBoundedChan 500 -- wild ass guess | ||
transform = mkTransform (target args) (attributes args) | ||
in | ||
(race_ (readSources channel transform [source args]) | ||
(doAction channel action)) >> | ||
putStrLn "finished" | ||
in | ||
channel >>= (\c -> | ||
(concurrently (readSources transform [source args] c) | ||
(doAction action c))) >> | ||
putStrLn "finished" | ||
|
||
readSources :: STM (Channel Copy) -> Transform -> [Directory] -> IO () | ||
readSources _ _ [] = return () | ||
readSources chan t (d:ds) = do | ||
readSources :: Transform -> | ||
[Directory] -> | ||
BoundedChan (Maybe Copy) -> | ||
IO () | ||
readSources _ [] chan = writeChan chan Nothing | ||
readSources t (d:ds) chan = do | ||
(dirs, copies) <- handleDirectory d t | ||
atomically $ enqueueCopies chan copies | ||
readSources chan t (ds ++ dirs) | ||
|
||
enqueueCopies :: STM (Channel Copy) -> [Copy] -> STM () | ||
enqueueCopies chan cs = chan >>= writeChan cs | ||
forM_ (Just <$> copies) (writeChan chan) | ||
readSources t (ds ++ dirs) chan | ||
|
||
doAction :: STM (Channel Copy) -> Action -> IO () | ||
doAction chan action = | ||
atomically (chan >>= readChan) >>= action | ||
doAction :: Action -> BoundedChan (Maybe Copy) -> IO () | ||
doAction action chan = do | ||
copy <- readChan chan | ||
case copy of | ||
(Just c) -> action c >> doAction action chan | ||
Nothing -> return () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters