wxt-zustand
Version:
High-performance Zustand state management for WXT web extensions with seamless cross-tab synchronization and sub-10ms React re-renders
26 lines • 925 B
JavaScript
import { ChangeType } from '../types';
/**
* Applies a shallow patch to reconstruct state from a diff
* Uses efficient diffing algorithm for optimal state synchronization performance
*
* @param obj - The base state object to apply patches to
* @param difference - Array of changes to apply
* @returns New state object with patches applied
*/
export function shallowPatch(obj, difference) {
const newObj = Object.assign({}, obj);
difference.forEach(({ change, key, value }) => {
switch (change) {
case ChangeType.UPDATED:
newObj[key] = value;
break;
case ChangeType.REMOVED:
Reflect.deleteProperty(newObj, key);
break;
default:
console.warn(`Unknown change type ${change} for key ${key} (value: ${value})`);
}
});
return newObj;
}
//# sourceMappingURL=shallowPatch.js.map