An open API service indexing awesome lists of open source software.

https://github.com/choojs/nanocache

Cache Nanocomponents.
https://github.com/choojs/nanocache

Last synced: 12 months ago
JSON representation

Cache Nanocomponents.

Awesome Lists containing this project

README

          

# nanocache
[![npm version][2]][3] [![build status][4]][5]
[![downloads][8]][9] [![js-standard-style][10]][11]

Cache Nanocomponents.

## Usage
### my-component.js
```js
var Component = require('choo/component')
var html = require('choo/html')

module.exports = class Article extends Component {
static id (article) {
return `article-${article.id}`
}

createElement (article) {
return html`

${article.title}


${article.body}



`
}

update () {
return false
}
}
```

### example.js
```js
var myComponent = require('./my-component')
var Nanocache = require('nanocache')

var cache = new Nanocache()
cache(myComponent)
// => create a new instance of myComponent

cache(myComponent)
// => return cached instance of myComponent
```

## API
### `cache = Nanocache()`
Create a new Nanocache instance.

### `cache.render(Nanocomponent)`
Render a Nanocomponent instance. It checks a static `id` method that
returns an id. If the id is not registered in the cache, it creates a new
instance and caches it. If the id already exists, it returns the cached
instance.

### `cache.prune()`
Remove all components from the cache that don't currently have a DOM node
attached.

## Installation
```sh
$ npm install @choojs/nanocache
```

## See Also
- [choojs/nanocomponent](https://github.com/choojs/nanocomponent)

## License
[Apache-2.0](./LICENSE)

[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square
[1]: https://nodejs.org/api/documentation.html#documentation_stability_index
[2]: https://img.shields.io/npm/v/@choojs/nanocache.svg?style=flat-square
[3]: https://npmjs.org/package/@choojs/nanocache
[4]: https://img.shields.io/travis/yoshuawuyts/nanocache/master.svg?style=flat-square
[5]: https://travis-ci.org/yoshuawuyts/nanocache
[6]: https://img.shields.io/codecov/c/github/yoshuawuyts/nanocache/master.svg?style=flat-square
[7]: https://codecov.io/github/yoshuawuyts/nanocache
[8]: http://img.shields.io/npm/dm/nanocache.svg?style=flat-square
[9]: https://npmjs.org/package/nanocache
[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[11]: https://github.com/feross/standard