Skip to content

Commit

Permalink
Removing uncaught promise error. Fixes jakearchibald#127.
Browse files Browse the repository at this point in the history
  • Loading branch information
jakearchibald committed Oct 7, 2019
1 parent 113cd23 commit 4b013b4
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 43 deletions.
6 changes: 3 additions & 3 deletions build/cjs/async-iterators.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ async function* iterate(...args) {
}
}
function isIteratorProp(target, prop) {
return (prop === Symbol.asyncIterator &&
__chunk_1.instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) || (prop === 'iterate' &&
__chunk_1.instanceOfAny(target, [IDBIndex, IDBObjectStore]));
return ((prop === Symbol.asyncIterator &&
__chunk_1.instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) ||
(prop === 'iterate' && __chunk_1.instanceOfAny(target, [IDBIndex, IDBObjectStore])));
}
__chunk_1.addTraps(oldTraps => ({
get(target, prop, receiver) {
Expand Down
36 changes: 24 additions & 12 deletions build/cjs/chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@ let idbProxyableTypes;
let cursorAdvanceMethods;
// This is a function to prevent it throwing up in node environments.
function getIdbProxyableTypes() {
return idbProxyableTypes ||
(idbProxyableTypes = [IDBDatabase, IDBObjectStore, IDBIndex, IDBCursor, IDBTransaction]);
return (idbProxyableTypes ||
(idbProxyableTypes = [
IDBDatabase,
IDBObjectStore,
IDBIndex,
IDBCursor,
IDBTransaction,
]));
}
// This is a function to prevent it throwing up in node environments.
function getCursorAdvanceMethods() {
return cursorAdvanceMethods || (cursorAdvanceMethods = [
IDBCursor.prototype.advance,
IDBCursor.prototype.continue,
IDBCursor.prototype.continuePrimaryKey,
]);
return (cursorAdvanceMethods ||
(cursorAdvanceMethods = [
IDBCursor.prototype.advance,
IDBCursor.prototype.continue,
IDBCursor.prototype.continuePrimaryKey,
]));
}
const cursorRequestMap = new WeakMap();
const transactionDoneMap = new WeakMap();
Expand All @@ -39,14 +46,16 @@ function promisifyRequest(request) {
request.addEventListener('success', success);
request.addEventListener('error', error);
});
promise.then((value) => {
promise
.then(value => {
// Since cursoring reuses the IDBRequest (*sigh*), we cache it for later retrieval
// (see wrapFunction).
if (value instanceof IDBCursor) {
cursorRequestMap.set(value, request);
}
// Catching to avoid "Uncaught Promise exceptions"
}).catch(() => { });
})
.catch(() => { });
// This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This
// is because we create many promises from a single IDBRequest.
reverseTransformCache.set(promise, request);
Expand Down Expand Up @@ -89,16 +98,19 @@ let idbProxyTraps = {
}
// Make tx.store return the only store in the transaction, or undefined if there are many.
if (prop === 'store') {
return receiver.objectStoreNames[1] ?
undefined : receiver.objectStore(receiver.objectStoreNames[0]);
return receiver.objectStoreNames[1]
? undefined
: receiver.objectStore(receiver.objectStoreNames[0]);
}
}
// Else transform whatever we get back.
return wrap(target[prop]);
},
has(target, prop) {
if (target instanceof IDBTransaction && (prop === 'done' || prop === 'store'))
if (target instanceof IDBTransaction &&
(prop === 'done' || prop === 'store')) {
return true;
}
return prop in target;
},
};
Expand Down
13 changes: 8 additions & 5 deletions build/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ function openDB(name, version, { blocked, upgrade, blocking } = {}) {
const request = indexedDB.open(name, version);
const openPromise = __chunk_1.wrap(request);
if (upgrade) {
request.addEventListener('upgradeneeded', (event) => {
request.addEventListener('upgradeneeded', event => {
upgrade(__chunk_1.wrap(request.result), event.oldVersion, event.newVersion, __chunk_1.wrap(request.transaction));
});
}
if (blocked)
request.addEventListener('blocked', () => blocked());
if (blocking)
openPromise.then(db => db.addEventListener('versionchange', blocking));
if (blocking) {
openPromise.then(db => db.addEventListener('versionchange', blocking)).catch(() => { });
}
return openPromise;
}
/**
Expand All @@ -43,8 +44,9 @@ const cachedMethods = new Map();
function getMethod(target, prop) {
if (!(target instanceof IDBDatabase &&
!(prop in target) &&
typeof prop === 'string'))
typeof prop === 'string')) {
return;
}
if (cachedMethods.get(prop))
return cachedMethods.get(prop);
const targetFuncName = prop.replace(/FromIndex$/, '');
Expand All @@ -53,8 +55,9 @@ function getMethod(target, prop) {
if (
// Bail if the target doesn't exist on the target. Eg, getAll isn't in Edge.
!(targetFuncName in (useIndex ? IDBIndex : IDBObjectStore).prototype) ||
!(isWrite || readMethods.includes(targetFuncName)))
!(isWrite || readMethods.includes(targetFuncName))) {
return;
}
const method = async function (storeName, ...args) {
// isWrite ? 'readwrite' : undefined gzipps better, but fails in Edge :(
const tx = this.transaction(storeName, isWrite ? 'readwrite' : 'readonly');
Expand Down
6 changes: 3 additions & 3 deletions build/esm/async-iterators.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ async function* iterate(...args) {
}
}
function isIteratorProp(target, prop) {
return (prop === Symbol.asyncIterator &&
instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) || (prop === 'iterate' &&
instanceOfAny(target, [IDBIndex, IDBObjectStore]));
return ((prop === Symbol.asyncIterator &&
instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) ||
(prop === 'iterate' && instanceOfAny(target, [IDBIndex, IDBObjectStore])));
}
addTraps(oldTraps => ({
get(target, prop, receiver) {
Expand Down
36 changes: 24 additions & 12 deletions build/esm/chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ let idbProxyableTypes;
let cursorAdvanceMethods;
// This is a function to prevent it throwing up in node environments.
function getIdbProxyableTypes() {
return idbProxyableTypes ||
(idbProxyableTypes = [IDBDatabase, IDBObjectStore, IDBIndex, IDBCursor, IDBTransaction]);
return (idbProxyableTypes ||
(idbProxyableTypes = [
IDBDatabase,
IDBObjectStore,
IDBIndex,
IDBCursor,
IDBTransaction,
]));
}
// This is a function to prevent it throwing up in node environments.
function getCursorAdvanceMethods() {
return cursorAdvanceMethods || (cursorAdvanceMethods = [
IDBCursor.prototype.advance,
IDBCursor.prototype.continue,
IDBCursor.prototype.continuePrimaryKey,
]);
return (cursorAdvanceMethods ||
(cursorAdvanceMethods = [
IDBCursor.prototype.advance,
IDBCursor.prototype.continue,
IDBCursor.prototype.continuePrimaryKey,
]));
}
const cursorRequestMap = new WeakMap();
const transactionDoneMap = new WeakMap();
Expand All @@ -37,14 +44,16 @@ function promisifyRequest(request) {
request.addEventListener('success', success);
request.addEventListener('error', error);
});
promise.then((value) => {
promise
.then(value => {
// Since cursoring reuses the IDBRequest (*sigh*), we cache it for later retrieval
// (see wrapFunction).
if (value instanceof IDBCursor) {
cursorRequestMap.set(value, request);
}
// Catching to avoid "Uncaught Promise exceptions"
}).catch(() => { });
})
.catch(() => { });
// This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This
// is because we create many promises from a single IDBRequest.
reverseTransformCache.set(promise, request);
Expand Down Expand Up @@ -87,16 +96,19 @@ let idbProxyTraps = {
}
// Make tx.store return the only store in the transaction, or undefined if there are many.
if (prop === 'store') {
return receiver.objectStoreNames[1] ?
undefined : receiver.objectStore(receiver.objectStoreNames[0]);
return receiver.objectStoreNames[1]
? undefined
: receiver.objectStore(receiver.objectStoreNames[0]);
}
}
// Else transform whatever we get back.
return wrap(target[prop]);
},
has(target, prop) {
if (target instanceof IDBTransaction && (prop === 'done' || prop === 'store'))
if (target instanceof IDBTransaction &&
(prop === 'done' || prop === 'store')) {
return true;
}
return prop in target;
},
};
Expand Down
13 changes: 8 additions & 5 deletions build/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ function openDB(name, version, { blocked, upgrade, blocking } = {}) {
const request = indexedDB.open(name, version);
const openPromise = wrap(request);
if (upgrade) {
request.addEventListener('upgradeneeded', (event) => {
request.addEventListener('upgradeneeded', event => {
upgrade(wrap(request.result), event.oldVersion, event.newVersion, wrap(request.transaction));
});
}
if (blocked)
request.addEventListener('blocked', () => blocked());
if (blocking)
openPromise.then(db => db.addEventListener('versionchange', blocking));
if (blocking) {
openPromise.then(db => db.addEventListener('versionchange', blocking)).catch(() => { });
}
return openPromise;
}
/**
Expand All @@ -40,8 +41,9 @@ const cachedMethods = new Map();
function getMethod(target, prop) {
if (!(target instanceof IDBDatabase &&
!(prop in target) &&
typeof prop === 'string'))
typeof prop === 'string')) {
return;
}
if (cachedMethods.get(prop))
return cachedMethods.get(prop);
const targetFuncName = prop.replace(/FromIndex$/, '');
Expand All @@ -50,8 +52,9 @@ function getMethod(target, prop) {
if (
// Bail if the target doesn't exist on the target. Eg, getAll isn't in Edge.
!(targetFuncName in (useIndex ? IDBIndex : IDBObjectStore).prototype) ||
!(isWrite || readMethods.includes(targetFuncName)))
!(isWrite || readMethods.includes(targetFuncName))) {
return;
}
const method = async function (storeName, ...args) {
// isWrite ? 'readwrite' : undefined gzipps better, but fails in Edge :(
const tx = this.transaction(storeName, isWrite ? 'readwrite' : 'readonly');
Expand Down
2 changes: 1 addition & 1 deletion build/iife/index-min.js

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

Loading

0 comments on commit 4b013b4

Please sign in to comment.