mirror of
https://github.com/ilyakooo0/purescript-indexedDB.git
synced 2024-09-11 06:55:48 +03:00
add event handlers to Database and Transaction
This commit is contained in:
parent
bd0306927e
commit
652e6518f6
@ -59,6 +59,38 @@ exports._objectStoreNames = function _objectStoreNames(db) {
|
||||
return toArray(db.objectStoreNames);
|
||||
};
|
||||
|
||||
exports._onAbort = function _onAbort(db, f) {
|
||||
return function eff() {
|
||||
db.onabort = function onabort() {
|
||||
f();
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
exports._onClose = function _onClose(db, f) {
|
||||
return function eff() {
|
||||
db.onclose = function onclose() {
|
||||
f();
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
exports._onError = function _onError(db, f) {
|
||||
return function eff() {
|
||||
db.onerror = function onerror(e) {
|
||||
f(new Error(e.target.error.name))();
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
exports._onVersionChange = function _onVersionChange(db, f) {
|
||||
return function eff() {
|
||||
db.onversionchange = function onversionchange(e) {
|
||||
f({ oldVersion: e.oldVersion, newVersion: e.newVersion })();
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
exports._transaction = function _transaction(show, db, stores, mode) {
|
||||
return function eff() {
|
||||
try {
|
||||
|
@ -4,12 +4,16 @@ module Database.IndexedDB.IDBDatabase
|
||||
, name
|
||||
, objectStoreNames
|
||||
, version
|
||||
, onAbort
|
||||
, onClose
|
||||
, onError
|
||||
, onVersionChange
|
||||
) where
|
||||
|
||||
import Prelude (Unit, show)
|
||||
|
||||
import Control.Monad.Eff (Eff)
|
||||
import Control.Monad.Eff.Exception (EXCEPTION)
|
||||
import Control.Monad.Eff.Exception (EXCEPTION, Error)
|
||||
import Data.Function.Uncurried as Fn
|
||||
import Data.Function.Uncurried (Fn2, Fn3, Fn4)
|
||||
|
||||
@ -51,7 +55,24 @@ version =
|
||||
--------------------
|
||||
-- EVENT HANDLERS
|
||||
--
|
||||
-- onAbort :: forall e. Database -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit
|
||||
onAbort :: forall e e'. Database -> Eff ( | e') Unit -> Eff (idb :: IDB | e) Unit
|
||||
onAbort db f =
|
||||
Fn.runFn2 _onAbort db f
|
||||
|
||||
|
||||
onClose :: forall e e'. Database -> Eff ( | e') Unit -> Eff (idb :: IDB | e) Unit
|
||||
onClose db f =
|
||||
Fn.runFn2 _onClose db f
|
||||
|
||||
|
||||
onError :: forall e e'. Database -> (Error -> Eff ( | e') Unit) -> Eff (idb :: IDB | e) Unit
|
||||
onError db f =
|
||||
Fn.runFn2 _onError db f
|
||||
|
||||
|
||||
onVersionChange :: forall e e'. Database -> ({ oldVersion :: Int, newVersion :: Int } -> Eff ( | e') Unit) -> Eff (idb :: IDB | e) Unit
|
||||
onVersionChange db f =
|
||||
Fn.runFn2 _onVersionChange db f
|
||||
|
||||
|
||||
|
||||
@ -90,6 +111,18 @@ foreign import _name :: Database -> String
|
||||
foreign import _objectStoreNames :: Database -> Array String
|
||||
|
||||
|
||||
foreign import _onAbort :: forall db e e'. Fn2 db (Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
|
||||
|
||||
|
||||
foreign import _onClose :: forall db e e'. Fn2 db (Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
|
||||
|
||||
|
||||
foreign import _onError :: forall db e e'. Fn2 db (Error -> Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
|
||||
|
||||
|
||||
foreign import _onVersionChange :: forall db e e'. Fn2 db ({ oldVersion :: Int, newVersion :: Int } -> Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
|
||||
|
||||
|
||||
foreign import _transaction :: forall db e. Fn4 (db -> String) db (Array String) TransactionMode (Eff (idb :: IDB, exception :: EXCEPTION | e) Transaction)
|
||||
|
||||
|
||||
|
@ -41,7 +41,7 @@ exports._open = function _open(fromMaybe, name, mver, req) {
|
||||
};
|
||||
|
||||
request.onupgradeneeded = function onUpgradeNeeded(e) {
|
||||
fromMaybe(noOp2)(req.onUpgradeNeeded)(e.target.result)();
|
||||
fromMaybe(noOp2)(req.onUpgradeNeeded)(e.target.result)(e.target.transaction)();
|
||||
};
|
||||
|
||||
request.onerror = errorHandler(error);
|
||||
|
@ -15,8 +15,8 @@ import Database.IndexedDB.Core
|
||||
-- INTERFACE
|
||||
--
|
||||
type OpenRequest e =
|
||||
{ onBlocked :: Maybe (Eff (idb :: IDB | e) Unit)
|
||||
, onUpgradeNeeded :: Maybe (Database -> Eff (idb :: IDB | e) Unit)
|
||||
{ onBlocked :: Maybe (Eff (| e) Unit)
|
||||
, onUpgradeNeeded :: Maybe (Database -> Eff (| e) Unit)
|
||||
}
|
||||
|
||||
|
||||
@ -25,7 +25,7 @@ deleteDatabase =
|
||||
_deleteDatabase
|
||||
|
||||
|
||||
open :: forall e. String -> Maybe Int -> OpenRequest e -> Aff (idb :: IDB | e) Database
|
||||
open :: forall e e'. String -> Maybe Int -> OpenRequest e' -> Aff (idb :: IDB | e) Database
|
||||
open name mver req =
|
||||
Fn.runFn4 _open fromMaybe name mver req
|
||||
|
||||
@ -36,4 +36,4 @@ open name mver req =
|
||||
foreign import _deleteDatabase :: forall e. String -> Aff (idb :: IDB | e) Int
|
||||
|
||||
|
||||
foreign import _open :: forall a e. Fn4 (a -> Maybe a -> a) String (Maybe Int) (OpenRequest e) (Aff (idb :: IDB | e) Database)
|
||||
foreign import _open :: forall a e e'. Fn4 (a -> Maybe a -> a) String (Maybe Int) (OpenRequest e') (Aff (idb :: IDB | e) Database)
|
||||
|
@ -117,3 +117,8 @@ exports._put = function _put(store, value, key) {
|
||||
request.onerror = errorHandler(error);
|
||||
};
|
||||
};
|
||||
|
||||
exports._transaction = function _transaction(store) {
|
||||
return store.transaction;
|
||||
};
|
||||
|
||||
|
@ -7,6 +7,7 @@ module Database.IndexedDB.IDBObjectStore
|
||||
, indexNames
|
||||
, keyPath
|
||||
, name
|
||||
, transaction
|
||||
, defaultParameters
|
||||
) where
|
||||
|
||||
@ -21,7 +22,7 @@ import Data.Function.Uncurried (Fn2, Fn3, Fn4)
|
||||
import Data.Maybe (Maybe)
|
||||
import Data.Nullable (Nullable, toNullable)
|
||||
|
||||
import Database.IndexedDB.Core (IDB, Index, KeyRange, KeyPath, ObjectStore)
|
||||
import Database.IndexedDB.Core (IDB, Index, KeyRange, KeyPath, ObjectStore, Transaction)
|
||||
import Database.IndexedDB.IDBIndex.Internal (class IDBIndex, IDBIndexParameters, count, get, getAllKeys, getKey, openCursor, openKeyCursor)
|
||||
import Database.IndexedDB.IDBKey.Internal (Key(Key), extractForeign)
|
||||
|
||||
@ -71,6 +72,11 @@ name =
|
||||
_name
|
||||
|
||||
|
||||
transaction :: ObjectStore -> Transaction
|
||||
transaction =
|
||||
_transaction
|
||||
|
||||
|
||||
--------------------
|
||||
-- INSTANCES
|
||||
--
|
||||
@ -138,3 +144,6 @@ foreign import _name :: ObjectStore -> String
|
||||
|
||||
|
||||
foreign import _put :: forall value e. Fn3 ObjectStore value (Nullable Foreign) (Aff (idb :: IDB | e) Foreign)
|
||||
|
||||
|
||||
foreign import _transaction :: ObjectStore -> Transaction
|
||||
|
@ -33,3 +33,27 @@ exports._objectStore = function _objectStore(tx, name) {
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
exports._onAbort = function _onAbort(tx, f) {
|
||||
return function eff() {
|
||||
tx.onabort = function onabort() {
|
||||
f();
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
exports._onComplete = function _onComplete(tx, f) {
|
||||
return function eff() {
|
||||
tx.oncomplete = function oncomplete() {
|
||||
f();
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
exports._onError = function _onError(tx, f) {
|
||||
return function eff() {
|
||||
tx.onerror = function onerror(e) {
|
||||
f(new Error(e.target.error.name))();
|
||||
};
|
||||
};
|
||||
};
|
||||
|
@ -2,6 +2,9 @@ module Database.IndexedDB.IDBTransaction
|
||||
(class IDBTransaction, abort, objectStore
|
||||
, error
|
||||
, mode
|
||||
, onAbort
|
||||
, onComplete
|
||||
, onError
|
||||
) where
|
||||
|
||||
import Prelude (Unit, (>>>))
|
||||
@ -37,6 +40,24 @@ mode =
|
||||
Fn.runFn4 _mode ReadOnly ReadWrite VersionChange
|
||||
|
||||
|
||||
--------------------
|
||||
-- EVENT HANDLERS
|
||||
--
|
||||
onAbort :: forall e e'. Transaction -> Eff ( | e') Unit -> Eff (idb :: IDB | e) Unit
|
||||
onAbort db f =
|
||||
Fn.runFn2 _onAbort db f
|
||||
|
||||
|
||||
onComplete :: forall e e'. Transaction -> Eff ( | e') Unit -> Eff (idb :: IDB | e) Unit
|
||||
onComplete db f =
|
||||
Fn.runFn2 _onComplete db f
|
||||
|
||||
|
||||
onError :: forall e e'. Transaction -> (Error -> Eff ( | e') Unit) -> Eff (idb :: IDB | e) Unit
|
||||
onError db f =
|
||||
Fn.runFn2 _onError db f
|
||||
|
||||
|
||||
--------------------
|
||||
-- INSTANCES
|
||||
--
|
||||
@ -61,3 +82,12 @@ foreign import _mode :: Fn4 TransactionMode TransactionMode TransactionMode Tran
|
||||
|
||||
|
||||
foreign import _objectStore :: forall tx e. Fn2 tx String (Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore)
|
||||
|
||||
|
||||
foreign import _onAbort :: forall tx e e'. Fn2 tx (Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
|
||||
|
||||
|
||||
foreign import _onComplete :: forall tx e e'. Fn2 tx (Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
|
||||
|
||||
|
||||
foreign import _onError :: forall tx e e'. Fn2 tx (Error -> Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
|
||||
|
Loading…
Reference in New Issue
Block a user