Skip to content

Commit dd38985

Browse files
refactor(IDBStore): remove use promise in add() method, remove onerror and change onsuccess handler in the index() method.
1 parent 55494bf commit dd38985

File tree

1 file changed

+19
-47
lines changed

1 file changed

+19
-47
lines changed

src/lib/idb-store.class.ts

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// RxJS.
2-
import { of, Subscription } from 'rxjs';
2+
import { of } from 'rxjs';
33

44
// Class.
55
import { IDBData } from './idb-data.class';
66

77
// Type.
8-
import { IDBStoreParameters } from './type/idb-store-parameters.type';
9-
import { IDBRequestTransaction } from './type/idb-request-transaction.type';
108
import { IDBRequestOnSuccess } from './type/idb-request-on-success.type';
9+
import { IDBRequestTransaction } from './type/idb-request-transaction.type';
10+
import { IDBStoreParameters } from './type/idb-store-parameters.type';
1111

1212
// Interface.
1313
import { IDBStoreInterface } from './interface/idb-store.interface';
@@ -90,25 +90,20 @@ export class IDBStore<
9090
storeNames: StoreNames | StoreNames[] = this.#database.connection.storeNames,
9191
mode: IDBTransactionMode = "readwrite"
9292
): this {
93-
this.#promise((resolve, reject, subscription) => (
94-
subscription.add(
95-
(Array.isArray(value) ? of(...value) : of(value)).subscribe({
96-
next: value => this.#add(
97-
storeName,
98-
value as any,
99-
key,
100-
onsuccess,
101-
onerror,
102-
transaction,
103-
storeNames,
104-
mode
105-
),
106-
complete: (resolve(subscription), complete),
107-
error: err => (resolve(subscription), reject(err), typeof error === 'function' && error(err), err)
108-
}),
93+
(Array.isArray(value) ? of(...value) : of(value)).subscribe({
94+
next: value => this.#add(
95+
storeName,
96+
value as any,
97+
key,
98+
onsuccess,
99+
onerror,
100+
transaction,
101+
storeNames,
102+
mode
109103
),
110-
subscription
111-
));
104+
complete,
105+
error
106+
})
112107
return this;
113108
}
114109

@@ -362,8 +357,7 @@ export class IDBStore<
362357
name: string,
363358

364359
// Request.
365-
onsuccess?: IDBRequestOnSuccess<any, any> | null,
366-
onerror?: (this: IDBRequest<any>, ev: Event) => any,
360+
onsuccess?: (index: IDBIndex) => any,
367361

368362
// Transaction.
369363
transaction?: IDBRequestTransaction,
@@ -374,9 +368,7 @@ export class IDBStore<
374368
): this {
375369
this.#database.objectStore(
376370
storeName,
377-
store => {
378-
const index = store.index(name);
379-
},
371+
store => typeof onsuccess === 'function' && onsuccess(store.index(name)),
380372
transaction?.oncomplete,
381373
transaction?.onabort,
382374
transaction?.onerror,
@@ -404,7 +396,7 @@ export class IDBStore<
404396
direction?: IDBCursorDirection,
405397

406398
// Request.
407-
onsuccess?: IDBRequestOnSuccess<any, IDBCursorWithValue | null> | null,
399+
onsuccess?: IDBRequestOnSuccess<IDBCursorWithValue, IDBCursorWithValue | null> | null,
408400
onerror?: (this: IDBRequest<IDBCursorWithValue | null>, ev: Event) => any | null,
409401

410402
// Transaction.
@@ -471,7 +463,6 @@ export class IDBStore<
471463
);
472464
return this;
473465
}
474-
475466

476467
/**
477468
*
@@ -520,24 +511,5 @@ export class IDBStore<
520511
);
521512
return this;
522513
}
523-
524-
/**
525-
*
526-
* @param executor
527-
* @returns
528-
*/
529-
#promise(
530-
executor: (
531-
resolve: (value: Subscription | PromiseLike<Subscription>) => void,
532-
reject: (reason?: any) => void,
533-
subscription: Subscription
534-
) => Subscription
535-
): this {
536-
new Promise<Subscription>((resolve, reject) => executor(resolve, reject, new Subscription()))
537-
.then(subscription => subscription.unsubscribe())
538-
.catch(() => { })
539-
.finally();
540-
return this;
541-
}
542514
}
543515

0 commit comments

Comments
 (0)