UNPKG

tsp-component

Version:

提供多端和react版本的UI组件

68 lines (67 loc) 2.23 kB
var Cache = (function () { function Cache(dbName, tableName) { this.dbName = dbName; this.tableName = tableName; } Cache.prototype.open = function (method) { var _this = this; var requests = indexedDB.open(this.dbName, 1); requests.onupgradeneeded = function (e) { var db = e.target['result']; if (!db.objectStoreNames.contains(_this.tableName)) { db.createObjectStore(_this.tableName, { keyPath: 'api' }); } }; requests.onsuccess = function (e) { if (method) { var db = e.target['result']; method(db); } }; requests.onerror = function () { console.log('indexeddb onerror'); }; requests.onblocked = function () { console.log('indexeddb onblocked'); }; }; Cache.prototype.add = function (data) { var _this = this; this.open(function (db) { db.transaction([_this.tableName], 'readwrite') .objectStore(_this.tableName) .add(data) .onsuccess = function (e) { }; }); }; Cache.prototype.put = function (data) { var _this = this; this.open(function (db) { db.transaction([_this.tableName], 'readwrite') .objectStore(_this.tableName) .put(data) .onsuccess = function (e) { }; }); }; Cache.prototype.read = function (option) { var _this = this; this.open(function (db) { db.transaction([_this.tableName], 'readonly') .objectStore(_this.tableName) .get(option.key) .onsuccess = function (e) { if (option.onsuccess) { var result = { value: '' }; if (e.target.result) { result = e.target.result; } option.onsuccess(result); } }; }); }; return Cache; }()); export default Cache;