Skip to content

Commit

Permalink
Merge pull request #99 from Expensify/marcaaron-rollBackWritePrevention
Browse files Browse the repository at this point in the history
Rollback changes causing issues for initWithStoredValues
  • Loading branch information
AndrewGable authored Aug 17, 2021
2 parents 2908a47 + 8a26c92 commit 8ec2073
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions lib/Onyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,8 @@ function keysChanged(collectionKey, collection) {
*
* @param {string} key
* @param {mixed} data
* @param {boolean} [hasNewValue=true]
*/
function keyChanged(key, data, hasNewValue = true) {
function keyChanged(key, data) {
// Add or remove this key from the recentlyAccessedKeys lists
if (!_.isNull(data)) {
addLastAccessedKey(key);
Expand All @@ -283,11 +282,6 @@ function keyChanged(key, data, hasNewValue = true) {
// Find all subscribers that were added with connect() and trigger the callback or setState() with the new data
_.each(callbackToStateMapping, (subscriber) => {
if (subscriber && isKeyMatch(subscriber.key, key)) {
// If data is not new then only trigger the callback or setState() for subscribers
// which are not initialized with Stored value
if (!hasNewValue && (subscriber.initWithStoredValues !== false)) {
return;
}
if (_.isFunction(subscriber.callback)) {
subscriber.callback(data, key);
}
Expand Down Expand Up @@ -479,19 +473,11 @@ function evictStorageAndRetry(error, ionMethod, ...args) {
* @returns {Promise}
*/
function set(key, val) {
const shouldCacheNewValue = !cache.hasCacheForKey(key) || !_.isEqual(val, cache.getValue(key));

// Adds the key to cache when it's not available
if (shouldCacheNewValue) {
cache.set(key, val);
}
cache.set(key, val);

// Optimistically inform subscribers on the next tick
Promise.resolve().then(() => keyChanged(key, val, shouldCacheNewValue));

if (!shouldCacheNewValue) {
return Promise.resolve();
}
Promise.resolve().then(() => keyChanged(key, val));

// Write the thing to persistent storage, which will trigger a storage event for any other tabs open on this domain
return AsyncStorage.setItem(key, JSON.stringify(val))
Expand Down

0 comments on commit 8ec2073

Please sign in to comment.