-
Notifications
You must be signed in to change notification settings - Fork 3
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
Add a adapter for Async Storage and Encrypted Storage (React Native) #30
Comments
Hello! I think, you can create
(no need for And I'll make import { persist as asyncRNPersist } from 'effector-storage/rn/async'
import { persist as encryptedRNPersist } from 'effector-storage/rn/encrypted' What do you think? Also I have a few questions: Does those storages supports any kind of feedback? Like What about custom serialization/deserialization? Does those storages supports only plain text values, or they can store any values (so serialization is not needed at all, maybe)? Does those storages supports any kind of customization, which should be added as well? |
I see those two adapters quite similar, does those storages have same API? Maybe it will be more convenient to add single configurable async adapter? |
I think, yes, that's a good idea.
No, neither Async Storage nor Encrypted Storage support change events because they are asynchronous storage.
These storages only support string data format, so serialization is necessary.
These storages do not have any settings, but there are other methods of data management. For example, there is a method for merging values (Async Storage only), or a method for clearing storage (both storage). You can learn more here: Async Storage API and Encrypted Storage API. |
It will be clear from the previous answer that the basic getItem() and setItem() methods are similar, so if you leave only these methods and the clear() method (if needed), you can combine these two adapters into one configurable one. import { Domain } from "effector";
import { persist, StorageAdapter } from "effector-storage";
import AsyncStorage from "@react-native-async-storage/async-storage";
import EncryptedStorage from "react-native-encrypted-storage";
interface AdapterOptions {
storage: typeof AsyncStorage | typeof EncryptedStorage;
}
const defaultAdapterOptions: AdapterOptions = {
storage: AsyncStorage,
};
const adapter = (options: AdapterOptions): StorageAdapter => {
return (key) => ({
get: async () => {
return options.storage
.getItem(key)
.then((value) => (value ? JSON.parse(value) : undefined));
},
set: async (value) => {
return options.storage.setItem(key, JSON.stringify(value));
},
});
};
export const withStorageAdapter = (
domain: Domain,
options: AdapterOptions = defaultAdapterOptions,
) => {
domain.onCreateStore((store) => {
return persist({ store, adapter: adapter(options) });
});
}; This construction, I think, can easily be developed in the future by adding more options, etc. |
Ok, got it 👍 Then add those two adapters in the PR, please :) |
Thank you! |
I've reworked your adapters, made single one configurable (largely based on existing storage adapter), and added tests (also based on existing storage adapter tests). Also I changed build a bit, because I've found an issue with duplicating code, I think, I'll add readme files and issue version 4.5.0. @rushelex Will you be able to test new |
Published new release 4.5.0 @rushelex try, please, in your project: // asyncStorageAdapter.ts
import type { Domain } from "effector";
import { persist } from 'effector-storage/rn/async';
export const withAsyncStorageAdapter = (domain: Domain) => {
domain.onCreateStore((store) => persist({ store }));
}; // encryptedStorageAdapter.ts
import type { Domain } from "effector";
import { persist } from 'effector-storage/rn/encrypted';
export const withEncryptedStorageAdapter = (domain: Domain) => {
domain.onCreateStore((store) => persist({ store }));
};
|
Closing this, @rushelex if you have any issues with those new adapters — feel free to reopen! |
Hi.
I wrote a simple driver for Async Storage and Encrypted Storage for a React Native application.
I can see from the source code that the driver needs to be converted to a persist function, but I'm not sure I can get it right to match the current styleguide.
Can I just submit a PR with my written withAsyncStorageAdapter and withEncryptedStorageAdapter functions?
Example:
The text was updated successfully, but these errors were encountered: