react-native-storage
Advanced tools
Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "react-native-storage", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "This is a local storage wrapper for both react-native(AsyncStorage) and browser(localStorage). ES6/babel is needed.", | ||
@@ -5,0 +5,0 @@ "main": "storage.js", |
@@ -14,7 +14,8 @@ # react-native-storage | ||
### Import 导入 | ||
You need to use [babel](https://babeljs.io/) to enable module import. For React-Native development, add 'es6.modules' to the **whitelist** in `node_modules/react-native/packager/transformer.js` | ||
你需要使用[babel](https://babeljs.io/)来支持模块导入功能。 如果是React-Native开发,在`node_modules/react-native/packager/transformer.js的whitelist`中加入'es6.modules'即可。 | ||
var Storage = require('react-native-storage'); | ||
import Storage from 'react-native-storage' | ||
or 或者 | ||
import Storage from 'react-native-storage'; | ||
### Save & Load 保存和读取 | ||
@@ -92,3 +93,3 @@ Storage.save(id, data, global, expires) | ||
There is a notable difference between the two methods except the arguments. **getBatchDataWithKeys** will invoke different sync methods(since the keys are different) one by one when corresponding data is missing. However, **getBatchDataWithIds** will collect missing data, push their ids to a array, then pass the array to the corresponding sync method(to avoid too many requests), so you need to implement array query on server end. | ||
这两个方法除了参数形式不同,还有个值得注意的差异。**getBatchDataWithKeys**会在数据缺失时挨个调用不同的sync方法(因为key不同)。但是**getBatchDataWithIds**却会把缺失的数据统计起来,将它们的id收集到一个数组中,然后一次传递给对应的sync方法(避免挨个查询导致同时发起大量请求),所以你需要在服务端实现通过数组来查询返回。 | ||
There is a notable difference between the two methods except the arguments. **getBatchDataWithKeys** will invoke different sync methods(since the keys are 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), so you need to implement array query on server end. | ||
这两个方法除了参数形式不同,还有个值得注意的差异。**getBatchDataWithKeys**会在数据缺失时挨个调用不同的sync方法(因为key不同)。但是**getBatchDataWithIds**却会把缺失的数据统计起来,将它们的id收集到一个数组中,然后一次传递给对应的sync方法(避免挨个查询导致同时发起大量请求),所以你需要在服务端实现通过数组来查询返回。 |
/* | ||
* local storage(web/react native) wrapper | ||
* sunny 2015-07-27 | ||
* sunnylqm 2015-08-23 | ||
* version 0.0.4 | ||
*/ | ||
let cache = {}; | ||
let m; | ||
let _SIZE = 1000; | ||
@@ -11,5 +13,6 @@ let _s; | ||
try { | ||
window.localStorage.setItem('test','test'); | ||
window.localStorage.setItem('test', 'test'); | ||
_s = window.localStorage; | ||
isBrowser = true; | ||
m = JSON.parse(_s.getItem('map')) || { index: 0 }; | ||
} | ||
@@ -22,2 +25,5 @@ catch(e){ | ||
_s = require('react-native').AsyncStorage; | ||
_s.getItem('map').then( map => { | ||
m = JSON.parse(map) || { index: 0 }; | ||
}); | ||
} | ||
@@ -39,5 +45,2 @@ export default class Storage { | ||
let s = Storage._s; | ||
if(!m){ | ||
m = { index: 0 }; | ||
} | ||
if(m[id]){ | ||
@@ -51,4 +54,4 @@ s.setItem('map_' + m[id],d); | ||
m[id] = m.index; | ||
s.setItem('map_' + m.index,d); | ||
s.setItem('map',JSON.stringify(m)); | ||
s.setItem('map_' + m.index, d); | ||
s.setItem('map', JSON.stringify(m)); | ||
} | ||
@@ -77,11 +80,12 @@ } | ||
else{ | ||
if(isBrowser){ | ||
let m = JSON.parse(s.getItem('map')); | ||
Storage._saveToMap(m, id, d); | ||
} | ||
else{ | ||
s.getItem('map').then( m => { | ||
Storage._saveToMap(JSON.parse(m), id, d); | ||
}) | ||
} | ||
Storage._saveToMap(id, d); | ||
//if(isBrowser){ | ||
// let m = Storage.cache.map || (Storage.cache.map = JSON.parse(s.getItem('map'))); | ||
// Storage._saveToMap(m, id, d); | ||
//} | ||
//else{ | ||
// s.getItem('map').then( m => { | ||
// Storage._saveToMap(JSON.parse(m), id, d); | ||
// }) | ||
//} | ||
@@ -176,6 +180,5 @@ } | ||
ret = JSON.parse(ret); | ||
if(ret && ret.expires < new Date().getTime()){ | ||
if(kv.length > 1 && Storage.sync[kv[0]]){ | ||
Storage.sync[kv[0]](kv[1]); | ||
} | ||
if(autoSync && ret && ret.expires < new Date().getTime() | ||
&& kv.length > 1 && Storage.sync[kv[0]]){ | ||
Storage.sync[kv[0]](kv[1]); | ||
} | ||
@@ -186,3 +189,3 @@ ret.objWrap && (ret = ret.objWrap); | ||
} | ||
static _lookUpInMap(m, id, resolve, reject, autoSync){ | ||
static _lookUpInMap(id, resolve, reject, autoSync){ | ||
let s = Storage._s, | ||
@@ -222,11 +225,12 @@ kv = id.split('_'), | ||
else{ | ||
if(isBrowser){ | ||
let m = JSON.parse(s.getItem('map')); | ||
Storage._lookUpInMap(m, id, resolve, reject, autoSync); | ||
} | ||
else{ | ||
s.getItem('map').then( m => { | ||
Storage._lookUpInMap(JSON.parse(m), id, resolve, reject, autoSync); | ||
}) | ||
} | ||
Storage._lookUpInMap(id, resolve, reject, autoSync); | ||
//if(isBrowser){ | ||
// let m = JSON.parse(s.getItem('map')); | ||
// Storage._lookUpInMap(m, id, resolve, reject, autoSync); | ||
//} | ||
//else{ | ||
// s.getItem('map').then( m => { | ||
// Storage._lookUpInMap(JSON.parse(m), id, resolve, reject, autoSync); | ||
// }) | ||
//} | ||
@@ -240,2 +244,3 @@ } | ||
s.removeItem('map'); | ||
m = { index: 0 }; | ||
//var s = Storage._s, | ||
@@ -268,3 +273,25 @@ // m = JSON.parse(s.getItem('map')); | ||
Storage.sync = { | ||
//TODO add your own sync method. | ||
//TODO implement your own sync methods like the following. | ||
//TODO Do not forget append **resolve** and **reject** | ||
//user(id, resolve, reject){ | ||
// fetch('user/', { | ||
// method: 'GET', | ||
// body: 'id=' + id | ||
// }).then(function(response) { | ||
// return response.json(); | ||
// }).then(function(data) { | ||
// //console.info(data); | ||
// if(data && data.user){ | ||
// data = data.user; | ||
// Storage.save('user_' + data.id, data); | ||
// resolve && resolve(data); | ||
// } | ||
// else{ | ||
// reject && reject(); | ||
// } | ||
// }).catch((error) => { | ||
// console.warn(error); | ||
// }); | ||
//} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15018
284
94