npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cache-component

v5.2.0

Published

Cache bel components

Downloads

30

Readme

cache-component stability

npm version build status downloads js-standard-style

Cached bel components. Makes rendering elements very fast™. Analogous to React's .shouldComponentUpdate() method, but only using native DOM methods.

Runs a _render function whenever arguments changed according to an _update function. If the _update function determines an update is needed, a newly _rendered bel element is calculated and then nanomorphed onto the children nodes of the last render. After the first render, a _proxy element is always returned. When the component is removed from the live DOM tree, all internal proxy and element references are deleted.

Features

  • makes rendering elements very fast™
  • implemented in only a few lines
  • only uses native DOM methods
  • Class based components, which offer a great place to store methods for re-use.

Usage

// Implementer API
var CacheComponent = require('cache-component')
var html = require('bel')

function CachedButton () {
  if (!(this instanceof CachedButton)) return new CachedButton()
  this._color = null
  CacheComponent.call(this)
}
CachedButton.prototype = Object.create(CacheComponent.prototype)

CachedButton.prototype._render = function (color) {
  this._color = color
  return html`
    <button style="background-color: ${color}">
      Click Me
    </button>
  `
}

// Override default shallow compare _update function
CachedButton.prototype._update = function (newColor) {
  return newColor !== this._color
}

var element = CachedButton()

let el = element.render('red') // creates new element
let el = element.render('red') // returns cached element (proxy)
let el = element.render('blue') // returns cached element (proxy) and mutates children
// Consumer API
var CachedButton = require('./cached-button.js')
var cachedButton = CachedButton()
document.body.appendChild(cachedButton.render('green'))

API

CacheComponent.prototype()

Inheritable CachedComponent prototype. Should be inherited from using CacheComponent.call(this) and prototype = Object.create(CacheComponent.prototype).

Internal properties are:

  • this._proxy: proxy element that's returned on subsequent render() calls that don't pass the ._update() check.
  • this._element: rendered element that should be returned from the ._render() call. This is a DOM pointer to the DOM node in the live DOM tree that you actually see and interact with.
  • this._hasWindow: boolean if window exists. Can be used to create elements that render both in the browser and in Node.
  • this._args: a reference to the arguments array that was used during the last _render() call.

CacheComponent.prototype._render([arguments])

Must be implemented. Render an HTML node with arguments. The Node that's returned is cached as this._element. Only called on first render and whenever you return true from prototype._update(). You must return a DOM node from this function on every call.

CacheComponent.prototype._update([arguments])

Return a boolean to determine if prototype._render() should be called. Not called on the first render. Defaults to the following shallow compare function:

CacheElement.prototype._update = function () {
  var length = arguments.length
  if (length !== this._args.length) return true

  for (var i = 0; i < length; i++) {
    if (arguments[i] !== this._args[i]) return true
  }
  return false
}

CacheComponent.prototype._willMount(el)

Called before returning a fully rendered dom node that is presumably inserted into the document. This is called on first render, and once every subsequent render after the element is found to have been removed from the dom. It gets passed a reference el to the dom node that will be returned.

CacheComponent.prototype._didMount(el)

This function is called after the fully rendered dom node is returned and receives a reference el to that dom node. In practice, this hooks indicates the dom node has been mounted and can be interacted with to set scroll position and other attributes.

CacheComponent.prototype._willUpdate(el)

Called before the component will update. _willUpdate gets a el reference so that you can modify the element that will be use to internally morph the mounted dom node.

CacheComponent.prototype._didUpdate()

Called after a mounted component updates. You can use this hook to call scroll to or other dom methods on the mounted component. You can access this._element to reference the root node mounted in the page. This hook does not get a el argument as this node is tossed away at this stage.

Installation

$ npm install cache-component

FAQ

Where does this run?

Make sure you're running a diffing engine that checks for .isSameNode(), if it doesn't you'll end up with super weird results because proxy nodes will probably be rendered which is not what should happen. Probably make sure you're using morphdom or nanomorph. Seriously.

What's a proxy node?

It's a node that overloads Node.isSameNode() to compare it to another node. This is needed because a given DOM node can only exist in one DOM tree at the time, so we need a way to reference mounted nodes in the tree without actually using them. Hence the proxy pattern, and the recently added support for it in certain diffing engines:

var html = require('bel')

var el1 = html`<div>pink is the best</div>`
var el2 = html`<div>blue is the best</div>`

// let's proxy el1
var proxy = html`<div></div>`
proxy.isSameNode = function (targetNode) {
  return (targetNode === el1)
}

el1.isSameNode(el1)   // true
el1.isSameNode(el2)   // false
proxy.isSameNode(el1) // true
proxy.isSameNode(el2) // false

How does it work?

Morphdom is a diffing engine that diffs real DOM trees. It runs a series of checks between nodes to see if they should either be replaced, removed, updated or reordered. This is done using a series of property checks on the nodes.

Since v2.1.0 morphdom also runs Node.isSameNode(otherNode). This allows us to override the function and replace it with a custom function that proxies an existing node. Check out the code to see how it works. The result is that if every element in our tree uses cache-component, only elements that have changed will be recomputed and rerendered making things very fast.

nanomorph, which saw first use in choo 5, has supported isSameNode since it's conception.

What's the exact difference between cache-component and nanocomponent?

  • cache-component is very similar to nanocomponent, except it handles morphing for you if you want by re-running the _render function. It works similar to react's component class. Additionally, it retains the class interface so you can store your event handlers on the prototype chain and on the class instance. Once the component is rendered + mounted in the DOM for the first time, cache-component always returns a proxy node.
  • nanocomponent will render a new node initially and always return a proxy node on subsequent calls to prototype.render. This means the component is responsible for mutating any internal changes.

Whats the relationship beteen cache-component and cache-element?

This module was essentially a merge of cache-element v2.0.1 with the API of nanomorph before cache-element switched over to using nanomorph and essentially had a different purpose. There are still ongoing discussions on the future of cache-element. The idea behind the inheritance API is that it provides a handy place to store event handler functions so they don't get redeclared between render frames like inline functions do.

See Also

Similar Packages

License

MIT