react-native-storage
This is a local storage wrapper for both react-native(AsyncStorage) and browser(localStorage). ES6 syntax, promise for async load, fully tested with jest.
查看中文文档请点击README-CHN.md
Install
npm install react-native-storage --save
Usage
Config
For Web
You need to use webpack and babel to enable es6 modules for web development.
You should add the following lines to your webpack config:
// ... externals: "react-native": {} // This line is required! Otherwise an error would be thrown. module: loaders: // ... test: /\.js?$/ include: //path.join(__dirname, 'your-own-js-files'), //path.join(__dirname, 'node_modules/some-other-lib-that-needs-babel'), path loader: 'babel' query: cacheDirectory: true presets: 'es2015' 'stage-1' 'react' plugins: 'transform-runtime'
For React Native
You don't have to configure anything(but require react native version >= 0.13).
Import
import Storage from 'react-native-storage';
Do not use require('react-native-storage')
, which would cause error in react native version >= 0.16.
Init
var storage = // maximum capacity, default 1000 size: 1000 // expire time, default 1 day(1000 * 3600 * 24 secs) defaultExpires: 1000 * 3600 * 24 // cache data in the memory. default is true. enableCache: true // if data was not found in storage or expired, // the corresponding sync method will be invoked and return // the latest data. sync : // we'll talk about the details later. // I suggest you have one(and only one) storage instance in global scope. // for web// window.storage = storage; // for react native// global.storage = storage;
Save & Load & Remove
// Save something with key only. // Something more unique, and constantly being used.// They are perminently stored unless you remove.// Even expires, the data won't be removed. Only sync method would be invoked.storage; // loadstorage // -------------------------------------------------- // Save something with key and id. Something of the same type(key). // There is a quota over "key-id" data(the size parameter you pass in constructor).// By default the 1001th data will overwrite the 1st data. // If you then load the 1st data, a catch(data not found) or sync will be invoked.var userA = name: 'A' age: 20 tags: 'geek' 'nerd' 'otaku' ; storage; // loadstorage // -------------------------------------------------- //remove single recordstorage;storage; //!! clear map and remove all key-id data (but keep the key-only data)storage;
Sync remote data(refresh)
You can pass sync methods as one object parameter to the storage constructor, but also you can add it any time.
storagesync = // The name of the sync method must be the same of the data's key // And the passed params will be an all-in-one object. // You can use promise here. // Or plain callback function with resolve/reject, like: { let id resolve reject = params; ; }
With this example sync method, when you invoke:
storage
If there is no user 1002 stored currently, then storage.sync.user would be invoked to fetch remote data and returned.
Load batch data
// Load batch data with the same parameters as storage.load, but in an array.// It will invoke sync methods on demand, // and finally return them all in an ordered array. storage // Load batch data with one key and an array of ids.storage
There is a notable difference between the two methods except the arguments. getBatchData will invoke different sync methods(since the keys may be different) one by one when corresponding data is missing. However, getBatchDataWithIds will collect missing data, push their ids to an array, then pass the array to the corresponding sync method(to avoid too many requests) once, so you need to implement array query on server end and handle the parameters of sync method properly(cause the id parameter can be a single string or an array of strings).
issues page.
You are welcome to ask any question in theChangelog
0.0.16
- getBatchDataWithIds now won't invoke sync if everything is ready in storage.
0.0.15
- Fix bugs in promise chain.
- Can be used without any storage backend.(Use in-memory map)
0.0.10
- All methods except remove and clearMap are now totally promisified. Even custom sync methods can be promise. So you can chain them now.
- Adjust map structure.
- Improved some test cases.