@@ -6,6 +6,7 @@ defmodule Buffer.State do
66 :flush_meta ,
77 :max_length ,
88 :max_size ,
9+ :ordering ,
910 :partition ,
1011 :size_callback ,
1112 :timeout ,
@@ -36,6 +37,7 @@ defmodule Buffer.State do
3637
3738 @ doc false
3839 @ spec items ( t ( ) ) :: list ( )
40+ def items ( % { ordering: :lifo } = buffer ) , do: buffer . buffer
3941 def items ( buffer ) , do: Enum . reverse ( buffer . buffer )
4042
4143 @ doc false
@@ -46,12 +48,14 @@ defmodule Buffer.State do
4648 { :ok , size_callback } <- get_size_callback ( opts ) ,
4749 { :ok , max_length } <- get_max_length ( opts , jitter ) ,
4850 { :ok , max_size } <- get_max_size ( opts , jitter ) ,
49- { :ok , timeout } <- get_timeout ( opts , jitter ) do
51+ { :ok , timeout } <- get_timeout ( opts , jitter ) ,
52+ { :ok , ordering } <- get_ordering ( opts ) do
5053 buffer = % __MODULE__ {
5154 flush_callback: flush_callback ,
5255 flush_meta: Keyword . get ( opts , :flush_meta ) ,
5356 max_length: max_length ,
5457 max_size: max_size ,
58+ ordering: ordering ,
5559 partition: Keyword . get ( opts , :partition , 0 ) ,
5660 size_callback: size_callback ,
5761 timeout: timeout
@@ -103,6 +107,13 @@ defmodule Buffer.State do
103107 validate_limit ( Keyword . get ( opts , :buffer_timeout , :infinity ) , jitter )
104108 end
105109
110+ defp get_ordering ( opts ) do
111+ case Keyword . get ( opts , :ordering , :fifo ) do
112+ ordering when ordering in [ :fifo , :lifo ] -> { :ok , ordering }
113+ _ -> { :error , :invalid_ordering }
114+ end
115+ end
116+
106117 defp validate_callback ( fun , arity ) when is_function ( fun , arity ) , do: { :ok , fun }
107118 defp validate_callback ( _ , _ ) , do: { :error , :invalid_callback }
108119
0 commit comments