Skip to content

Commit

Permalink
rename OnyxCache.get
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispader committed May 27, 2024
1 parent 9407b6c commit d574d6d
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 60 deletions.
10 changes: 5 additions & 5 deletions lib/Onyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function init({

if (shouldSyncMultipleInstances) {
Storage.keepInstancesSync?.((key, value) => {
const prevValue = cache.getValue(key, false) as OnyxValue<typeof key>;
const prevValue = cache.get(key, false) as OnyxValue<typeof key>;
cache.set(key, value);
OnyxUtils.keyChanged(key, value as OnyxValue<typeof key>, prevValue);
});
Expand Down Expand Up @@ -214,7 +214,7 @@ function set<TKey extends OnyxKey>(key: TKey, value: NonUndefined<OnyxEntry<KeyV
delete OnyxUtils.getMergeQueue()[key];
}

const existingValue = cache.getValue(key, false);
const existingValue = cache.get(key, false);

// If the existing value as well as the new value are null, we can return early.
if (value === null && existingValue === null) {
Expand Down Expand Up @@ -285,7 +285,7 @@ function multiSet(data: Partial<NullableKeyValueMapping>): Promise<void> {
});

const updatePromises = keyValuePairsToUpdate.map(([key, value]) => {
const prevValue = cache.getValue(key, false);
const prevValue = cache.get(key, false);

// Update cache and optimistically inform subscribers on the next tick
cache.set(key, value);
Expand Down Expand Up @@ -575,7 +575,7 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise<void> {
// 2. Figure out whether it is a collection key or not,
// since collection key subscribers need to be updated differently
if (!isKeyToPreserve) {
const oldValue = cache.getValue(key);
const oldValue = cache.get(key);
const newValue = defaultKeyStates[key] ?? null;
if (newValue !== oldValue) {
cache.set(key, newValue);
Expand Down Expand Up @@ -603,7 +603,7 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise<void> {

// Notify the subscribers for each key/value group so they can receive the new values
Object.entries(keyValuesToResetIndividually).forEach(([key, value]) => {
updatePromises.push(OnyxUtils.scheduleSubscriberUpdate(key, value, cache.getValue(key, false)));
updatePromises.push(OnyxUtils.scheduleSubscriberUpdate(key, value, cache.get(key, false)));
});
Object.entries(keyValuesToResetAsCollection).forEach(([key, value]) => {
updatePromises.push(OnyxUtils.scheduleNotifyCollectionSubscribers(key, value));
Expand Down
2 changes: 1 addition & 1 deletion lib/OnyxCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class OnyxCache {
* Get a cached value from storage
* @param [shouldReindexCache] – This is an LRU cache, and by default accessing a value will make it become last in line to be evicted. This flag can be used to skip that and just access the value directly without side-effects.
*/
getValue(key: OnyxKey, shouldReindexCache = true): OnyxValue<OnyxKey> {
get(key: OnyxKey, shouldReindexCache = true): OnyxValue<OnyxKey> {
if (shouldReindexCache) {
this.addToAccessedKeys(key);
}
Expand Down
16 changes: 8 additions & 8 deletions lib/OnyxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function get(key: OnyxKey): Promise<OnyxValue<OnyxKey>> {
// When we already have the value in cache (or the key has been set to null,
// and therefore no data is set in cache), resolve right away.
if (cache.hasCacheForKey(key)) {
return Promise.resolve(cache.getValue(key));
return Promise.resolve(cache.get(key));
}

const taskName = `get:${key}`;
Expand All @@ -225,7 +225,7 @@ function get(key: OnyxKey): Promise<OnyxValue<OnyxKey>> {
.then((val) => {
if (val === undefined) {
cache.addNullishStorageKey(key);
return val;
return undefined;
}

cache.set(key, val);
Expand Down Expand Up @@ -307,7 +307,7 @@ function isSafeEvictionKey(testKey: OnyxKey): boolean {
* If the requested key is a collection, it will return an object with all the collection members.
*/
function tryGetCachedValue<TKey extends OnyxKey>(key: TKey, mapping?: Partial<WithOnyxConnectOptions<TKey>>): OnyxValue<OnyxKey> {
let val = cache.getValue(key);
let val = cache.get(key);

if (isCollectionKey(key)) {
const allCacheKeys = cache.getAllKeys();
Expand All @@ -320,7 +320,7 @@ function tryGetCachedValue<TKey extends OnyxKey>(key: TKey, mapping?: Partial<Wi

const matchingKeys = Array.from(allCacheKeys).filter((k) => k.startsWith(key));
const values = matchingKeys.reduce((finalObject: NonNullable<OnyxCollection<KeyValueMapping[TKey]>>, matchedKey) => {
const cachedValue = cache.getValue(matchedKey);
const cachedValue = cache.get(matchedKey);
if (cachedValue) {
// This is permissible because we're in the process of constructing the final object in a reduce function.
// eslint-disable-next-line no-param-reassign
Expand Down Expand Up @@ -421,7 +421,7 @@ function getCachedCollection<TKey extends CollectionKeyBase>(collectionKey: TKey
return;
}

collection[key] = cache.getValue(key);
collection[key] = cache.get(key);
});

return collection;
Expand Down Expand Up @@ -832,7 +832,7 @@ function getCollectionDataAndSendAsObject<TKey extends OnyxKey>(matchingKeys: Co
* These missingKeys will be later to use to multiGet the data from the storage.
*/
matchingKeys.forEach((key) => {
const cacheValue = cache.getValue(key) as OnyxValue<TKey>;
const cacheValue = cache.get(key) as OnyxValue<TKey>;
if (cacheValue) {
data[key] = cacheValue;
return;
Expand Down Expand Up @@ -920,7 +920,7 @@ function scheduleNotifyCollectionSubscribers<TKey extends OnyxKey>(
* Remove a key from Onyx and update the subscribers
*/
function remove<TKey extends OnyxKey>(key: TKey): Promise<void> {
const prevValue = cache.getValue(key, false) as OnyxValue<TKey>;
const prevValue = cache.get(key, false) as OnyxValue<TKey>;
cache.drop(key);
scheduleSubscriberUpdate(key, null as OnyxValue<TKey>, prevValue);
return Storage.removeItem(key).then(() => undefined);
Expand Down Expand Up @@ -975,7 +975,7 @@ function evictStorageAndRetry<TMethod extends typeof Onyx.set | typeof Onyx.mult
* Notifies subscribers and writes current value to cache
*/
function broadcastUpdate<TKey extends OnyxKey>(key: TKey, value: OnyxValue<TKey>, hasChanged?: boolean): Promise<void> {
const prevValue = cache.getValue(key, false) as OnyxValue<TKey>;
const prevValue = cache.get(key, false) as OnyxValue<TKey>;

// Update subscribers if the cached value has changed, or when the subscriber specifically requires
// all updates regardless of value changes (indicated by initWithStoredValues set to false).
Expand Down
44 changes: 22 additions & 22 deletions tests/unit/onyxCacheTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('Onyx', () => {
// Given empty cache

// When a value is retrieved
const result = cache.getValue('mockKey');
const result = cache.get('mockKey');

// Then it should be undefined
expect(result).not.toBeDefined();
Expand All @@ -92,8 +92,8 @@ describe('Onyx', () => {

// When a value is retrieved
// Then it should be the correct value
expect(cache.getValue('mockKey')).toEqual({items: ['mockValue', 'mockValue2']});
expect(cache.getValue('mockKey2')).toEqual('mockValue3');
expect(cache.get('mockKey')).toEqual({items: ['mockValue', 'mockValue2']});
expect(cache.get('mockKey2')).toEqual('mockValue3');
});
});

Expand Down Expand Up @@ -155,7 +155,7 @@ describe('Onyx', () => {
cache.set('mockKey', {value: 'mockValue'});

// Then data should be cached
const data = cache.getValue('mockKey');
const data = cache.get('mockKey');
expect(data).toEqual({value: 'mockValue'});
});

Expand All @@ -178,7 +178,7 @@ describe('Onyx', () => {
cache.set('mockKey2', {value: []});

// Then the value should be overwritten
expect(cache.getValue('mockKey2')).toEqual({value: []});
expect(cache.get('mockKey2')).toEqual({value: []});
});
});

Expand All @@ -193,7 +193,7 @@ describe('Onyx', () => {

// Then a value should not be available in cache
expect(cache.hasCacheForKey('mockKey')).toBe(false);
expect(cache.getValue('mockKey')).not.toBeDefined();
expect(cache.get('mockKey')).not.toBeDefined();
expect(cache.getAllKeys().has('mockKey')).toBe(false);
});
});
Expand All @@ -209,8 +209,8 @@ describe('Onyx', () => {
});

// Then data should be created in cache
expect(cache.getValue('mockKey')).toEqual({value: 'mockValue'});
expect(cache.getValue('mockKey2')).toEqual({value: 'mockValue2'});
expect(cache.get('mockKey')).toEqual({value: 'mockValue'});
expect(cache.get('mockKey2')).toEqual({value: 'mockValue2'});
});

it('Should merge data to existing cache value', () => {
Expand All @@ -225,12 +225,12 @@ describe('Onyx', () => {
});

// Then the values should be merged together in cache
expect(cache.getValue('mockKey')).toEqual({
expect(cache.get('mockKey')).toEqual({
value: 'mockValue',
mockItems: [],
});

expect(cache.getValue('mockKey2')).toEqual({
expect(cache.get('mockKey2')).toEqual({
other: 'overwrittenMockValue',
items: [1, 2],
mock: 'mock',
Expand All @@ -247,7 +247,7 @@ describe('Onyx', () => {
});

// Then the values should be merged together in cache
expect(cache.getValue('mockKey')).toEqual({
expect(cache.get('mockKey')).toEqual({
value: 'mockValue',
mockItems: [],
otherValue: 'overwritten',
Expand All @@ -264,7 +264,7 @@ describe('Onyx', () => {
});

// Then the arrays should be replaced as expected
expect(cache.getValue('mockKey')).toEqual([{ID: 3}, {added: 'field'}, {}, {ID: 1000}]);
expect(cache.get('mockKey')).toEqual([{ID: 3}, {added: 'field'}, {}, {ID: 1000}]);
});

it('Should merge arrays inside objects correctly', () => {
Expand All @@ -277,7 +277,7 @@ describe('Onyx', () => {
});

// Then the first array is completely replaced by the second array
expect(cache.getValue('mockKey')).toEqual({ID: [2]});
expect(cache.get('mockKey')).toEqual({ID: [2]});
});

it('Should work with primitive values', () => {
Expand All @@ -288,31 +288,31 @@ describe('Onyx', () => {
cache.merge({mockKey: false});

// Then the object should be overwritten with a bool value
expect(cache.getValue('mockKey')).toEqual(false);
expect(cache.get('mockKey')).toEqual(false);

// When merge is called with number
cache.merge({mockKey: 0});

// Then the value should be overwritten
expect(cache.getValue('mockKey')).toEqual(0);
expect(cache.get('mockKey')).toEqual(0);

// When merge is called with string
cache.merge({mockKey: '123'});

// Then the value should be overwritten
expect(cache.getValue('mockKey')).toEqual('123');
expect(cache.get('mockKey')).toEqual('123');

// When merge is called with string again
cache.merge({mockKey: '123'});

// Then strings should not have been concatenated
expect(cache.getValue('mockKey')).toEqual('123');
expect(cache.get('mockKey')).toEqual('123');

// When merge is called with an object
cache.merge({mockKey: {value: 'myMockObject'}});

// Then the old primitive value should be overwritten with the object
expect(cache.getValue('mockKey')).toEqual({value: 'myMockObject'});
expect(cache.get('mockKey')).toEqual({value: 'myMockObject'});
});

it('Should ignore `undefined` values', () => {
Expand All @@ -323,12 +323,12 @@ describe('Onyx', () => {
cache.merge({mockKey: {ID: undefined}});

// Then the key should still be in cache and the value unchanged
expect(cache.getValue('mockKey')).toEqual({ID: 5});
expect(cache.get('mockKey')).toEqual({ID: 5});

cache.merge({mockKey: undefined});

// Then the key should still be in cache and the value unchanged
expect(cache.getValue('mockKey')).toEqual({ID: 5});
expect(cache.get('mockKey')).toEqual({ID: 5});
});

it('Should update storageKeys when new keys are created', () => {
Expand Down Expand Up @@ -363,8 +363,8 @@ describe('Onyx', () => {

cache.merge({mockKey: null});

expect(cache.getValue('mockKey')).toEqual(undefined);
expect(cache.getValue('mockNullKey')).toEqual(undefined);
expect(cache.get('mockKey')).toEqual(undefined);
expect(cache.get('mockNullKey')).toEqual(undefined);
});
});

Expand Down
8 changes: 4 additions & 4 deletions tests/unit/onyxClearNativeStorageTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('Set data while storage is clearing', () => {
return waitForPromisesToResolve().then(() => {
// Then the value in Onyx, the cache, and Storage is the default key state
expect(onyxValue).toBe(DEFAULT_VALUE);
const cachedValue = cache.getValue(ONYX_KEYS.DEFAULT_KEY);
const cachedValue = cache.get(ONYX_KEYS.DEFAULT_KEY);
expect(cachedValue).toBe(DEFAULT_VALUE);
return StorageMock.getItem(ONYX_KEYS.DEFAULT_KEY).then((storedValue) => expect(parseInt(storedValue as string, 10)).toBe(DEFAULT_VALUE));
});
Expand All @@ -70,7 +70,7 @@ describe('Set data while storage is clearing', () => {
return waitForPromisesToResolve().then(() => {
// Then the value in Onyx, the cache, and Storage is the default key state
expect(onyxValue).toBe(DEFAULT_VALUE);
const cachedValue = cache.getValue(ONYX_KEYS.DEFAULT_KEY);
const cachedValue = cache.get(ONYX_KEYS.DEFAULT_KEY);
expect(cachedValue).toBe(DEFAULT_VALUE);
return StorageMock.getItem(ONYX_KEYS.DEFAULT_KEY).then((storedValue) => expect(parseInt(storedValue as string, 10)).toBe(DEFAULT_VALUE));
});
Expand All @@ -94,7 +94,7 @@ describe('Set data while storage is clearing', () => {
return waitForPromisesToResolve().then(() => {
// Then the value of the preserved key is also still set in both the cache and storage
expect(regularKeyOnyxValue).toBe(SET_VALUE);
expect(cache.getValue(ONYX_KEYS.REGULAR_KEY)).toBe(SET_VALUE);
expect(cache.get(ONYX_KEYS.REGULAR_KEY)).toBe(SET_VALUE);
return StorageMock.getItem(ONYX_KEYS.REGULAR_KEY).then((storedValue) => expect(parseInt(storedValue as string, 10)).toBe(SET_VALUE));
});
});
Expand All @@ -111,7 +111,7 @@ describe('Set data while storage is clearing', () => {
return waitForPromisesToResolve().then(() => {
// Then the value in Onyx, the cache, and Storage is the set value
expect(onyxValue).toBe(SET_VALUE);
const cachedValue = cache.getValue(ONYX_KEYS.DEFAULT_KEY);
const cachedValue = cache.get(ONYX_KEYS.DEFAULT_KEY);
expect(cachedValue).toBe(SET_VALUE);
return StorageMock.getItem(ONYX_KEYS.DEFAULT_KEY).then((storedValue) => expect(parseInt(storedValue as string, 10)).toBe(SET_VALUE));
});
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/onyxClearWebStorageTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('Set data while storage is clearing', () => {
return waitForPromisesToResolve().then(() => {
// Then the value in Onyx, the cache, and Storage is the default key state
expect(onyxValue).toBe(DEFAULT_VALUE);
const cachedValue = cache.getValue(ONYX_KEYS.DEFAULT_KEY);
const cachedValue = cache.get(ONYX_KEYS.DEFAULT_KEY);
expect(cachedValue).toBe(DEFAULT_VALUE);
const storedValue = StorageMock.getItem(ONYX_KEYS.DEFAULT_KEY);
return expect(storedValue).resolves.toBe(DEFAULT_VALUE);
Expand All @@ -73,7 +73,7 @@ describe('Set data while storage is clearing', () => {
return waitForPromisesToResolve().then(() => {
// Then the value in Onyx, the cache, and Storage is the default key state
expect(onyxValue).toBe(DEFAULT_VALUE);
const cachedValue = cache.getValue(ONYX_KEYS.DEFAULT_KEY);
const cachedValue = cache.get(ONYX_KEYS.DEFAULT_KEY);
expect(cachedValue).toBe(DEFAULT_VALUE);
const storedValue = StorageMock.getItem(ONYX_KEYS.DEFAULT_KEY);
return expect(storedValue).resolves.toBe(DEFAULT_VALUE);
Expand All @@ -98,7 +98,7 @@ describe('Set data while storage is clearing', () => {
return waitForPromisesToResolve().then(() => {
// Then the value of the preserved key is also still set in both the cache and storage
expect(regularKeyOnyxValue).toBe(SET_VALUE);
const regularKeyCachedValue = cache.getValue(ONYX_KEYS.REGULAR_KEY);
const regularKeyCachedValue = cache.get(ONYX_KEYS.REGULAR_KEY);
expect(regularKeyCachedValue).toBe(SET_VALUE);
const regularKeyStoredValue = StorageMock.getItem(ONYX_KEYS.REGULAR_KEY);
return expect(regularKeyStoredValue).resolves.toBe(SET_VALUE);
Expand All @@ -117,7 +117,7 @@ describe('Set data while storage is clearing', () => {
return waitForPromisesToResolve().then(() => {
// Then the value in Onyx, the cache, and Storage is the set value
expect(onyxValue).toBe(SET_VALUE);
const cachedValue = cache.getValue(ONYX_KEYS.DEFAULT_KEY);
const cachedValue = cache.get(ONYX_KEYS.DEFAULT_KEY);
expect(cachedValue).toBe(SET_VALUE);
const storedValue = StorageMock.getItem(ONYX_KEYS.DEFAULT_KEY);
return expect(storedValue).resolves.toBe(SET_VALUE);
Expand Down
Loading

0 comments on commit d574d6d

Please sign in to comment.