mirror of
https://github.com/ilyakooo0/purescript-indexedDB.git
synced 2024-09-11 06:55:48 +03:00
wrap all native calls with Aff in try catch (they can throw out of the onerror callback :|)
This commit is contained in:
parent
a2bf21df62
commit
9c145f91c5
@ -44,9 +44,13 @@ exports._continuePrimaryKey = function _continuePrimaryKey(cursor, key, primaryK
|
|||||||
|
|
||||||
exports._delete = function _delete(cursor) {
|
exports._delete = function _delete(cursor) {
|
||||||
return function aff(success, error) {
|
return function aff(success, error) {
|
||||||
const request = cursor.delete();
|
try {
|
||||||
request.onsuccess = successHandler(success);
|
const request = cursor.delete();
|
||||||
request.onerror = errorHandler(error);
|
request.onsuccess = successHandler(success);
|
||||||
|
request.onerror = errorHandler(error);
|
||||||
|
} catch (e) {
|
||||||
|
error(new Error(e.name));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -71,9 +75,13 @@ exports._source = function _source(IDBObjectStore, IDBIndex, cursor) {
|
|||||||
|
|
||||||
exports._update = function _update(cursor, value) {
|
exports._update = function _update(cursor, value) {
|
||||||
return function aff(success, error) {
|
return function aff(success, error) {
|
||||||
const request = cursor.update(value);
|
try {
|
||||||
request.onsuccess = successHandler(success);
|
const request = cursor.update(value);
|
||||||
request.onerror = errorHandler(error);
|
request.onsuccess = successHandler(success);
|
||||||
|
request.onerror = errorHandler(error);
|
||||||
|
} catch (e) {
|
||||||
|
error(new Error(e.name));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -99,10 +99,10 @@ exports._onVersionChange = function _onVersionChange(db, f) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
exports._transaction = function _transaction(show, db, stores, mode) {
|
exports._transaction = function _transaction(db, stores, mode) {
|
||||||
return function aff(success, error) {
|
return function aff(success, error) {
|
||||||
try {
|
try {
|
||||||
const transaction = db.transaction(stores, show(mode));
|
const transaction = db.transaction(stores, mode);
|
||||||
success(transaction);
|
success(transaction);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error(new Error(e.name));
|
error(new Error(e.name));
|
||||||
|
@ -29,7 +29,7 @@ class IDBDatabase db where
|
|||||||
close :: forall e. db -> Aff (idb :: IDB | e) Unit
|
close :: forall e. db -> Aff (idb :: IDB | e) Unit
|
||||||
createObjectStore :: forall e. db -> StoreName -> IDBObjectStoreParameters -> Aff (idb :: IDB | e) ObjectStore
|
createObjectStore :: forall e. db -> StoreName -> IDBObjectStoreParameters -> Aff (idb :: IDB | e) ObjectStore
|
||||||
deleteObjectStore :: forall e. db -> StoreName -> Aff (idb :: IDB | e) ObjectStore
|
deleteObjectStore :: forall e. db -> StoreName -> Aff (idb :: IDB | e) ObjectStore
|
||||||
transaction :: forall e. db -> KeyPath -> TransactionMode -> Aff (idb :: IDB | e) Transaction
|
transaction :: forall e. db -> Array StoreName -> TransactionMode -> Aff (idb :: IDB | e) Transaction
|
||||||
|
|
||||||
|
|
||||||
type StoreName = String
|
type StoreName = String
|
||||||
@ -91,7 +91,7 @@ instance idbDatabaseDatabase :: IDBDatabase Database where
|
|||||||
Fn.runFn2 _deleteObjectStore db name'
|
Fn.runFn2 _deleteObjectStore db name'
|
||||||
|
|
||||||
transaction db stores mode' =
|
transaction db stores mode' =
|
||||||
Fn.runFn4 _transaction show db stores mode'
|
Fn.runFn3 _transaction db stores (show mode')
|
||||||
|
|
||||||
|
|
||||||
--------------------
|
--------------------
|
||||||
@ -124,7 +124,7 @@ foreign import _onError :: forall db e e'. Fn2 db (Error -> Eff ( | e') Unit) (A
|
|||||||
foreign import _onVersionChange :: forall db e e'. Fn2 db ({ oldVersion :: Int, newVersion :: Int } -> Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
|
foreign import _onVersionChange :: forall db e e'. Fn2 db ({ oldVersion :: Int, newVersion :: Int } -> Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
|
||||||
|
|
||||||
|
|
||||||
foreign import _transaction :: forall db e. Fn4 (db -> String) db (Array String) TransactionMode (Aff (idb :: IDB | e) Transaction)
|
foreign import _transaction :: forall db e. Fn3 db (Array String) String (Aff (idb :: IDB | e) Transaction)
|
||||||
|
|
||||||
|
|
||||||
foreign import _version :: Database -> Int
|
foreign import _version :: Database -> Int
|
||||||
|
@ -17,13 +17,17 @@ const noOp2 = function noOp2() {
|
|||||||
|
|
||||||
exports._deleteDatabase = function _deleteDatabase(name) {
|
exports._deleteDatabase = function _deleteDatabase(name) {
|
||||||
return function aff(success, error) {
|
return function aff(success, error) {
|
||||||
const request = indexedDB.deleteDatabase(name);
|
try {
|
||||||
|
const request = indexedDB.deleteDatabase(name);
|
||||||
|
|
||||||
request.onsuccess = function onSuccess(e) {
|
request.onsuccess = function onSuccess(e) {
|
||||||
success(e.oldVersion);
|
success(e.oldVersion);
|
||||||
};
|
};
|
||||||
|
|
||||||
request.onerror = errorHandler(error);
|
request.onerror = errorHandler(error);
|
||||||
|
} catch (e) {
|
||||||
|
error(new Error(e.name));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -31,19 +35,23 @@ exports._open = function _open(fromMaybe, name, mver, req) {
|
|||||||
const ver = fromMaybe(undefined)(mver);
|
const ver = fromMaybe(undefined)(mver);
|
||||||
|
|
||||||
return function aff(success, error) {
|
return function aff(success, error) {
|
||||||
const request = indexedDB.open(name, ver);
|
try {
|
||||||
request.onsuccess = function onSuccess(e) {
|
const request = indexedDB.open(name, ver);
|
||||||
success(e.target.result);
|
request.onsuccess = function onSuccess(e) {
|
||||||
};
|
success(e.target.result);
|
||||||
|
};
|
||||||
|
|
||||||
request.onblocked = function onBlocked() {
|
request.onblocked = function onBlocked() {
|
||||||
fromMaybe(noOp)(req.onBlocked)();
|
fromMaybe(noOp)(req.onBlocked)();
|
||||||
};
|
};
|
||||||
|
|
||||||
request.onupgradeneeded = function onUpgradeNeeded(e) {
|
request.onupgradeneeded = function onUpgradeNeeded(e) {
|
||||||
fromMaybe(noOp2)(req.onUpgradeNeeded)(e.target.result)(e.target.transaction)();
|
fromMaybe(noOp2)(req.onUpgradeNeeded)(e.target.result)(e.target.transaction)();
|
||||||
};
|
};
|
||||||
|
|
||||||
request.onerror = errorHandler(error);
|
request.onerror = errorHandler(error);
|
||||||
|
} catch (e) {
|
||||||
|
error(new Error(e.name));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -55,9 +55,13 @@ exports._count = function _count(index, query) {
|
|||||||
|
|
||||||
exports._get = function _get(index, range) {
|
exports._get = function _get(index, range) {
|
||||||
return function aff(success, error) {
|
return function aff(success, error) {
|
||||||
const request = index.get(range);
|
try {
|
||||||
request.onsuccess = successHandler(success);
|
const request = index.get(range);
|
||||||
request.onerror = errorHandler(error);
|
request.onsuccess = successHandler(success);
|
||||||
|
request.onerror = errorHandler(error);
|
||||||
|
} catch (e) {
|
||||||
|
error(new Error(e.name));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -78,32 +82,48 @@ exports._getAll = function _getAll(index, query, count) {
|
|||||||
|
|
||||||
exports._getAllKeys = function _getAllKeys(index, range, count) {
|
exports._getAllKeys = function _getAllKeys(index, range, count) {
|
||||||
return function aff(success, error) {
|
return function aff(success, error) {
|
||||||
const request = index.getAllKeys(range, count || undefined);
|
try {
|
||||||
request.onsuccess = successHandler(success);
|
const request = index.getAllKeys(range, count || undefined);
|
||||||
request.onerror = errorHandler(error);
|
request.onsuccess = successHandler(success);
|
||||||
|
request.onerror = errorHandler(error);
|
||||||
|
} catch (e) {
|
||||||
|
error(new Error(e.name));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
exports._getKey = function _getKey(index, range) {
|
exports._getKey = function _getKey(index, range) {
|
||||||
return function aff(success, error) {
|
return function aff(success, error) {
|
||||||
const request = index.getKey(range);
|
try {
|
||||||
request.onsuccess = successHandler(success);
|
const request = index.getKey(range);
|
||||||
request.onerror = errorHandler(error);
|
request.onsuccess = successHandler(success);
|
||||||
|
request.onerror = errorHandler(error);
|
||||||
|
} catch (e) {
|
||||||
|
error(new Error(e.name));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
exports._openCursor = function _openCursor(index, query, dir) {
|
exports._openCursor = function _openCursor(index, query, dir) {
|
||||||
return function aff(success, error) {
|
return function aff(success, error) {
|
||||||
const request = index.openCursor(query, dir);
|
try {
|
||||||
request.onsuccess = successHandler(success);
|
const request = index.openCursor(query, dir);
|
||||||
request.onerror = errorHandler(error);
|
request.onsuccess = successHandler(success);
|
||||||
|
request.onerror = errorHandler(error);
|
||||||
|
} catch (e) {
|
||||||
|
error(new Error(e.name));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
exports._openKeyCursor = function _openKeyCursor(index, query, dir) {
|
exports._openKeyCursor = function _openKeyCursor(index, query, dir) {
|
||||||
return function aff(success, error) {
|
return function aff(success, error) {
|
||||||
const request = index.openKeyCursor(query, dir);
|
try {
|
||||||
request.onsuccess = successHandler(success);
|
const request = index.openKeyCursor(query, dir);
|
||||||
request.onerror = errorHandler(error);
|
request.onsuccess = successHandler(success);
|
||||||
|
request.onerror = errorHandler(error);
|
||||||
|
} catch (e) {
|
||||||
|
error(new Error(e.name));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -17,9 +17,13 @@ const toArray = function toArray(xs) {
|
|||||||
|
|
||||||
exports._add = function _add(store, value, key) {
|
exports._add = function _add(store, value, key) {
|
||||||
return function aff(success, error) {
|
return function aff(success, error) {
|
||||||
const request = store.add(value, key || undefined);
|
try {
|
||||||
request.onsuccess = successHandler(success);
|
const request = store.add(value, key || undefined);
|
||||||
request.onerror = errorHandler(error);
|
request.onsuccess = successHandler(success);
|
||||||
|
request.onerror = errorHandler(error);
|
||||||
|
} catch (e) {
|
||||||
|
error(new Error(e.name));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -29,9 +33,13 @@ exports._autoIncrement = function _autoIncrement(store) {
|
|||||||
|
|
||||||
exports._clear = function _clear(store) {
|
exports._clear = function _clear(store) {
|
||||||
return function aff(success, error) {
|
return function aff(success, error) {
|
||||||
const request = store.clear();
|
try {
|
||||||
request.onsuccess = successHandler(success);
|
const request = store.clear();
|
||||||
request.onerror = errorHandler(error);
|
request.onsuccess = successHandler(success);
|
||||||
|
request.onerror = errorHandler(error);
|
||||||
|
} catch (e) {
|
||||||
|
error(new Error(e.name));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -74,9 +82,13 @@ exports._deleteIndex = function _deleteIndex(store, name) {
|
|||||||
|
|
||||||
exports._delete = function _delete(store, query) {
|
exports._delete = function _delete(store, query) {
|
||||||
return function aff(success, error) {
|
return function aff(success, error) {
|
||||||
const request = store.delete(query);
|
try {
|
||||||
request.onsuccess = successHandler(success);
|
const request = store.delete(query);
|
||||||
request.onerror = errorHandler(error);
|
request.onsuccess = successHandler(success);
|
||||||
|
request.onerror = errorHandler(error);
|
||||||
|
} catch (e) {
|
||||||
|
error(new Error(e.name));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -115,9 +127,13 @@ exports._name = function _name(store) {
|
|||||||
|
|
||||||
exports._put = function _put(store, value, key) {
|
exports._put = function _put(store, value, key) {
|
||||||
return function aff(success, error) {
|
return function aff(success, error) {
|
||||||
const request = store.put(value, key || undefined);
|
try {
|
||||||
request.onsuccess = successHandler(success);
|
const request = store.put(value, key || undefined);
|
||||||
request.onerror = errorHandler(error);
|
request.onsuccess = successHandler(success);
|
||||||
|
request.onerror = errorHandler(error);
|
||||||
|
} catch (e) {
|
||||||
|
error(new Error(e.name));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user