{"id":16642003,"url":"https://github.com/artskydj/debouncer","last_synced_at":"2025-05-07T13:04:37.240Z","repository":{"id":20169547,"uuid":"23440319","full_name":"ArtskydJ/debouncer","owner":"ArtskydJ","description":":no_entry_sign: Does not allow an action to be executed repeatedly. The time delay can lengthen on each successful call.","archived":false,"fork":false,"pushed_at":"2017-12-09T18:54:59.000Z","size":25,"stargazers_count":1,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-07T13:04:27.971Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://artskydj.github.io/debouncer/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ArtskydJ.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-28T19:30:37.000Z","updated_at":"2019-09-22T22:32:50.000Z","dependencies_parsed_at":"2022-07-12T15:17:36.201Z","dependency_job_id":null,"html_url":"https://github.com/ArtskydJ/debouncer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArtskydJ%2Fdebouncer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArtskydJ%2Fdebouncer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArtskydJ%2Fdebouncer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArtskydJ%2Fdebouncer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ArtskydJ","download_url":"https://codeload.github.com/ArtskydJ/debouncer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252883226,"owners_count":21819158,"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-10-12T07:48:35.477Z","updated_at":"2025-05-07T13:04:37.220Z","avatar_url":"https://github.com/ArtskydJ.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"debouncer\r\n=============\r\n\r\n[![Build Status](https://travis-ci.org/ArtskydJ/debouncer.svg)](https://travis-ci.org/ArtskydJ/debouncer)\r\n\r\n### [demo](http://artskydj.github.io/debouncer/)\r\n\r\n# notes on stepping\r\n\r\nEach time the action is allowed, the internal step number goes up. The step number can be used to determine how long to disallow the action. If the action is disallowed for 1 minute and is not attempted within 2 minutes, the internal step number goes down.\r\n\r\nIf `delayTimeMs` is set as a function, it will be passed the step number as it's only argument. It is the function that turns the step number into the disallowed length of time.\r\n\r\n# examples\r\n\r\nUse `debounce()` with different keys:\r\n\r\n```js\r\nvar debounce = Debouncer(database, {\r\n\tdelayTimeMs: function (step) { //ignores step\r\n\t\treturn 5000 //always allow after 5 seconds\r\n\t}\r\n})\r\n\r\nvar callback = function (err, allowed) {\r\n\tif (err) {\r\n\t\tconsole.warn(err)\r\n\t}\r\n\tconsole.log(allowed)\r\n}\r\n\r\ndebounce('foo', callback) //true (first time)\r\n\r\nsetTimeout(function () {\r\n\tdebounce('foo', callback) //false\r\n\tdebounce('bar', callback) //true, (note 'bar')\r\n}, 2500)\r\n\r\nsetTimeout(function () {\r\n\tdebounce('foo', callback) //true, (been over 5 sec since last success)\r\n\tdebounce('bar', callback) //false, (been under 5 sec since last success)\r\n}, 5100)\r\n\r\nsetTimeout(function () {\r\n\tdebounce('foo', callback) //true, (been over 5 sec since last success)\r\n\tdebounce('bar', callback) //true, (been over 5 sec since last success)\r\n}, 12000)\r\n```\r\nHere's what it looks like in a timeline:\r\n```\r\nseconds   0 1 2 3 4 5 6 7 8 9 10  12\r\n          | | | | | | | | | | | | |\r\nfoo       Y    N    Y             Y\r\nbar            Y    N             Y\r\n```\r\n\r\nScaling delay:\r\n\r\n```js\r\nvar debounce = Debouncer(database, {\r\n\tdelayTimeMs: function (step) {\r\n\t\treturn step*1000 //allow after `step` seconds (`step` is the number of successes)\r\n\t}\r\n\t//same as doing the following:\r\n\t//delayTimeMs: 1000\r\n})\r\n\r\nvar callback = function (err, allowed) {\r\n\tif (err) {\r\n\t\tthrow err\r\n\t}\r\n\tconsole.log(allowed)\r\n}\r\n\r\ndebounce('foo', callback) //true (will be false until 1 sec after this)\r\ndebounce('foo', callback) //false\r\n\r\nsetTimeout(function () {\r\n\tdebounce('foo', callback) //false\r\n}, 900)\r\nsetTimeout(function () {\r\n\tdebounce('foo', callback) //true (will be false until 2 sec after this)\r\n}, 1100)\r\n\r\nsetTimeout(function () {\r\n\tdebounce('foo', callback) //false\r\n}, 2300)\r\nsetTimeout(function () {\r\n\tdebounce('foo', callback) //false\r\n}, 3000)\r\nsetTimeout(function () {\r\n\tdebounce('foo', callback) //true (will be false until 3 sec after this)\r\n}, 3200)\r\n\r\nsetTimeout(function () {\r\n\tdebounce('foo', callback) //true (will be false until 4 sec after this)\r\n}, 6300)\r\n\r\nsetTimeout(function () {\r\n\tdebounce('foo', callback) //true (will be false until 5 sec after this)\r\n}, 10400)\r\n```\r\n\r\n# api\r\n\r\n```js\r\nvar Debouncer = require('debouncer')\r\n```\r\n\r\n## `Debouncer(db, opts)`\r\n\r\nReturns a [`debounce()`](#debouncekey-cb) function.\r\n\r\n- `db` takes a level db object.\r\n- `opts` takes an object with the following properties:\r\n\t- `delayTimeMs` can be a function, a number, or an array.\r\n\t\t- If it is a function, the step is passed as it's first parameter, and the return value is the delay time. `return func(step)`\r\n\t\t- If it is a number, the return value is the step multiplied by the number. `return number * step`\r\n\t\t- If it is an array, the return value is the array element at the index of step. If step is to large, it defaults to the last element. `return array[step]`\r\n\r\n## `debounce(key, cb)`\r\n\r\n- `key` takes a string. One key will not cause a different key to get debounced.\r\n- `cb` takes a function with the following arguments:\r\n\t- `err` is an error object, or `null`.\r\n\t- `allowed` is whether or not the action is allowed.\r\n\t- `remaining` is the remaining time (in ms) if the action was not allowed.\r\n\r\n# instantiation Examples\r\n\r\n### function\r\n\r\nAlways allow after 5 seconds since last allowance:\r\n\r\n```js\r\nvar debounce = Debouncer(database, {\r\n\tdelayTimeMs: function (step) { //ignores `step`\r\n\t\treturn 5000\r\n\t}\r\n})\r\n```\r\n\r\nAllow after a random number of seconds between 0 and step: (I am pretty sure that this is not useful in any way.)\r\n\r\n```js\r\nvar debounce = Debouncer(database, {\r\n\tdelayTimeMs: function (step) {\r\n\t\treturn Math.floor(Math.random() * step) * 1000\r\n\t}\r\n})\r\n\r\n```\r\n\r\n### number\r\n\r\nAdd 2 seconds after each allowance:\r\n\r\n```js\r\nvar debounce = Debouncer(database, {\r\n\tdelayTimeMs: 2000\r\n})\r\n```\r\n\r\n### array\r\n\r\n1. Allow right away.\r\n2. Allow after 1 second.\r\n3. Allow after 5 seconds.\r\n4. Allow after 20 seconds.\r\n5. Allow after 20 seconds.\r\n6. Allow after 20 seconds.\r\n7. You get it...\r\n\r\n```js\r\nvar debounce = Debouncer(database, {\r\n\tdelayTimeMs: [0, 1000, 5000, 20000]\r\n})\r\n```\r\n\r\nThis will act like the one above. Element 0 is set to 0, but the original object is not modified.\r\n\r\n```js\r\nvar debounce = Debouncer(database, {\r\n\tdelayTimeMs: [1000, 5000, 20000]\r\n})\r\n```\r\n\r\n# license\r\n\r\n[VOL](http://veryopenlicense.com)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartskydj%2Fdebouncer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fartskydj%2Fdebouncer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartskydj%2Fdebouncer/lists"}