Refactor Batch input

pull/17/head
Bram van den Heuvel 2024-01-11 22:35:14 +01:00
parent 79aff7af3b
commit 29f6a5e754
1 changed files with 65 additions and 29 deletions

View File

@ -1,6 +1,5 @@
module Internal.Values.Timeline exposing module Internal.Values.Timeline exposing
( Batch, fromToken, fromSlice ( Batch, Timeline
, Timeline
, empty, singleton , empty, singleton
, mostRecentEvents , mostRecentEvents
, addSync, insert , addSync, insert
@ -19,12 +18,7 @@ and maintain this room state.
## Batch ## Batch
@docs Batch, fromToken, fromSlice @docs Batch, Timeline
## Timeline
@docs Timeline
## Create ## Create
@ -142,8 +136,16 @@ type alias TokenValue =
batch to the front of the Timeline. batch to the front of the Timeline.
-} -}
addSync : Batch -> Timeline -> Timeline addSync : Batch -> Timeline -> Timeline
addSync _ timeline = addSync batch timeline =
timeline case insertBatch batch timeline of
( Timeline tl, { start, end } ) ->
let
oldSync : ITokenPTR
oldSync =
tl.mostRecentSync
in
Timeline { tl | mostRecentSync = end }
|> connectITokenToIToken oldSync start
{-| Append a token at the end of a batch. {-| Append a token at the end of a batch.
@ -196,19 +198,23 @@ connectITokenToIToken : ITokenPTR -> ITokenPTR -> Timeline -> Timeline
connectITokenToIToken pointer1 pointer2 (Timeline tl) = connectITokenToIToken pointer1 pointer2 (Timeline tl) =
case ( pointer1, pointer2 ) of case ( pointer1, pointer2 ) of
( ITokenPTR early, ITokenPTR late ) -> ( ITokenPTR early, ITokenPTR late ) ->
Timeline if early == late then
{ tl Timeline tl
| tokens =
tl.tokens else
|> Hashdict.map early Timeline
(\data -> { tl
{ data | behind = Set.insert late data.behind } | tokens =
) tl.tokens
|> Hashdict.map late |> Hashdict.map early
(\data -> (\data ->
{ data | inFrontOf = Set.insert early data.inFrontOf } { data | behind = Set.insert late data.behind }
) )
} |> Hashdict.map late
(\data ->
{ data | inFrontOf = Set.insert early data.inFrontOf }
)
}
( _, _ ) -> ( _, _ ) ->
Timeline tl Timeline tl
@ -247,13 +253,15 @@ getITokenFromPTR pointer (Timeline { tokens }) =
{-| Insert a batch anywhere else in the timeline. {-| Insert a batch anywhere else in the timeline.
-} -}
insert : Batch -> Timeline -> Timeline insert : Batch -> Timeline -> Timeline
insert batch (Timeline tl) = insert batch timeline =
Timeline tl timeline
|> insertBatch batch
|> Tuple.first
{-| Insert a batch into the timeline. {-| Insert a batch into the timeline.
-} -}
insertBatch : Batch -> Timeline -> Timeline insertBatch : Batch -> Timeline -> ( Timeline, { start : ITokenPTR, end : ITokenPTR } )
insertBatch batch timeline = insertBatch batch timeline =
case batch.start of case batch.start of
Just start -> Just start ->
@ -261,26 +269,30 @@ insertBatch batch timeline =
|> invokeIToken start |> invokeIToken start
|> Tuple.mapSecond (invokeIToken batch.end) |> Tuple.mapSecond (invokeIToken batch.end)
|> (\( startPTR, ( endPTR, newTimeline ) ) -> |> (\( startPTR, ( endPTR, newTimeline ) ) ->
insertIBatch ( insertIBatch
{ events = batch.events { events = batch.events
, filter = batch.filter , filter = batch.filter
, start = startPTR , start = startPTR
, end = endPTR , end = endPTR
} }
newTimeline newTimeline
, { start = startPTR, end = endPTR }
)
) )
Nothing -> Nothing ->
timeline timeline
|> invokeIToken batch.end |> invokeIToken batch.end
|> (\( endPTR, newTimeline ) -> |> (\( endPTR, newTimeline ) ->
insertIBatch ( insertIBatch
{ events = batch.events { events = batch.events
, filter = batch.filter , filter = batch.filter
, start = StartOfTimeline , start = StartOfTimeline
, end = endPTR , end = endPTR
} }
newTimeline newTimeline
, { start = StartOfTimeline, end = endPTR }
)
) )
@ -290,7 +302,31 @@ insertIBatch : IBatch -> Timeline -> Timeline
insertIBatch ibatch (Timeline tl) = insertIBatch ibatch (Timeline tl) =
case Iddict.insert ibatch tl.batches of case Iddict.insert ibatch tl.batches of
( batchPTR, newBatches ) -> ( batchPTR, newBatches ) ->
{ tl | batches = newBatches } { tl
| batches = newBatches
, events =
List.foldl
(\event dict ->
Dict.update event
(\value ->
case value of
Nothing ->
Just ( IBatchPTR batchPTR, [] )
Just ( head, tail ) ->
Just ( IBatchPTR batchPTR, head :: tail )
)
dict
)
tl.events
ibatch.events
, filledBatches =
if List.isEmpty ibatch.events then
tl.filledBatches
else
tl.filledBatches + 1
}
|> Timeline |> Timeline
|> connectITokenToIBatch ibatch.start (IBatchPTR batchPTR) |> connectITokenToIBatch ibatch.start (IBatchPTR batchPTR)
|> connectIBatchToIToken (IBatchPTR batchPTR) ibatch.end |> connectIBatchToIToken (IBatchPTR batchPTR) ibatch.end