Skip to content

Commit

Permalink
fix: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvia1106 committed Nov 7, 2019
1 parent 03b2403 commit ef114ae
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 345 deletions.
4 changes: 4 additions & 0 deletions build/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ export declare class CustomDB {
deleteItemsInRange(tableIndexRanges: TableIndexRange[]): Promise<void>;
}
export declare function deleteDB(dbName: string): Promise<void>;
export declare function getItemFromDB(dbName: string, tableName: string, primaryKeyValue: any): Promise<import("./interface").ItemInTable | null>;
export declare function getItemsInRangeFromDB(dbName: string, tableIndexRange: TableIndexRange): Promise<any[]>;
declare const _default: {
idbIsSupported: typeof idbIsSupported;
CustomDB: typeof CustomDB;
deleteDB: typeof deleteDB;
getItemFromDB: typeof getItemFromDB;
getItemsInRangeFromDB: typeof getItemsInRangeFromDB;
};
export default _default;
1 change: 1 addition & 0 deletions build/lib/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ export declare const ParamCheckerEnum: {
export declare function isNonNegativeInteger(x: any): boolean;
export declare function optionWithBackup(param: any, backup: any): any;
export declare function paramChecker(param: any, checker: ParamChecker, paramName: string, optional: boolean): void;
export declare function deduplicateList(list: any[]): any;
export {};
12 changes: 6 additions & 6 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
{
tableName: 'table1',
item: {
key1: 'xixi',
value1: new Date().getSeconds()
key1: 'xixix',
value1: 1
},
itemDuration: 1000
},
Expand Down Expand Up @@ -142,15 +142,15 @@
}
function deleteItems() {
db.deleteItemsInRange([
{
tableName: 'table3'
},
{
tableName: 'table1',
indexRange: {
indexName: 'key1',
indexName: 'key',
lowerIndex: 'key6'
}
},
{
tableName: 'table3'
}
])
.then(function(ob) {
Expand Down
224 changes: 72 additions & 152 deletions demo/js/idb-managed.js

Large diffs are not rendered by default.

152 changes: 0 additions & 152 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@
},
"homepage": "https://github.com/sylvia1106/idb-managed#readme",
"devDependencies": {
"@babel/cli": "^7.7.0",
"@babel/plugin-proposal-class-properties": "^7.7.0",
"@babel/plugin-transform-runtime": "^7.6.2",
"@babel/preset-env": "^7.7.0",
"@babel/preset-typescript": "^7.7.0",
"@babel/runtime": "^7.7.0",
"@babel/runtime-corejs2": "^7.7.1",
"@babel/runtime-corejs3": "^7.7.0",
"@types/jest": "^24.0.21",
"babel-loader": "^8.0.6",
Expand All @@ -57,6 +53,7 @@
"iOS >= 9"
],
"files": [
"/build"
"/build",
"/src"
]
}
22 changes: 8 additions & 14 deletions src/db_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
// @ts-ignore
import { IDB } from './lib/idb';
import { deduplicateList } from './lib/utils'
import {
IndexRange,
ItemConfig,
Expand Down Expand Up @@ -269,15 +270,14 @@ async function atomicTrans(transaction: any, db: any, tryStatement: Function) {
try {
await tryStatement();
} catch (transError) {
// Abort transaction manually to keep it atomic.
try {
transaction.abort();
transaction.abort()
} catch (e) {
// Do nothing if transaction abort failed.
}
try {
// Catch the Promise error caused by transaction abortion
await transaction.done;
await transaction.complete;
} catch (e) {
throw transError;
}
Expand All @@ -290,9 +290,7 @@ async function deleteItemsFromDB(db: any, tableIndexRanges: TableIndexRange[]) {
const validRanges = tableIndexRanges.filter(indexRange => {
return db.objectStoreNames.contains(indexRange.tableName);
});
const dedupTableNameList: string[] = Array.from(
new Set(validRanges.map(tableIndexRange => tableIndexRange.tableName))
);
const dedupTableNameList: string[] = deduplicateList(validRanges.map(tableIndexRange => tableIndexRange.tableName))
const deleteItemsTrans = db.transaction(dedupTableNameList, 'readwrite');
await atomicTrans(deleteItemsTrans, db, async () => {
for (const tableIndexRange of validRanges) {
Expand All @@ -311,14 +309,11 @@ async function deleteItemsFromDB(db: any, tableIndexRanges: TableIndexRange[]) {
}
}
}
await deleteItemsTrans.done;
});
}

export async function addItems(dbInfo: DB, items: ItemConfig[]) {
const dedupTableNameList: string[] = Array.from(
new Set(items.map(item => item.tableName))
);
const dedupTableNameList: string[] = deduplicateList(items.map(item => item.tableName));
await deleteItems(
dbInfo.name,
dedupTableNameList.map(tableName => {
Expand All @@ -335,11 +330,10 @@ export async function addItems(dbInfo: DB, items: ItemConfig[]) {
const db = await createDB(dbInfo);
const addItemsTrans = db.transaction(dedupTableNameList, 'readwrite');
await atomicTrans(addItemsTrans, db, async () => {
items.forEach(item => {
for (const item of items) {
const table = addItemsTrans.objectStore(item.tableName);
table.put(itemWrapper(item));
});
await addItemsTrans.done;
await table.put(itemWrapper(item));
}
});
}

Expand Down
Loading

0 comments on commit ef114ae

Please sign in to comment.