{"id":13451808,"url":"https://github.com/lukeed/saturated","last_synced_at":"2025-10-09T18:25:05.128Z","repository":{"id":66007897,"uuid":"206640284","full_name":"lukeed/saturated","owner":"lukeed","description":"A tiny (203B) utility to enqueue items for batch processing and/or satisfying rate limits.","archived":false,"fork":false,"pushed_at":"2019-09-14T22:20:03.000Z","size":14,"stargazers_count":113,"open_issues_count":0,"forks_count":2,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-28T18:15:59.857Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/lukeed.png","metadata":{"files":{"readme":"readme.md","changelog":null,"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}},"created_at":"2019-09-05T19:20:36.000Z","updated_at":"2024-10-28T04:06:20.000Z","dependencies_parsed_at":"2023-07-26T07:02:01.481Z","dependency_job_id":null,"html_url":"https://github.com/lukeed/saturated","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fsaturated","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fsaturated/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fsaturated/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fsaturated/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lukeed","download_url":"https://codeload.github.com/lukeed/saturated/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245158343,"owners_count":20570166,"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":"2024-07-31T07:01:03.263Z","updated_at":"2025-10-09T18:25:00.107Z","avatar_url":"https://github.com/lukeed.png","language":"JavaScript","readme":"# saturated [![build status](https://badgen.now.sh/github/status/lukeed/saturated)](https://github.com/lukeed/saturated/actions) [![codecov](https://badgen.now.sh/codecov/c/github/lukeed/saturated)](https://codecov.io/gh/lukeed/saturated)\n\n\u003e A tiny (203B) utility to enqueue items for batch processing and/or satisfying rate limits.\n\nBuild a queue for your function – ideal for communicating with APIs that prefer batch/bulk processing or services that enforce rate limiting.\n\nWith `saturated` you provide a `handler` which will only be called every N items or after every X milliseconds... whichever comes first! This then allows you to [`push()`](#isaturatedpushvalue) your payload(s) into the queue, waiting for the next tick.\n\nThis module exposes three module definitions:\n\n* **CommonJS**: `dist/saturated.js`\n* **ES Module**: `dist/saturated.mjs`\n* **UMD**: `dist/saturated.min.js`\n\n\n## Install\n\n```\n$ npm install --save saturated\n```\n\n\n## Usage\n\n```js\nimport saturated from 'saturated';\n\n// Setup an instance\nconst rated = saturated(arr =\u003e {\n  if (arr.length \u003e 0) {\n    console.log('~\u003e Received', arr);\n  } else {\n    console.log('~\u003e Empty...');\n  }\n}, {\n  max: 5, // limit\n  interval: 3e3 // 3s\n});\n\n// Now we have a `saturated` instance that will\n//   call our function every 3 seconds or once it\n//   has 5 items in its queue – whichever comes first.\n\n// For demo purposes\nconst sleep = ms =\u003e new Promise(r =\u003e setTimeout(r, ms));\n\n// Demo usage\nasync function demo() {\n  rated.push('hello'); //=\u003e 1\n  rated.push('world'); //=\u003e 2\n  rated.push('how'); //=\u003e 3\n  rated.push('are'); //=\u003e 4\n  rated.push('you'); //=\u003e 5\n\n  // Queue received 5th item, immediately invoke!\n  // ~\u003e Received ['hello', 'world', 'how', 'are', 'you']\n  rated.size(); //=\u003e 0 (flushed)\n\n  rated.push('hola'); //=\u003e 1\n  rated.push('mundo'); //=\u003e 2\n  rated.size(); //=\u003e 2\n  await sleep(3e3);\n\n  // Interval waited 3 seconds\n  // ~\u003e Received ['hola', 'mundo']\n  rated.size(); //=\u003e 0 (flushed)\n\n  // Wait another 3s ...\n  await sleep(3e3);\n\n  // ~\u003e Empty...\n  rated.size(); //=\u003e 0 (flushed anyway)\n}\n\n// Init demo\ndemo().then(() =\u003e {\n  rated.end(); // quit\n});\n```\n\n\n## API\n\n### saturated(handler, options)\nReturns: `ISaturated`\n\n#### handler\nType: `Function`\u003cbr\u003e\nRequired: `true`\n\nThe function to invoke once a threshold has been met.\u003cbr\u003e\nIt will always receive an `Array` of whatever item(s) you previously [`push`](#isaturatedpushvalue)ed.\n\n\u003e **Note:** You may be passed an empty Array!\n\n#### options.max\nType: `Number`\u003cbr\u003e\nDefault: `Infinity`\n\nThe maximum size of the queue.\u003cbr\u003e\nFor example, with `max: 5`, your `handler` will be invoked immediately after the 5th item was pushed to queue.\n\n\u003e **Important:** Your function will be called if `max` is met ***before*** the next `interval` tick.\n\n#### options.interval\nType: `Number`\u003cbr\u003e\nDefault: `10000`\n\nThe amount of time, in milliseconds, to wait before calling your `handler` function.\u003cbr\u003e\nDefaults to calling your `handler` every 10 seconds, even if the queue is empty.\n\n\n### ISaturated.size()\nReturns: `Number`\n\nGet the current size of the queue.\n\n\n### ISaturated.push(value)\nReturns: `Number`\n\nAdd an item/value into the queue stack.\u003cbr\u003e\nDoing so will return the current size of the queue.\n\n#### value\nType: `Any`\n\nYou may push any value into queue.\n\n\u003e **Important:** Anything you push will be added to an Array!\n\u003e Pushing an Array will have your `handler` receive an Array of your Arrays.\n\n\n### ISaturated.end(toFlush)\nReturns: `undefined`\n\nCancels the internal `setInterval` timer.\n\n#### toFlush\nType: `Boolean`\u003cbr\u003e\nDefault: `false`\n\nWhen `true`, will also [`flush()`](#isaturatedflush) the queue so that remaining items will be passed to your `handler` function.\n\n\n### ISaturated.flush()\nReturns: `undefined`\n\nForcibly invoke your `handler` will _all_ items currently in the queue.\n\nCalling `flush` will restart the internal `setInterval` timer and empty the queue.\n\n\n## License\n\nMIT © [Luke Edwards](https://lukeed.com)\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukeed%2Fsaturated","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flukeed%2Fsaturated","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukeed%2Fsaturated/lists"}