{"id":24835743,"url":"https://github.com/tobilg/buffered-queue","last_synced_at":"2025-06-21T04:10:05.433Z","repository":{"id":57191031,"uuid":"37536372","full_name":"tobilg/buffered-queue","owner":"tobilg","description":"A simple to use buffering queue (no dependencies) which flexibly buffers objects, strings, integers etc. until either a maximum size is reached, or an interval has come to an end.","archived":false,"fork":false,"pushed_at":"2022-11-01T10:57:22.000Z","size":85,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-25T23:04:33.590Z","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/tobilg.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}},"created_at":"2015-06-16T14:46:47.000Z","updated_at":"2025-03-01T04:03:04.000Z","dependencies_parsed_at":"2023-01-20T17:37:21.564Z","dependency_job_id":null,"html_url":"https://github.com/tobilg/buffered-queue","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tobilg%2Fbuffered-queue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tobilg%2Fbuffered-queue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tobilg%2Fbuffered-queue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tobilg%2Fbuffered-queue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tobilg","download_url":"https://codeload.github.com/tobilg/buffered-queue/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tobilg%2Fbuffered-queue/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":257518128,"owners_count":22557983,"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-01-31T04:51:34.673Z","updated_at":"2025-06-21T04:10:00.420Z","avatar_url":"https://github.com/tobilg.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# buffered-queue\n\nA simple to use buffering queue as Node.js module (no dependencies), which flexibly buffers objects, strings, integers etc. until either a maximum size is reached, or an interval has come to an end.\n\n## Installation\n\nYou can simply install this module by running \n\n`npm install buffered-queue --save`\n\nfrom the command line withing your project's folder.\n\n## Usage\n\nThe module can be required by\n\n    var Queue = require('buffered-queue');\n    var q = new Queue(name, options);\n\nThere are two optional parameters, the `name` of the queue and the configuration `options` object.\n\n### Basic configuration\n```\nvar q = new Queue('pageviews', {\n    size: 3,               // The maximum size before the queue is flushed (default: 10)\n    flushTimeout: 1000,    // The timeout in milliseconds after the queue is flushed\n                           // (default: null, so there will be no recurring queue flush configured)\n    verbose: true          // Whether debug info should be logged to console.log or not (default: false)\n});\n```\n\n### Add items to the queue\n\nBasically you can add every type except functions to the queue. It's advisable that you use the same type though, or, if not, use a custom result function which handles the streamlining process (see below). \n\n#### Objects\n```\nq.add({foo: \"bar\"});\n```\n\n#### Strings\n```\nq.add(\"This is a test!\");\n```\n\n### Queue flush\n\nIf the queue flushes, a `flush` event is triggered. If can be received as follows:\n\n```\nq.on(\"flush\", function(data, name){\n    console.log(data);\n});\n```\n\n### The \"Custom result function\"\n\nYou can also pass a function within the `options` object, which is then used to control the result once the flush is triggered. If there's no such function configured, the queue will just return the array in which it stored the items. \n\n#### Example\n\nThe result function below will uppercase all items (considered they're strings):\n\n```\nvar q = new Queue('test', {\n    customResultFunction: function(items) {\n        var temp = [];\n        items.forEach(function(item, index, array) {\n            temp.push(item.toUpperCase());\n        });\n        return temp;\n    }\n});\n\n```\n\n### Complete example\n\nThink about a use case where you receive input from a process, and want to log the results only after 3 seconds have passed, or 5 items have been added.  \n\n```\nvar Queue = require('buffered-queue');\n\nvar q = new Queue('example', {\n    size: 5,\n    flushTimeout: 3000,\n    verbose: true,\n    customResultFunction: function(items) {\n        var temp = [];\n        items.forEach(function(item, index, array) {\n            temp.push(item.toUpperCase());\n        });\n        return temp.join('\\n');\n    }\n});\n\nq.on(\"flush\", function(data, name){\n    console.log(data);\n});\n\nq.add(\"Message 1\");\nq.add(\"Message 2\");\nq.add(\"Message 3\");\nq.add(\"Message 4\");\n\nsetTimeout(function() {\n    q.add(\"Message 5\");\n    q.add(\"Message 6\");\n}, 4000);\n```\n\nThe output will be the following:\n\n```\nQueue (example): Added item:  Message 1\nQueue (example): Maximum Queue size not reached. Currently 1 of 5 in queue!\nQueue (example): Added item:  Message 2\nQueue (example): Maximum Queue size not reached. Currently 2 of 5 in queue!\nQueue (example): Added item:  Message 3\nQueue (example): Maximum Queue size not reached. Currently 3 of 5 in queue!\nQueue (example): Added item:  Message 4\nQueue (example): Maximum Queue size not reached. Currently 4 of 5 in queue!\nQueue (example): The timeout triggered a Queue flush! 4 items are in the Queue.\nMESSAGE 1\nMESSAGE 2\nMESSAGE 3\nMESSAGE 4\nQueue (example): Added item:  Message 5\nQueue (example): Maximum Queue size not reached. Currently 1 of 5 in queue!\nQueue (example): Added item:  Message 6\nQueue (example): Maximum Queue size not reached. Currently 2 of 5 in queue!\nQueue (example): The timeout triggered a Queue flush! 2 items are in the Queue.\nMESSAGE 5\nMESSAGE 6\n```\n\n## Testing\n\n`Jest` is used for testing. Just run `npm test`. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftobilg%2Fbuffered-queue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftobilg%2Fbuffered-queue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftobilg%2Fbuffered-queue/lists"}