Added executeFn and resolveRequest functions

This commit is contained in:
Damjan Bursac 2019-05-14 10:08:46 +02:00
parent f4bc8eda96
commit 930d35d3b1
1 changed files with 47 additions and 48 deletions

View File

@ -29,58 +29,50 @@ export default class Store {
Store.storeCount++ Store.storeCount++
request.onerror = (event) => { request.onerror = (ev) => {
// Don't forget to handle errors! // Don't forget to handle errors!
} }
request.onsuccess = (event: any) => { request.onsuccess = (ev: any) => {
this.db = event.target.result; this.db = ev.target.result;
// EventEmmitter.emit('dbOpened') // EventEmmitter.emit('dbOpened')
this.eventEmitter.notify() this.eventEmitter.notify()
} }
request.onupgradeneeded = (event: any) => { request.onupgradeneeded = (ev: any) => {
// Save the IDBDatabase interface // Save the IDBDatabase interface
this.db = event.target.result; this.db = ev.target.result;
// Create an objectStore for this database // Create an objectStore for this database
this.db.createObjectStore('torrent_chunk_store'); this.db.createObjectStore('torrent_chunk_store');
}; };
} }
get (index: number, options, cb) { // TODO define cb parameters
public get (index: number, options, cb: Function) {
let self = this let self = this
if (typeof options === 'function') { if (typeof options === 'function') {
return this.get(index, null, options) return this.get(index, null, options)
} }
if (!cb) { if (typeof cb !== 'function') {
cb = () => {} cb = (err: Error, ev?: Event): void => {}
} }
if (!this.db) { this.executeFn(() => {
// Execute listener
this.eventEmitter.subscribe(_get.bind(this, index))
} else {
return _get(index)
}
function _get (index) {
// return new Promise(function (resolve, reject) {
let transaction = self.db.transaction('torrent_chunk_store', 'readonly'); let transaction = self.db.transaction('torrent_chunk_store', 'readonly');
let request = transaction.objectStore('torrent_chunk_store').get(index); let request = transaction.objectStore('torrent_chunk_store').get(index);
transaction.onerror = (event) => { transaction.onerror = (ev) => {
// Don't forget to handle errors! // Don't forget to handle errors!
} }
request.onsuccess = (event) => { this.resolveRequest(request, (err, ev) => {
// resolve(event.target) let target = <IDBRequest> ev.target
let target = <IDBRequest> event.target
if (target.result === undefined) { if (target.result === undefined) {
// cb(null, new Buffer(0)) // cb(null, new Buffer(0))
this.nextTick(cb, new Error('Try to access non existen index')) this.nextTick(cb, new Error('Try to access non-existent index'))
} else { } else {
const buffer = new Buffer(target.result); const buffer = new Buffer(target.result);
if (!options) { if (!options) {
@ -90,56 +82,45 @@ export default class Store {
const length = options.length || (target.result.length - offset) const length = options.length || (target.result.length - offset)
cb(null, buffer.slice(offset, offset + length)) cb(null, buffer.slice(offset, offset + length))
} }
} })
// }) })
}
} }
put (index: number, chunkBuffer: Buffer, cb: Function) { // TODO define cb parameters
public put (index: number, chunkBuffer: Buffer, cb: (err: Error, ev?: Event) => void) {
let self = this let self = this
if (!cb) { if (typeof cb !== 'function') {
cb = () => {} cb = (err: Error, ev?: Event): void => {}
} }
if (chunkBuffer.length !== this.chunkLength) { if (chunkBuffer.length !== this.chunkLength) {
return this.nextTick(cb, new Error('Chunk length must be: ' + this.chunkLength)) return this.nextTick(cb, new Error('Chunk length must be: ' + this.chunkLength))
} }
if (!this.db) { this.executeFn(() => {
// Execute listener
this.eventEmitter.subscribe(_put.bind(this, index, chunkBuffer))
} else {
_put()
}
function _put () {
let transaction = self.db.transaction('torrent_chunk_store', 'readwrite') let transaction = self.db.transaction('torrent_chunk_store', 'readwrite')
transaction.onerror = (event) => { transaction.onerror = (ev) => {
// Don't forget to handle errors! // Don't forget to handle errors!
} }
let request = transaction.objectStore('torrent_chunk_store').put(chunkBuffer, index) let request = transaction.objectStore('torrent_chunk_store').put(chunkBuffer, index)
request.onsuccess = (event) => { this.resolveRequest(request, (err, ev) => {
cb(null, event) cb(err, ev)
} })
})
request.onerror = (err) => {
cb(err)
}
}
} }
close (cb) { public close (cb) {
if (this.closed) return this.nextTick(cb, new Error('Storage is closed')); if (this.closed) return this.nextTick(cb, new Error('Storage is closed'));
if (!this.db) return this.nextTick(cb, undefined) if (!this.db) return this.nextTick(cb, undefined)
this.closed = true; this.closed = true;
this.nextTick(cb, null, null); this.nextTick(cb, null, null);
} }
destroy (cb) { public destroy (cb) {
// Currently same implementation as close // Currently same implementation as close
// For indexeddb would be different: // For indexeddb would be different:
// -- Close would empty database // -- Close would empty database
@ -148,7 +129,7 @@ export default class Store {
this.idb.deleteDatabase(this.getDatabaseName()) this.idb.deleteDatabase(this.getDatabaseName())
} }
nextTick (cb, err, val?) { private nextTick (cb, err, val?) {
setTimeout(function () { setTimeout(function () {
if (cb) cb(err, val) if (cb) cb(err, val)
}, 0) }, 0)
@ -166,4 +147,22 @@ export default class Store {
return databaseName + '_' + this.currentStoreCount return databaseName + '_' + this.currentStoreCount
} }
private resolveRequest (request: IDBRequest, cb?: (err: Error, ev?: Event) => void) {
request.onsuccess = (ev) => {
cb(null, ev)
}
request.onerror = () => {
cb(request.error)
}
}
private executeFn (cb) {
if (!this.db) {
this.eventEmitter.subscribe(cb)
} else {
cb()
}
}
} }