Skip to content

Commit 39d927e

Browse files
Copilotccamel
andcommitted
refactor: reorder fold/4 callback parameters
Co-authored-by: ccamel <9574336+ccamel@users.noreply.github.com>
1 parent b4271f2 commit 39d927e

6 files changed

Lines changed: 21 additions & 21 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ The event store is a core component in this experiment, designed as a customizab
126126
Events :: [event()].
127127

128128
% Folds events from a stream using a provided function
129-
-callback fold(StreamId, Options, FoldFun, InitialAcc) -> {ok, Acc} | {error, term()}
129+
-callback fold(StreamId, FoldFun, InitialAcc, Options) -> {ok, Acc} | {error, term()}
130130
when StreamId :: stream_id(),
131-
Options :: fold_events_opts(),
132131
FoldFun :: fold_events_fun(),
133-
InitialAcc :: Acc.
132+
InitialAcc :: Acc,
133+
Options :: fold_events_opts().
134134
```
135135

136136
#### Snapshot Support

apps/event_sourcing_contract/src/event_sourcing_event_store_behaviour.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,20 @@ event in sequence order, and returns the final accumulator. It’s typically use
6868
rebuild application state by replaying events.
6969

7070
- StreamId is an atom identifying the event stream (e.g., order-123).
71+
- FoldFun is a function `fun((Event, AccIn) -> AccOut)` to process each event.
72+
- InitialAcc is the initial accumulator value (e.g., an empty state).
7173
- Options is A list of filters:
7274
- `{from, Sequence}`: Start at this sequence (default: 0).
7375
- `{to, Sequence | infinity}`: End at this sequence (default: infinity).
7476
- `{limit, Limit}`: Maximum number of events to retrieve (default: infinity).
75-
- FoldFun is a function `fun((Event, AccIn) -> AccOut)` to process each event.
76-
- InitialAcc is the initial accumulator value (e.g., an empty state).
7777

7878
Returns the final accumulator after folding all events.
7979
""".
80-
-callback fold(StreamId, Options, Fun, Acc0) -> Acc1 when
80+
-callback fold(StreamId, Fun, Acc0, Options) -> Acc1 when
8181
StreamId :: stream_id(),
82-
Options :: fold_events_opts(),
8382
Fun :: fun((Event :: event(), AccIn) -> AccOut),
8483
Acc0 :: term(),
84+
Options :: fold_events_opts(),
8585
Acc1 :: term(),
8686
AccIn :: term(),
8787
AccOut :: term().

apps/event_sourcing_core/src/event_sourcing_core_aggregate.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ init({Aggregate, StoreContext, Id, Opts}) ->
153153
event_sourcing_core_store:fold(
154154
StoreContext,
155155
Id,
156-
#{from => SequenceFromSnapshot + 1},
157156
FoldFun,
158-
{StateFromSnapshot, SequenceFromSnapshot}
157+
{StateFromSnapshot, SequenceFromSnapshot},
158+
#{from => SequenceFromSnapshot + 1}
159159
),
160160
Timeout = maps:get(timeout, Opts, ?INACTIVITY_TIMEOUT),
161161
SnapshotInterval = maps:get(snapshot_interval, Opts, 0),

apps/event_sourcing_core/src/event_sourcing_core_store.erl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,17 @@ Folds events from the event store into an accumulator using the specified persis
123123
This is the core operation for event replay and state reconstruction. The backend
124124
retrieves events matching the given criteria and applies the fold function in sequence order.
125125
""".
126-
-spec fold(StoreContext, StreamId, Options, Fun, Acc0) -> Acc1 when
126+
-spec fold(StoreContext, StreamId, Fun, Acc0, Options) -> Acc1 when
127127
StoreContext :: store_context(),
128128
StreamId :: stream_id(),
129-
Options :: fold_events_opts(),
130129
Fun :: fun((Event :: event(), AccIn) -> AccOut),
131130
Acc0 :: term(),
131+
Options :: fold_events_opts(),
132132
Acc1 :: term(),
133133
AccIn :: term(),
134134
AccOut :: term().
135-
fold({EventModule, _}, StreamId, Options, Fun, InitialResult) ->
136-
EventModule:fold(StreamId, Options, Fun, InitialResult).
135+
fold({EventModule, _}, StreamId, Fun, InitialResult, Options) ->
136+
EventModule:fold(StreamId, Fun, InitialResult, Options).
137137

138138
-doc """
139139
Retrieves events for a given stream using the specified store module and options.
@@ -149,9 +149,9 @@ retrieve_events(StoreContext, StreamId, Options) ->
149149
fold(
150150
StoreContext,
151151
StreamId,
152-
Options,
153152
fun(Event, Acc) -> Acc ++ [Event] end,
154-
[]
153+
[],
154+
Options
155155
).
156156

157157
-doc """

apps/event_sourcing_store_ets/src/event_sourcing_store_ets.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ append(_, Events) ->
7979
erlang:error(duplicate_event)
8080
end.
8181

82-
-spec fold(StreamId, Options, Fun, Acc0) -> Acc1 when
82+
-spec fold(StreamId, Fun, Acc0, Options) -> Acc1 when
8383
StreamId :: stream_id(),
84-
Options :: fold_events_opts(),
8584
Fun :: fun((Event :: event(), AccIn) -> AccOut),
8685
Acc0 :: term(),
86+
Options :: fold_events_opts(),
8787
Acc1 :: term(),
8888
AccIn :: term(),
8989
AccOut :: term().
90-
fold(StreamId, Options, FoldFun, InitialAcc) when
90+
fold(StreamId, FoldFun, InitialAcc, Options) when
9191
is_map(Options), is_function(FoldFun, 2)
9292
->
9393
From = maps:get(from, Options, 0),

apps/event_sourcing_store_mnesia/src/event_sourcing_store_mnesia.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ persist_events_in_tx(StreamId, [Event | Rest]) ->
119119
persist_events_in_tx(StreamId, Rest)
120120
end.
121121

122-
-spec fold(StreamId, Options, Fun, Acc0) -> Acc1 when
122+
-spec fold(StreamId, Fun, Acc0, Options) -> Acc1 when
123123
StreamId :: stream_id(),
124-
Options :: fold_events_opts(),
125124
Fun :: fun((Event :: event(), AccIn) -> AccOut),
126125
Acc0 :: term(),
126+
Options :: fold_events_opts(),
127127
Acc1 :: term(),
128128
AccIn :: term(),
129129
AccOut :: term().
130-
fold(StreamId, Options, FoldFun, InitialAcc) when
130+
fold(StreamId, FoldFun, InitialAcc, Options) when
131131
is_map(Options), is_function(FoldFun, 2)
132132
->
133133
From = maps:get(from, Options, 0),

0 commit comments

Comments
 (0)