@exodus/atoms
Advanced tools
Comparing version 5.7.1 to 5.7.2
{ | ||
"name": "@exodus/atoms", | ||
"version": "5.7.1", | ||
"version": "5.7.2", | ||
"main": "src/index.js", | ||
@@ -13,3 +13,4 @@ "description": "Abstraction for encapsulating a piece of data behind a simple unified interface: get, set, observe", | ||
"files": [ | ||
"src" | ||
"src", | ||
"CHANGELOG.md" | ||
], | ||
@@ -40,3 +41,3 @@ "repository": { | ||
}, | ||
"gitHead": "ac180dfc636ccb393938aefa88e3824471646f8b" | ||
"gitHead": "cb5b1ba99a8fb8fe272f76ff61eb94490006c8f7" | ||
} |
import assert from 'minimalistic-assert' | ||
import { isEqual as deepEqual } from 'lodash' | ||
const STRINGIFY_LIMIT = 1000 | ||
/** | ||
* Serialize an object to a JSON string with a limit on the number of properties shown | ||
* @param {Object} obj - The object to be serialized | ||
* @param {number} limit - The maximum number of properties to include in the output | ||
* @returns {string} - The JSON string representation of the object | ||
*/ | ||
const limitedStringify = (obj, limit = STRINGIFY_LIMIT) => { | ||
let count = 0 | ||
return JSON.stringify(obj, (_, value) => { | ||
if (count > limit) { | ||
return | ||
} | ||
if (count === limit) { | ||
count++ | ||
return '...more properties not shown' | ||
} | ||
count++ | ||
return value | ||
}) | ||
} | ||
const warnOnSameValueSet = ({ atom, isEqual = deepEqual, logger }) => { | ||
@@ -11,4 +37,5 @@ assert(atom, 'expected "atom') | ||
if (value !== undefined && isEqual(value, previousValue)) { | ||
// limit value shown to avoid a CPU spike that can lead to Android freezing | ||
logger.warn( | ||
`Atom was called with the same value it currently holds: ${JSON.stringify(value)}` | ||
`Atom was called with the same value it currently holds: ${limitedStringify(value)}` | ||
) | ||
@@ -15,0 +42,0 @@ } |
43001
26
716