Skip to content

Commit

Permalink
Remove TransactAsyncEx, tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Feb 20, 2021
1 parent d4b7662 commit b312893
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Equinox/Decider.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ type Decider<'event, 'state>
/// 1b. Tries up to <c>maxAttempts</c> times in the case of a conflict, throwing <c>MaxResyncsExhaustedException</c> to signal failure.
/// 2. Uses <c>mapResult</c> to render the final outcome from the <c>'result</c> and/or the final <c>ISyncContext</c>
/// 3. Yields the outcome
member __.TransactEx(decide : ISyncContext<'state> -> Async<'result * 'event list>, mapResult : 'result -> ISyncContext<'state> -> 'resultEx) : Async<'resultEx> =
member __.TransactEx(decide : ISyncContext<'state> -> Async<'result * 'event list>, mapResult : 'result -> ISyncContext<'state> -> 'view) : Async<'view> =
transact decide mapResult

/// Project from the folded <c>'state</c>, without executing a decision flow as <c>Transact</c> does
member __.Query(projection : 'state -> 'view) : Async<'view> =
Flow.query (stream, log, fun syncState -> projection (syncState :> ISyncContext<'state>).State)
Flow.query (stream, log, fun context -> projection (context :> ISyncContext<'state>).State)

/// Project from the stream's <c>'state<c> (including extended context), without executing a decision flow as <c>Transact<c> does
member __.QueryEx(projection : ISyncContext<'state> -> 'view) : Async<'view> =
Expand Down
32 changes: 16 additions & 16 deletions src/Equinox/Flow.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type ISyncContext<'state> =
module internal Flow =

/// Represents stream and folding state between the load and run/render phases
type SyncState<'event, 'state>
type SyncContext<'event, 'state>
( originState : StreamToken * 'state,
trySync : ILogger * StreamToken * 'state * 'event list -> Async<SyncResult<'state>>) =
let mutable tokenAndState = originState
Expand Down Expand Up @@ -78,45 +78,45 @@ module internal Flow =
/// 2b. if saved without conflict, exit with updated state
/// 2b. if conflicting changes, retry by recommencing at step 1 with the updated state
let run (log : ILogger) (maxSyncAttempts : int, resyncRetryPolicy, createMaxAttemptsExhaustedException)
(syncState : SyncState<'event, 'state>)
(context : SyncContext<'event, 'state>)
(decide : ISyncContext<'state> -> Async<'result * 'event list>)
(mapResult : 'result -> SyncState<'event, 'state> -> 'resultEx)
: Async<'resultEx> =
(mapResult : 'result -> SyncContext<'event, 'state> -> 'view)
: Async<'view> =

if maxSyncAttempts < 1 then raise <| System.ArgumentOutOfRangeException("maxSyncAttempts", maxSyncAttempts, "should be >= 1")

/// Run a decision cycle - decide what events should be appended given the presented state
let rec loop attempt : Async<'resultEx> = async {
let rec loop attempt : Async<'view> = async {
let log = if attempt = 1 then log else log.ForContext("syncAttempt", attempt)
let! result, events = decide (syncState :> ISyncContext<'state>)
let! result, events = decide (context :> ISyncContext<'state>)
if List.isEmpty events then
log.Debug "No events generated"
return mapResult result syncState
return mapResult result context
elif attempt = maxSyncAttempts then
// Special case: on final attempt, we won't be `resync`ing; we're giving up
let! committed = syncState.TryWithoutResync(log, events)
let! committed = context.TryWithoutResync(log, events)
if not committed then
log.Debug "Max Sync Attempts exceeded"
return raise (createMaxAttemptsExhaustedException attempt)
else
return mapResult result syncState
return mapResult result context
else
let! committed = syncState.TryOrResync(resyncRetryPolicy, attempt, log, events)
let! committed = context.TryOrResync(resyncRetryPolicy, attempt, log, events)
if not committed then
log.Debug "Resyncing and retrying"
return! loop (attempt + 1)
else
return mapResult result syncState }
return mapResult result context }

// Commence, processing based on the incoming state
loop 1

let transact (maxAttempts, resyncRetryPolicy, createMaxAttemptsExhaustedException) (stream : IStream<_, _>, log) decide mapResult : Async<'result> = async {
let! streamState = stream.Load log
let syncState = SyncState(streamState, stream.TrySync)
return! run log (maxAttempts, resyncRetryPolicy, createMaxAttemptsExhaustedException) syncState decide mapResult }
let context = SyncContext(streamState, stream.TrySync)
return! run log (maxAttempts, resyncRetryPolicy, createMaxAttemptsExhaustedException) context decide mapResult }

let query (stream : IStream<'event, 'state>, log : ILogger, project: SyncState<'event, 'state> -> 'result) : Async<'result> = async {
let query (stream : IStream<'event, 'state>, log : ILogger, project: SyncContext<'event, 'state> -> 'result) : Async<'result> = async {
let! streamState = stream.Load log
let syncState = SyncState(streamState, stream.TrySync)
return project syncState }
let context = SyncContext(streamState, stream.TrySync)
return project context }

0 comments on commit b312893

Please sign in to comment.