Skip to content

Commit

Permalink
Merge 3fa6428 into 64582ac
Browse files Browse the repository at this point in the history
  • Loading branch information
cbbfcd committed Dec 19, 2018
2 parents 64582ac + 3fa6428 commit 9de926d
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 113 deletions.
85 changes: 54 additions & 31 deletions dist/store.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,59 @@

'use strict';

var storage = window.localStorage;
var storage = window.localStorage,
_inMemoryStorage = {},
_length = 0; // deal QuotaExceededError if user use incognito mode in browser

(function () {
function isSupported() {
var _KEY = '_Is_Incognit',
_VALUE = 'yes';

try {
storage.setItem(_KEY, _VALUE);
storage.removeItem(_KEY);
return true;
} catch (error) {
return false;
}
}

if (!isSupported()) {
storage.__proto__ = {
setItem: function setItem(key, value) {
_inMemoryStorage[key] = '' + value;

if (!_inMemoryStorage.hasOwnProperty(key)) {
_length++;
}
},
getItem: function getItem(key) {
if (key in _inMemoryStorage) {
return _inMemoryStorage[key];
}

return null;
},
removeItem: function removeItem(key) {
if (_inMemoryStorage.hasOwnProperty(key)) {
delete _inMemoryStorage[key];
_length--;
}
},
clear: function clear() {
_inMemoryStorage = {};
_length = 0;
},
length: function length() {
return _length;
},
key: function key(index) {
return Object.keys(_inMemoryStorage)[index] || null;
}
};
}
})();

function isJSON(obj) {
obj = JSON.stringify(obj);
Expand Down Expand Up @@ -44,36 +96,7 @@ function isFunction(value) {

function isArray(value) {
return Object.prototype.toString.call(value) === "[object Array]";
} // https://github.com/jaywcjlove/store.js/pull/8
// Error: QuotaExceededError


function dealIncognito(storage) {
var _KEY = '_Is_Incognit',
_VALUE = 'yes';

try {
storage.setItem(_KEY, _VALUE);
} catch (e) {
if (e.name === 'QuotaExceededError') {
var _nothing = function _nothing() {};

storage.__proto__ = {
setItem: _nothing,
getItem: _nothing,
removeItem: _nothing,
clear: _nothing
};
}
} finally {
if (storage.getItem(_KEY) === _VALUE) storage.removeItem(_KEY);
}

return storage;
} // deal QuotaExceededError if user use incognito mode in browser


storage = dealIncognito(storage);
}

function Store() {
if (!(this instanceof Store)) {
Expand Down
2 changes: 1 addition & 1 deletion dist/store.common.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 54 additions & 31 deletions dist/store.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,59 @@
* Licensed under the MIT license.
*/

var storage = window.localStorage;
var storage = window.localStorage,
_inMemoryStorage = {},
_length = 0; // deal QuotaExceededError if user use incognito mode in browser

(function () {
function isSupported() {
var _KEY = '_Is_Incognit',
_VALUE = 'yes';

try {
storage.setItem(_KEY, _VALUE);
storage.removeItem(_KEY);
return true;
} catch (error) {
return false;
}
}

if (!isSupported()) {
storage.__proto__ = {
setItem: function setItem(key, value) {
_inMemoryStorage[key] = '' + value;

if (!_inMemoryStorage.hasOwnProperty(key)) {
_length++;
}
},
getItem: function getItem(key) {
if (key in _inMemoryStorage) {
return _inMemoryStorage[key];
}

return null;
},
removeItem: function removeItem(key) {
if (_inMemoryStorage.hasOwnProperty(key)) {
delete _inMemoryStorage[key];
_length--;
}
},
clear: function clear() {
_inMemoryStorage = {};
_length = 0;
},
length: function length() {
return _length;
},
key: function key(index) {
return Object.keys(_inMemoryStorage)[index] || null;
}
};
}
})();

function isJSON(obj) {
obj = JSON.stringify(obj);
Expand Down Expand Up @@ -42,36 +94,7 @@ function isFunction(value) {

function isArray(value) {
return Object.prototype.toString.call(value) === "[object Array]";
} // https://github.com/jaywcjlove/store.js/pull/8
// Error: QuotaExceededError


function dealIncognito(storage) {
var _KEY = '_Is_Incognit',
_VALUE = 'yes';

try {
storage.setItem(_KEY, _VALUE);
} catch (e) {
if (e.name === 'QuotaExceededError') {
var _nothing = function _nothing() {};

storage.__proto__ = {
setItem: _nothing,
getItem: _nothing,
removeItem: _nothing,
clear: _nothing
};
}
} finally {
if (storage.getItem(_KEY) === _VALUE) storage.removeItem(_KEY);
}

return storage;
} // deal QuotaExceededError if user use incognito mode in browser


storage = dealIncognito(storage);
}

function Store() {
if (!(this instanceof Store)) {
Expand Down
85 changes: 54 additions & 31 deletions dist/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,59 @@
(global.store = factory());
}(this, (function () { 'use strict';

var storage = window.localStorage;
var storage = window.localStorage,
_inMemoryStorage = {},
_length = 0; // deal QuotaExceededError if user use incognito mode in browser

(function () {
function isSupported() {
var _KEY = '_Is_Incognit',
_VALUE = 'yes';

try {
storage.setItem(_KEY, _VALUE);
storage.removeItem(_KEY);
return true;
} catch (error) {
return false;
}
}

if (!isSupported()) {
storage.__proto__ = {
setItem: function setItem(key, value) {
_inMemoryStorage[key] = '' + value;

if (!_inMemoryStorage.hasOwnProperty(key)) {
_length++;
}
},
getItem: function getItem(key) {
if (key in _inMemoryStorage) {
return _inMemoryStorage[key];
}

return null;
},
removeItem: function removeItem(key) {
if (_inMemoryStorage.hasOwnProperty(key)) {
delete _inMemoryStorage[key];
_length--;
}
},
clear: function clear() {
_inMemoryStorage = {};
_length = 0;
},
length: function length() {
return _length;
},
key: function key(index) {
return Object.keys(_inMemoryStorage)[index] || null;
}
};
}
})();

function isJSON(obj) {
obj = JSON.stringify(obj);
Expand Down Expand Up @@ -48,36 +100,7 @@

function isArray(value) {
return Object.prototype.toString.call(value) === "[object Array]";
} // https://github.com/jaywcjlove/store.js/pull/8
// Error: QuotaExceededError


function dealIncognito(storage) {
var _KEY = '_Is_Incognit',
_VALUE = 'yes';

try {
storage.setItem(_KEY, _VALUE);
} catch (e) {
if (e.name === 'QuotaExceededError') {
var _nothing = function _nothing() {};

storage.__proto__ = {
setItem: _nothing,
getItem: _nothing,
removeItem: _nothing,
clear: _nothing
};
}
} finally {
if (storage.getItem(_KEY) === _VALUE) storage.removeItem(_KEY);
}

return storage;
} // deal QuotaExceededError if user use incognito mode in browser


storage = dealIncognito(storage);
}

function Store() {
if (!(this instanceof Store)) {
Expand Down
2 changes: 1 addition & 1 deletion dist/store.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 48 additions & 18 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,51 @@
var storage = window.localStorage;
var storage = window.localStorage, _inMemoryStorage = {}, _length = 0;

// deal QuotaExceededError if user use incognito mode in browser
(function(){
function isSupported(){
var _KEY = '_Is_Incognit', _VALUE = 'yes';
try {
storage.setItem(_KEY, _VALUE);
storage.removeItem(_KEY);
return true
} catch (error) {
return false;
}
}

if(!isSupported()){
storage.__proto__ = {
setItem: function (key, value){
_inMemoryStorage[key] = '' + value;
if(!_inMemoryStorage.hasOwnProperty(key)){
_length++;
}
},
getItem: function (key){
if(key in _inMemoryStorage){
return _inMemoryStorage[key];
}
return null;
},
removeItem: function (key){
if(_inMemoryStorage.hasOwnProperty(key)){
delete _inMemoryStorage[key];
_length--;
}
},
clear: function () {
_inMemoryStorage = {};
_length = 0;
},
length: function (){
return _length;
},
key: function (index){
return Object.keys(_inMemoryStorage)[index] || null;
}
}
}
})()

function isJSON(obj) {
obj = JSON.stringify(obj);
Expand All @@ -18,23 +65,6 @@ function deserialize(value) {
}
function isFunction(value) { return ({}).toString.call(value) === "[object Function]"; }
function isArray(value) { return Object.prototype.toString.call(value) === "[object Array]"; }
// https://github.com/jaywcjlove/store.js/pull/8
// Error: QuotaExceededError
function dealIncognito(storage) {
var _KEY = '_Is_Incognit', _VALUE = 'yes';
try { storage.setItem(_KEY, _VALUE) }
catch (e) {
if (e.name === 'QuotaExceededError') {
var _nothing = function () { };
storage.__proto__ = { setItem: _nothing, getItem: _nothing, removeItem: _nothing, clear: _nothing };
}
}
finally { if (storage.getItem(_KEY) === _VALUE) storage.removeItem(_KEY); }
return storage;
}

// deal QuotaExceededError if user use incognito mode in browser
storage = dealIncognito(storage);

function Store() {
if (!(this instanceof Store)) {
Expand Down

0 comments on commit 9de926d

Please sign in to comment.