git.iko.soy/purescript-indexed-DB
on master help sign in
add event handlers to Database and Transaction
commit652e6518f6691e54a7be892254fb2ac4f4e00130 authorKtorZ <matthias.benkort@gmail.com> dateFri 30 Jun 2017, 14:28 (9 years ago) parentbd03069
8 files changed+141−8
src/Database/IndexedDB/IDBDatabase.js+32 src/Database/IndexedDB/IDBDatabase.purs+35−2 src/Database/IndexedDB/IDBFactory.js+1−1 src/Database/IndexedDB/IDBFactory.purs+4−4 src/Database/IndexedDB/IDBObjectStore.js+5 src/Database/IndexedDB/IDBObjectStore.purs+10−1 src/Database/IndexedDB/IDBTransaction.js+24 src/Database/IndexedDB/IDBTransaction.purs+30
src/Database/IndexedDB/IDBDatabase.js+32
@@ -59,6 +59,38 @@ exports._objectStoreNames = function _objectStoreNames(db) {
5959 return toArray(db.objectStoreNames);
6060};
6161
62+exports._onAbort = function _onAbort(db, f) {
63+ return function eff() {
64+ db.onabort = function onabort() {
65+ f();
66+ };
67+ };
68+};
69+
70+exports._onClose = function _onClose(db, f) {
71+ return function eff() {
72+ db.onclose = function onclose() {
73+ f();
74+ };
75+ };
76+};
77+
78+exports._onError = function _onError(db, f) {
79+ return function eff() {
80+ db.onerror = function onerror(e) {
81+ f(new Error(e.target.error.name))();
82+ };
83+ };
84+};
85+
86+exports._onVersionChange = function _onVersionChange(db, f) {
87+ return function eff() {
88+ db.onversionchange = function onversionchange(e) {
89+ f({ oldVersion: e.oldVersion, newVersion: e.newVersion })();
90+ };
91+ };
92+};
93+
6294exports._transaction = function _transaction(show, db, stores, mode) {
6395 return function eff() {
6496 try {
src/Database/IndexedDB/IDBDatabase.purs+35−2
@@ -4,12 +4,16 @@ module Database.IndexedDB.IDBDatabase
44 , name
55 , objectStoreNames
66 , version
7+ , onAbort
8+ , onClose
9+ , onError
10+ , onVersionChange
711 ) where
812
913import Prelude (Unit, show)
1014
1115import Control.Monad.Eff (Eff)
12−import Control.Monad.Eff.Exception (EXCEPTION)
16+import Control.Monad.Eff.Exception (EXCEPTION, Error)
1317import Data.Function.Uncurried as Fn
1418import Data.Function.Uncurried (Fn2, Fn3, Fn4)
1519
@@ -51,7 +55,24 @@ version =
5155--------------------
5256-- EVENT HANDLERS
5357--
54−-- onAbort :: forall e. Database -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit
58+onAbort :: forall e e'. Database -> Eff ( | e') Unit -> Eff (idb :: IDB | e) Unit
59+onAbort db f =
60+ Fn.runFn2 _onAbort db f
61+
62+
63+onClose :: forall e e'. Database -> Eff ( | e') Unit -> Eff (idb :: IDB | e) Unit
64+onClose db f =
65+ Fn.runFn2 _onClose db f
66+
67+
68+onError :: forall e e'. Database -> (Error -> Eff ( | e') Unit) -> Eff (idb :: IDB | e) Unit
69+onError db f =
70+ Fn.runFn2 _onError db f
71+
72+
73+onVersionChange :: forall e e'. Database -> ({ oldVersion :: Int, newVersion :: Int } -> Eff ( | e') Unit) -> Eff (idb :: IDB | e) Unit
74+onVersionChange db f =
75+ Fn.runFn2 _onVersionChange db f
5576
5677
5778
@@ -90,6 +111,18 @@ foreign import _name :: Database -> String
90111foreign import _objectStoreNames :: Database -> Array String
91112
92113
114+foreign import _onAbort :: forall db e e'. Fn2 db (Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
115+
116+
117+foreign import _onClose :: forall db e e'. Fn2 db (Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
118+
119+
120+foreign import _onError :: forall db e e'. Fn2 db (Error -> Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
121+
122+
123+foreign import _onVersionChange :: forall db e e'. Fn2 db ({ oldVersion :: Int, newVersion :: Int } -> Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
124+
125+
93126foreign import _transaction :: forall db e. Fn4 (db -> String) db (Array String) TransactionMode (Eff (idb :: IDB, exception :: EXCEPTION | e) Transaction)
94127
95128
src/Database/IndexedDB/IDBFactory.js+1−1
@@ -41,7 +41,7 @@ exports._open = function _open(fromMaybe, name, mver, req) {
4141 };
4242
4343 request.onupgradeneeded = function onUpgradeNeeded(e) {
44− fromMaybe(noOp2)(req.onUpgradeNeeded)(e.target.result)();
44+ fromMaybe(noOp2)(req.onUpgradeNeeded)(e.target.result)(e.target.transaction)();
4545 };
4646
4747 request.onerror = errorHandler(error);
src/Database/IndexedDB/IDBFactory.purs+4−4
@@ -15,8 +15,8 @@ import Database.IndexedDB.Core
1515-- INTERFACE
1616--
1717type OpenRequest e =
18− { onBlocked :: Maybe (Eff (idb :: IDB | e) Unit)
19− , onUpgradeNeeded :: Maybe (Database -> Eff (idb :: IDB | e) Unit)
18+ { onBlocked :: Maybe (Eff (| e) Unit)
19+ , onUpgradeNeeded :: Maybe (Database -> Eff (| e) Unit)
2020 }
2121
2222
@@ -25,7 +25,7 @@ deleteDatabase =
2525 _deleteDatabase
2626
2727
28−open :: forall e. String -> Maybe Int -> OpenRequest e -> Aff (idb :: IDB | e) Database
28+open :: forall e e'. String -> Maybe Int -> OpenRequest e' -> Aff (idb :: IDB | e) Database
2929open name mver req =
3030 Fn.runFn4 _open fromMaybe name mver req
3131
@@ -36,4 +36,4 @@ open name mver req =
3636foreign import _deleteDatabase :: forall e. String -> Aff (idb :: IDB | e) Int
3737
3838
39−foreign import _open :: forall a e. Fn4 (a -> Maybe a -> a) String (Maybe Int) (OpenRequest e) (Aff (idb :: IDB | e) Database)
39+foreign import _open :: forall a e e'. Fn4 (a -> Maybe a -> a) String (Maybe Int) (OpenRequest e') (Aff (idb :: IDB | e) Database)
src/Database/IndexedDB/IDBObjectStore.js+5
@@ -117,3 +117,8 @@ exports._put = function _put(store, value, key) {
117117 request.onerror = errorHandler(error);
118118 };
119119};
120+
121+exports._transaction = function _transaction(store) {
122+ return store.transaction;
123+};
124+
src/Database/IndexedDB/IDBObjectStore.purs+10−1
@@ -7,6 +7,7 @@ module Database.IndexedDB.IDBObjectStore
77 , indexNames
88 , keyPath
99 , name
10+ , transaction
1011 , defaultParameters
1112 ) where
1213
@@ -21,7 +22,7 @@ import Data.Function.Uncurried (Fn2, Fn3, Fn4)
2122import Data.Maybe (Maybe)
2223import Data.Nullable (Nullable, toNullable)
2324
24−import Database.IndexedDB.Core (IDB, Index, KeyRange, KeyPath, ObjectStore)
25+import Database.IndexedDB.Core (IDB, Index, KeyRange, KeyPath, ObjectStore, Transaction)
2526import Database.IndexedDB.IDBIndex.Internal (class IDBIndex, IDBIndexParameters, count, get, getAllKeys, getKey, openCursor, openKeyCursor)
2627import Database.IndexedDB.IDBKey.Internal (Key(Key), extractForeign)
2728
@@ -71,6 +72,11 @@ name =
7172 _name
7273
7374
75+transaction :: ObjectStore -> Transaction
76+transaction =
77+ _transaction
78+
79+
7480--------------------
7581-- INSTANCES
7682--
@@ -138,3 +144,6 @@ foreign import _name :: ObjectStore -> String
138144
139145
140146foreign import _put :: forall value e. Fn3 ObjectStore value (Nullable Foreign) (Aff (idb :: IDB | e) Foreign)
147+
148+
149+foreign import _transaction :: ObjectStore -> Transaction
src/Database/IndexedDB/IDBTransaction.js+24
@@ -33,3 +33,27 @@ exports._objectStore = function _objectStore(tx, name) {
3333 }
3434 };
3535};
36+
37+exports._onAbort = function _onAbort(tx, f) {
38+ return function eff() {
39+ tx.onabort = function onabort() {
40+ f();
41+ };
42+ };
43+};
44+
45+exports._onComplete = function _onComplete(tx, f) {
46+ return function eff() {
47+ tx.oncomplete = function oncomplete() {
48+ f();
49+ };
50+ };
51+};
52+
53+exports._onError = function _onError(tx, f) {
54+ return function eff() {
55+ tx.onerror = function onerror(e) {
56+ f(new Error(e.target.error.name))();
57+ };
58+ };
59+};
src/Database/IndexedDB/IDBTransaction.purs+30
@@ -2,6 +2,9 @@ module Database.IndexedDB.IDBTransaction
22 (class IDBTransaction, abort, objectStore
33 , error
44 , mode
5+ , onAbort
6+ , onComplete
7+ , onError
58 ) where
69
710import Prelude (Unit, (>>>))
@@ -37,6 +40,24 @@ mode =
3740 Fn.runFn4 _mode ReadOnly ReadWrite VersionChange
3841
3942
43+--------------------
44+-- EVENT HANDLERS
45+--
46+onAbort :: forall e e'. Transaction -> Eff ( | e') Unit -> Eff (idb :: IDB | e) Unit
47+onAbort db f =
48+ Fn.runFn2 _onAbort db f
49+
50+
51+onComplete :: forall e e'. Transaction -> Eff ( | e') Unit -> Eff (idb :: IDB | e) Unit
52+onComplete db f =
53+ Fn.runFn2 _onComplete db f
54+
55+
56+onError :: forall e e'. Transaction -> (Error -> Eff ( | e') Unit) -> Eff (idb :: IDB | e) Unit
57+onError db f =
58+ Fn.runFn2 _onError db f
59+
60+
4061--------------------
4162-- INSTANCES
4263--
@@ -61,3 +82,12 @@ foreign import _mode :: Fn4 TransactionMode TransactionMode TransactionMode Tran
6182
6283
6384foreign import _objectStore :: forall tx e. Fn2 tx String (Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore)
85+
86+
87+foreign import _onAbort :: forall tx e e'. Fn2 tx (Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
88+
89+
90+foreign import _onComplete :: forall tx e e'. Fn2 tx (Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
91+
92+
93+foreign import _onError :: forall tx e e'. Fn2 tx (Error -> Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)