Skip to content

Commit

Permalink
Merge pull request #81 from kidroca/kidroca/onyx-cleanCache-fix
Browse files Browse the repository at this point in the history
Fix: `Onyx.cleanCache`
  • Loading branch information
marcaaron authored Jun 9, 2021
2 parents 9b03d95 + b0cf26c commit 52c254f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
12 changes: 6 additions & 6 deletions lib/Onyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ function cleanCache(key) {

// When this is a collection - also recursively remove any unused individual items
if (isCollectionKey(key)) {
cache.remove(key);
cache.drop(key);

getAllKeys().then(cachedKeys => _.chain(cachedKeys)
.filter(name => name.startsWith(key))
Expand All @@ -436,8 +436,8 @@ function cleanCache(key) {
}
}

// Otherwise remove the item from cache
cache.remove(key);
// Otherwise remove the value from cache
cache.drop(key);
}

/**
Expand Down Expand Up @@ -471,8 +471,8 @@ function disconnect(connectionID, keyToRemoveFromEvictionBlocklist) {
* @return {Promise}
*/
function remove(key) {
// Remove from cache
cache.remove(key);
// Cache the fact that the value was removed
cache.set(key, null);

// Optimistically inform subscribers
keyChanged(key, null);
Expand Down Expand Up @@ -639,7 +639,7 @@ function clear() {
.then((keys) => {
_.each(keys, (key) => {
keyChanged(key, null);
cache.remove(key);
cache.set(key, null);
});
})
.then(AsyncStorage.clear)
Expand Down
7 changes: 3 additions & 4 deletions lib/OnyxCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class OnyxCache {
// bind all methods to prevent problems with `this`
_.bindAll(
this,
'getAllKeys', 'getValue', 'hasCacheForKey', 'addKey', 'set', 'remove', 'merge',
'getAllKeys', 'getValue', 'hasCacheForKey', 'addKey', 'set', 'drop', 'merge',
'hasPendingTask', 'getTaskPromise', 'captureTask',
);
}
Expand Down Expand Up @@ -89,11 +89,10 @@ class OnyxCache {
}

/**
* Remove data from cache
* Forget the cached value for the given key
* @param {string} key
*/
remove(key) {
this.storageKeys.delete(key);
drop(key) {
delete this.storageMap[key];
}

Expand Down
12 changes: 6 additions & 6 deletions tests/unit/onyxCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,19 @@ describe('Onyx', () => {
});
});

describe('remove', () => {
it('Should remove the key from all keys', () => {
describe('drop', () => {
it('Should NOT remove the key from all keys', () => {
// GIVEN cache with some items
cache.set('mockKey', 'mockValue');
cache.set('mockKey2', 'mockValue');
cache.set('mockKey3', 'mockValue');

// WHEN an key is removed
cache.remove('mockKey2');
cache.drop('mockKey2');

// THEN getAllKeys should not include the removed value
// THEN getAllKeys should still include the key
const allKeys = cache.getAllKeys();
expect(allKeys).toEqual(['mockKey', 'mockKey3']);
expect(allKeys).toEqual(expect.arrayContaining(['mockKey2']));
});

it('Should remove the key from cache', () => {
Expand All @@ -193,7 +193,7 @@ describe('Onyx', () => {
cache.set('mockKey2', 'mockValue3');

// WHEN a key is removed
cache.remove('mockKey');
cache.drop('mockKey');

// THEN a value should not be available in cache
expect(cache.hasCacheForKey('mockKey')).toBe(false);
Expand Down

0 comments on commit 52c254f

Please sign in to comment.