{"id":26360588,"url":"https://github.com/simphotonics/callback_controller","last_synced_at":"2025-03-16T16:53:15.545Z","repository":{"id":281168440,"uuid":"944053827","full_name":"simphotonics/callback_controller","owner":"simphotonics","description":"Controls the call frequency of a callback and exposes a stream  that emits the controller state.","archived":false,"fork":false,"pushed_at":"2025-03-07T10:24:37.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-07T11:28:08.516Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simphotonics.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-06T17:52:32.000Z","updated_at":"2025-03-07T10:24:40.000Z","dependencies_parsed_at":"2025-03-07T11:38:29.848Z","dependency_job_id":null,"html_url":"https://github.com/simphotonics/callback_controller","commit_stats":null,"previous_names":["simphotonics/callback_controller"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simphotonics%2Fcallback_controller","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simphotonics%2Fcallback_controller/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simphotonics%2Fcallback_controller/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simphotonics%2Fcallback_controller/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simphotonics","download_url":"https://codeload.github.com/simphotonics/callback_controller/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243902250,"owners_count":20366258,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-16T16:53:14.922Z","updated_at":"2025-03-16T16:53:15.538Z","avatar_url":"https://github.com/simphotonics.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Callback Controller\n\n[![Dart](https://github.com/simphotonics/callback_controller/actions/workflows/dart.yml/badge.svg)](https://github.com/simphotonics/callback_controller/actions/workflows/dart.yml)\n\n\n## Introduction\n\nFunctions that require network calls, database queries, or writing/reading\nlocal data, have the potential of slowing down an app or program.\nIn some situations, it makes sense to restrict frequent calls to such\nfunctions.\n\nThe package introduced here, provides the eponymous\nclass [`CallbackController`][CallbackController] that can be used to limit the\nnumber of times a\ncallback is executed. When limiting the number of times a function is called\nthere are two commonly used strategies:\n* throttling: Calling the function as soon as possible and then\nrejecting subsequent calls for a certain time duration.\nThis kind of behaviour can be achieved by using the methods\n`run` and `runAsync` provided by the class [`CallbackLimiter`][CallbackLimiter].\n\n* debouncing: Starting a countdown timer when a function is called, but\naccepting  subsequent calls. After the timer has completed its countdown, the\nlatest call is finally executed. Debouncing can be achieved using the method\n`run` and `runAsync` provided by [`CallbackDelayer`][CallbackDelayer].\n\n\n## Usage\n\nTo use this library include [`callback_controller`][callback_controller]\nas a dependency in your `pubspec.yaml` file.\n\n\n[`CallbackController`][CallbackController] exposes a [stream] emitting events of\ntype [`CallbackControllerState`][CallbackControllerState].\nThe stream can be used\nwith Flutter's [StreamBuilder] to create responsive widgets. For example, a\nbutton could be styled differently if the current callback controller *state* is\n[ready][ready], [busy][busy], or [delaying][delaying].\n\n### Limiting Function Calls\n\nTo call a function as soon as possible and then\nreject subsequent calls for a certain time duration, one can use a controller\nof type [`CallbackLimiter`][CallbackLimiter]:\n\n```Dart\nimport 'dart:async';\n\nimport 'package:callback_controller/callback_controller.dart';\n\nvoid callback(){\n  print('In callback');\n}\n\nFuture\u003cvoid\u003e main(List\u003cString\u003e arguments) async {\n  final duration = Duration(milliseconds: 200);\n\n  // Defining the controller.\n  final limiter = CallbackLimiter(duration: duration));\n\n  // Alternatively, use the provided factory constructor:\n  // final limiter = CallbackController.limiter(duration: duration));\n\n  // Passing the the callback to the method run of the controller.\n  limiter.run(callback);\n}\n```\n\nThe example below shows how to limit the number of function calls using\na controller of type [`CallbackLimiter`][CallbackLimiter].\nThe program includes a loop in which a callback is run five times.\nThe actual callback is passed to the method `limiter.run` and the controller\nis configured to *delay* subsequent calls by at least 200 milliseconds.\n\n\u003cdetails\u003e \u003csummary\u003e Click to show the entire program listing. \u003c/summary\u003e\n\n```Dart\nimport 'dart:async';\n\nimport 'package:callback_controller/callback_controller.dart';\n\nFuture\u003cvoid\u003e main(List\u003cString\u003e arguments) async {\n  print('Example: Callback limiter with duration: 200 ms');\n  print('                  delay between calls:   100 ms');\n  print(' ');\n\n  final limiter = CallbackLimiter(duration: Duration(milliseconds: 200));\n\n  // ignore: unused_local_variable\n  final subscription = limiter.stream.listen(\n    (event) =\u003e print('    \u003e stream event: $event'),\n    onDone: () =\u003e print('Done'),\n    onError: (error) =\u003e print(error),\n  );\n\n  for (var i = 0; i \u003c 5; i++) {\n    print(\n        'Step $i -------------------------- ${DateTime.now().smsus} ----'\n        '-------');\n    limiter.run(() {\n      print('    in callback from step $i: ${limiter.currentState}');\n    });\n    await Future.delayed(Duration(milliseconds: 100));\n  }\n}\n```\n\u003c/details\u003e\n\u003cdetails\u003e \u003csummary\u003e Click to show the console output. \u003c/summary\u003e\n\n```Console\n$ dart bin/limiter_example.dart\nExample: Callback delayer with duration: 200 ms\n                  delay between calls:   100 ms\n\nStep 0 -------------------------- 9s:322ms:442us -----------\n    in callback from step 0: busy 9s:325ms:711us\n    \u003e stream event: ready\n    \u003e stream event: busy\n    \u003e stream event: delaying\nStep 1 -------------------------- 9s:434ms:902us -----------\nStep 2 -------------------------- 9s:537ms:329us -----------\n    in callback from step 2: busy 9s:537ms:732us\n    \u003e stream event: ready\n    \u003e stream event: busy\n    \u003e stream event: delaying\nStep 3 -------------------------- 9s:639ms:426us -----------\nStep 4 -------------------------- 9s:740ms:443us -----------\n    in callback from step 4: busy 9s:740ms:799us\n    \u003e stream event: ready\n    \u003e stream event: busy\n    \u003e stream event: delaying\n\n```\n\u003c/details\u003e\n\nNote that there is a delay of 100 milliseconds\nbetween subsequent callback runs.\nAs the console output shows, the callback is run immediately in step 0,\nand then again in step 2 after a delay of 212 milliseconds,\nand in step 4, after a delay of 203 milliseconds.\n\n\n### Delaying and Limiting Function Calls\n\nThe example below shows how to *delay and limit* the number of function calls\nusing a controller of type [`CallbackDelayer`][CallbackDelayer]. A callback\ndelayer can be used, for example, to wrap the [`onChanged`][onChanged]\nmethod of a Flutter [`TextField`][TextField]. Ths will allow the user to type\nfor a certain duration before [`onChanged`][onChanged] is called.\n\n\u003cdetails\u003e \u003csummary\u003e Click to show the entire program listing. \u003c/summary\u003e\n\n```Dart\nimport 'dart:async';\n\nimport 'package:callback_controller/callback_controller.dart';\n\nFuture\u003cvoid\u003e main(List\u003cString\u003e arguments) async {\n  print('Example: Callback delayer with duration: 200 ms');\n  print('                  delay between calls:   100 ms');\n  print(' ');\n\n  final delayer = CallbackDelayer(duration: Duration(milliseconds: 200));\n\n  // ignore: unused_local_variable\n  final subscription = delayer.stream.listen(\n    (event) =\u003e print('    \u003e stream event: $event'),\n    onDone: () =\u003e print('Done'),\n    onError: (error) =\u003e print(error),\n  );\n\n  for (var i = 0; i \u003c 5; i++) {\n    print(\n        'Step $i -------------------------- ${DateTime.now().smsus} ----'\n        '-------');\n    delayer.run(() {\n      print('    in callback from step $i: ${delayer.currentState}');\n    });\n    await Future.delayed(Duration(milliseconds: 100));\n  }\n}\n```\n\u003c/details\u003e\n\u003cdetails\u003e \u003csummary\u003e Click to show the console output. \u003c/summary\u003e\n\n```Console\n$ dart bin/delayer_example.dart\nExample: Callback delayer with duration: 200 ms\n                  delay between calls:   100 ms\n\nStep 0 -------------------------- 31s:238ms:322us -----------\n    \u003e stream event: ready\n    \u003e stream event: delaying\nStep 1 -------------------------- 31s:352ms:356us -----------\n    in callback from step 1: busy 31s:447ms:234us\n    \u003e stream event: busy\n    \u003e stream event: ready\nStep 2 -------------------------- 31s:453ms:446us -----------\n    \u003e stream event: delaying\nStep 3 -------------------------- 31s:554ms:423us -----------\n    in callback from step 3: busy 31s:654ms:410us\n    \u003e stream event: busy\n    \u003e stream event: ready\nStep 4 -------------------------- 31s:655ms:617us -----------\n    \u003e stream event: delaying\n    in callback from step 4: busy 31s:856ms:396us\n    \u003e stream event: busy\n    \u003e stream event: ready\n```\n\u003c/details\u003e\n\nThe program above includes a loop in which a callback is run five times.\nThe actual callback is passed to the method `delayer.run` and the controller\nis configured to delay subsequent calls by at least 200 milliseconds. Note that\nthere is a delay of 100 milliseconds between subsequent calls.\nAs the console output shows, the callback is *not* run in step 0.\nInstead, it is called in step 1 after a delay of 209 ms. The callback is run\nagain in step 3 after a delay of 207 ms. The callback is also run after\nstep 4 following a delay of 202 milliseconds.\n\n## Examples\n\nFor further information see [example].\n\n## Features and bugs\n\nPlease file feature requests and bugs at the [issue tracker].\n\n\n\u003c!-- Links --\u003e\n\n[issue tracker]: https://github.com/simphotonics/callback_controller/issues\n\n[example]: https://github.com/simphotonics/callback_controller/tree/main/example\n\n[callback_controller]: https://pub.dev/packages/callback_controller\n\n[CallbackController]: https://pub.dev/documentation/callback_controller/latest/callback_controller/CallbackController-class.html\n\n[CallbackControllerState]: https://pub.dev/documentation/callback_controller/latest/callback_controller/CallbackControllerState.html\n\n[CallbackLimiter]: https://pub.dev/documentation/callback_controller/latest/callback_controller/CallbackLimiter-class.html\n\n[CallbackDelayer]: https://pub.dev/documentation/callback_controller/latest/callback_controller/CallbackDelayer-class.html\n\n[stream]: https://pub.dev/documentation/callback_controller/latest/callback_controller/CallbackController/stream.html\n\n[StreamBuilder]: https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html\n\n[ready]:https://pub.dev/documentation/callback_controller/latest/callback_controller/ready-constant.html\n\n[busy]: https://pub.dev/documentation/callback_controller/latest/callback_controller/busy-constant.html\n\n[delaying]:https://pub.dev/documentation/callback_controller/latest/callback_controller/delaying-constant.html\n\n[TextField]: https://api.flutter.dev/flutter/material/TextField-class.html\n\n[onChanged]:  https://api.flutter.dev/flutter/material/TextField/onChanged.html","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimphotonics%2Fcallback_controller","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimphotonics%2Fcallback_controller","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimphotonics%2Fcallback_controller/lists"}