react-native-storage
Advanced tools
Comparing version 0.0.14 to 0.0.15
var localStorageMock = (function() { | ||
var oStorage = { | ||
var localStorage = { | ||
}; | ||
//Object.defineProperty(oStorage, "key", { | ||
// value: function (nKeyId) { | ||
// var key = Object.keys(oStorage)[nKeyId]; | ||
// return (typeof key === "undefined") ? null : unescape(key); | ||
// }, | ||
// writable: false, | ||
// configurable: false, | ||
// enumerable: false | ||
//}); | ||
Object.defineProperty(oStorage, "getItem", { | ||
Object.defineProperty(localStorage, "getItem", { | ||
value: function (key) { | ||
return (oStorage[key] === undefined) ? null : oStorage[key]; | ||
return (localStorage[key] === undefined) ? null : localStorage[key]; | ||
}, | ||
@@ -21,8 +12,8 @@ writable: false, | ||
}); | ||
Object.defineProperty(oStorage, "setItem", { | ||
Object.defineProperty(localStorage, "setItem", { | ||
value: function (sKey, sValue) { | ||
if (typeof sValue !== "object") { | ||
oStorage[sKey] = sValue + ""; | ||
localStorage[sKey] = sValue + ""; | ||
} else { | ||
oStorage[sKey] = sValue; | ||
localStorage[sKey] = sValue; | ||
} | ||
@@ -34,3 +25,3 @@ }, | ||
}); | ||
Object.defineProperty(oStorage, "removeItem", { | ||
Object.defineProperty(localStorage, "removeItem", { | ||
value: function (sKey) { | ||
@@ -40,3 +31,3 @@ if (!sKey) { | ||
} | ||
delete oStorage[sKey] | ||
delete localStorage[sKey] | ||
}, | ||
@@ -47,5 +38,5 @@ writable: false, | ||
}); | ||
Object.defineProperty(oStorage, "length", { | ||
Object.defineProperty(localStorage, "length", { | ||
get: function () { | ||
return Object.keys(oStorage).length; | ||
return Object.keys(localStorage).length; | ||
}, | ||
@@ -55,7 +46,7 @@ configurable: false, | ||
}); | ||
Object.defineProperty(oStorage, "clear", { | ||
Object.defineProperty(localStorage, "clear", { | ||
value: function () { | ||
Object.keys(oStorage).forEach(function (key) { | ||
Object.keys(localStorage).forEach(function (key) { | ||
delete oStorage[key]; | ||
delete localStorage[key]; | ||
}); | ||
@@ -67,4 +58,30 @@ }, | ||
}); | ||
return oStorage; | ||
return localStorage; | ||
})(); | ||
Object.defineProperty(window, 'localStorage', { value: localStorageMock }); | ||
var asyncStorageMock = (function() { | ||
var data = {} | ||
var asyncStorage = { | ||
getItem: function (key) { | ||
return new Promise((resolve, reject) => { | ||
resolve(data[key]) | ||
}); | ||
}, | ||
setItem: function (key, value) { | ||
return new Promise((resolve, reject) => { | ||
data[key] = value; | ||
resolve(); | ||
}); | ||
}, | ||
removeItem: function (key) { | ||
return new Promise((resolve, reject) => { | ||
if(data[key]) { | ||
delete data[key]; | ||
} | ||
resolve(); | ||
}); | ||
} | ||
}; | ||
return asyncStorage; | ||
})(); | ||
Object.defineProperty(window, 'localStorage', { value: localStorageMock }); | ||
Object.defineProperty(window, 'asyncStorage', { value: asyncStorageMock }); |
{ | ||
"name": "react-native-storage", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"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", |
@@ -251,3 +251,3 @@ # react-native-storage [![Build Status](https://travis-ci.org/sunnylqm/react-native-storage.svg)](https://travis-ci.org/sunnylqm/react-native-storage) [![npm version](https://badge.fury.io/js/react-native-storage.svg)](http://badge.fury.io/js/react-native-storage) | ||
#### 0.0.14 | ||
#### 0.0.15 | ||
1. 修复promise链式调用中的bug. | ||
@@ -254,0 +254,0 @@ 2. 现在可以在没有任何storage引擎的情况下使用(仅使用内存map)而不会报错 |
@@ -263,3 +263,3 @@ # react-native-storage [![Build Status](https://travis-ci.org/sunnylqm/react-native-storage.svg)](https://travis-ci.org/sunnylqm/react-native-storage) [![npm version](https://badge.fury.io/js/react-native-storage.svg)](http://badge.fury.io/js/react-native-storage) | ||
#### 0.0.14 | ||
#### 0.0.15 | ||
1. Fix bugs in promise chain. | ||
@@ -266,0 +266,0 @@ 2. Can be used without any storage backend.(Use in-memory map) |
/* | ||
* local storage(web/react native) wrapper | ||
* sunnylqm 2016-02-24 | ||
* version 0.0.14 | ||
* version 0.0.15 | ||
*/ | ||
@@ -15,24 +15,26 @@ | ||
me.enableCache = options.enableCache || true; | ||
me._s = options.storageBackend || null; | ||
me.isPromise = options.isPromise || true; | ||
me._innerVersion = 10; | ||
me.cache = {}; | ||
//detect browser or ios javascriptCore | ||
me.isBrowser = false; | ||
if(window && window.localStorage) { | ||
try { | ||
// avoid key conflict | ||
window.localStorage.setItem('__react_native_storage_test', 'test'); | ||
if(!me._s) { | ||
if(window && window.localStorage) { | ||
try { | ||
// avoid key conflict | ||
window.localStorage.setItem('__react_native_storage_test', 'test'); | ||
me._s = window.localStorage; | ||
me.isBrowser = true; | ||
me._s = window.localStorage; | ||
me.isPromise = false; | ||
} | ||
catch(e) { | ||
console.warn(e); | ||
delete me._s; | ||
throw e; | ||
} | ||
} | ||
catch(e) { | ||
console.warn(e); | ||
delete me._s; | ||
throw e; | ||
else { | ||
me._s = require('react-native').AsyncStorage; | ||
} | ||
} | ||
else { | ||
me._s = require('react-native').AsyncStorage; | ||
} | ||
@@ -47,3 +49,3 @@ me._mapPromise = me.getItem('map').then( map => { | ||
? | ||
this.isBrowser ? Promise.resolve(this._s.getItem(key)) : this._s.getItem(key) | ||
this.isPromise ? this._s.getItem(key) : Promise.resolve(this._s.getItem(key)) | ||
: | ||
@@ -55,3 +57,3 @@ Promise.resolve(); | ||
? | ||
this.isBrowser ? Promise.resolve(this._s.setItem(key, value)) : this._s.setItem(key, value) | ||
this.isPromise ? this._s.setItem(key, value) : Promise.resolve(this._s.setItem(key, value)) | ||
: | ||
@@ -62,3 +64,3 @@ Promise.resolve(); | ||
return this._s | ||
? this.isBrowser ? Promise.resolve(this._s.removeItem(key)) : this._s.removeItem(key) | ||
? this.isPromise ? this._s.removeItem(key) : Promise.resolve(this._s.removeItem(key)) | ||
: | ||
@@ -252,28 +254,29 @@ Promise.resolve(); | ||
remove(params) { | ||
let me = this, | ||
m = me._m; | ||
let { key, id } = params; | ||
return this._mapPromise.then(() => { | ||
let me = this, | ||
m = me._m; | ||
let { key, id } = params; | ||
if(id === undefined) { | ||
if(me.enableCache && me.cache[key]) { | ||
delete me.cache[key]; | ||
if(id === undefined) { | ||
if(me.enableCache && me.cache[key]) { | ||
delete me.cache[key]; | ||
} | ||
return me.removeItem(key); | ||
} | ||
return me.removeItem(key); | ||
} | ||
let newId = me._getId(key, id); | ||
let newId = me._getId(key, id); | ||
//remove existed data | ||
if(m[newId] !== undefined) { | ||
if(me.enableCache && me.cache[newId]) { | ||
delete me.cache[newId]; | ||
//remove existed data | ||
if(m[newId] !== undefined) { | ||
if(me.enableCache && me.cache[newId]) { | ||
delete me.cache[newId]; | ||
} | ||
let idTobeDeleted = m[newId]; | ||
delete m[newId]; | ||
me.setItem('map', JSON.stringify(m)); | ||
return me.removeItem('map_' + idTobeDeleted); | ||
} | ||
let idTobeDeleted = m[newId]; | ||
delete m[newId]; | ||
me.setItem('map', JSON.stringify(m)); | ||
return me.removeItem('map_' + idTobeDeleted); | ||
} | ||
}); | ||
} | ||
load(params) { | ||
let me = this, | ||
m = me._m; | ||
let me = this; | ||
let { key, id, autoSync, syncInBackground } = params; | ||
@@ -280,0 +283,0 @@ if(autoSync === undefined) { |
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
57624
12
1422