-
Notifications
You must be signed in to change notification settings - Fork 92
Perf: use idb-keyval #293
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
Perf: use idb-keyval #293
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| jest.mock('./lib/storage'); | ||
| jest.mock('./lib/storage/NativeStorage', () => require('./lib/storage/__mocks__')); | ||
| jest.mock('./lib/storage/WebStorage', () => require('./lib/storage/__mocks__')); | ||
| jest.mock('./lib/storage/providers/LocalForage', () => require('./lib/storage/__mocks__')); | ||
| jest.mock('./lib/storage/providers/IDBKeyVal', () => require('./lib/storage/__mocks__')); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { | ||
| set, | ||
| keys, | ||
| getMany, | ||
| setMany, | ||
| get, | ||
| clear, | ||
| del, | ||
| delMany, | ||
| createStore, | ||
| promisifyRequest, | ||
| } from 'idb-keyval'; | ||
| import _ from 'underscore'; | ||
| import fastMerge from '../../fastMerge'; | ||
|
|
||
| const customStore = createStore('OnyxDB', 'keyvaluepairs'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. confirming: This will have the existing data from the current DB previously set up by
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes exactly 😊 It can just be replaced in place |
||
|
|
||
| const provider = { | ||
| /** | ||
| * Sets the value for a given key. The only requirement is that the value should be serializable to JSON string | ||
| * @param {String} key | ||
| * @param {*} value | ||
| * @return {Promise<void>} | ||
| */ | ||
| setItem: (key, value) => set(key, value, customStore), | ||
|
|
||
| /** | ||
| * Get multiple key-value pairs for the give array of keys in a batch. | ||
| * This is optimized to use only one database transaction. | ||
| * @param {String[]} keysParam | ||
| * @return {Promise<Array<[key, value]>>} | ||
| */ | ||
| multiGet: keysParam => getMany(keysParam, customStore) | ||
| .then(values => _.map(values, (value, index) => [keysParam[index], value])), | ||
|
|
||
| /** | ||
| * Multiple merging of existing and new values in a batch | ||
| * @param {Array<[key, value]>} pairs | ||
| * @return {Promise<void>} | ||
| */ | ||
| multiMerge: pairs => customStore('readwrite', (store) => { | ||
| // Note: we are using the manual store transaction here, to fit the read and update | ||
| // of the items in one transaction to achieve best performance. | ||
|
|
||
| const getValues = Promise.all(_.map(pairs, ([key]) => promisifyRequest(store.get(key)))); | ||
|
|
||
| return getValues.then((values) => { | ||
| const upsertMany = _.map(pairs, ([key, value], index) => { | ||
| const prev = values[index]; | ||
| const newValue = _.isObject(prev) ? fastMerge(prev, value) : value; | ||
| return promisifyRequest(store.put(newValue, key)); | ||
| }); | ||
| return Promise.all(upsertMany); | ||
| }); | ||
| }), | ||
|
|
||
| /** | ||
| * Merging an existing value with a new one | ||
| * @param {String} key | ||
| * @param {any} _changes - not used, as we rely on the pre-merged data from the `modifiedData` | ||
| * @param {any} modifiedData - the pre-merged data from `Onyx.applyMerge` | ||
| * @return {Promise<void>} | ||
| */ | ||
| mergeItem(key, _changes, modifiedData) { | ||
| return provider.multiMerge([[key, modifiedData]]); | ||
| }, | ||
|
|
||
| /** | ||
| * Stores multiple key-value pairs in a batch | ||
| * @param {Array<[key, value]>} pairs | ||
| * @return {Promise<void>} | ||
| */ | ||
| multiSet: pairs => setMany(pairs, customStore), | ||
|
|
||
| /** | ||
| * Clear everything from storage and also stops the SyncQueue from adding anything more to storage | ||
| * @returns {Promise<void>} | ||
| */ | ||
| clear: () => clear(customStore), | ||
|
|
||
| /** | ||
| * Returns all keys available in storage | ||
| * @returns {Promise<String[]>} | ||
| */ | ||
| getAllKeys: () => keys(customStore), | ||
|
|
||
| /** | ||
| * Get the value of a given key or return `null` if it's not available in storage | ||
| * @param {String} key | ||
| * @return {Promise<*>} | ||
| */ | ||
| getItem: key => get(key, customStore), | ||
|
|
||
| /** | ||
| * Remove given key and it's value from storage | ||
| * @param {String} key | ||
| * @returns {Promise<void>} | ||
| */ | ||
| removeItem: key => del(key, customStore), | ||
|
|
||
| /** | ||
| * Remove given keys and their values from storage | ||
| * | ||
| * @param {Array} keysParam | ||
| * @returns {Promise} | ||
| */ | ||
| removeItems: keysParam => delMany(keysParam, customStore), | ||
| }; | ||
|
|
||
| export default provider; | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.