Skip to content

Commit

Permalink
feat: implement multiGet function
Browse files Browse the repository at this point in the history
  • Loading branch information
kidroca committed Jul 14, 2021
1 parent 85f2356 commit f29cfc9
Showing 1 changed file with 53 additions and 21 deletions.
74 changes: 53 additions & 21 deletions lib/Onyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,65 @@ deferredInit.promise = new Promise((res, rej) => {
});

/**
* Get some data from the store
* Get multiple keys in a single batch
*
* @param {string} key
* @returns {Promise<*>}
* @param {string[]} keys
* @returns {Promise<Record<string, *>>} - an object of key -> value mapping
*/
function get(key) {
// When we already have the value in cache - resolve right away
if (cache.hasCacheForKey(key)) {
return Promise.resolve(cache.getValue(key));
}
function multiGet(keys) {
const [fromCache, hardReads] = _.partition(
keys,
key => cache.hasCacheForKey(key) || cache.hasPendingTask(`get:${key}`)
);

// Init a list of tasks containing any pending reads from storage
const tasks = fromCache
.filter(key => cache.hasPendingTask(`get:${key}`))
.map(key => Promise.resolve(cache.getTaskPromise(`get:${key}`)));

// Append a task for any remaining "hard reads" the current list
if (hardReads.length > 0) {
const readKeysFromStorage = AsyncStorage.multiGet(hardReads)
.then(pairs => pairs.forEach(([key, val]) => {
const parsed = val && JSON.parse(val);
cache.set(key, parsed || null);
}));

const taskName = `get:${key}`;
tasks.push(readKeysFromStorage);

// When a value retrieving task for this key is still running hook to it
if (cache.hasPendingTask(taskName)) {
return cache.getTaskPromise(taskName);
/* Capture a resolver for potential multiGet calls that happen while we retrieve keys
* example one one call retrieves keys A,B,C another call retrieves B,C,D
* we don't need to start a new read for B,C we'll hook them from the first call
*/
hardReads.forEach((key) => {
cache.captureTask(`get:${key}`, readKeysFromStorage.then(() => cache.getValue(key)));
});
}

// Otherwise retrieve the value from storage and capture a promise to aid concurrent usages
const promise = AsyncStorage.getItem(key)
.then((val) => {
const parsed = val && JSON.parse(val);
cache.set(key, parsed);
return parsed;
})
.catch(err => logInfo(`Unable to get item from persistent storage. Key: ${key} Error: ${err}`));
// Return a grouped result
return Promise.all(tasks)
.then(() => {
/* At this point we have everything in cache
* Instead of searching for the value inside the result, we can retrieved it by key from cache */
const result = _.chain(keys)
.map(key => [key, cache.getValue(key)])
.object()
.value();

return cache.captureTask(taskName, promise);
return result;
});
}

/**
* Get some data from the store
*
* @param {string} key
* @returns {Promise<*>}
*/
function get(key) {
/* We use multiGet as it will batch multiple calls and flush them on the next iteration of
* the event loop. AsyncStorage uses `setImmediate` to do this internally */
return multiGet([key]).then(result => result[key]);
}

/**
Expand Down Expand Up @@ -781,6 +812,7 @@ function applyDecorators() {
// Re-assign with decorated functions
/* eslint-disable no-func-assign */
get = decorate.decorateWithMetrics(get, 'Onyx:get');
multiGet = decorate.decorateWithMetrics(multiGet, 'Onyx:multiGet');
set = decorate.decorateWithMetrics(set, 'Onyx:set');
multiSet = decorate.decorateWithMetrics(multiSet, 'Onyx:multiSet');
clear = decorate.decorateWithMetrics(clear, 'Onyx:clear');
Expand Down

0 comments on commit f29cfc9

Please sign in to comment.