From 7a3c0a9df57e3d5d8b5b3d37067358260e365f66 Mon Sep 17 00:00:00 2001 From: Peter Velkov Date: Tue, 8 Jun 2021 15:20:50 +0300 Subject: [PATCH 1/5] fix: update cache on storage events --- lib/Onyx.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/Onyx.js b/lib/Onyx.js index d6a2d13a..fadd81e4 100644 --- a/lib/Onyx.js +++ b/lib/Onyx.js @@ -731,7 +731,10 @@ function init({ initializeWithDefaultKeyStates(); // Update any key whose value changes in storage - registerStorageEventListener((key, newValue) => keyChanged(key, newValue)); + registerStorageEventListener((key, newValue) => { + cache.set(key, newValue); + keyChanged(key, newValue); + }); } const Onyx = { From 8b32b16bffeeef38baf68d2519ee7e08a17e1607 Mon Sep 17 00:00:00 2001 From: Peter Velkov Date: Tue, 8 Jun 2021 15:21:46 +0300 Subject: [PATCH 2/5] docs: remove todo comments --- tests/unit/withOnyxTest.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/unit/withOnyxTest.js b/tests/unit/withOnyxTest.js index 79d1351e..6053bac4 100644 --- a/tests/unit/withOnyxTest.js +++ b/tests/unit/withOnyxTest.js @@ -43,7 +43,6 @@ describe('withOnyx', () => { }); it('should update withOnyx subscriber multiple times when merge is used', () => { - // Todo: ViewWithCollections does not expect a `text` prop const TestComponentWithOnyx = withOnyx({ text: { key: ONYX_KEYS.COLLECTION.TEST_KEY, @@ -62,7 +61,6 @@ describe('withOnyx', () => { }); it('should update withOnyx subscriber just once when mergeCollection is used', () => { - // Todo: ViewWithCollections does not expect a `text` prop const TestComponentWithOnyx = withOnyx({ text: { key: ONYX_KEYS.COLLECTION.TEST_KEY, @@ -79,7 +77,6 @@ describe('withOnyx', () => { }); it('should update withOnyx subscribing to individual key if mergeCollection is used', () => { - // Todo: ViewWithCollections does not expect a `text` prop const collectionItemID = 1; const TestComponentWithOnyx = withOnyx({ text: { @@ -97,7 +94,6 @@ describe('withOnyx', () => { }); it('should update withOnyx subscribing to individual key with merged value if mergeCollection is used', () => { - // Todo: ViewWithCollections does not expect a `text` prop const collectionItemID = 4; const TestComponentWithOnyx = withOnyx({ text: { From 05779f731d50b298a5bed4ac2c2bd49fde7bb783 Mon Sep 17 00:00:00 2001 From: Peter Velkov Date: Tue, 8 Jun 2021 15:22:58 +0300 Subject: [PATCH 3/5] docs: fix typos --- lib/OnyxCache.js | 2 +- lib/decorateWithMetrics.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/OnyxCache.js b/lib/OnyxCache.js index 841f6bc9..b70deb34 100644 --- a/lib/OnyxCache.js +++ b/lib/OnyxCache.js @@ -98,7 +98,7 @@ class OnyxCache { } /** - * Deep merge data to cache, any non existing keys will be crated + * Deep merge data to cache, any non existing keys will be created * @param {Record} data - a map of (cache) key - values */ merge(data) { diff --git a/lib/decorateWithMetrics.js b/lib/decorateWithMetrics.js index e9b7a852..26f2c12d 100644 --- a/lib/decorateWithMetrics.js +++ b/lib/decorateWithMetrics.js @@ -26,7 +26,7 @@ function decorateWithMetrics(func, alias = func.name) { /* * Then handlers added here are not affecting the original promise - * They crate a separate chain that's not exposed (returned) to the original caller + * They create a separate chain that's not exposed (returned) to the original caller * */ originalPromise .finally(() => { From 280d066ef54f6236da5faf62d9a78e45d62aa844 Mon Sep 17 00:00:00 2001 From: Peter Velkov Date: Tue, 8 Jun 2021 15:32:52 +0300 Subject: [PATCH 4/5] test: update `AsyncStorageMock` require calls --- tests/unit/onyxCacheTest.js | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/tests/unit/onyxCacheTest.js b/tests/unit/onyxCacheTest.js index de5f0a35..cf9d7d65 100644 --- a/tests/unit/onyxCacheTest.js +++ b/tests/unit/onyxCacheTest.js @@ -377,6 +377,7 @@ describe('Onyx', () => { describe('Onyx with Cache', () => { let Onyx; let withOnyx; + let AsyncStorageMock; /** @type OnyxCache */ let cache; @@ -393,6 +394,7 @@ describe('Onyx', () => { const OnyxModule = require('../../index'); Onyx = OnyxModule.default; withOnyx = OnyxModule.withOnyx; + AsyncStorageMock = require('@react-native-community/async-storage').default; cache = require('../../lib/OnyxCache').default; Onyx.init({ @@ -413,8 +415,6 @@ describe('Onyx', () => { }); it('Expect a single call to getItem when multiple components use the same key', () => { - const AsyncStorageMock = require('@react-native-community/async-storage/jest/async-storage-mock'); - // GIVEN a component connected to Onyx const TestComponentWithOnyx = withOnyx({ text: { @@ -444,8 +444,6 @@ describe('Onyx', () => { }); it('Expect a single call to getAllKeys when multiple components use the same key', () => { - const AsyncStorageMock = require('@react-native-community/async-storage/jest/async-storage-mock'); - // GIVEN a component connected to Onyx const TestComponentWithOnyx = withOnyx({ text: { @@ -476,8 +474,6 @@ describe('Onyx', () => { }); it('Expect multiple calls to getItem when no existing component is using a key', () => { - const AsyncStorageMock = require('@react-native-community/async-storage/jest/async-storage-mock'); - // GIVEN a component connected to Onyx const TestComponentWithOnyx = withOnyx({ text: { @@ -509,8 +505,6 @@ describe('Onyx', () => { }); it('Expect multiple calls to getItem when multiple keys are used', () => { - const AsyncStorageMock = require('@react-native-community/async-storage/jest/async-storage-mock'); - // GIVEN two component const TestComponentWithOnyx = withOnyx({ testObject: { @@ -550,8 +544,6 @@ describe('Onyx', () => { }); it('Expect a single call to getItem when at least one component is still subscribed to a key', () => { - const AsyncStorageMock = require('@react-native-community/async-storage/jest/async-storage-mock'); - // GIVEN a component connected to Onyx const TestComponentWithOnyx = withOnyx({ text: { @@ -589,8 +581,6 @@ describe('Onyx', () => { }); it('Should remove collection items from cache when collection is disconnected', () => { - const AsyncStorageMock = require('@react-native-community/async-storage/jest/async-storage-mock'); - // GIVEN a component subscribing to a collection const TestComponentWithOnyx = withOnyx({ collections: { @@ -638,8 +628,6 @@ describe('Onyx', () => { }); it('Should not remove item from cache when it still used in a collection', () => { - const AsyncStorageMock = require('@react-native-community/async-storage/jest/async-storage-mock'); - // GIVEN component that uses a collection and a component that uses a collection item const COLLECTION_ITEM_KEY = `${ONYX_KEYS.COLLECTION.MOCK_COLLECTION}10`; const TestComponentWithOnyx = withOnyx({ From 5d838438260449f923e89ee5827b3d8b006e5b4d Mon Sep 17 00:00:00 2001 From: Peter Velkov Date: Tue, 8 Jun 2021 18:01:06 +0300 Subject: [PATCH 5/5] fix: OnyxCache.merge does not update storage keys --- lib/OnyxCache.js | 4 ++++ tests/unit/onyxCacheTest.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/OnyxCache.js b/lib/OnyxCache.js index b70deb34..b2d521a1 100644 --- a/lib/OnyxCache.js +++ b/lib/OnyxCache.js @@ -103,6 +103,10 @@ class OnyxCache { */ merge(data) { this.storageMap = lodashMerge({}, this.storageMap, data); + + const storageKeys = this.getAllKeys(); + const mergedKeys = _.keys(data); + this.storageKeys = new Set([...storageKeys, ...mergedKeys]); } /** diff --git a/tests/unit/onyxCacheTest.js b/tests/unit/onyxCacheTest.js index cf9d7d65..f188ebc5 100644 --- a/tests/unit/onyxCacheTest.js +++ b/tests/unit/onyxCacheTest.js @@ -318,6 +318,22 @@ describe('Onyx', () => { expect(cache.hasCacheForKey('mockKey')).toBe(true); expect(cache.getValue('mockKey')).toEqual({ID: 5}); }); + + it('Should update storageKeys when new keys are created', () => { + // GIVEN cache with some items + cache.set('mockKey', {value: 'mockValue'}); + cache.set('mockKey2', {other: 'otherMockValue', mock: 'mock', items: [3, 4, 5]}); + + // WHEN merge is called with existing key value pairs + cache.merge({ + mockKey: {mockItems: []}, + mockKey3: {ID: 3}, + mockKey4: {ID: 4}, + }); + + // THEN getAllStorage keys should return updated storage keys + expect(cache.getAllKeys()).toEqual(['mockKey', 'mockKey2', 'mockKey3', 'mockKey4']); + }); }); describe('hasPendingTask', () => {