Remove self and fix warnings

This commit is contained in:
Damjan Bursac 2019-05-16 08:53:40 +02:00
parent f43e26b1b9
commit df4947d22a
1 changed files with 19 additions and 22 deletions

View File

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