{"id":15497493,"url":"https://github.com/bcomnes/nanocomponent-cache","last_synced_at":"2025-04-22T21:44:30.306Z","repository":{"id":57307864,"uuid":"115381401","full_name":"bcomnes/nanocomponent-cache","owner":"bcomnes","description":"Nanocomponent instance cache by key","archived":false,"fork":false,"pushed_at":"2018-02-13T18:01:04.000Z","size":31,"stargazers_count":4,"open_issues_count":0,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T23:07:14.942Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/nanocomponent-cache","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/bcomnes.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2017-12-26T03:19:51.000Z","updated_at":"2018-12-12T09:59:20.000Z","dependencies_parsed_at":"2022-09-09T01:22:39.563Z","dependency_job_id":null,"html_url":"https://github.com/bcomnes/nanocomponent-cache","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/bcomnes%2Fnanocomponent-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Fnanocomponent-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Fnanocomponent-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Fnanocomponent-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bcomnes","download_url":"https://codeload.github.com/bcomnes/nanocomponent-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250330446,"owners_count":21412982,"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-02T08:38:43.514Z","updated_at":"2025-04-22T21:44:30.284Z","avatar_url":"https://github.com/bcomnes.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NanocomponentCache [![stability][0]][1]\n[![npm version][2]][3] [![build status][4]][5]\n[![downloads][8]][9] [![js-standard-style][10]][11]\n\nCache a nanocomponent instance by key.  Creates a new instance if the key doesn't exist, otherwise returns the cached instance.  A subclass of [class-cache][cc] providing sane GC function defaults and a set of examples of intended usage.  Optional LRU caching.\n\n## Usage\n\n```js\nconst NanocomponentCache = require('nanocomponent-cache')\nconst compare = require('nanocomponent/compare')\nconst Nanocomponent = require('nanocomponent')\nconst Tweet = require('twitter-component')\nconst assert = require('assert')\nconst html = require('bel')\n\nclass TweetList extends Nanocomponent {\n  constructor () {\n    super()\n    this.tweetList = null\n    // auto-eject last used instances over 100 total cached\n    this.nc = new NanocomponentCache({ lru: 100 })\n    // Register the components you will be caching\n    this.nc.register(Tweet, {args: [{ placeholder: false }]} )\n  }\n\n  createElement (tweetList) {\n    assert(isArray(tweetList), 'tweetList must be an array of tweet URLs')\n    this.tweetList = tweetList // Cache a reference to tweetList\n    const nc = this.nc\n    return html`\n      \u003cdiv\u003e\n        ${tweetList.map(tweetURL =\u003e nc.get(tweetURL).render(tweetURL))}\n      \u003c/div\u003e\n    `\n  }\n\n  update (tweetList) {\n    assert(isArray(tweetList), 'tweetList must be an array of tweet URLs')\n    return compare(this.tweetList, tweetList)\n  }\n\n  afterupdate (el) {\n    // Periodically run the GC function to clean up unused instances.\n    this.nc.gc()\n  }\n}\n\nmodule.exports = TweetList\n```\n\n## Examples\n\n- [homogeneous](example/homogeneous/) ([🌎](https://nanocomponent-cache-homogeneous.netlify.com))\n- [heterogeneous](example/heterogeneous/) ([🌎](https://nanocomponent-cache-heterogeneous.netlify.com))\n- [hella-buttons](example/hella-buttons/) ([🌎](https://nanocomponent-cache-buttons.netlify.com))\n- [shared](example/shared/) ([🌎](https://nanocomponent-cache-shared.netlify.com))\n\n## Installation\n```sh\n$ npm install nanocomponent-cache\n```\n## API\n### `NanocomponentCache = require('nanocomponent-cache')`\nRequire `NanocomponentCache` class.\n\n### `c = new NanocomponentCache([opts])`\nCreate a new cache instance.\n\n`opts` include:\n\n```js\n{\n  gc: (component) =\u003e !component.element // a default garbage collection function\n  args: [] // Default args used for instantiating all classes,\n  lru: 0 // Enable LRU gc by setting this to an integer greater than 0\n}\n```\n\n### `c.register([typeKey = 'default'], SomeNanocomponent, [opts])`\n\nDefine a `Class` for the optional `typeKey`.  The default `typeKey` is `default`, which is used whenever a `typeKey` is omitted during `get`s and `set`s.  `opts` include: \n\n```js\n{\n  gc: undefined // a typeKey specific GC function.\n  args: undefined // default arguments instance arguments for `typeKey`. \n  // These options delegate to the top level options if left un-implemented\n}\n```\n\nThis is a shortcut for defining with a typeObject:\n\n```js\nc.register({\n  typeKey: { class: SomeNanocomponent, ...opts }\n})\n```\n\n### `c.register({ typeObject })`\n\nDefine class 'type's using a `typeObject` definition.  A typeObject is an object who's keys define the type name which are associated with a `Class` and optionally `args` and a type specific `gc` function.\n\n```js\nc.register({\n  default: SomeNanocomponent, // SomeNanocomponent with no args or gc.  Uses instance gc function.\n  baz: { class: SomeNanocomponent, ...opts }\n})\n```\n\nTypes are `Object.assign`ed over previously registered types.  The `opts` keys are the same as above.\n\n### `c.unregister(...types)`\n\nPass typeKeys as arguments to un-register them.  Instances are untouched during this process. \n\n### `c.get(key, [Class || typeKey], [opts])`\n\nThe primary method used to retrieve and create instances.  Return instance of `Class` or defined `type` class at `key`.  If an instance does not yet exist at `key`, it will be instantiated with `args` along with a `key` specific `gc` function.  If `type` is not defined, this method will throw.\n\nOmitting optional method arguments delegates to the next most specific option. \n\n```js\nc.get('some-key') // Return or create the 'default' Class\nc.get('some-key', {args: ['arg0', 'arg2']})\nc.get('some-key', null, {args: ['arg0', 'arg2']}) // Return the default registered class with specific args\nc.get('some-key', 'some-type', { args: ['arg0', 'arg2'] }) // Return the `some-type` class at `some-key`.\nc.get('some-key', SomeOtherNanocomponent, { args: ['arg0', 'arg2'], gc: instance =\u003e true })\n```\n\nIf `key` is already instantiated, `args` is ignored.  Pass changing properties as subsequent calls to the returned instance.  If `type` or `Class` changes, the `key` instance is re-instantiated.\n\n### `c.set(key, [Class || type], [opts])`\n\nForce instantiate the class instance at `key`.  Follows the same override behavior as `get`.  If you must change `args` on a key, this is the safest way to do that.\n\nReturns the newly created instance.\n\n### `c.gc()`\n\nRun the various `gc` functions defined.  For each key, only the most specific `gc` function set is run.  Return `true` from the `gc` functions to garbage collect that instance, and `false` to preserve.\n\nThis is used to clean out instances you no longer need.  Because this iterates over all keys with instances, run this often enough so that the key set doesn't grow too large but not too often to create unnecessary delays in render loops.\n\n### `c.clear()`\n\nClear all `key` instances.  The `gc` functions for each instance will be run receiving the following signature: `(instance, key, true) =\u003e {}`.  If your instance needs to let go of resources, watch for the second argument to equal true, indicating tht the instance will be deleted.  \n\n### `c.delete(key)`\n\nDelete specific `key` instance.  Will run the `gc` function passing `true` as the second argument (`(instance, key, true) =\u003e {}`).\n\n### `c.has(key)`\n\nReturn true if `key` exists. \n\nSee examples for more details.\n\n## Examples\n\nSee the `examples` folder for various ideas on how to use this library.\n\n## See Also\n\n- [nanocomponent][nc]\n- [choo][choo]\n- [choo component thread](https://github.com/choojs/choo/issues/593#issuecomment-364555843)\n- [class-cache](https://github.com/bcomnes/class-cache)\n\n## License\n[MIT](https://tldrlegal.com/license/mit-license)\n\n[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square\n[1]: https://nodejs.org/api/documentation.html#documentation_stability_index\n[2]: https://img.shields.io/npm/v/nanocomponent-cache.svg?style=flat-square\n[3]: https://npmjs.org/package/nanocomponent-cache\n[4]: https://img.shields.io/travis/bcomnes/nanocomponent-cache/master.svg?style=flat-square\n[5]: https://travis-ci.org/bcomnes/nanocomponent-cache\n[8]: http://img.shields.io/npm/dm/nanocomponent-cache.svg?style=flat-square\n[9]: https://npmjs.org/package/nanocomponent-cache\n[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square\n[11]: https://github.com/feross/standard\n[bel]: https://github.com/shama/bel\n[yoyoify]: https://github.com/shama/yo-yoify\n[md]: https://github.com/patrick-steele-idem/morphdom\n[210]: https://github.com/patrick-steele-idem/morphdom/pull/81\n[nm]: https://github.com/yoshuawuyts/nanomorph\n[ce]: https://github.com/yoshuawuyts/cache-element\n[class]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes\n[isSameNode]: https://github.com/choojs/nanomorph#caching-dom-elements\n[onload]: https://github.com/shama/on-load\n[choo]: https://github.com/choojs/choo\n[nca]: https://github.com/choojs/nanocomponent-adapters\n[nc]: https://github.com/choojs/nanocomponent\n[cc]: https://github.com/bcomnes/class-cache\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcomnes%2Fnanocomponent-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbcomnes%2Fnanocomponent-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcomnes%2Fnanocomponent-cache/lists"}