natty-storage
Advanced tools
Comparing version 1.0.0-rc5 to 1.0.0
@@ -75,2 +75,3 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
var isPlainObject = _require.isPlainObject; | ||
var noop = _require.noop; | ||
@@ -86,3 +87,3 @@ var win = window; | ||
var VERSION = undefined; | ||
(VERSION = "1.0.0-rc5"); | ||
(VERSION = "1.0.0"); | ||
@@ -119,3 +120,6 @@ var support = { | ||
// 有效期至, 时间戳 | ||
until: 0 | ||
until: 0, | ||
// 是否以异步方式使用set/get/has/remove | ||
async: false | ||
}; | ||
@@ -131,3 +135,3 @@ | ||
* // 验证是否有效,如果是首次创建该LS,则不执行验证 | ||
* tag: '1.0' | ||
* id: '1.0' | ||
* }) | ||
@@ -200,3 +204,3 @@ */ | ||
t._storage.set(t._CHECK_KEY, t._checkData = { | ||
tag: t.config.tag, | ||
id: t.config.id, | ||
lastUpdate: t._createStamp, | ||
@@ -216,3 +220,3 @@ duration: t.config.duration, | ||
var t = this; | ||
if (t.config.tag && t.config.tag !== t._checkData.tag) { | ||
if (t.config.id && t.config.id !== t._checkData.id) { | ||
return TRUE; | ||
@@ -252,4 +256,3 @@ } | ||
// 同步到storage | ||
return new Promise(function (resolve, reject) { | ||
var todo = function todo(resolve, reject) { | ||
try { | ||
@@ -275,3 +278,9 @@ if (!t._data) { | ||
} | ||
}); | ||
}; | ||
if (t.config.async) { | ||
return new Promise(todo); | ||
} else { | ||
todo(noop, throwError); | ||
} | ||
} | ||
@@ -292,5 +301,5 @@ | ||
var t = this; | ||
return new Promise(function (resolve, reject) { | ||
var data = undefined; | ||
var todo = function todo(resolve, reject) { | ||
try { | ||
var data = undefined; | ||
if (!t._data) { | ||
@@ -311,3 +320,10 @@ t._lazyInit(); | ||
} | ||
}); | ||
}; | ||
if (t.config.async) { | ||
return new Promise(todo); | ||
} else { | ||
todo(noop, throwError); | ||
return data; | ||
} | ||
} | ||
@@ -324,6 +340,5 @@ | ||
var t = this; | ||
return new Promise(function (resolve, reject) { | ||
var result = undefined; | ||
var todo = function todo(resolve, reject) { | ||
try { | ||
var has = undefined; | ||
if (!t._data) { | ||
@@ -339,11 +354,14 @@ t._lazyInit(); | ||
resolve(hasValueByPath(path, t._data) ? { | ||
result = hasValueByPath(path, t._data) ? { | ||
has: true, | ||
value: getValueByPath(path, t._data) | ||
} : {}); | ||
} : {}; | ||
resolve(result); | ||
} else { | ||
resolve(t._data.hasOwnProperty(PLACEHOLDER) ? { | ||
result = t._data.hasOwnProperty(PLACEHOLDER) ? { | ||
has: true, | ||
value: t._data[PLACEHOLDER] | ||
} : {}); | ||
} : {}; | ||
resolve(result); | ||
} | ||
@@ -353,3 +371,10 @@ } catch (e) { | ||
} | ||
}); | ||
}; | ||
if (t.config.async) { | ||
return new Promise(todo); | ||
} else { | ||
todo(noop, throwError); | ||
return result; | ||
} | ||
} | ||
@@ -365,3 +390,3 @@ | ||
var t = this; | ||
return new Promise(function (resolve, reject) { | ||
var todo = function todo(resolve, reject) { | ||
try { | ||
@@ -382,3 +407,9 @@ if (!t._data) { | ||
} | ||
}); | ||
}; | ||
if (t.config.async) { | ||
return new Promise(todo); | ||
} else { | ||
todo(noop, throwError); | ||
} | ||
} | ||
@@ -434,2 +465,6 @@ | ||
function throwError(e) { | ||
throw new Error(e); | ||
} | ||
function createStorage(storage) { | ||
@@ -662,4 +697,7 @@ storage = win[storage]; | ||
var noop = function noop() {}; | ||
module.exports = { | ||
extend: redo(extend), | ||
noop: noop, | ||
isPlainObject: isPlainObject | ||
@@ -666,0 +704,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("nattyStorage",[],e):"object"==typeof exports?exports.nattyStorage=e():t.nattyStorage=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var a=n[r]={exports:{},id:r,loaded:!1};return t[r].call(a.exports,a,a.exports,e),a.loaded=!0,a.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){"use strict";t.exports=n(1)},function(t,e,n){"use strict";function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function a(t){var e={x:"x"},n="natty-storage-test",r=o(t);r.set(n,e);var a=JSON.stringify(r.get(n))===JSON.stringify(e);return r.remove(n),a}function o(t){return t=y[t],{set:function(e,n){t.setItem(e,JSON.stringify(n))},get:function(e){var n=t.getItem(e);if(!n)return w;try{n=JSON.parse(n)}catch(r){}return n},remove:function(e){t.removeItem(e)}}}function i(){var t=P;return{set:function(e,n){t[e]=n},get:function(e){return e in t?t[e]:w},remove:function(e){delete t[e]}}}function c(t){return t.split(m).reverse().join(m)}function u(t){var e;if(-1===t.indexOf("\\."))e=t.split(".");else{e=c(t).split(/\.(?!\\)/).reverse();for(var n=0,r=e.length;r>n;n++)e[n]=c(e[n].replace(/\.\\/g,"."))}return e}function s(t,e,n){for(var r=u(t),a=n;r.length;){var o=r.shift();if(r.length)a[o]=a[o]||{},a=a[o];else{if(!p(a))throw new Error("Cannot create property `"+o+"` on non-object value, path:`"+t+"`");a[o]=e}}return n}function f(t,e,n){if(n=n||!1,n===!0||-1===t.indexOf("."))return e[t];for(var r=u(t);r.length;){var a=r.shift();if(e=f(a,e,!0),"object"!=typeof e||void 0===e){r.length&&(e=void 0);break}}return e}function l(t,e,n){if(n=n||-1===t.indexOf("."))return e.hasOwnProperty(t);for(var r=u(t);r.length;){var a=r.shift(),o=e.hasOwnProperty(a);if(!o||!r.length)return o;if(e=f(a,e,!0),!p(e))return O}}function d(t,e){for(var n=u(t),r=e;n.length;){var a=n.shift();n.length?(r[a]=r[a]||{},r=r[a]):delete r[a]}return e}function _(t){var e=b;for(var n in t){e=O;break}return e}var g=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),h=n(2),v=h.extend,p=h.isPlainObject,y=window,k="undefined"!=typeof y,w=null,m="",b=!0,O=!b,S="_placeholder",x=void 0;x="1.0.0-rc5";var D={localStorage:k&&!!y.localStorage&&a("localStorage"),sessionStorage:k&&!!y.sessionStorage&&a("sessionStorage")},E={type:"localStorage",key:m,tag:m,duration:0,until:0},A=v({},E),K=function(){function t(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];r(this,t);var n=this;if(n.config=v({},A,e),!n.config.key)throw new Error("`key` is missing, please check the options passed in `nattyStorage` constructor.");n._storage=D[n.config.type]?o(n.config.type):i(),n._CHECK_KEY="nattyStorageCheck:"+n.config.key,n._DATA_KEY="nattyStorageData:"+n.config.key,n._placeholderUsed=O,n._createStamp=+new Date}return g(t,[{key:"_lazyInit",value:function(){var t=this;t._checkData=t._storage.get(t._CHECK_KEY),t._isNew=t._checkData===w,t._isNew||t.isOutdated()?t._storage.set(t._DATA_KEY,t._data={}):(t._data=t._storage.get(t._DATA_KEY),t._data===w&&t._storage.set(t._DATA_KEY,t._data={})),t._storage.set(t._CHECK_KEY,t._checkData={tag:t.config.tag,lastUpdate:t._createStamp,duration:t.config.duration,until:t.config.until})}},{key:"isOutdated",value:function(){var t=this;if(t.config.tag&&t.config.tag!==t._checkData.tag)return b;var e=+new Date;return t._checkData.duration&&e-t._checkData.lastUpdate>t._checkData.duration?b:t._checkData.until&&e>t._checkData.until?b:O}},{key:"set",value:function(t,e){var n=this,r=arguments.length;return new Promise(function(a,o){try{n._data||n._lazyInit(),1===r?p(t)?n._data=t:(n._data[S]=t,n._placeholderUsed=b):s(t,e,n._data),n._storage.set(n._DATA_KEY,n._data),a()}catch(i){o(i)}})}},{key:"get",value:function(t){var e=this;return new Promise(function(n,r){try{var a=void 0;e._data||e._lazyInit(),a=t?f(t,e._data):e._placeholderUsed?e._data[S]:e._data,n(a)}catch(o){r(o)}})}},{key:"has",value:function(t){var e=this;return new Promise(function(n,r){try{if(e._data||e._lazyInit(),e._placeholderUsed||_(e._data))n(e._data.hasOwnProperty(S)?{has:!0,value:e._data[S]}:{});else{if(!t)throw new Error("a `path` argument should be passed into the `has` method");n(l(t,e._data)?{has:!0,value:f(t,e._data)}:{})}}catch(a){r(a)}})}},{key:"remove",value:function(t){var e=this;return new Promise(function(n,r){try{e._data||e._lazyInit(),t?(d(t,e._data),e._storage.set(e._DATA_KEY,e._data)):e.set({}),n()}catch(a){r(a)}})}},{key:"destroy",value:function(){var t=this;t._storage.remove(t._CHECK_KEY),t._storage.remove(t._DATA_KEY)}},{key:"dump",value:function(){JSON&&console&&console.log(JSON.stringify(this._data,w,4))}}]),t}(),j=function(t){return new K(t)};j.version=x,j._variable=P,j.support=D,j.create=function(t){return new K(t)},j.setGlobal=function(t){A=v({},E,t)},j.getGlobal=function(t){return t?A[t]:A};var P={};t.exports=j},function(t,e,n){"use strict";var r=null,a=function(t){return function(){for(var e=arguments,n=t(e[0],e[1]),r=2,a=e.length;a>r;r++)n=t(n,e[r]);return n}},o="object",i=function(t){return typeof t===o},c=function(t){return t!==r&&t===t.window},u=function(t){return t!==r&&i(t)&&!c(t)&&Object.getPrototypeOf(t)===Object.prototype},s=Array.isArray,f=function l(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];for(var n in e)e.hasOwnProperty(n)&&void 0!==e[n]&&(s(e[n])?t[n]=[].concat(e[n]):u(e[n])?t[n]=l({},e[n]):t[n]=e[n]);return t};t.exports={extend:a(f),isPlainObject:u}}])}); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("nattyStorage",[],e):"object"==typeof exports?exports.nattyStorage=e():t.nattyStorage=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var a=n[r]={exports:{},id:r,loaded:!1};return t[r].call(a.exports,a,a.exports,e),a.loaded=!0,a.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){"use strict";t.exports=n(1)},function(t,e,n){"use strict";function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function a(t){var e={x:"x"},n="natty-storage-test",r=i(t);r.set(n,e);var a=JSON.stringify(r.get(n))===JSON.stringify(e);return r.remove(n),a}function o(t){throw new Error(t)}function i(t){return t=k[t],{set:function(e,n){t.setItem(e,JSON.stringify(n))},get:function(e){var n=t.getItem(e);if(!n)return b;try{n=JSON.parse(n)}catch(r){}return n},remove:function(e){t.removeItem(e)}}}function c(){var t=Y;return{set:function(e,n){t[e]=n},get:function(e){return e in t?t[e]:b},remove:function(e){delete t[e]}}}function u(t){return t.split(O).reverse().join(O)}function s(t){var e;if(-1===t.indexOf("\\."))e=t.split(".");else{e=u(t).split(/\.(?!\\)/).reverse();for(var n=0,r=e.length;r>n;n++)e[n]=u(e[n].replace(/\.\\/g,"."))}return e}function f(t,e,n){for(var r=s(t),a=n;r.length;){var o=r.shift();if(r.length)a[o]=a[o]||{},a=a[o];else{if(!p(a))throw new Error("Cannot create property `"+o+"` on non-object value, path:`"+t+"`");a[o]=e}}return n}function l(t,e,n){if(n=n||!1,n===!0||-1===t.indexOf("."))return e[t];for(var r=s(t);r.length;){var a=r.shift();if(e=l(a,e,!0),"object"!=typeof e||void 0===e){r.length&&(e=void 0);break}}return e}function d(t,e,n){if(n=n||-1===t.indexOf("."))return e.hasOwnProperty(t);for(var r=s(t);r.length;){var a=r.shift(),o=e.hasOwnProperty(a);if(!o||!r.length)return o;if(e=l(a,e,!0),!p(e))return E}}function _(t,e){for(var n=s(t),r=e;n.length;){var a=n.shift();n.length?(r[a]=r[a]||{},r=r[a]):delete r[a]}return e}function g(t){var e=S;for(var n in t){e=E;break}return e}var v=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),h=n(2),y=h.extend,p=h.isPlainObject,w=h.noop,k=window,m="undefined"!=typeof k,b=null,O="",S=!0,E=!S,x="_placeholder",D=void 0;D="1.0.0";var A={localStorage:m&&!!k.localStorage&&a("localStorage"),sessionStorage:m&&!!k.sessionStorage&&a("sessionStorage")},K={type:"localStorage",key:O,tag:O,duration:0,until:0,async:!1},j=y({},K),P=function(){function t(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];r(this,t);var n=this;if(n.config=y({},j,e),!n.config.key)throw new Error("`key` is missing, please check the options passed in `nattyStorage` constructor.");n._storage=A[n.config.type]?i(n.config.type):c(),n._CHECK_KEY="nattyStorageCheck:"+n.config.key,n._DATA_KEY="nattyStorageData:"+n.config.key,n._placeholderUsed=E,n._createStamp=+new Date}return v(t,[{key:"_lazyInit",value:function(){var t=this;t._checkData=t._storage.get(t._CHECK_KEY),t._isNew=t._checkData===b,t._isNew||t.isOutdated()?t._storage.set(t._DATA_KEY,t._data={}):(t._data=t._storage.get(t._DATA_KEY),t._data===b&&t._storage.set(t._DATA_KEY,t._data={})),t._storage.set(t._CHECK_KEY,t._checkData={id:t.config.id,lastUpdate:t._createStamp,duration:t.config.duration,until:t.config.until})}},{key:"isOutdated",value:function(){var t=this;if(t.config.id&&t.config.id!==t._checkData.id)return S;var e=+new Date;return t._checkData.duration&&e-t._checkData.lastUpdate>t._checkData.duration?S:t._checkData.until&&e>t._checkData.until?S:E}},{key:"set",value:function(t,e){var n=this,r=arguments.length,a=function(a,o){try{n._data||n._lazyInit(),1===r?p(t)?n._data=t:(n._data[x]=t,n._placeholderUsed=S):f(t,e,n._data),n._storage.set(n._DATA_KEY,n._data),a()}catch(i){o(i)}};return n.config.async?new Promise(a):void a(w,o)}},{key:"get",value:function(t){var e=this,n=void 0,r=function(r,a){try{e._data||e._lazyInit(),n=t?l(t,e._data):e._placeholderUsed?e._data[x]:e._data,r(n)}catch(o){a(o)}};return e.config.async?new Promise(r):(r(w,o),n)}},{key:"has",value:function(t){var e=this,n=void 0,r=function(r,a){try{if(e._data||e._lazyInit(),e._placeholderUsed||g(e._data))n=e._data.hasOwnProperty(x)?{has:!0,value:e._data[x]}:{},r(n);else{if(!t)throw new Error("a `path` argument should be passed into the `has` method");n=d(t,e._data)?{has:!0,value:l(t,e._data)}:{},r(n)}}catch(o){a(o)}};return e.config.async?new Promise(r):(r(w,o),n)}},{key:"remove",value:function(t){var e=this,n=function(n,r){try{e._data||e._lazyInit(),t?(_(t,e._data),e._storage.set(e._DATA_KEY,e._data)):e.set({}),n()}catch(a){r(a)}};return e.config.async?new Promise(n):void n(w,o)}},{key:"destroy",value:function(){var t=this;t._storage.remove(t._CHECK_KEY),t._storage.remove(t._DATA_KEY)}},{key:"dump",value:function(){JSON&&console&&console.log(JSON.stringify(this._data,b,4))}}]),t}(),C=function(t){return new P(t)};C.version=D,C._variable=Y,C.support=A,C.setGlobal=function(t){j=y({},K,t)},C.getGlobal=function(t){return t?j[t]:j};var Y={};t.exports=C},function(t,e,n){"use strict";var r=null,a=function(t){return function(){for(var e=arguments,n=t(e[0],e[1]),r=2,a=e.length;a>r;r++)n=t(n,e[r]);return n}},o="object",i=function(t){return typeof t===o},c=function(t){return t!==r&&t===t.window},u=function(t){return t!==r&&i(t)&&!c(t)&&Object.getPrototypeOf(t)===Object.prototype},s=Array.isArray,f=function d(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];for(var n in e)e.hasOwnProperty(n)&&void 0!==e[n]&&(s(e[n])?t[n]=[].concat(e[n]):u(e[n])?t[n]=d({},e[n]):t[n]=e[n]);return t},l=function(){};t.exports={extend:a(f),noop:l,isPlainObject:u}}])}); |
{ | ||
"name": "natty-storage", | ||
"version": "1.0.0-rc5", | ||
"version": "1.0.0", | ||
"description": "", | ||
@@ -20,4 +20,3 @@ "repository": "https://github.com/Jias/natty-storage.git", | ||
"data", | ||
"database", | ||
"promise-api" | ||
"database" | ||
], | ||
@@ -24,0 +23,0 @@ "devDependencies": { |
@@ -10,5 +10,5 @@ # natty-storage | ||
* 以异步(`Promise`)方式使用`localStorage`和`sessionStorage`,不再阻塞,并优雅地捕获异常(如超出浏览器最大限制)。 | ||
* 支持以路径(`Path`)方式设置、获取和删除数据,相对于直接使用原生对象,大大减少了代码量。 | ||
* 封装了三种有效性(`validity`)判断,标记(`tag`)、有效期长(`duration`)、有效期至(`until`),不再重复编码。 | ||
* 支持异步方式使用`localStorage`和`sessionStorage`,避免阻塞,并优雅地捕获异常(如超出浏览器最大限制)。当然,同步方式使用依然是默认的使用方式。 | ||
* 支持以路径(`Path`)方式设置、获取和删除数据,相对于直接使用原生`localStorage/sessionStorage`对象,大大减少了代码量。 | ||
* 封装了三种有效性判断,标记(`id`)、有效期长(`duration`)、有效期至(`until`),不再重复编码。 | ||
* 隐身模式下,有些浏览器不支持`localStorage`,此时自动降级为`variable`模式。 | ||
@@ -19,3 +19,3 @@ * `gzip`后的只有`2.3K`。 | ||
## 创建缓存对象 | ||
## nattyStorage(options) | ||
@@ -26,5 +26,6 @@ 创建缓存对象的实例 | ||
let storage = nattyStorage({ | ||
async: true, // 是否以异步方式使用 | ||
type: 'localStorage', // 缓存方式 | ||
key: 'ChinaCity', // !!! 唯一必选的参数,用于内部存储 | ||
tag: 'v1.0', // 缓存的标记,用于判断是否有效 | ||
id: 'v1.0', // 缓存的标记,用于判断是否有效 | ||
duration: 1000*60*10, // 缓存的有效期长,以毫秒数指定 | ||
@@ -35,2 +36,41 @@ until: 1464759086797 // 缓存的到期时间,以具体日期时间的时间戳指定 | ||
## options | ||
#### `async`(可选):布尔值 | ||
是否开启异步方式使用,默认为`false`。如果开启,则`set/get/has/remove`四个方法的返回值都是`Promise`实例,可以调用`then`方法。 | ||
默认情况下,以同步方式使用 | ||
```js | ||
let storage = nattyStorage({ | ||
key: 'foo' | ||
}); | ||
// 设置值 | ||
try { | ||
storage.set('x', 'x'); | ||
} catch (error) { | ||
// 处理错误 | ||
} | ||
// 获取值 | ||
console.log(storage.get('x')); // => 'x' | ||
``` | ||
开启异步方式使用 | ||
```js | ||
let storage = nattyStorage({ | ||
async: true, // 开启异步方式使用 | ||
key: 'foo' | ||
}); | ||
// 设置值 | ||
storage.set('x', 'x').then(function(){ | ||
// 获取值 | ||
console.log(storage.get('x')); // => 'x' | ||
}).catch(function(error){ | ||
// 处理错误 | ||
}); | ||
``` | ||
#### `type`(可选):枚举值 | ||
@@ -46,7 +86,7 @@ | ||
#### `tag`(可选):字符串 | ||
#### `id`(可选):字符串 | ||
通过一个标记来判断缓存对象所存储的数据是否有效。`tag`不同则缓存失效。 | ||
通过一个标记来判断缓存对象所存储的数据是否有效。`id`不同则缓存失效。 | ||
> 通常tag的值是一个字符串标识,比如版本号。 | ||
> 通常`id`的值是一个字符串标识,比如版本号。 | ||
@@ -62,3 +102,3 @@ #### `duration`(可选):毫秒数 | ||
## 设置数据 | ||
## set() | ||
@@ -88,3 +128,3 @@ 设置数据包括添加新数据和修改已有的数据,都很方便。 | ||
## 获取数据 | ||
## get() | ||
@@ -111,4 +151,6 @@ 获取数据支持获取全部数据和以路径方式获取部分数据。 | ||
## 判断数据是否存在 | ||
## has() | ||
判断数据是否存在 | ||
```js | ||
@@ -136,3 +178,3 @@ // 根据指定的路径,判断数据是否存在 | ||
## 删除数据 | ||
## remove() | ||
@@ -149,3 +191,3 @@ 删除数据会同时删除指定的键和对应的值。 | ||
## 销毁实例 | ||
## destroy() | ||
@@ -155,3 +197,3 @@ 销毁缓存对象实例 | ||
```js | ||
storage.destory(); | ||
storage.destroy(); | ||
``` | ||
@@ -185,3 +227,3 @@ | ||
## 开发 | ||
## dev | ||
@@ -195,3 +237,3 @@ 把代码`clone`到本地,在根目录下执行: | ||
## 构建 | ||
## build | ||
@@ -198,0 +240,0 @@ ```shell |
"use strict"; | ||
const {extend, isPlainObject} = require('./util'); | ||
const {extend, isPlainObject, noop} = require('./util'); | ||
const win = window; | ||
@@ -45,3 +45,6 @@ const hasWindow = 'undefined' !== typeof win; | ||
// 有效期至, 时间戳 | ||
until: 0 | ||
until: 0, | ||
// 是否以异步方式使用set/get/has/remove | ||
async: false | ||
}; | ||
@@ -57,3 +60,3 @@ | ||
* // 验证是否有效,如果是首次创建该LS,则不执行验证 | ||
* tag: '1.0' | ||
* id: '1.0' | ||
* }) | ||
@@ -117,3 +120,3 @@ */ | ||
t._storage.set(t._CHECK_KEY, t._checkData = { | ||
tag: t.config.tag, | ||
id: t.config.id, | ||
lastUpdate: t._createStamp, | ||
@@ -131,3 +134,3 @@ duration: t.config.duration, | ||
let t = this; | ||
if (t.config.tag && t.config.tag !== t._checkData.tag) { | ||
if (t.config.id && t.config.id !== t._checkData.id) { | ||
return TRUE; | ||
@@ -164,5 +167,4 @@ } | ||
let argumentLength = arguments.length; | ||
// 同步到storage | ||
return new Promise(function(resolve, reject) { | ||
let todo = (resolve, reject) => { | ||
try { | ||
@@ -188,3 +190,9 @@ if (!t._data) { | ||
} | ||
}); | ||
} | ||
if (t.config.async) { | ||
return new Promise(todo); | ||
} else { | ||
todo(noop, throwError); | ||
} | ||
} | ||
@@ -203,5 +211,5 @@ | ||
let t = this; | ||
return new Promise(function (resolve, reject) { | ||
let data; | ||
let todo = function (resolve, reject) { | ||
try { | ||
let data; | ||
if (!t._data) { | ||
@@ -222,3 +230,10 @@ t._lazyInit(); | ||
} | ||
}); | ||
} | ||
if (t.config.async) { | ||
return new Promise(todo); | ||
} else { | ||
todo(noop, throwError); | ||
return data; | ||
} | ||
} | ||
@@ -233,6 +248,5 @@ | ||
let t = this; | ||
return new Promise(function (resolve, reject) { | ||
let result; | ||
let todo = function (resolve, reject) { | ||
try { | ||
let has; | ||
if (!t._data) { | ||
@@ -248,11 +262,14 @@ t._lazyInit(); | ||
resolve(hasValueByPath(path, t._data) ? { | ||
result = hasValueByPath(path, t._data) ? { | ||
has: true, | ||
value: getValueByPath(path, t._data) | ||
}: {}); | ||
}: {} | ||
resolve(result); | ||
} else { | ||
resolve(t._data.hasOwnProperty(PLACEHOLDER) ? { | ||
result = t._data.hasOwnProperty(PLACEHOLDER) ? { | ||
has: true, | ||
value: t._data[PLACEHOLDER] | ||
} : {}); | ||
} : {} | ||
resolve(result); | ||
} | ||
@@ -262,3 +279,10 @@ } catch (e) { | ||
} | ||
}); | ||
} | ||
if (t.config.async) { | ||
return new Promise(todo); | ||
} else { | ||
todo(noop, throwError); | ||
return result; | ||
} | ||
} | ||
@@ -272,3 +296,3 @@ | ||
let t = this; | ||
return new Promise(function (resolve, reject) { | ||
let todo = function (resolve, reject) { | ||
try { | ||
@@ -289,3 +313,9 @@ if (!t._data) { | ||
} | ||
}); | ||
} | ||
if (t.config.async) { | ||
return new Promise(todo); | ||
} else { | ||
todo(noop, throwError); | ||
} | ||
} | ||
@@ -317,2 +347,4 @@ | ||
/** | ||
@@ -336,2 +368,5 @@ * 执行全局配置 | ||
function throwError(e) { | ||
throw new Error(e); | ||
} | ||
@@ -338,0 +373,0 @@ function createStorage(storage) { |
@@ -69,5 +69,8 @@ const NULL = null; | ||
let noop = () => {}; | ||
module.exports = { | ||
extend: redo(extend), | ||
noop, | ||
isPlainObject | ||
}; |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
53153
1117
1
230