@@ -12,12 +12,12 @@ module Events =
1212 | Delta of Delta
1313 interface TypeShape.UnionContract.IUnionContract
1414 let codec = FsCodec.NewtonsoftJson.Codec.Create< Event>()
15- let [<Literal>] categoryId = " LocationEpoch"
16- let (| AggregateId |) ( locationId , epochId ) =
15+ let [<Literal>] category = " LocationEpoch"
16+ let (| For |) ( locationId , epochId ) =
1717 let id = sprintf " %s _%s " ( LocationId.toString locationId) ( LocationEpochId.toString epochId)
18- Equinox.AggregateId( categoryId , id)
18+ Equinox.AggregateId( category , id)
1919
20- module Folds =
20+ module Fold =
2121
2222 type Balance = int
2323 type OpenState = { count : int ; value : Balance }
@@ -36,55 +36,54 @@ module Folds =
3636/// Holds events accumulated from a series of decisions while also evolving the presented `state` to reflect the pended events
3737type private Accumulator () =
3838 let acc = ResizeArray()
39- member __.Ingest state : 'res * Events.Event list -> 'res * Folds .State = function
39+ member __.Ingest state : 'res * Events.Event list -> 'res * Fold .State = function
4040 | res, [] -> res, state
41- | res, [ e] -> acc.Add e; res, Folds .evolve state e
42- | res, xs -> acc.AddRange xs; res, Folds .fold state ( Seq.ofList xs)
41+ | res, [ e] -> acc.Add e; res, Fold .evolve state e
42+ | res, xs -> acc.AddRange xs; res, Fold .fold state ( Seq.ofList xs)
4343 member __.Accumulated = List.ofSeq acc
4444
45- type Result < 't > = { balance : Folds .Balance ; result : 't option ; isOpen : bool }
45+ type Result < 't > = { balance : Fold .Balance ; result : 't option ; isOpen : bool }
4646
47- let sync ( balanceCarriedForward : Folds .Balance option) ( decide : ( Folds .Balance -> 't * Events.Event list )) shouldClose state : Result < 't >* Events.Event list =
47+ let sync ( balanceCarriedForward : Fold .Balance option) ( decide : ( Fold .Balance -> 't * Events.Event list )) shouldClose state : Result < 't >* Events.Event list =
4848 let acc = Accumulator()
4949 // We always want to have a CarriedForward event at the start of any Epoch's event stream
5050 let (), state =
5151 acc.Ingest state <|
5252 match state with
53- | Folds .Initial -> (), [ Events.CarriedForward { initial = Option.get balanceCarriedForward }]
54- | Folds .Open _ | Folds .Closed _ -> (), []
53+ | Fold .Initial -> (), [ Events.CarriedForward { initial = Option.get balanceCarriedForward }]
54+ | Fold .Open _ | Fold .Closed _ -> (), []
5555 // Run, unless we determine we're in Closed state
5656 let result , state =
5757 acc.Ingest state <|
5858 match state with
59- | Folds .Initial -> failwith " We've just guaranteed not Initial"
60- | Folds .Open { value = bal } -> let r , es = decide bal in Some r, es
61- | Folds .Closed _ -> None, []
59+ | Fold .Initial -> failwith " We've just guaranteed not Initial"
60+ | Fold .Open { value = bal } -> let r , es = decide bal in Some r, es
61+ | Fold .Closed _ -> None, []
6262 // Finally (iff we're `Open`, have run a `decide` and `shouldClose`), we generate a Closed event
6363 let ( balance , isOpen ), _ =
6464 acc.Ingest state <|
6565 match state with
66- | Folds .Initial -> failwith " Can't be Initial"
67- | Folds .Open ({ value = bal } as openState) when shouldClose openState -> ( bal, false ), [ Events.Closed]
68- | Folds .Open { value = bal } -> ( bal, true ), []
69- | Folds .Closed bal -> ( bal, false ), []
66+ | Fold .Initial -> failwith " Can't be Initial"
67+ | Fold .Open ({ value = bal } as openState) when shouldClose openState -> ( bal, false ), [ Events.Closed]
68+ | Fold .Open { value = bal } -> ( bal, true ), []
69+ | Fold .Closed bal -> ( bal, false ), []
7070 { balance = balance; result = result; isOpen = isOpen }, acc.Accumulated
7171
72- type Service internal ( resolve , ? maxAttempts ) =
72+ type Service internal ( log , resolve , maxAttempts ) =
7373
74- let log = Serilog.Log.ForContext< Service>()
75- let (| Stream |) ( Events.AggregateId id ) = Equinox.Stream< Events.Event, Folds.State>( log, resolve id, maxAttempts = defaultArg maxAttempts 2 )
74+ let resolve ( Events.For id ) = Equinox.Stream< Events.Event, Fold.State>( log, resolve id, maxAttempts)
7675
7776 member __.Sync < 'R >( locationId , epochId , prevEpochBalanceCarriedForward , decide , shouldClose ) : Async < Result < 'R >> =
78- let ( Stream stream) = ( locationId, epochId)
77+ let stream = resolve ( locationId, epochId)
7978 stream.Transact( sync prevEpochBalanceCarriedForward decide shouldClose)
8079
81- let create resolve maxAttempts = Service( resolve, maxAttempts)
80+ let create resolve maxAttempts = Service( Serilog.Log.ForContext < Service >(), resolve, maxAttempts = maxAttempts)
8281
8382module Cosmos =
8483
8584 open Equinox.Cosmos
8685 let resolve ( context , cache ) =
8786 let cacheStrategy = CachingStrategy.SlidingWindow ( cache, System.TimeSpan.FromMinutes 20. )
88- Resolver( context, Events.codec, Folds .fold, Folds .initial, cacheStrategy, AccessStrategy.Unoptimized) .Resolve
87+ Resolver( context, Events.codec, Fold .fold, Fold .initial, cacheStrategy, AccessStrategy.Unoptimized) .Resolve
8988 let createService ( context , cache , maxAttempts ) =
9089 create ( resolve ( context, cache)) maxAttempts
0 commit comments