-
-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throttling particular message type #598
Comments
Did you enter the expected/desired behavior and actual behavior incorrectly? |
@xperiandri the core dispatch loop of Elmish is single-threaded, so you would want to avoid generating messages in the first place that you don't want immediately applied to the model. However, if what you're looking for is a way to avoid UI updates every time Elmish updates the model, look at the Threading and Threading.Core example. If you call Saying that out loud, I should probably wrap that in a separate top-level function that does all of the threaded housekeeping for you. |
I need a way to wait 0.5 sec for messages to stop till I send a request to a server |
This should easily be possible using Reactive and |
I use R3 instead. This is what I implemented namespace eCierge.Elmish
open System
[<AutoOpen>]
module Dispatching =
open Elmish
open R3
let asDispatchWrapper<'msg> (configure : Observable<'msg> -> Observable<'msg>) (dispatch : Dispatch<'msg>) : Dispatch<'msg> =
let subject = new Subject<_> ()
(subject |> configure).Subscribe dispatch |> ignore
fun msg -> async.Return (subject.OnNext msg) |> Async.Start
let throttle<'msg> timespan =
/// Ignores elements from an observable sequence which are followed by another element within a specified relative time duration.
let throttle (dueTime : TimeSpan) (source : Observable<'Source>) : Observable<'Source> = source.ThrottleLast (dueTime)
throttle timespan |> asDispatchWrapper<'msg>
[<Literal>]
let DefaultThrottleTimeout = 500.0
[<Literal>]
let HalfThrottleTimeout = 250.0
open Elmish.Uno
open System.Runtime.InteropServices
[<AbstractClass; Sealed>]
type BindingT private () =
static member twoWayThrottle
(get : 'model -> 'a, setWithModel : 'a -> 'model -> 'msg, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWay (get, setWithModel = setWithModel)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayThrottle
(
get : 'model -> 'a,
setWithModel : 'a -> 'model -> 'msg,
[<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float
)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayThrottle (get, setWithModel, (TimeSpan.FromMilliseconds timeout))
static member twoWayThrottle (get : 'model -> 'a, set : 'a -> 'msg, timespan) : string -> Binding<'model, 'msg, 'a> =
BindingT.twoWay (get, set)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayThrottle
(get : 'model -> 'a, set : 'a -> 'msg, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayThrottle (get, set, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptThrottle
(getOpt : 'model -> 'a option, setWithModel : 'a option -> 'model -> 'msg, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOpt (getOpt, setWithModel = setWithModel)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptThrottle
(
getOpt : 'model -> 'a option,
setWithModel : 'a option -> 'model -> 'msg,
[<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float
)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptThrottle (getOpt, setWithModel, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptThrottle
(get : 'model -> 'a option, set : 'a option -> 'msg, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOpt (get, set)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptThrottle
(
get : 'model -> 'a option,
set : 'a option -> 'msg,
[<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float
)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptThrottle (get, set, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptThrottle
(getVOpt : 'model -> 'a voption, setWithModel : 'a voption -> 'model -> 'msg, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOpt (getVOpt = getVOpt, setWithModel = setWithModel)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptThrottle
(
getVOpt : 'model -> 'a voption,
setWithModel : 'a voption -> 'model -> 'msg,
[<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float
)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptThrottle (getVOpt, setWithModel, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptThrottle (get : 'model -> 'a voption, set : 'a voption -> 'msg, timespan) : string -> Binding<'model, 'msg, 'a> =
BindingT.twoWayOpt (get, set)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptThrottle (get : 'model -> 'a voption, set : 'a voption -> 'msg, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float) : string -> Binding<'model, 'msg, 'a> =
BindingT.twoWayOptThrottle (get, set, (TimeSpan.FromMilliseconds timeout))
static member twoWayValidateThrottle
(get : 'model -> 'a, setWithModel : 'a -> 'model -> 'msg, validate : 'model -> string list, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidate (get = get, setWithModel = setWithModel, validate = validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayValidateThrottle
(get : 'model -> 'a, setWithModel : 'a -> 'model -> 'msg, validate : 'model -> string list, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidateThrottle (get, setWithModel, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayValidateThrottle
(get : 'model -> 'a, set : 'a -> 'msg, validate : 'model -> string list, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidate (get, set, validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayValidateThrottle
(get : 'model -> 'a, set : 'a -> 'msg, validate : 'model -> string list, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidateThrottle (get, set, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayValidateThrottle
(get : 'model -> 'a, setWithModel : 'a -> 'model -> 'msg, validate : 'model -> string voption, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidate (get, setWithModel = setWithModel, validate = validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayValidateThrottle
(get : 'model -> 'a, setWithModel : 'a -> 'model -> 'msg, validate : 'model -> string voption, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidateThrottle (get, setWithModel, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayValidateThrottle
(get : 'model -> 'a, set : 'a -> 'msg, validate : 'model -> string voption, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidate (get, set, validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayValidateThrottle
(get : 'model -> 'a, set : 'a -> 'msg, validate : 'model -> string voption, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidateThrottle (get, set, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayValidateThrottle
(get : 'model -> 'a, setWithModel : 'a -> 'model -> 'msg, validate : 'model -> string option, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidate (get, setWithModel = setWithModel, validate = validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayValidateThrottle
(get : 'model -> 'a, setWithModel : 'a -> 'model -> 'msg, validate : 'model -> string option, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidateThrottle (get, setWithModel, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayValidateThrottle
(get : 'model -> 'a, set : 'a -> 'msg, validate : 'model -> string option, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidate (get, set, validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayValidateThrottle
(get : 'model -> 'a, set : 'a -> 'msg, validate : 'model -> string option, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidateThrottle (get, set, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayValidateThrottle
(get : 'model -> 'a, setWithModel : 'a -> 'model -> 'msg, validate : 'model -> Result<'ignored, string>, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidate (get, setWithModel = setWithModel, validate = validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayValidateThrottle
(get : 'model -> 'a, setWithModel : 'a -> 'model -> 'msg, validate : 'model -> Result<'ignored, string>, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidateThrottle (get, setWithModel, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayValidateThrottle
(get : 'model -> 'a, set : 'a -> 'msg, validate : 'model -> Result<'ignored, string>, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidate (get, set, validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayValidateThrottle
(get : 'model -> 'a, set : 'a -> 'msg, validate : 'model -> Result<'ignored, string>, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayValidateThrottle (get, set, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(getVOpt : 'model -> 'a voption, setWithModel : 'a voption -> 'model -> 'msg, validate : 'model -> string list, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (getVOpt = getVOpt, setWithModel = setWithModel, validate = validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(getVOpt : 'model -> 'a voption, setWithModel : 'a voption -> 'model -> 'msg, validate : 'model -> string list, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (getVOpt, setWithModel, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(get : 'model -> 'a voption, set : 'a voption -> 'msg, validate : 'model -> string list, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (get, set, validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(get : 'model -> 'a voption, set : 'a voption -> 'msg, validate : 'model -> string list, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (get, set, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(get : 'model -> 'a voption, setWithModel : 'a voption -> 'model -> 'msg, validate : 'model -> string voption, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (get, setWithModel = setWithModel, validate = validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(get : 'model -> 'a voption, setWithModel : 'a voption -> 'model -> 'msg, validate : 'model -> string voption, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (get, setWithModel, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(get : 'model -> 'a voption, set : 'a voption -> 'msg, validate : 'model -> string voption, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (get, set, validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(get : 'model -> 'a voption, set : 'a voption -> 'msg, validate : 'model -> string voption, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (get, set, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(get : 'model -> 'a voption, setWithModel : 'a voption -> 'model -> 'msg, validate : 'model -> string option, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (get, setWithModel = setWithModel, validate = validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(get : 'model -> 'a voption, setWithModel : 'a voption -> 'model -> 'msg, validate : 'model -> string option, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (get, setWithModel, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(get : 'model -> 'a voption, set : 'a voption -> 'msg, validate : 'model -> string option, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (get, set, validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(get : 'model -> 'a voption, set : 'a voption -> 'msg, validate : 'model -> string option, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (get, set, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(get : 'model -> 'a voption, setWithModel : 'a voption -> 'model -> 'msg, validate : 'model -> Result<'ignored, string>, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (get, setWithModel = setWithModel, validate = validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(get : 'model -> 'a voption, setWithModel : 'a voption -> 'model -> 'msg, validate : 'model -> Result<'ignored, string>, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (get, setWithModel, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(get : 'model -> 'a voption, set : 'a voption -> 'msg, validate : 'model -> Result<'ignored, string>, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (get, set, validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(get : 'model -> 'a voption, set : 'a voption -> 'msg, validate : 'model -> Result<'ignored, string>, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (get, set, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, setWithModel : 'a option -> 'model -> 'msg, validate : 'model -> string list, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (get, setWithModel = setWithModel, validate = validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, setWithModel : 'a option -> 'model -> 'msg, validate : 'model -> string list, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (get, setWithModel, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, set : 'a option -> 'msg, validate : 'model -> string list, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (get, set, validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, set : 'a option -> 'msg, validate : 'model -> string list, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (get, set, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, setWithModel : 'a option -> 'model -> 'msg, validate : 'model -> string voption, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (get, setWithModel = setWithModel, validate = validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, setWithModel : 'a option -> 'model -> 'msg, validate : 'model -> string voption, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (get, setWithModel, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, set : 'a option -> 'msg, validate : 'model -> string voption, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (get, set, validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, set : 'a option -> 'msg, validate : 'model -> string voption, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (get, set, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, setWithModel : 'a option -> 'model -> 'msg, validate : 'model -> string option, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (get, setWithModel = setWithModel, validate = validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, setWithModel : 'a option -> 'model -> 'msg, validate : 'model -> string option, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (get, setWithModel, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, set : 'a option -> 'msg, validate : 'model -> string option, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (get, set, validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, set : 'a option -> 'msg, validate : 'model -> string option, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (get, set, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, setWithModel : 'a option -> 'model -> 'msg, validate : 'model -> Result<'ignored, string>, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (get, setWithModel = setWithModel, validate = validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, setWithModel : 'a option -> 'model -> 'msg, validate : 'model -> Result<'ignored, string>, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (get, setWithModel, validate, (TimeSpan.FromMilliseconds timeout))
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, set : 'a option -> 'msg, validate : 'model -> Result<'ignored, string>, timespan)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidate (get, set, validate)
>> Binding.alterMsgStream (throttle timespan)
static member twoWayOptValidateThrottle
(get : 'model -> 'a option, set : 'a option -> 'msg, validate : 'model -> Result<'ignored, string>, [<Optional; DefaultParameterValue(DefaultThrottleTimeout)>] timeout : float)
: string -> Binding<'model, 'msg, 'a>
=
BindingT.twoWayOptValidateThrottle (get, set, validate, (TimeSpan.FromMilliseconds timeout)) |
High-level description
I've created such throttling function
But it throttles all the messages which causes input data to be missed.
How would you suggest to fix that?
Expected or desired behavior
All messages are throttled
Actual behavior
Only a particular message type is throttled
The text was updated successfully, but these errors were encountered: