Skip to content

Commit 96c60a2

Browse files
committed
Move 'for_each_while_ok/2' helper to rabbit_misc
1 parent 7059582 commit 96c60a2

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

deps/rabbit/src/rabbit_db_queue.erl

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ update_durable_in_khepri(UpdateFun, FilterFun) ->
731731
end, [], Props),
732732
Res = rabbit_khepri:transaction(
733733
fun() ->
734-
for_each_while_ok(
734+
rabbit_misc:for_each_while_ok(
735735
fun({Path, Q}) -> khepri_tx:put(Path, Q) end,
736736
Updates)
737737
end),
@@ -749,16 +749,6 @@ update_durable_in_khepri(UpdateFun, FilterFun) ->
749749
Error
750750
end.
751751

752-
for_each_while_ok(Fun, [Elem | Rest]) ->
753-
case Fun(Elem) of
754-
ok ->
755-
for_each_while_ok(Fun, Rest);
756-
{error, _} = Error ->
757-
Error
758-
end;
759-
for_each_while_ok(_, []) ->
760-
ok.
761-
762752
%% -------------------------------------------------------------------
763753
%% exists().
764754
%% -------------------------------------------------------------------

deps/rabbit_common/src/rabbit_misc.erl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
maps_put_falsy/3
9090
]).
9191
-export([remote_sup_child/2]).
92+
-export([for_each_while_ok/2]).
9293

9394
%% Horrible macro to use in guards
9495
-define(IS_BENIGN_EXIT(R),
@@ -1632,3 +1633,25 @@ remote_sup_child(Node, Sup) ->
16321633
[] -> {error, no_child};
16331634
{badrpc, {'EXIT', {noproc, _}}} -> {error, no_sup}
16341635
end.
1636+
1637+
-spec for_each_while_ok(ForEachFun, List) -> Ret when
1638+
ForEachFun :: fun((Element) -> ok | {error, ErrReason}),
1639+
ErrReason :: any(),
1640+
Element :: any(),
1641+
List :: [Element],
1642+
Ret :: ok | {error, ErrReason}.
1643+
%% @doc Calls the given `ForEachFun' for each element in the given `List',
1644+
%% short-circuiting if the function returns `{error,_}'.
1645+
%%
1646+
%% @returns the first `{error,_}' returned by `ForEachFun' or `ok' if
1647+
%% `ForEachFun' never returns an error tuple.
1648+
1649+
for_each_while_ok(Fun, [Elem | Rest]) ->
1650+
case Fun(Elem) of
1651+
ok ->
1652+
for_each_while_ok(Fun, Rest);
1653+
{error, _} = Error ->
1654+
Error
1655+
end;
1656+
for_each_while_ok(_, []) ->
1657+
ok.

0 commit comments

Comments
 (0)