{"id":15662293,"url":"https://github.com/pubkey/custom-idle-queue","last_synced_at":"2025-10-25T00:11:08.512Z","repository":{"id":66053637,"uuid":"104577551","full_name":"pubkey/custom-idle-queue","owner":"pubkey","description":"Optimize the performance of important tasks by delaying background-tasks","archived":false,"fork":false,"pushed_at":"2020-05-26T22:17:11.000Z","size":62,"stargazers_count":21,"open_issues_count":2,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-19T12:13:50.917Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://github.com/pubkey/custom-idle-queue/blob/master/docs.md","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pubkey.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-23T15:24:20.000Z","updated_at":"2024-12-30T04:07:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"42b87b61-92e2-4598-b3ee-6e0686d982bc","html_url":"https://github.com/pubkey/custom-idle-queue","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubkey%2Fcustom-idle-queue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubkey%2Fcustom-idle-queue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubkey%2Fcustom-idle-queue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubkey%2Fcustom-idle-queue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pubkey","download_url":"https://codeload.github.com/pubkey/custom-idle-queue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252591523,"owners_count":21773126,"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-03T13:31:19.102Z","updated_at":"2025-10-25T00:11:08.190Z","avatar_url":"https://github.com/pubkey.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Custom Idle Queue\n\nThis is a npm-module that lets you optimize the performance of important tasks by delaying background-tasks. It works a bit like [requestIdleCallback](https://developer.mozilla.org/de/docs/Web/API/Window/requestIdleCallback) but instead of fetching idle-time of the CPU, you can use this for any given limited ressource.\n\n## Quickstart\n\nIn this example we define `database-requests` as limited ressource. We create an idleQueue arround all calls to the ressource to ensure our `importantTask` is as fast as possible and the `backgroundTask` only runs when no `importantTask` is using the database.\n\n`npm install custom-idle-queue --save`\n\n```javascript\n// require\nconst { IdleQueue } = require('custom-idle-queue');\n\n// OR import\nimport { IdleQueue } from 'custom-idle-queue';\n\n// create a new queue\nconst myQueue = new IdleQueue();\n\n\n// wrap all calls to your limited ressource\nconst readFromDatabase = key =\u003e myQueue.wrapCall(\n    () =\u003e pseudoDatabaseModule.get(key)\n);\nconst writeToDatabase = (key, value) =\u003e myQueue.wrapCall(\n    () =\u003e pseudoDatabaseModule.set(key, value);\n);\nconst deleteFromDatabase = (key) =\u003e myQueue.wrapCall(\n    () =\u003e pseudoDatabaseModule.delete(key, value);\n);\n\n// this is the important task\nconst importantTask = async function increaseClickNumber() {\n    const oldNumber = await readFromDatabase('nr');\n    const newNumber = oldNumber++;\n    await writeToDatabase('nr', newNumber);\n    await writeToDatabase('time_' + newNumber, new Date().getTime());\n    return newNumber;\n};\n\n// this is the background task\nconst backgroundTask = async function cleanUpOldClicks() {\n    const newest = await readFromDatabase('nr');\n    const limitDate = new Date().getTime() - 1000*60*60;\n    for (let i = 0; i \u003c newest; i++) {\n        const date = await readFromDatabase('time_' + i);\n        if(date \u003c limitDate){\n            await deleteFromDatabase('time_' + i);\n        }\n    }\n}\n\n// we now run the backgroundTask in an intervall without slowing down the importantTask\n(async() =\u003e {\n    while(true){\n        await myQueue.requestIdlePromise(); // wait until database-requests in idle\n        await backgroundTask();\n\n        await new Promise(res =\u003e setTimeout(res, 2000)); // wait 2 seconds\n    }\n})();\n\n// if we now run the importantTask, it will not be slowed down by the backgroundTask\ndocument\n    .querySelector('#myButton')\n    .addEventListener('click', () =\u003e {\n        const newNr = await importantTask();\n        labelDomElement.innerHTML = newNr.toString();\n    });\n\n// You can find the full documentation here https://github.com/pubkey/custom-idle-queue/blob/master/docs.md\n\n```\n\n## Use cases\nThis module can be used on any limited ressource like\n\n- HTTP-Requests\n- Database-Calls\n- Service-Worker-Calls\n- Animations\n\n## Limitations\n\n- **IdleQueue cannot predict the future**\n\nWhen you start a `backgroundTask` first and the `importantTask` afterwards, the `backgroundTask` will slow down the `importantTask` because it is already running. To prevent this, you should use `requestIdlePromise` as granular as possible. The backgroundTask-function from the example would be better when it awaits the idle-state before each usage of the limited ressource. This will ensure that the `backgroundTask` will be paused until the `importantTask` has finished.\n\n```js\n// this is the background task\nconst backgroundTask = async function cleanUpOldClicks() {\n    await myQueue.requestIdlePromise(); // request idle-state each time\n    const newest = await readFromDatabase('nr');\n    const limitDate = new Date().getTime() - 1000*60*60;\n    for (let i = 0; i \u003c newest; i++) {\n        await myQueue.requestIdlePromise(); // request idle-state each time\n        const date = await readFromDatabase('time_' + i);\n        if(date \u003c limitDate){\n            await myQueue.requestIdlePromise(); // request idle-state each time\n            await deleteFromDatabase('time_' + i);\n        }\n    }\n}\n```\n\n- **You cannot optimize CPU-only ressources**\n\nBecause javascript runs in a single process, it doesn't make sense to define CPU as limited ressource. For example if you have a CPU-only-Function like `calculatePrimeNumber`, you should not limit the access to the function with an idle-queue because at the time you call `idleQueue.lock()` or `idleQueue.wrapCall()`, the process will instantly run `calculatePrimeNumber` before it even can change the idle-queue state.\n\n\n\n## Browser-Support\n\nThis module is using the [Promise-](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Promise) and the [Map](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Map)-Object. If your runtime does not support them, you have to add them via polyfills.\n\n## [Read the full documentation here](https://github.com/pubkey/custom-idle-queue/blob/master/docs.md) \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpubkey%2Fcustom-idle-queue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpubkey%2Fcustom-idle-queue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpubkey%2Fcustom-idle-queue/lists"}