Added window declaration, added dbName to construcotr
This commit is contained in:
parent
df4947d22a
commit
7dc7c0ca69
@ -1,13 +1,13 @@
|
|||||||
export default class EventEmitter {
|
export default class EventEmitter {
|
||||||
private subscribers: Array<Function> = []
|
private subscribers: Array<Function> = [];
|
||||||
|
|
||||||
subscribe (subscriber) {
|
subscribe (subscriber) {
|
||||||
this.subscribers.push(subscriber)
|
this.subscribers.push(subscriber);
|
||||||
}
|
}
|
||||||
|
|
||||||
notify () {
|
notify () {
|
||||||
this.subscribers.forEach((subscriber) => {
|
this.subscribers.forEach((subscriber) => {
|
||||||
subscriber()
|
subscriber();
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
19
src/Store.ts
19
src/Store.ts
@ -18,17 +18,18 @@ export default class Store {
|
|||||||
/** @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, dbName?: string) {
|
||||||
this.currentStoreCount = Store.storeCount;
|
this.currentStoreCount = Store.storeCount;
|
||||||
this.chunkLength = chunkLength;
|
this.chunkLength = chunkLength;
|
||||||
|
|
||||||
|
// Dependency with EventEmitter because of constructor spec
|
||||||
this.eventEmitter = new EventEmitter();
|
this.eventEmitter = new EventEmitter();
|
||||||
|
|
||||||
// Open indexedDB (torrent_chunks) and
|
// Open indexedDB (torrent_chunks) and
|
||||||
// create objectStore (torrent_chunk_store)
|
// create objectStore (torrent_chunk_store)
|
||||||
this.idb = window.indexedDB || {}; // || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
|
this.idb = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
|
||||||
|
|
||||||
let request = this.idb.open(this.getDatabaseName(), 1);
|
let request = this.idb.open(this.getDatabaseName(dbName), 1);
|
||||||
|
|
||||||
Store.storeCount++;
|
Store.storeCount++;
|
||||||
|
|
||||||
@ -52,14 +53,13 @@ export default class Store {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO define cb parameters
|
public get (index: number, options, cb: (err: Error, ev?: Buffer) => void) {
|
||||||
public get (index: number, options, cb: Function) {
|
|
||||||
if (typeof options === 'function') {
|
if (typeof options === 'function') {
|
||||||
return this.get(index, null, options)
|
return this.get(index, null, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof cb !== 'function') {
|
if (typeof cb !== 'function') {
|
||||||
cb = (err: Error, ev?: Event): void => {}
|
cb = (err: Error, ev?: Buffer): void => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.executeFn(() => {
|
this.executeFn(() => {
|
||||||
@ -88,7 +88,6 @@ export default class Store {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
||||||
if (typeof cb !== 'function') {
|
if (typeof cb !== 'function') {
|
||||||
cb = (err: Error, ev?: Event): void => {}
|
cb = (err: Error, ev?: Event): void => {}
|
||||||
@ -121,8 +120,6 @@ export default class Store {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public destroy (cb) {
|
public destroy (cb) {
|
||||||
// Currently same implementation as close
|
|
||||||
// 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);
|
||||||
@ -138,8 +135,8 @@ export default class Store {
|
|||||||
/**
|
/**
|
||||||
* Get database name based on storeCount
|
* Get database name based on storeCount
|
||||||
*/
|
*/
|
||||||
private getDatabaseName () {
|
private getDatabaseName (dbName?: string) {
|
||||||
const databaseName = 'torrent_chunk_store';
|
const databaseName = dbName || 'torrent_chunk_store';
|
||||||
|
|
||||||
if (this.currentStoreCount === 0) {
|
if (this.currentStoreCount === 0) {
|
||||||
return databaseName
|
return databaseName
|
||||||
|
5
src/index.d.ts
vendored
Normal file
5
src/index.d.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
interface Window {
|
||||||
|
mozIndexedDB: IDBFactory;
|
||||||
|
webkitIndexedDB: IDBFactory;
|
||||||
|
msIndexedDB: IDBFactory;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user