{"id":23773435,"url":"https://github.com/beenotung/cache-compute","last_synced_at":"2025-10-05T00:24:47.137Z","repository":{"id":57192413,"uuid":"430486715","full_name":"beenotung/cache-compute","owner":"beenotung","description":"Memorize and re-run a function when its arguments are changed","archived":false,"fork":false,"pushed_at":"2021-12-22T19:35:30.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-02T20:05:43.500Z","etag":null,"topics":["arguments","cache","callback","memorize","options","side-effect","side-effects"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/beenotung.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":"2021-11-21T21:40:15.000Z","updated_at":"2021-12-22T19:35:33.000Z","dependencies_parsed_at":"2022-08-24T03:10:29.273Z","dependency_job_id":null,"html_url":"https://github.com/beenotung/cache-compute","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fcache-compute","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fcache-compute/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fcache-compute/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fcache-compute/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beenotung","download_url":"https://codeload.github.com/beenotung/cache-compute/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239960812,"owners_count":19725398,"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":["arguments","cache","callback","memorize","options","side-effect","side-effects"],"created_at":"2025-01-01T05:40:01.734Z","updated_at":"2025-10-05T00:24:42.098Z","avatar_url":"https://github.com/beenotung.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cache-compute\n\nMemorize and re-run a function when its arguments are changed.\nThe output is passed to callback function upon each update.\n\n[![npm Package Version](https://img.shields.io/npm/v/cache-compute.svg?maxAge=3600)](https://www.npmjs.com/package/cache-compute.ts)\n\n## Main Functions\n\n- Memorize variadic function by arguments (compare each argument)\n- Memorize unary function by options object (compare each key-value pairs)\n\n## Features\n\n- Built-in Typescript support\n- 100% tested with ts-mocha\n- No dependencies\\*\n\n\\*: except tslib to support await for pre-es2017 runtime\n\n## Usage Example\n\n### Using compute cache pool\n\n\u003cdetails\u003e\n\u003csummary\u003eTypescript Example\u003c/summary\u003e\n\n```typescript\nimport {\n  newComputeByArgumentsCachePool,\n  newComputeByOptionsCachePool,\n  Callback,\n} from 'cache-compute'\n\ntype State = { acc: number }\n\nlet sum2 = (a: number, b: number): number =\u003e {\n  console.log('perform some expensive computation...:', [a, b])\n  return a + b\n}\nlet sum2Cache = newComputeByArgumentsCachePool(sum2)\n\ntype SumOptions = { a: number; b: number }\nlet sumDict = ({ a, b }: SumOptions): number =\u003e {\n  console.log('perform some expensive computation...:', { a, b })\n  return a + b\n}\nlet sumDictCache = newComputeByOptionsCachePool(sumDict)\n\nexport let selector_dict = {\n  getBigger2(\n    state: State,\n    options: { delta: number },\n    callback: Callback\u003cnumber\u003e,\n  ) {\n    let compute = sum2Cache(callback)\n    compute(state.acc, options.delta)\n  },\n  getBiggerDict(\n    state: State,\n    options: { delta: number },\n    callback: Callback\u003cnumber\u003e,\n  ) {\n    let compute = sumDictCache(callback)\n    let a = state.acc\n    let b = options.delta\n    compute({ a, b })\n  },\n}\n```\n\n\u003c/details\u003e\n\nMore example refers to [demo-selector.ts](./example/demo-selector.ts) and [cache-compute.spec.ts](./test/cache-compute.spec.ts)\n\n### Using compute cache directly\n\n\u003cdetails\u003e\n\u003csummary\u003eTypescript Example\u003c/summary\u003e\n\n```typescript\nimport { makeSyncComputeCacheByArguments } from 'cache-compute'\n\nlet users = [\n  { name: 'Alice', birthday: new Date('2000-01-01').getTime() },\n  { name: 'Bob', birthday: new Date('2000-02-02').getTime() },\n]\n\nconst YEAR = 1000 * 60 * 60 * 24 * 365.25\nfunction toProfile(user) {\n  const now = Date.now()\n  return {\n    name: user.name,\n    age: Math.floor((now - user.birthday) / YEAR),\n  }\n}\n\nfunction updateUser(index, user) {\n  users = [...users]\n  users[index] = { ...users[index], ...user }\n  runSelectors()\n}\n\nlet selectors = []\nfunction runSelectors() {\n  selectors.forEach(fn =\u003e fn())\n}\n\nfunction watchProfile(index) {\n  const watchUsers = profile =\u003e\n    // e.g. push to websocket client\n    console.log({ index, profile })\n  const checkUsers = makeSyncComputeCacheByArguments(toProfile, watchUsers)\n  const selector = () =\u003e checkUsers(users[index])\n  selector()\n  selectors.push(selector)\n}\n\nconsole.log('== init ==')\nwatchProfile(0)\nwatchProfile(1)\n// will print both user profiles\n\nconsole.log('== update alice ==')\nupdateUser(0, { name: 'Alex' })\n// only print first user profile\n\nconsole.log('== update bob ==')\nupdateUser(1, { name: 'Bobby' })\n// only print second user profile\n```\n\n\u003c/details\u003e\n\nMore example refers to [demo.ts](./example/demo.ts) and [cache-compute.spec.ts](./test/cache-compute.spec.ts)\n\n## How it works\n\nThe input (a.k.a. dependencies) of a function is memorized.\n\nWhen the checker function is called, it compares the current input against that last input.\n\nIf any of the input is changed, the provided function will be re-run, and the return value will be passed to the callback function.\n\n## Remark\n\nError handling is intentionally not handled by the checker function.\nTo handle errors properly, either do application-specific try-catch in the compute function or when calling the checker function.\n\n## License\n\nThis project is licensed with [BSD-2-Clause](./LICENSE)\n\nThis is free, libre, and open-source software. It comes down to four essential freedoms [[ref]](https://seirdy.one/2021/01/27/whatsapp-and-the-domestication-of-users.html#fnref:2):\n\n- The freedom to run the program as you wish, for any purpose\n- The freedom to study how the program works, and change it so it does your computing as you wish\n- The freedom to redistribute copies so you can help others\n- The freedom to distribute copies of your modified versions to others\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeenotung%2Fcache-compute","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeenotung%2Fcache-compute","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeenotung%2Fcache-compute/lists"}