Skip to content

Commit

Permalink
refactored getMaxKey()
Browse files Browse the repository at this point in the history
  • Loading branch information
dfahlander committed Jan 31, 2018
1 parent f5c2cf4 commit 4dd65d3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
8 changes: 2 additions & 6 deletions src/dexie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Promise from './helpers/promise';
import { extend, override } from './functions/utils';
import Events from './helpers/Events';
import { maxString } from './globals/constants';
import { getMaxKey } from './functions/quirks';

export interface DbReadyState {
dbOpenError: any;
Expand Down Expand Up @@ -174,12 +175,7 @@ export class Dexie implements IDexie {
console.warn(`Upgrade '${this.name}' blocked by other connection holding version ${ev.oldVersion / 10}`);
});

this._maxKey = [[]];
try {
options.IDBKeyRange.only([[]]);
} catch (e) {
this._maxKey = maxString;
}
this._maxKey = getMaxKey(options.IDBKeyRange);

// Call each addon:
addons.forEach(addon => addon(this));
Expand Down
12 changes: 12 additions & 0 deletions src/functions/quirks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { IDBKeyRangeConstructor } from '../public/types/indexeddb';
import { maxString } from '../globals/constants';

export function safariMultiStoreFix(storeNames: string[]) {
return storeNames.length === 1 ? storeNames[0] : storeNames;
}
Expand All @@ -6,3 +9,12 @@ export function getNativeGetDatabaseNamesFn(indexedDB) {
var fn = indexedDB && (indexedDB.getDatabaseNames || indexedDB.webkitGetDatabaseNames);
return fn && fn.bind(indexedDB);
}

export function getMaxKey (IDBKeyRange: IDBKeyRangeConstructor) {
try {
IDBKeyRange.only([[]]);
return [[]];
} catch (e) {
return maxString;
}
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { dexieStackFrameFilter, minKey, connections, DEXIE_VERSION } from './glo
import Events from './helpers/Events';
import { exceptions } from './errors';
import { errnames } from './errors';
import { getMaxKey } from './functions/quirks';

let databaseLister: DatabaseLister | undefined;

Expand Down Expand Up @@ -220,5 +221,5 @@ Promise.rejectionMapper = mapError;
// Initialize databaseLister (won't ever be opened on chromium browsers')
try {
databaseLister = DatabaseLister(Dexie.dependencies.indexedDB);
Dexie.maxKey = new Dexie('', { addons: [] })._maxKey;
Dexie.maxKey = getMaxKey(Dexie.dependencies.IDBKeyRange);
} catch (e) { }

0 comments on commit 4dd65d3

Please sign in to comment.