react-native-onyx
Version:
State management for React Native
32 lines (31 loc) • 1.81 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.
OnyxUtils_1.default.broadcastUpdate(key, mergedValue, hasChanged);
const shouldSkipStorageOperations = !hasChanged || OnyxUtils_1.default.isRamOnlyKey(key);
// If the value has not changed, calling Storage.setItem() would be redundant and a waste of performance, so return early instead.
// If the key is marked as RAM-only, it should not be saved nor updated in the storage.
if (shouldSkipStorageOperations) {
return Promise.resolve({ mergedValue });
}
// 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,
}));
};
const OnyxMerge = {
applyMerge,
};
exports.default = OnyxMerge;