This provides a component that can "debounce" a changing value: monitor a time-varying sequence of values and output the latest value every time there is no further change for some minimum interval.
This implementation attempts to minimize the number of update
calls by using
Process.sleep
to manage the settling time (rather than recalculating elapsed
interval on every fine-grained tick). An added tuple element in the update
function's return value provides the notification to the parent of the settled
value. It's also possible to poll the settled value.
{ data : datatype
, settled : datatype
, sleepCount : Basics.Int
, settleTime : Basics.Float
}
Debouncer model. Each instance handles a single time-varying sequence of the
same type (the datatype
).
Use the Change
message to pass a new value to debouncer.
init : Basics.Float -> datatype -> Model datatype
Initialize the debouncer with the time interval (in milliseconds) to wait for changing values to settle and the initial settled value.
update : Msg datatype -> Model datatype -> ( Model datatype, Platform.Cmd.Cmd (Msg datatype), Maybe datatype )
Update the debouncer as a typical TEA component. The return value adds a
final tuple element that is Nothing
while the value is still changing and
Just x
when the value has settled to x
.
settled : Model datatype -> datatype
Access the settled value.