diff --git a/apps/frontend/src/renderer/stores/utils.ts b/apps/frontend/src/renderer/stores/utils.ts new file mode 100644 index 00000000..22b94307 --- /dev/null +++ b/apps/frontend/src/renderer/stores/utils.ts @@ -0,0 +1,36 @@ +/** + * Shallow comparison function for Zustand selectors. + * Returns true if a and b are shallowly equal (same reference or same primitives). + */ +export function shallow(objA: T, objB: T): boolean { + if (Object.is(objA, objB)) { + return true; + } + + if ( + typeof objA !== 'object' || + objA === null || + typeof objB !== 'object' || + objB === null + ) { + return false; + } + + const keysA = Object.keys(objA as unknown as object); + const keysB = Object.keys(objB as unknown as object); + + if (keysA.length !== keysB.length) { + return false; + } + + for (const key of keysA) { + if ( + !Object.prototype.hasOwnProperty.call(objB, key) || + !Object.is((objA as any)[key], (objB as any)[key]) + ) { + return false; + } + } + + return true; +}