module Data.Woot
( WootClient
, wootClientId
, wootClientClock
, wootClientString
, wootClientOperationQueue
, makeWootClient
, makeWootClientEmpty
, sendOperation
, sendOperations
, sendLocalDelete
, sendLocalInsert
, WString
, WChar(..)
, WCharId(..)
, Operation(..)
, ClientId
) where
import Data.Woot.Core
import Data.Woot.Operation
import Data.Woot.WChar
import Data.Woot.WString
data WootClient = WootClient
{ wootClientId :: Int
, wootClientClock :: Int
, wootClientString :: WString
, wootClientOperationQueue :: [Operation]
} deriving (Eq, Show)
incClock :: WootClient -> WootClient
incClock client = client {wootClientClock = succ $ wootClientClock client}
makeWootClient :: WString -> ClientId -> WootClient
makeWootClient ws cid = WootClient cid 0 ws []
makeWootClientEmpty :: ClientId -> WootClient
makeWootClientEmpty = makeWootClient emptyWString
sendOperation :: WootClient -> Operation -> WootClient
sendOperation (WootClient cid clock ws ops) op = WootClient cid clock ws' ops'
where
(ops', ws') = integrateAll (op:ops) ws
sendOperations :: WootClient -> [Operation] -> WootClient
sendOperations = foldl sendOperation
sendLocalOperation :: WootClient -> Operation -> WootClient
sendLocalOperation client = incClock . sendOperation client
sendLocalDelete :: WootClient -> Int -> (Maybe Operation, WootClient)
sendLocalDelete client pos = (op,) $ maybe client (sendLocalOperation client) op
where
op = makeDeleteOperation (wootClientId client) pos (wootClientString client)
sendLocalInsert :: WootClient -> Int -> Char -> (Maybe Operation, WootClient)
sendLocalInsert client@(WootClient cid clock ws _) pos a =
(op,) $ maybe client (sendLocalOperation client) op
where
op = makeInsertOperation (WCharId cid clock) pos a ws