You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var react = require('react');
function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
var getCircularReplacer = function getCircularReplacer() {
var seen = new WeakSet();
return function (key, value) {
if (_typeof(value) === 'object' && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
/**
* 一个更加安全的 stringify可以解决循环依赖的问题
* @param value
*/
var stringify = function stringify(value) {
return JSON.stringify(value, getCircularReplacer());
};
var jsonCompareEquals = function jsonCompareEquals(value, nextValue) {
try {
return stringify(value) === stringify(nextValue);
} catch (error) {// do something
}
return false;
};
function useJsonCompareMemoize(value) {
var ref = react.useRef(''); // it can be done by using useMemo as well
// but useRef is rather cleaner and easier
if (!jsonCompareEquals(value, ref.current)) {
ref.current = JSON.stringify(value, getCircularReplacer());
}
return ref.current;
}
function useDeepJSONEffect(effect, dependencies) {
react.useEffect(effect, [useJsonCompareMemoize(dependencies)]);
}
exports.default = useDeepJSONEffect;
exports.stringify = stringify;