3

Does anyone have a better way to implement throttle on value changes in c# ?

I'm using this right now and I find it a bit "too much lines"

@highlight
searchThrottle.Throttle(TimeSpan.FromMilliseconds(200))
.Select(e => ((string)e.Value)?.Trim())
.DistinctUntilChanged()
.Subscribe((x) =>
{
quisckSearch = x;
InvokeAsync(async () => await LoadFirstPage());
});

Comments
  • 0
    No idea how C# works, but here an idea

    time_range = TimeSpan.FromMilliseconds(200)
    value = (string)e.Value)?.Trim()

    def throttling_action(x):
    {
    quisckSearch = x;
    InvokeAsync(async () => await LoadFirstPage());
    }

    @highlight
    searchThrottle.Throttle(time_range)
    .Select(e => (value)
    .DistinctUntilChanged()
    .Subscribe((x) => throttling_action(x));

    Refactor first to have everything as separate readably named values first
    It already looks nicer in my opinion

    ================
    Then make an attempt to wrap thing thingy too

    time_range = TimeSpan.FromMilliseconds(200)
    value = (string)e.Value)?.Trim()

    def throttling_action(x):
    {
    quisckSearch = x;
    InvokeAsync(async () => await LoadFirstPage());
    }

    def trotthler_on_value_change(throttler)
    return throttler.Select(e => (value)
    .DistinctUntilChanged()
    .Subscribe((x) => throttling_action(x))

    def throttler():
    return highlight
    searchThrottle.Throttle(time_range).
    trotthler_on_value_change()

    @throttler();
  • 1
    If I understood correcrly you're looking for a debouncer not a throttler, if you invoke the same method in a threshold tineframe you cancel the previous request, right?
  • 0
    @prodigy214 that's exactly what this code does. I'm just looking for "less" lines ! But thanks !
  • 0
    @dontbeevil In this case now.

    It's really a throttle.

    BUT there is a debouncer at the level of LoadFirstPage, which will cancel old task and start a new one.

    because that can be called while pagination happens or filter is applied or quick search is performed.
Add Comment