-
Notifications
You must be signed in to change notification settings - Fork 74
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
[Performance] JSI Storage implementation #95
Changes from all commits
dbf8a12
56ee80f
5cc4e2a
3cad095
51ab035
7bf214f
6793002
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as Storage from '@react-native-async-storage/async-storage/jest/async-storage-mock'; | ||
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. In tests storage is mocked by by the AsyncStorage's mock |
||
|
||
export default Storage; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import StorageProvider from './providers/AsyncStorageProvider'; | ||
|
||
const instance = new StorageProvider(); | ||
|
||
export default instance; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import StorageProvider from './providers/MMKV_Provider'; | ||
|
||
const instance = new StorageProvider(); | ||
|
||
export default instance; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
|
||
/** | ||
* @implements {ProviderInterface} | ||
*/ | ||
class AsyncStorageProvider { | ||
async getAllKeys() { | ||
return AsyncStorage.getAllKeys(); | ||
} | ||
|
||
async getItem(key) { | ||
return AsyncStorage.getItem(key); | ||
} | ||
|
||
async multiGet(keys) { | ||
return AsyncStorage.multiGet(keys); | ||
} | ||
|
||
async removeItem(key) { | ||
return AsyncStorage.removeItem(key); | ||
} | ||
|
||
async setItem(key, value) { | ||
return AsyncStorage.setItem(key, value); | ||
} | ||
|
||
async multiSet(pairs) { | ||
return AsyncStorage.multiSet(pairs); | ||
} | ||
|
||
async multiMerge(pairs) { | ||
return AsyncStorage.multiMerge(pairs); | ||
} | ||
|
||
async clear() { | ||
return AsyncStorage.clear(); | ||
} | ||
} | ||
|
||
export default AsyncStorageProvider; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {MMKV} from 'react-native-mmkv'; | ||
import _ from 'underscore'; | ||
import lodashMerge from 'lodash/merge'; | ||
|
||
/** | ||
* @implements {ProviderInterface} | ||
*/ | ||
class MMKV_Provider { | ||
async clear() { | ||
MMKV.clearAll(); | ||
return Promise.resolve(); | ||
} | ||
|
||
async getAllKeys() { | ||
const keys = MMKV.getAllKeys(); | ||
return Promise.resolve(keys); | ||
} | ||
|
||
async getItem(key) { | ||
const value = MMKV.getString(key); | ||
return Promise.resolve(value); | ||
} | ||
|
||
async multiGet(keys) { | ||
const pairs = keys.map(key => [key, MMKV.getString(key)]); | ||
return Promise.resolve(pairs); | ||
} | ||
|
||
async multiMerge(pairs) { | ||
pairs.forEach(([key, delta]) => { | ||
let merged = JSON.parse(delta); | ||
const existing = MMKV.getString(key); | ||
if (existing) { | ||
const parsed = JSON.parse(existing); | ||
if (_.isObject(parsed)) { | ||
merged = lodashMerge(parsed, delta); | ||
} | ||
} | ||
|
||
const asString = JSON.stringify(merged); | ||
MMKV.set(key, asString); | ||
}); | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
async multiSet(pairs) { | ||
pairs.forEach(([key, value]) => MMKV.set(key, value)); | ||
return Promise.resolve(); | ||
} | ||
|
||
async removeItem(key) { | ||
MMKV.delete(key); | ||
return Promise.resolve(); | ||
} | ||
|
||
async setItem(key, value) { | ||
MMKV.set(key, value); | ||
return Promise.resolve(); | ||
} | ||
} | ||
|
||
export default MMKV_Provider; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
function notImplemented() { | ||
throw new Error('Method not implemented'); | ||
} | ||
|
||
/** | ||
* @interface | ||
* The interface Onyx uses to communicate with the storage layer | ||
*/ | ||
class ProviderInterface { | ||
Comment on lines
+5
to
+9
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. This can serve as an abstract class and even provide some base functionality like parsing/stringifying data ATM no-one is extending or using the class created here it serves for documentation purposes
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.
Can we maybe explore this when there is a clear need? It feels like an unnecessary distraction for the current exercise. |
||
/** | ||
* @returns {Promise<string[]>} | ||
*/ | ||
async getAllKeys() { | ||
return notImplemented(); | ||
} | ||
|
||
/** | ||
* @template T | ||
* @param {string} key | ||
* @returns {Promise<T>} | ||
*/ | ||
async getItem(key) { | ||
return notImplemented(key); | ||
} | ||
|
||
/** | ||
* @param {string[]} keys | ||
* @returns {Promise<Array<[string, *]>>} | ||
*/ | ||
async multiGet(keys) { | ||
return notImplemented(keys); | ||
} | ||
|
||
/** | ||
* @param {string} key | ||
* @returns {Promise<void>} | ||
*/ | ||
async removeItem(key) { | ||
notImplemented(key); | ||
} | ||
|
||
/** | ||
* @param {string} key | ||
* @param {*} value | ||
* @returns {Promise<void>} | ||
*/ | ||
async setItem(key, value) { | ||
notImplemented(key, value); | ||
} | ||
|
||
/** | ||
* @param {Array<[string, *]>} pairs | ||
* @returns {Promise<void>} | ||
*/ | ||
async multiSet(pairs) { | ||
notImplemented(pairs); | ||
} | ||
|
||
/** | ||
* @param {Array<[string, *]>} pairs | ||
* @returns {Promise<void>} | ||
*/ | ||
async multiMerge(pairs) { | ||
notImplemented(pairs); | ||
} | ||
|
||
/** | ||
* @returns {Promise<void>} | ||
*/ | ||
async clear() { | ||
notImplemented(); | ||
} | ||
} | ||
|
||
export default ProviderInterface; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
jest.mock('../lib/storage'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using AsyncStorage or MMKV directly, we use the
Storage
facadeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would love to see a simpler version of this PR where we do something like
import AsyncStorage from './AsyncStorage
Then have
index.js
- import / export from@react-native-async-storage/async-storage
index.native.js
- export a customAsyncStorage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should use the name AsyncStorage if the underlying storage implementation could be of a different library. A more generic term should be used so it's clear we're not tied to AsyncStorage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure it matters for the purposes of the POC - the code will run the same there will just be less of it.