bug fix: IDB searchData

This commit is contained in:
sairajzero 2020-01-08 14:39:01 +05:30
parent 782fbdc665
commit 607a53f1be

View File

@ -8343,26 +8343,34 @@ Bitcoin.Util = {
}); });
}, },
searchData: function (obsName, patternEval, dbName = this.dbName) { searchData: function (obsName, options = {}, dbName = this.dbName) {
return new Promise((resolve, reject) => { options.lowerKey = options.atKey || options.lowerKey || 0
this.openDB(dbName).then(db => { options.upperKey = options.atKey || options.upperKey || false
var obs = db.transaction(obsName, "readonly").objectStore(obsName); options.patternEval = options.patternEval || ((k,v) => {return true})
var filteredResult = {} options.lastOnly = options.lastOnly || false
let curReq = obs.openCursor(); return new Promise((resolve, reject) => {
curReq.onsuccess = (evt) => { this.openDB(dbName).then(db => {
var cursor = evt.target.result; var obs = db.transaction(obsName, "readonly").objectStore(obsName);
if (cursor) { var filteredResult = {}
if (patternEval(cursor.primaryKey, cursor.value)) let curReq = obs.openCursor(
filteredResult[cursor.primaryKey] = cursor.value; options.upperKey ? IDBKeyRange.bound(options.lowerKey,options.upperKey) : IDBKeyRange.lowerBound(options.lowerKey),
cursor.continue(); options.lastOnly ? "prev":"next" );
} else curReq.onsuccess = (evt) => {
resolve(filteredResult); var cursor = evt.target.result;
} if (cursor) {
curReq.onerror = (evt) => reject( if (options.patternEval(cursor.primaryKey, cursor.value)){
`Search unsuccessful [${evt.target.error.name}] ${evt.target.error.message}`); filteredResult[cursor.primaryKey] = cursor.value;
db.close(); options.lastOnly ? resolve(filteredResult) : cursor.continue();
}).catch(error => reject(error)); }else
}); cursor.continue();
} else
resolve(filteredResult);
}
curReq.onerror = (evt) => reject(
`Search unsuccessful [${evt.target.error.name}] ${evt.target.error.message}`);
db.close();
}).catch(error => reject(error));
});
} }
} }
</script> </script>