{"id":22865368,"url":"https://github.com/toplan/lazy-enqueue","last_synced_at":"2025-03-31T09:46:56.890Z","repository":{"id":85627675,"uuid":"82383986","full_name":"toplan/lazy-enqueue","owner":"toplan","description":":turtle: Make your enqueue function lazily","archived":false,"fork":false,"pushed_at":"2017-05-04T10:18:54.000Z","size":15,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-06T14:27:09.366Z","etag":null,"topics":["enqueue","lazy","queue"],"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/toplan.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-02-18T12:07:31.000Z","updated_at":"2023-03-10T10:01:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"12eba1ef-d1fd-41eb-957c-c42a36abc46a","html_url":"https://github.com/toplan/lazy-enqueue","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toplan%2Flazy-enqueue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toplan%2Flazy-enqueue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toplan%2Flazy-enqueue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toplan%2Flazy-enqueue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/toplan","download_url":"https://codeload.github.com/toplan/lazy-enqueue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246450402,"owners_count":20779406,"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":["enqueue","lazy","queue"],"created_at":"2024-12-13T11:36:46.873Z","updated_at":"2025-03-31T09:46:56.863Z","avatar_url":"https://github.com/toplan.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Intro\nA lightweight and no dependencies library to make your enqueue function lazily.\n\n# Install\n```\nnpm i --save lazy-enqueue\n```\n\n# Usage\n\n```javascript\nimport lazy from 'lazy-enqueue'\n\nconst queue = []\nconst enqueue = queue.unshift.bind(queue)\nconst lazilyEnqueue = lazy(enqueue, {\n    delay: 1000,\n    bufferSize: 3,\n    will: data =\u003e {\n        console.group()\n        console.timeEnd('time')\n        console.log('[will] data:', data)\n    },\n    success: (value, data) =\u003e {\n        if (value \u003e 3) queue.pop()\n        console.log('[success] queue:', queue)\n        console.groupEnd()\n    },\n    failure: (err, data) =\u003e {\n        console.warn('[failure] data:', data, err)\n    }\n})\n\nconsole.time('time')\nlazilyEnqueue(0);lazilyEnqueue(1)\nsetTimeout(() =\u003e {\n    lazilyEnqueue(2);lazilyEnqueue(3);lazilyEnqueue(4);lazilyEnqueue(5)\n}, 2000)\n```\n\n# API\n\n## lazy(enqueue, [options])\n```javascript\nimport lazy from 'lazy-enqueue'\n```\nCreate a lazily higher order function of the original enqueue function.\n\n#### Arguments\n1. `enqueue` (Function)\n\n    The original enqueue function.\n\n2. `[options]` (Object)\n\n    - delay (Number|Promise|Function): The global delay value.\n\n    - will (Function): The global hook, called before the enqueue action.\n\n    - success (Function): The global hook, called after successfully enqueue.\n\n    - failure (Function): The global hook, called if failed with any reason.\n\n    - bufferSize (Number): The size of buffer. Default `Infinity`.\n\n#### Returns\n`(lazilyEnqueue)`: A lazily higher order function.\n\n## delay(value)\n```javascript\nconst {delay} = lazilyEnqueue(data)\n```\nSet private delay value for the current enqueue action. In other words, override the global delay value.\n\n\u003e All of the functions(api) return by `(lazilyEnqueue)` support chain calls, like this:\n\u003e `delay(100).hook('will', fn).hook('success', fn)`\n\n#### Arguments\n`value` (Number|Promise|Function)\n\nIf this value is a promise,\nthe return value(if has) will override the original data, witch be passed to the `(lazilyEnqueue)` function.\n\nIf this value is a function,\nit will be called with specified parameters, witch passed to the `(lazilyEnqueue)` function,\nand the return value should be a number or promise.\n\n## hook(name, fn)\n```javascript\nconst {hook} = lazilyEnqueue(data)\n```\nAdd private hooks for the current enqueue action. These private hooks called before the global hooks.\n\n#### Arguments\n1. `name` (String)\n\n    The hook name, there are only three optional value: `will`, `success`, `failure`.\n\n2. `fn` (Function)\n\n    The hook handler.\n\n    If the hook name is `will`,\n    the parameters of this hook function is same to the parameters passed to the `(lazilyEnqueue)` function,\n    and the return value(if has) will be passed to the next `will` hook as its parameter.\n    In the end, the latest return value will be pushed to the queue (override the original data).\n\n    If the hook name is `success`, the first parameter of this hook function is the return value of the original enqueue function,\n    and the remaining parameters is same to the parameters passed to the `(lazilyEnqueue)` function.\n\n    If the hook name is `failure`, the first parameter of this hook function is the error information.\n    and the remaining parameters is same to the parameters passed to the `(lazilyEnqueue)` function.\n\n## will(fn)\n```javascript\nconst {will} = lazilyEnqueue(data)\n```\nIt's same to `hook('will', fn)`.\n\n## done(onSuccess, [onFailure])\n```javascript\nconst {done} = lazilyEnqueue(data)\n```\nIt's same to `hook('success', onSuccess).hook('failure', onFailure)`.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoplan%2Flazy-enqueue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftoplan%2Flazy-enqueue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoplan%2Flazy-enqueue/lists"}