hexo-theme-amane/layout/util/cache.jsx

45 lines
1.6 KiB
React
Raw Normal View History

2019-12-22 17:16:19 +00:00
'use strict';
const crypto = require('crypto');
2019-12-22 17:16:19 +00:00
const { Component } = require('inferno'); // eslint-disable-line no-unused-vars
const { createElement } = require('inferno-create-element');
const cache = {};
function computeHash(props) {
2019-12-22 17:16:19 +00:00
return crypto.createHash('md5').update(JSON.stringify(props)).digest('hex');
}
module.exports = {
2019-12-22 17:16:19 +00:00
/**
* Create cached component from a given component class.
* The cache ID is caculated from the input props.
2019-12-22 17:16:19 +00:00
*
* @param {Component} type JSX component class
* @param {string} prefix Cache ID prefix
2019-12-22 17:16:19 +00:00
* @param {Function} transform Transform the input props to target props and
* its result is used to compute cache ID
* @returns Cached JSX element if cache ID is found.
* Return null if the props transform result is empty, which means the props
* passed to the createElement() indicates the element does not need to be created.
* Otherwise, create a new element and cache it if the transform function is provided.
*/
cacheComponent(type, prefix, transform) {
2019-12-22 17:16:19 +00:00
return props => {
const targetProps = transform(props);
if (!targetProps) {
return null;
}
const cacheId = prefix + '-' + computeHash(targetProps);
if (!cacheId) {
return createElement(type, targetProps);
}
if (!cache[cacheId]) {
cache[cacheId] = createElement(type, targetProps);
}
return cache[cacheId];
};
}
};