{"id":18794240,"url":"https://github.com/conversejs/mergebounce","last_synced_at":"2025-04-13T14:32:35.450Z","repository":{"id":37797009,"uuid":"350702987","full_name":"conversejs/mergebounce","owner":"conversejs","description":"Batch multiple function calls into one, without losing data.","archived":false,"fork":false,"pushed_at":"2025-04-03T14:28:10.000Z","size":404,"stargazers_count":10,"open_issues_count":10,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-03T14:41:12.985Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/conversejs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":null,"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":"2021-03-23T12:25:27.000Z","updated_at":"2025-04-03T14:28:16.000Z","dependencies_parsed_at":"2024-11-07T21:31:03.433Z","dependency_job_id":"808415b3-df4c-4982-9fd4-0f72d5c28b07","html_url":"https://github.com/conversejs/mergebounce","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/conversejs%2Fmergebounce","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/conversejs%2Fmergebounce/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/conversejs%2Fmergebounce/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/conversejs%2Fmergebounce/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/conversejs","download_url":"https://codeload.github.com/conversejs/mergebounce/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248728208,"owners_count":21152178,"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-11-07T21:28:46.553Z","updated_at":"2025-04-13T14:32:35.168Z","avatar_url":"https://github.com/conversejs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mergebounce\n\n`mergebounce` is a fork of lodash's [debounce](https://lodash.com/docs/4.17.15#debounce) function with the added feature that it'll use the lodash [merge](https://lodash.com/docs/4.17.15#merge)\nfunction to merge all passed in parameters before eventually invoking the debounced inner function.\n\nBy doing so, it's possible to combine multiple expensive calls into a single one\nwithout losing state changes that would have been made by individual calls.\n\n## Install\n\n```\nnpm i mergebounce\n```\n\n## usecases\n\n### Batching expensive writes\n\nImagine you have a frontend app that stores data in IndexedDB via [localForage](https://localforage.github.io/localForage).\nWrites to IndexedDB are slow, which can slow down your whole app.\n\nYou can increase performance by batching writes together.\n\nOne way to do this, is to install [localForage-setItems](https://github.com/localForage/localForage-setItems)\nand then to use `mergebounce` to combine multple calls to `setItems` into a\nsingle one.\n\n```javascript\nconst debouncedSetItems = mergebounce(\n    items =\u003e store.setItems(items),\n    50,\n    {'promise': true}\n);\n\nfunction save (json) {\n    const items = {}\n    items[json.id)] = json;\n    return debouncedSetItems(items);\n}\n```\n\nNow `save` can be called many times, but the actual writes (calls to\n`setItems`) will be far fewer.\n\n\n### Reducing requests\n\nLet's suppose you have a chat app and as chat messages appear you want to fetch\nuser data for new message authors.\n\nWhen you initially load the chat, there might be a 100 messages with 50\nauthors.\n\nInstead of making a request for every incoming message that has an\nas yet unknown author, you can mergebounce the function and combine multiple\ncalls into a single request.\n\nFor example:\n\n```javascript\nasync function _fetchUserData(nicknames) {\n  const response = await fetch('/user/data', {\n    body: JSON.stringify({nicknames}),\n    headers: { 'Content-Type': 'application/json' },\n  });\n\n  data.forEach(userdata =\u003e getUser().update(userdata));\n}\n\nconst fetchUserData = mergebounce(_fetchUserData, 250, {'concatArrays': true});\n\n// The following calls with result in one request with all the nicnames\n// concatenated into one array\nfetchUserData(['coolguy69', 'ilikecats', 'dielan']);\nfetchUserData(['coolboymew']);\nfetchUserData(['donkeykong']);\n```\n\n## options\n\nThe default `debounce` options are allowed, as well as the following option:\n\n* `concatArrays`:\n    By default arrays will be treated as objects when being merged. When\n    merging two arrays, the values in the 2nd arrray will replace the\n    corresponding values (i.e. those with the same indexes) in the first array.\n    When `concatArrays` is set to `true`, arrays will be concatenated instead.\n* `dedupeArrays`:\n    This option is similar to `concatArrays`, except that the concatenated\n    array will also be deduplicated. Thus any entries that are concatenated to the\n    existing array, which are already contained in the existing array, will\n    first be removed.\n* `promise`:\n    By default, when calling a mergebounced function that doesn't execute\n    immediately, you'll receive the result from its previous execution, or\n    `undefined` if it has never executed before. By setting the `promise`\n    option to `true`, a promise will be returned instead of the previous\n    execution result when the function is debounced. The promise will resolve\n    with the result of the next execution, as soon as it happens.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconversejs%2Fmergebounce","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fconversejs%2Fmergebounce","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconversejs%2Fmergebounce/lists"}