yux-storage
Advanced tools
Comparing version 1.1.3 to 1.1.4
{ | ||
"name": "yux-storage", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "yux-storage 是一个基于 HTML5 IndexedDB 封装的 Web 本地数据离线存储库", | ||
@@ -5,0 +5,0 @@ "main": "yux-storage.js", |
@@ -9,3 +9,3 @@ # yux-storage | ||
1. 支持回调和 [Promise](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise) 两种方式,各凭所愿。 | ||
1. 无任何依赖,非常轻量,不到 200 行左右的源码,压缩后更小。 | ||
1. 无任何依赖,非常轻量,100 行左右的源码,压缩后更小。 | ||
@@ -233,3 +233,3 @@ 以下是继承 IndexedDB 的特点 | ||
> !小心使用,其实就是遍历,然后执行 REMOVEITEM | ||
> !小心使用,会删除所有数据 | ||
@@ -236,0 +236,0 @@ *示例* |
@@ -12,3 +12,3 @@ /** | ||
constructor() { | ||
this.ready() | ||
this.ready(); | ||
} | ||
@@ -19,10 +19,9 @@ | ||
if (this.db) { | ||
resolve(this) | ||
resolve(this); | ||
} else { | ||
this.objectStoreName = 'yux-store'; | ||
const request = window.indexedDB.open('yux-project', 1); | ||
request.onsuccess = (event) => { | ||
this.db = event.target.result; | ||
resolve(this) | ||
resolve(this); | ||
}; | ||
@@ -42,26 +41,11 @@ request.onupgradeneeded = (event) => { | ||
setItem(key, value, callback) { | ||
init(request, callback) { | ||
return new Promise((resolve, reject) => { | ||
const fail = event => { | ||
const success = value => { | ||
if (callback && typeof callback === 'function') { | ||
callback(event); | ||
callback(false, value); | ||
} | ||
reject(event); | ||
resolve(value); | ||
} | ||
return this.ready().then(() => { | ||
const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).put(value, key); | ||
request.onsuccess = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(false, value); | ||
} | ||
resolve(value); | ||
}; | ||
request.onerror = fail; | ||
}).catch(fail) | ||
}) | ||
} | ||
getItem(key, callback) { | ||
return new Promise((resolve, reject) => { | ||
const fail = event => { | ||
const error = event => { | ||
if (callback && typeof callback === 'function') { | ||
@@ -73,106 +57,53 @@ callback(event); | ||
return this.ready().then(() => { | ||
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).get(key); | ||
request.onsuccess = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(false, request.result); | ||
} | ||
resolve(request.result); | ||
}; | ||
request.onerror = fail; | ||
}).catch(fail) | ||
request(success, error); | ||
}).catch(error) | ||
}) | ||
} | ||
setItem(key, value, callback) { | ||
return this.init((success, error)=>{ | ||
const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).put(value, key); | ||
request.onsuccess = () => success(value); | ||
request.onerror = error; | ||
}, callback) | ||
} | ||
getItem(key, callback) { | ||
return this.init((success, error)=>{ | ||
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).get(key); | ||
request.onsuccess = () => success(request.result); | ||
request.onerror = error; | ||
}, callback) | ||
} | ||
removeItem(key, callback) { | ||
return new Promise((resolve, reject) => { | ||
const fail = event => { | ||
if (callback && typeof callback === 'function') { | ||
callback(event); | ||
} | ||
reject(event); | ||
} | ||
return this.ready().then(() => { | ||
const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).delete(key); | ||
request.onsuccess = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(false, request.result); | ||
} | ||
resolve(); | ||
}; | ||
request.onerror = fail; | ||
}).catch(fail) | ||
}) | ||
return this.init((success, error)=>{ | ||
const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).delete(key); | ||
request.onsuccess = () => success(key); | ||
request.onerror = error; | ||
}, callback) | ||
} | ||
key(index, callback) { | ||
return new Promise((resolve, reject) => { | ||
const fail = event => { | ||
if (callback && typeof callback === 'function') { | ||
callback(event); | ||
} | ||
reject(event); | ||
} | ||
return this.ready().then(() => { | ||
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).getAllKeys(); | ||
request.onsuccess = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(false, request.result[index]); | ||
} | ||
resolve(request.result[index]); | ||
}; | ||
request.onerror = fail; | ||
}).catch(fail) | ||
}) | ||
return this.init((success, error)=>{ | ||
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).getAllKeys(); | ||
request.onsuccess = () => success(request.result[index]); | ||
request.onerror = error; | ||
}, callback) | ||
} | ||
keys(callback) { | ||
return new Promise((resolve, reject) => { | ||
const fail = event => { | ||
if (callback && typeof callback === 'function') { | ||
callback(event); | ||
} | ||
reject(event); | ||
} | ||
return this.ready().then(() => { | ||
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).getAllKeys(); | ||
request.onsuccess = (event) => { | ||
if (callback && typeof callback === 'function') { | ||
callback(false, request.result || []); | ||
} | ||
resolve(request.result || []); | ||
}; | ||
request.onerror = fail; | ||
}).catch(fail) | ||
}) | ||
return this.init((success, error)=>{ | ||
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).getAllKeys(); | ||
request.onsuccess = () => success(request.result); | ||
request.onerror = error; | ||
}, callback) | ||
} | ||
clear(callback) { | ||
return new Promise((resolve, reject) => { | ||
const fail = event => { | ||
if (callback && typeof callback === 'function') { | ||
callback(event); | ||
} | ||
reject(event); | ||
} | ||
return this.ready().then(() => { | ||
const objectStore = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName); | ||
const request = objectStore.getAllKeys(); | ||
request.onsuccess = (event) => { | ||
const p = request.result.map(key => { | ||
return new Promise((resolve, reject) => { | ||
const request = objectStore.delete(key); | ||
request.onsuccess = (event) => { | ||
resolve(); | ||
}; | ||
}) | ||
}) | ||
Promise.all(p).then(() => { | ||
if (callback && typeof callback === 'function') { | ||
callback(false); | ||
} | ||
resolve(); | ||
}).catch(fail); | ||
}; | ||
}).catch(fail) | ||
}) | ||
return this.init((success, error)=>{ | ||
const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).clear(); | ||
request.onsuccess = () => success(null); | ||
request.onerror = error; | ||
}, callback) | ||
} | ||
@@ -179,0 +110,0 @@ } |
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
11937
100