{"id":19858576,"url":"https://github.com/danreynolds/data_batcher","last_synced_at":"2025-10-13T03:24:41.487Z","repository":{"id":195095574,"uuid":"692237072","full_name":"danReynolds/data_batcher","owner":"danReynolds","description":"Data batcher batches and de-dupes data fetched in the same task of the event loop.","archived":false,"fork":false,"pushed_at":"2024-01-19T03:54:54.000Z","size":12,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-11T14:26:38.597Z","etag":null,"topics":["batching","data","flutter","hacktoberfest"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/data_batcher","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danReynolds.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":"2023-09-15T22:09:01.000Z","updated_at":"2024-11-11T06:58:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"e2240fa5-298b-474d-b64b-b167a06f4139","html_url":"https://github.com/danReynolds/data_batcher","commit_stats":null,"previous_names":["danreynolds/data_batcher"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danReynolds%2Fdata_batcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danReynolds%2Fdata_batcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danReynolds%2Fdata_batcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danReynolds%2Fdata_batcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danReynolds","download_url":"https://codeload.github.com/danReynolds/data_batcher/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241255130,"owners_count":19934815,"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":["batching","data","flutter","hacktoberfest"],"created_at":"2024-11-12T14:23:58.865Z","updated_at":"2025-10-13T03:24:36.469Z","avatar_url":"https://github.com/danReynolds.png","language":"Dart","readme":"# Data Batcher\n\nData batcher batches and de-dupes data fetched in the same task of the event loop.\n\n```dart\nfinal batcher = DataBatcher\u003cString\u003e(\n  execute: (ids) async {\n    print(ids) // ['1', '2', '3', '4']\n    return Api.fetch(ids);\n  },\n);\n\nbatcher.add('1');\nbatcher.add('2');\n\nbatcher.addMany(['2', '3']);\n\nawait batcher.add('4');\n```\n\nWhen IDs are added to a batcher, they are grouped by the current cycle of the event loop and scheduled to be executed on a micro-task.\nOnce executed successfully, the batcher resolves each caller's future with its data as shown below:\n\n```dart\nfinal batcher = DataBatcher\u003cString\u003e(\n  execute: (ids) async {\n    return ['a', 'b', 'c'];\n  },\n);\n\nbatcher.add('1').then(((resp) =\u003e print(resp)); // 'a'\nbatcher.add('2').then(((resp) =\u003e print(resp)); // 'b'\n\nbatcher.addMany(['2', '3']).then(((resp) =\u003e print(resp)) // ['b', 'c']\n```\n\nData added to a batcher across different ticks of the event loop is broken into separate batches:\n\n```dart\nfinal batcher = DataBatcher\u003cString\u003e(\n  execute: (ids) async {\n    print(ids);\n    // ['1', '2']\n    // ['3']\n    return Api.fetch(ids);\n  },\n);\n\nbatcher.add('1');\nawait batcher.add('2');\n\nbatcher.add('3');\n```\n\nBy default, data IDs that are still in-flight from a previous batch which are requested again are not re-fetched:\n\n```dart\nfinal batcher = DataBatcher\u003cString\u003e(\n  execute: (ids) async {\n    print(ids);\n    // ['1', '2']\n    return Future.delayed(Duration(seconds: 5));\n  },\n);\n\nbatcher.add('1');\nbatcher.add('2');\n\nawait Future.delayed(Duration(seconds: 1));\n\nbatcher.add('1');\n```\n\nThe second attempt to request data with ID 1 is de-duped, since it is called while the in-flight request for '1' and '2' has not resolved. The Future returned by the second call to add ID 1 will resolve when the first original batch succeeds and with its returned value for ID 1.\n\nIf de-duping of in-flight data is not preferred, the `dedupeInFlight` flag can be set to false:\n\n```dart\nfinal batcher = DataBatcher\u003cString\u003e(\n  dedupeInFlight: false,\n  execute: (ids) async {\n    print(ids);\n    // ['1', '2']\n    // ['1']\n    return Future.delayed(Duration(seconds: 5), () {...});\n  },\n);\n\nbatcher.add('1');\nbatcher.add('2');\n\nawait Future.delayed(Duration(seconds: 1));\n\nbatcher.add('1');\n```\n\nIDs are mapped to data responses using the order of the returned data. If needed, an `idExtractor` can be specified instead in order to associated response data with its matching input ID:\n\n```dart\nfinal batcher = DataBatcher\u003cDataModel\u003e(\n  idExtractor: (dataModel) =\u003e dataModel.id,\n  execute: (ids) async {\n    print(ids);\n    // ['1', '2']\n    return [DataModel('1'), DataModel('2')]\n  },\n);\n\nbatcher.add('1');\nbatcher.add('2');\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanreynolds%2Fdata_batcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanreynolds%2Fdata_batcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanreynolds%2Fdata_batcher/lists"}