Remove self and fix warnings
This commit is contained in:
parent
f43e26b1b9
commit
df4947d22a
41
src/Store.ts
41
src/Store.ts
|
@ -12,14 +12,14 @@ export default class Store {
|
|||
private eventEmitter: EventEmitter;
|
||||
|
||||
// Use to count number of store objects created. Determine database name
|
||||
private static storeCount: number = 0
|
||||
private readonly currentStoreCount: number = 0
|
||||
private static storeCount: number = 0;
|
||||
private readonly currentStoreCount: number = 0;
|
||||
|
||||
/** @var {string} osName - Object store name */
|
||||
private readonly osName: string = 'chunks'
|
||||
private readonly osName: string = 'chunks';
|
||||
|
||||
constructor (chunkLength: number) {
|
||||
this.currentStoreCount = Store.storeCount
|
||||
this.currentStoreCount = Store.storeCount;
|
||||
this.chunkLength = chunkLength;
|
||||
|
||||
this.eventEmitter = new EventEmitter();
|
||||
|
@ -30,18 +30,18 @@ export default class Store {
|
|||
|
||||
let request = this.idb.open(this.getDatabaseName(), 1);
|
||||
|
||||
Store.storeCount++
|
||||
Store.storeCount++;
|
||||
|
||||
request.onerror = (ev) => {
|
||||
// Don't forget to handle errors!
|
||||
}
|
||||
};
|
||||
|
||||
request.onsuccess = (ev: any) => {
|
||||
this.db = ev.target.result;
|
||||
|
||||
// EventEmmitter.emit('dbOpened')
|
||||
this.eventEmitter.notify()
|
||||
}
|
||||
};
|
||||
|
||||
request.onupgradeneeded = (ev: any) => {
|
||||
// Save the IDBDatabase interface
|
||||
|
@ -54,7 +54,6 @@ export default class Store {
|
|||
|
||||
// TODO define cb parameters
|
||||
public get (index: number, options, cb: Function) {
|
||||
let self = this
|
||||
if (typeof options === 'function') {
|
||||
return this.get(index, null, options)
|
||||
}
|
||||
|
@ -69,20 +68,20 @@ export default class Store {
|
|||
|
||||
transaction.onerror = (ev) => {
|
||||
// Don't forget to handle errors!
|
||||
}
|
||||
};
|
||||
|
||||
this.resolveRequest(request, (err, ev) => {
|
||||
let target = <IDBRequest> ev.target
|
||||
let target = <IDBRequest> ev.target;
|
||||
if (target.result === undefined) {
|
||||
// cb(null, new Buffer(0))
|
||||
this.nextTick(cb, new Error('Try to access non-existent index'))
|
||||
} else {
|
||||
const buffer = new Buffer(target.result);
|
||||
if (!options) {
|
||||
return self.nextTick(cb, null, buffer)
|
||||
return this.nextTick(cb, null, buffer)
|
||||
}
|
||||
const offset = options.offset || 0
|
||||
const length = options.length || (target.result.length - offset)
|
||||
const offset = options.offset || 0;
|
||||
const length = options.length || (target.result.length - offset);
|
||||
cb(null, buffer.slice(offset, offset + length))
|
||||
}
|
||||
})
|
||||
|
@ -91,8 +90,6 @@ export default class Store {
|
|||
|
||||
// TODO define cb parameters
|
||||
public put (index: number, chunkBuffer: Buffer, cb: (err: Error, ev?: Event) => void) {
|
||||
let self = this
|
||||
|
||||
if (typeof cb !== 'function') {
|
||||
cb = (err: Error, ev?: Event): void => {}
|
||||
}
|
||||
|
@ -102,13 +99,13 @@ export default class Store {
|
|||
}
|
||||
|
||||
this.executeFn(() => {
|
||||
let transaction = self.db.transaction('torrent_chunk_store', 'readwrite')
|
||||
let transaction = this.db.transaction(this.osName, 'readwrite');
|
||||
|
||||
transaction.onerror = (ev) => {
|
||||
// Don't forget to handle errors!
|
||||
}
|
||||
};
|
||||
|
||||
let request = transaction.objectStore(this.osName).put(chunkBuffer, index)
|
||||
let request = transaction.objectStore(this.osName).put(chunkBuffer, index);
|
||||
|
||||
this.resolveRequest(request, (err, ev) => {
|
||||
cb(err, ev)
|
||||
|
@ -118,7 +115,7 @@ export default class Store {
|
|||
|
||||
public close (cb) {
|
||||
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.nextTick(cb, null, null);
|
||||
}
|
||||
|
@ -128,7 +125,7 @@ export default class Store {
|
|||
// For indexeddb would be different:
|
||||
// -- Close would empty database
|
||||
// -- Destroy should delete database
|
||||
this.close(cb)
|
||||
this.close(cb);
|
||||
this.idb.deleteDatabase(this.getDatabaseName())
|
||||
}
|
||||
|
||||
|
@ -142,7 +139,7 @@ export default class Store {
|
|||
* Get database name based on storeCount
|
||||
*/
|
||||
private getDatabaseName () {
|
||||
const databaseName = 'torrent_chunk_store'
|
||||
const databaseName = 'torrent_chunk_store';
|
||||
|
||||
if (this.currentStoreCount === 0) {
|
||||
return databaseName
|
||||
|
@ -159,7 +156,7 @@ export default class Store {
|
|||
private resolveRequest (request: IDBRequest, cb?: (err: Error, ev?: Event) => void) {
|
||||
request.onsuccess = (ev) => {
|
||||
cb(null, ev)
|
||||
}
|
||||
};
|
||||
|
||||
request.onerror = () => {
|
||||
cb(request.error)
|
||||
|
|
Loading…
Reference in New Issue