react-native-onyx
Version:
State management for React Native
31 lines (30 loc) • 1.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const OnyxCache_1 = __importDefault(require("../OnyxCache"));
const OnyxUtils_1 = __importDefault(require("../OnyxUtils"));
const storage_1 = __importDefault(require("../storage"));
const applyMerge = (key, existingValue, validChanges) => {
const { result: mergedValue } = OnyxUtils_1.default.mergeChanges(validChanges, existingValue);
// In cache, we don't want to remove the key if it's null to improve performance and speed up the next merge.
const hasChanged = OnyxCache_1.default.hasValueChanged(key, mergedValue);
// Logging properties only since values could be sensitive things we don't want to log.
OnyxUtils_1.default.logKeyChanged(OnyxUtils_1.default.METHOD.MERGE, key, mergedValue, hasChanged);
// This approach prioritizes fast UI changes without waiting for data to be stored in device storage.
const updatePromise = OnyxUtils_1.default.broadcastUpdate(key, mergedValue, hasChanged);
// If the value has not changed, calling Storage.setItem() would be redundant and a waste of performance, so return early instead.
if (!hasChanged) {
return Promise.resolve({ mergedValue, updatePromise });
}
// For web platforms we use `setItem` since the object was already merged with its changes before.
return storage_1.default.setItem(key, mergedValue).then(() => ({
mergedValue,
updatePromise,
}));
};
const OnyxMerge = {
applyMerge,
};
exports.default = OnyxMerge;