Giter VIP home page Giter VIP logo

throttling's Introduction

THROTTLING DART LIBRARY

contain "throttling" and "debouncing" classes

Actions Status Coverage Pub License: MIT Effective Dart Star on Github

Using

See a demonstration of use at dartpad.dev

Throttling example

final thr = Throttling<void>(duration: const Duration(milliseconds: 200));
thr.throttle(() {print(' * 1');}); // print ' * 1'
await Future<void>.delayed(const Duration(milliseconds: 100));
thr.throttle(() {print(' * 2');});
await Future<void>.delayed(const Duration(milliseconds: 100));
thr.throttle(() {print(' * 3');}); // print ' * 3'
thr.close();

Debouncing example

final deb = Debouncing<void>(duration: const Duration(milliseconds: 200));
deb.debounce(() {print(' * 1');});
await Future<void>.delayed(const Duration(milliseconds: 100));
deb.debounce(() {print(' * 2');});
await Future<void>.delayed(const Duration(milliseconds: 100));
deb.debounce(() {print(' * 3');});
await Future<void>.delayed(const Duration(milliseconds: 200));
// print ' * 3'
deb.close();

throttling's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

throttling's Issues

dart complains when trying to return a future

Thanks for the work done on this package.

I'm trying to limit calls to https://pub.dev/packages/geocode to 1 per second, because of the API limit.

I created the following class:

class DebouncedGeoCode {
  final GeoCode geoCode = GeoCode();
  final debouncer =
      Debouncing<Address?>(duration: const Duration(milliseconds: 1000));

  Future<Address?> reverseGeocoding(
      {required double latitude, required double longitude}) async {
    return debouncer.debounce(() =>
        geoCode.reverseGeocoding(latitude: latitude, longitude: longitude));
  }
}

dart complains:

The return type 'Future<Address>' isn't a 'Address?', as required by the closure's context.

reversGeocoding returns Future<Address>

Debouncing: Bad state: No element

An error occurs if you run the code in DartPad.dev from the test example:

Uncaught Error: Bad state: No element
Uncaught Error: Bad state: Cannot add new events after calling close

final deb = Debouncing(duration: const Duration(seconds: 2));
deb.debounce(() {print(' * ping #1');});
await Future<void>.delayed(const Duration(seconds: 1));
deb.debounce(() {print(' * ping #2');});
await Future<void>.delayed(const Duration(seconds: 1));
deb.debounce(() {print(' * ping #3');});
await deb.close();

remove type variable from base class to actual method

the generic type on debounce (and probably also throttle), isn't used anywhere else except the debounce.

if we move the generic type to be only on the debounce method, the same debouncer can be used for different methods.

as in my last issue #9 , I'm trying to wrap the geocode package with a debouncer so it won't call geocode more than 1 once a second. the geocode package has two methods, each of them returns a different type.

it would be nice to be able to do this:

class DebouncedGeoCode {
  final GeoCode geoCode = GeoCode();
  final debouncer =
      Debouncing<Future<Address>>(duration: const Duration(milliseconds: 1000));

  Future<Address?> reverseGeocoding(
      {required double latitude, required double longitude}) async {
    var response = await debouncer.debounce<Future<Address>>(() =>
        geoCode.reverseGeocoding(latitude: latitude, longitude: longitude));
    return await response;
  }

  Future<Coordinates?> forwardGeocoding({required String address}) async {
    var response = await debouncer.debounce(() =>
        geoCode.forwardGeocoding(address: address));
    return await response;
  }
}

using one debouncer is preferable over two because I want to limit every request not just those of the same type

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.