Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Onyx.cleanCache #81

Merged
merged 2 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure why we need to do this. 🤔


// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

});
})
.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