git.iko.soy/purescript-indexed-DB
on master help sign in
replace all Eff with Aff for consistency and ease of use
commita2bf21df62479d83d135e5df6a1ad3380b51e2d5 authorKtorZ <matthias.benkort@gmail.com> dateFri 30 Jun 2017, 21:15 (9 years ago) parent8dcfdda
9 files changed+172−135
src/Database/IndexedDB/IDBCursor.js+7−6 src/Database/IndexedDB/IDBCursor.purs+10−12 src/Database/IndexedDB/IDBDatabase.js+23−14 src/Database/IndexedDB/IDBDatabase.purs+18−17 src/Database/IndexedDB/IDBObjectStore.js+11−8 src/Database/IndexedDB/IDBObjectStore.purs+6−8 src/Database/IndexedDB/IDBTransaction.js+13−8 src/Database/IndexedDB/IDBTransaction.purs+12−11 test/Main.purs+72−51
src/Database/IndexedDB/IDBCursor.js+7−6
@@ -12,31 +12,32 @@ const successHandler = function successHandler(cb) {
1212
1313
1414exports._advance = function _advance(cursor, count) {
15− return function eff() {
15+ return function aff(success, error) {
1616 try {
1717 cursor.advance(count);
18+ success();
1819 } catch (e) {
19− throw new Error(e.name);
20+ error(new Error(e.name));
2021 }
2122 };
2223};
2324
2425exports._continue = function _continue(cursor, key) {
25− return function eff() {
26+ return function aff(success, error) {
2627 try {
2728 cursor.continue(key || undefined);
2829 } catch (e) {
29− throw new Error(e.name);
30+ error(new Error(e.name));
3031 }
3132 };
3233};
3334
3435exports._continuePrimaryKey = function _continuePrimaryKey(cursor, key, primaryKey) {
35− return function eff() {
36+ return function aff(success, error) {
3637 try {
3738 cursor.continuePrimaryKey(key, primaryKey);
3839 } catch (e) {
39− throw new Error(e.name);
40+ error(new Error(e.name));
4041 }
4142 };
4243};
src/Database/IndexedDB/IDBCursor.purs+10−12
@@ -14,8 +14,6 @@ module Database.IndexedDB.IDBCursor
1414import Prelude (Unit, (>>>))
1515
1616import Control.Monad.Aff (Aff)
17−import Control.Monad.Eff (Eff)
18−import Control.Monad.Eff.Exception (EXCEPTION)
1917import Data.Function.Uncurried as Fn
2018import Data.Function.Uncurried (Fn2, Fn3)
2119import Data.Maybe (Maybe)
@@ -29,11 +27,11 @@ import Database.IndexedDB.Core
2927-- INTERFACES
3028--
3129class IDBCursor cursor where
32− advance :: forall e. cursor -> Int -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit
33− continue :: forall e. cursor -> Maybe Key -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit
34− continuePrimaryKey :: forall e. cursor -> Key -> Key -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit
35− delete :: forall e. cursor -> Aff (idb ::IDB, exception :: EXCEPTION | e) Unit
36− update :: forall val e. cursor -> val -> Aff (idb :: IDB, exception :: EXCEPTION | e) Key
30+ advance :: forall e. cursor -> Int -> Aff (idb :: IDB | e) Unit
31+ continue :: forall e. cursor -> Maybe Key -> Aff (idb :: IDB | e) Unit
32+ continuePrimaryKey :: forall e. cursor -> Key -> Key -> Aff (idb :: IDB | e) Unit
33+ delete :: forall e. cursor -> Aff (idb ::IDB | e) Unit
34+ update :: forall val e. cursor -> val -> Aff (idb :: IDB | e) Key
3735
3836
3937--------------------
@@ -110,16 +108,16 @@ instance valueCursorKeyCursor :: IDBCursor ValueCursor where
110108--------------------
111109-- FFI
112110--
113−foreign import _advance :: forall cursor e. Fn2 cursor Int (Eff (idb :: IDB, exception :: EXCEPTION | e) Unit)
111+foreign import _advance :: forall cursor e. Fn2 cursor Int (Aff (idb :: IDB | e) Unit)
114112
115113
116−foreign import _continue :: forall cursor e. Fn2 cursor (Nullable Key) (Eff (idb :: IDB, exception :: EXCEPTION | e) Unit)
114+foreign import _continue :: forall cursor e. Fn2 cursor (Nullable Key) (Aff (idb :: IDB | e) Unit)
117115
118116
119−foreign import _continuePrimaryKey :: forall cursor e. Fn3 cursor Key Key (Eff (idb :: IDB, exception :: EXCEPTION | e) Unit)
117+foreign import _continuePrimaryKey :: forall cursor e. Fn3 cursor Key Key (Aff (idb :: IDB | e) Unit)
120118
121119
122−foreign import _delete :: forall cursor e. cursor -> (Aff (idb ::IDB, exception :: EXCEPTION | e) Unit)
120+foreign import _delete :: forall cursor e. cursor -> (Aff (idb ::IDB | e) Unit)
123121
124122
125123foreign import _direction :: forall cursor. Fn2 (String -> Nullable CursorDirection) cursor CursorDirection
@@ -134,7 +132,7 @@ foreign import _primaryKey :: forall cursor. cursor -> Key
134132foreign import _source :: forall cursor. Fn3 (ObjectStore -> CursorSource) (Index -> CursorSource) cursor CursorSource
135133
136134
137−foreign import _update :: forall cursor e. Fn2 cursor Foreign (Aff (idb :: IDB, exception :: EXCEPTION | e) Key)
135+foreign import _update :: forall cursor e. Fn2 cursor Foreign (Aff (idb :: IDB | e) Key)
138136
139137
140138foreign import _value :: forall cursor val. cursor -> val
src/Database/IndexedDB/IDBDatabase.js+23−14
@@ -4,17 +4,18 @@ const toArray = $Core.toArray;
44
55
66exports._close = function _close(db) {
7− return function eff() {
7+ return function aff(success, error) {
88 try {
99 db.close();
10+ success();
1011 } catch (e) {
11− throw new Error(e.name);
12+ error(new Error(e.name));
1213 }
1314 };
1415};
1516
1617exports._createObjectStore = function _createObjectStore(db, name, opts) {
17− return function eff() {
18+ return function aff(success, error) {
1819 var keyPath;
1920
2021 try {
@@ -31,22 +32,25 @@ exports._createObjectStore = function _createObjectStore(db, name, opts) {
3132 default:
3233 keyPath = opts.keyPath;
3334 }
34− return db.createObjectStore(name, {
35+
36+ const store = db.createObjectStore(name, {
3537 autoIncrement: opts.autoIncrement,
3638 keyPath: keyPath,
3739 });
40+ success(store);
3841 } catch (e) {
39− throw new Error(e.name);
42+ error(new Error(e.name));
4043 }
4144 };
4245};
4346
4447exports._deleteObjectStore = function _deleteObjectStore(db, name) {
45− return function eff() {
48+ return function aff(success, error) {
4649 try {
4750 db.deleteObjectStore(name);
51+ success();
4852 } catch (e) {
49− throw new Error(e.name);
53+ error(new Error(e.name));
5054 }
5155 };
5256};
@@ -60,43 +64,48 @@ exports._objectStoreNames = function _objectStoreNames(db) {
6064};
6165
6266exports._onAbort = function _onAbort(db, f) {
63− return function eff() {
67+ return function aff(success) {
6468 db.onabort = function onabort() {
6569 f();
6670 };
71+ success();
6772 };
6873};
6974
7075exports._onClose = function _onClose(db, f) {
71− return function eff() {
76+ return function aff(success) {
7277 db.onclose = function onclose() {
7378 f();
7479 };
80+ success();
7581 };
7682};
7783
7884exports._onError = function _onError(db, f) {
79− return function eff() {
85+ return function aff(success) {
8086 db.onerror = function onerror(e) {
8187 f(new Error(e.target.error.name))();
8288 };
89+ success();
8390 };
8491};
8592
8693exports._onVersionChange = function _onVersionChange(db, f) {
87− return function eff() {
94+ return function aff(success) {
8895 db.onversionchange = function onversionchange(e) {
8996 f({ oldVersion: e.oldVersion, newVersion: e.newVersion })();
9097 };
98+ success();
9199 };
92100};
93101
94102exports._transaction = function _transaction(show, db, stores, mode) {
95− return function eff() {
103+ return function aff(success, error) {
96104 try {
97− return db.transaction(stores, show(mode));
105+ const transaction = db.transaction(stores, show(mode));
106+ success(transaction);
98107 } catch (e) {
99− throw new Error(e.name);
108+ error(new Error(e.name));
100109 }
101110 };
102111};
src/Database/IndexedDB/IDBDatabase.purs+18−17
@@ -12,8 +12,9 @@ module Database.IndexedDB.IDBDatabase
1212
1313import Prelude (Unit, show)
1414
15+import Control.Monad.Aff (Aff)
1516import Control.Monad.Eff (Eff)
16−import Control.Monad.Eff.Exception (EXCEPTION, Error)
17+import Control.Monad.Eff.Exception (Error)
1718import Data.Function.Uncurried as Fn
1819import Data.Function.Uncurried (Fn2, Fn3, Fn4)
1920
@@ -25,10 +26,10 @@ import Database.IndexedDB.IDBObjectStore (IDBObjectStoreParameters)
2526-- INTERFACE
2627--
2728class IDBDatabase db where
28− close :: forall e. db -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit
29− createObjectStore :: forall e. db -> StoreName -> IDBObjectStoreParameters -> Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore
30− deleteObjectStore :: forall e. db -> StoreName -> Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore
31− transaction :: forall e. db -> KeyPath -> TransactionMode -> Eff (idb :: IDB, exception :: EXCEPTION | e) Transaction
29+ close :: forall e. db -> Aff (idb :: IDB | e) Unit
30+ createObjectStore :: forall e. db -> StoreName -> IDBObjectStoreParameters -> Aff (idb :: IDB | e) ObjectStore
31+ deleteObjectStore :: forall e. db -> StoreName -> Aff (idb :: IDB | e) ObjectStore
32+ transaction :: forall e. db -> KeyPath -> TransactionMode -> Aff (idb :: IDB | e) Transaction
3233
3334
3435type StoreName = String
@@ -55,22 +56,22 @@ version =
5556--------------------
5657-- EVENT HANDLERS
5758--
58−onAbort :: forall e e'. Database -> Eff ( | e') Unit -> Eff (idb :: IDB | e) Unit
59+onAbort :: forall e e'. Database -> Eff ( | e') Unit -> Aff (idb :: IDB | e) Unit
5960onAbort db f =
6061 Fn.runFn2 _onAbort db f
6162
6263
63−onClose :: forall e e'. Database -> Eff ( | e') Unit -> Eff (idb :: IDB | e) Unit
64+onClose :: forall e e'. Database -> Eff ( | e') Unit -> Aff (idb :: IDB | e) Unit
6465onClose db f =
6566 Fn.runFn2 _onClose db f
6667
6768
68−onError :: forall e e'. Database -> (Error -> Eff ( | e') Unit) -> Eff (idb :: IDB | e) Unit
69+onError :: forall e e'. Database -> (Error -> Eff ( | e') Unit) -> Aff (idb :: IDB | e) Unit
6970onError db f =
7071 Fn.runFn2 _onError db f
7172
7273
73−onVersionChange :: forall e e'. Database -> ({ oldVersion :: Int, newVersion :: Int } -> Eff ( | e') Unit) -> Eff (idb :: IDB | e) Unit
74+onVersionChange :: forall e e'. Database -> ({ oldVersion :: Int, newVersion :: Int } -> Eff ( | e') Unit) -> Aff (idb :: IDB | e) Unit
7475onVersionChange db f =
7576 Fn.runFn2 _onVersionChange db f
7677
@@ -96,13 +97,13 @@ instance idbDatabaseDatabase :: IDBDatabase Database where
9697--------------------
9798-- FFI
9899--
99−foreign import _close :: forall db e. db -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit
100+foreign import _close :: forall db e. db -> Aff (idb :: IDB | e) Unit
100101
101102
102−foreign import _createObjectStore :: forall db e. Fn3 db String { keyPath :: Array String, autoIncrement :: Boolean } (Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore)
103+foreign import _createObjectStore :: forall db e. Fn3 db String { keyPath :: Array String, autoIncrement :: Boolean } (Aff (idb :: IDB | e) ObjectStore)
103104
104105
105−foreign import _deleteObjectStore :: forall db e. Fn2 db String (Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore)
106+foreign import _deleteObjectStore :: forall db e. Fn2 db String (Aff (idb :: IDB | e) ObjectStore)
106107
107108
108109foreign import _name :: Database -> String
@@ -111,19 +112,19 @@ foreign import _name :: Database -> String
111112foreign import _objectStoreNames :: Database -> Array String
112113
113114
114−foreign import _onAbort :: forall db e e'. Fn2 db (Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
115+foreign import _onAbort :: forall db e e'. Fn2 db (Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
115116
116117
117−foreign import _onClose :: forall db e e'. Fn2 db (Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
118+foreign import _onClose :: forall db e e'. Fn2 db (Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
118119
119120
120−foreign import _onError :: forall db e e'. Fn2 db (Error -> Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
121+foreign import _onError :: forall db e e'. Fn2 db (Error -> Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
121122
122123
123−foreign import _onVersionChange :: forall db e e'. Fn2 db ({ oldVersion :: Int, newVersion :: Int } -> Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
124+foreign import _onVersionChange :: forall db e e'. Fn2 db ({ oldVersion :: Int, newVersion :: Int } -> Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
124125
125126
126−foreign import _transaction :: forall db e. Fn4 (db -> String) db (Array String) TransactionMode (Eff (idb :: IDB, exception :: EXCEPTION | e) Transaction)
127+foreign import _transaction :: forall db e. Fn4 (db -> String) db (Array String) TransactionMode (Aff (idb :: IDB | e) Transaction)
127128
128129
129130foreign import _version :: Database -> Int
src/Database/IndexedDB/IDBObjectStore.js+11−8
@@ -36,7 +36,7 @@ exports._clear = function _clear(store) {
3636};
3737
3838exports._createIndex = function _createIndex(store, name, path, params) {
39− return function eff() {
39+ return function aff(success, error) {
4040 var keyPath;
4141
4242 try {
@@ -53,19 +53,21 @@ exports._createIndex = function _createIndex(store, name, path, params) {
5353 keyPath = path;
5454 }
5555
56− return store.createIndex(name, keyPath, params);
56+ const index = store.createIndex(name, keyPath, params);
57+ success(index);
5758 } catch (e) {
58− throw new Error(e.name);
59+ error(new Error(e.name));
5960 }
6061 };
6162};
6263
6364exports._deleteIndex = function _deleteIndex(store, name) {
64− return function eff() {
65+ return function aff(success, error) {
6566 try {
6667 store.deleteIndex(name);
68+ success();
6769 } catch (e) {
68− throw new Error(e.name);
70+ error(new Error(e.name));
6971 }
7072 };
7173};
@@ -79,11 +81,12 @@ exports._delete = function _delete(store, query) {
7981};
8082
8183exports._index = function _index(store, name) {
82− return function eff() {
84+ return function aff(success, error) {
8385 try {
84− return store.index(name);
86+ const index = store.index(name);
87+ success(index);
8588 } catch (e) {
86− throw new Error(e.name);
89+ error(new Error(e.name));
8790 }
8891 };
8992};
src/Database/IndexedDB/IDBObjectStore.purs+6−8
@@ -14,8 +14,6 @@ module Database.IndexedDB.IDBObjectStore
1414import Prelude (Unit, ($), (<$>), (>>>))
1515
1616import Control.Monad.Aff (Aff)
17−import Control.Monad.Eff (Eff)
18−import Control.Monad.Eff.Exception (EXCEPTION)
1917import Data.Foreign (Foreign)
2018import Data.Function.Uncurried as Fn
2119import Data.Function.Uncurried (Fn2, Fn3, Fn4)
@@ -33,10 +31,10 @@ import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key(Key), extractFore
3331class IDBObjectStore store where
3432 add :: forall v k e. (IDBKey k) => store -> v -> Maybe k -> Aff (idb :: IDB | e) Key
3533 clear :: forall e. store -> Aff (idb :: IDB | e) Unit
36− createIndex :: forall e. store -> IndexName -> KeyPath -> IDBIndexParameters -> Eff (idb :: IDB, exception :: EXCEPTION | e) Index
34+ createIndex :: forall e. store -> IndexName -> KeyPath -> IDBIndexParameters -> Aff (idb :: IDB | e) Index
3735 delete :: forall e. store -> KeyRange -> Aff (idb :: IDB | e) Unit
38− deleteIndex :: forall e. store -> IndexName -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit
39− index :: forall e. store -> IndexName -> Eff (idb :: IDB, exception :: EXCEPTION | e) Index
36+ deleteIndex :: forall e. store -> IndexName -> Aff (idb :: IDB | e) Unit
37+ index :: forall e. store -> IndexName -> Aff (idb :: IDB | e) Index
4038 put :: forall v k e. (IDBKey k) => store -> v -> Maybe k -> Aff (idb :: IDB | e) Key
4139
4240
@@ -122,16 +120,16 @@ foreign import _autoIncrement :: ObjectStore -> Boolean
122120foreign import _clear :: forall e. ObjectStore -> Aff (idb :: IDB | e) Unit
123121
124122
125−foreign import _createIndex :: forall e. Fn4 ObjectStore String (Array String) { unique :: Boolean, multiEntry :: Boolean } (Eff (idb :: IDB | e) Index)
123+foreign import _createIndex :: forall e. Fn4 ObjectStore String (Array String) { unique :: Boolean, multiEntry :: Boolean } (Aff (idb :: IDB | e) Index)
126124
127125
128126foreign import _delete :: forall e. Fn2 ObjectStore KeyRange (Aff (idb :: IDB | e) Unit)
129127
130128
131−foreign import _deleteIndex :: forall e. Fn2 ObjectStore String (Eff (idb :: IDB | e) Unit)
129+foreign import _deleteIndex :: forall e. Fn2 ObjectStore String (Aff (idb :: IDB | e) Unit)
132130
133131
134−foreign import _index :: forall e. Fn2 ObjectStore String (Eff (idb :: IDB | e) Index)
132+foreign import _index :: forall e. Fn2 ObjectStore String (Aff (idb :: IDB | e) Index)
135133
136134
137135foreign import _indexNames :: ObjectStore -> Array String
src/Database/IndexedDB/IDBTransaction.js+13−8
@@ -1,9 +1,10 @@
11exports._abort = function _abort(tx) {
2− return function eff() {
2+ return function aff(success, error) {
33 try {
44 tx.abort();
5+ success();
56 } catch (e) {
6− throw new Error(e.name);
7+ error(new Error(e.name));
78 }
89 };
910};
@@ -25,35 +26,39 @@ exports._mode = function _mode(ReadOnly, ReadWrite, VersionChange, tx) {
2526};
2627
2728exports._objectStore = function _objectStore(tx, name) {
28− return function eff() {
29+ return function aff(success, error) {
2930 try {
30− return tx.objectStore(name);
31+ const store = tx.objectStore(name);
32+ success(store);
3133 } catch (e) {
32− throw new Error(e.name);
34+ error(new Error(e.name));
3335 }
3436 };
3537};
3638
3739exports._onAbort = function _onAbort(tx, f) {
38− return function eff() {
40+ return function aff(success) {
3941 tx.onabort = function onabort() {
4042 f();
4143 };
44+ success();
4245 };
4346};
4447
4548exports._onComplete = function _onComplete(tx, f) {
46− return function eff() {
49+ return function aff(success) {
4750 tx.oncomplete = function oncomplete() {
4851 f();
4952 };
53+ success();
5054 };
5155};
5256
5357exports._onError = function _onError(tx, f) {
54− return function eff() {
58+ return function aff(success) {
5559 tx.onerror = function onerror(e) {
5660 f(new Error(e.target.error.name))();
5761 };
62+ success();
5863 };
5964};
src/Database/IndexedDB/IDBTransaction.purs+12−11
@@ -9,8 +9,9 @@ module Database.IndexedDB.IDBTransaction
99
1010import Prelude (Unit, (>>>))
1111
12+import Control.Monad.Aff (Aff)
1213import Control.Monad.Eff (Eff)
13−import Control.Monad.Eff.Exception (EXCEPTION, Error)
14+import Control.Monad.Eff.Exception (Error)
1415import Data.Function.Uncurried as Fn
1516import Data.Function.Uncurried (Fn2, Fn4)
1617import Data.Maybe (Maybe)
@@ -23,8 +24,8 @@ import Database.IndexedDB.Core (IDB, ObjectStore, Transaction, TransactionMo
2324-- INTERFACES
2425--
2526class IDBTransaction tx where
26− abort :: forall e. tx -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit
27− objectStore :: forall e. tx -> String -> Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore
27+ abort :: forall e. tx -> Aff (idb :: IDB | e) Unit
28+ objectStore :: forall e. tx -> String -> Aff (idb :: IDB | e) ObjectStore
2829
2930
3031--------------------
@@ -43,17 +44,17 @@ mode =
4344--------------------
4445-- EVENT HANDLERS
4546--
46−onAbort :: forall e e'. Transaction -> Eff ( | e') Unit -> Eff (idb :: IDB | e) Unit
47+onAbort :: forall e e'. Transaction -> Eff ( | e') Unit -> Aff (idb :: IDB | e) Unit
4748onAbort db f =
4849 Fn.runFn2 _onAbort db f
4950
5051
51−onComplete :: forall e e'. Transaction -> Eff ( | e') Unit -> Eff (idb :: IDB | e) Unit
52+onComplete :: forall e e'. Transaction -> Eff ( | e') Unit -> Aff (idb :: IDB | e) Unit
5253onComplete db f =
5354 Fn.runFn2 _onComplete db f
5455
5556
56−onError :: forall e e'. Transaction -> (Error -> Eff ( | e') Unit) -> Eff (idb :: IDB | e) Unit
57+onError :: forall e e'. Transaction -> (Error -> Eff ( | e') Unit) -> Aff (idb :: IDB | e) Unit
5758onError db f =
5859 Fn.runFn2 _onError db f
5960
@@ -72,7 +73,7 @@ instance idbTransactionTransaction :: IDBTransaction Transaction where
7273--------------------
7374-- FFI
7475--
75−foreign import _abort :: forall tx e. tx -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit
76+foreign import _abort :: forall tx e. tx -> Aff (idb :: IDB | e) Unit
7677
7778
7879foreign import _error :: Transaction -> (Nullable Error)
@@ -81,13 +82,13 @@ foreign import _error :: Transaction -> (Nullable Error)
8182foreign import _mode :: Fn4 TransactionMode TransactionMode TransactionMode Transaction TransactionMode
8283
8384
84−foreign import _objectStore :: forall tx e. Fn2 tx String (Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore)
85+foreign import _objectStore :: forall tx e. Fn2 tx String (Aff (idb :: IDB | e) ObjectStore)
8586
8687
87−foreign import _onAbort :: forall tx e e'. Fn2 tx (Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
88+foreign import _onAbort :: forall tx e e'. Fn2 tx (Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
8889
8990
90−foreign import _onComplete :: forall tx e e'. Fn2 tx (Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
91+foreign import _onComplete :: forall tx e e'. Fn2 tx (Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
9192
9293
93−foreign import _onError :: forall tx e e'. Fn2 tx (Error -> Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit)
94+foreign import _onError :: forall tx e e'. Fn2 tx (Error -> Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit)
test/Main.purs+72−51
@@ -34,12 +34,16 @@ import Database.IndexedDB.IDBTransaction as IDBTransaction
3434
3535infixr 7 Tuple as :+:
3636
37+launchAff' :: forall a e. Aff e a -> Eff (exception :: EXCEPTION | e) Unit
38+launchAff' aff =
39+ pure unit <* (launchAff aff)
40+
3741main :: forall eff. Eff (now :: NOW, mocha :: MOCHA, idb :: IDB, exception :: EXCEPTION, avar :: AVAR | eff) Unit
3842main = runMocha do
3943 describe "IDBFactory" do
4044 let
4145 tearDown name version db = do
42− liftEff $ IDBDatabase.close db
46+ IDBDatabase.close db
4347 version' <- IDBFactory.deleteDatabase name
4448 version' `shouldEqual` version
4549
@@ -73,7 +77,7 @@ main = runMocha do
7377 , onBlocked : Nothing
7478 }
7579 IDBDatabase.name db `shouldEqual` name
76− liftEff $ IDBDatabase.close db
80+ IDBDatabase.close db
7781 db <- IDBFactory.open name Nothing
7882 { onUpgradeNeeded : Nothing
7983 , onBlocked : Nothing
@@ -111,7 +115,7 @@ main = runMocha do
111115 }
112116 _ <- forkAff do
113117 delay (Milliseconds 100.0)
114− liftEff $ IDBDatabase.close db01
118+ IDBDatabase.close db01
115119
116120 db02 <- IDBFactory.open name (Just version)
117121 { onUpgradeNeeded : Nothing
@@ -277,14 +281,14 @@ main = runMocha do
277281 describe "IDBDatabase" do
278282 let
279283 tearDown db = do
280− liftEff $ IDBDatabase.close db
284+ IDBDatabase.close db
281285 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
282286 pure unit
283287
284288 setup storeParams = do
285− let onUpgradeNeeded var db _ = pure unit <* do
289+ let onUpgradeNeeded var db _ = launchAff' do
286290 store <- IDBDatabase.createObjectStore db "store" storeParams
287− _ <- (launchAff $ putVar var { db, store })
291+ _ <- putVar var { db, store }
288292 pure unit
289293
290294 var <- makeVar
@@ -320,14 +324,13 @@ main = runMocha do
320324 tearDown db
321325
322326 it "deleteObjectStore" do
323− let onUpgradeNeeded var db _ = do
327+ let onUpgradeNeeded var db _ = launchAff' do
324328 _ <- IDBDatabase.deleteObjectStore db "store"
325− _ <- (launchAff $ putVar var true)
326− pure unit
329+ putVar var true
327330
328331 var <- makeVar
329332 { db, store } <- setup IDBObjectStore.defaultParameters
330− liftEff $ IDBDatabase.close db
333+ IDBDatabase.close db
331334 db <- IDBFactory.open "db" (Just 999) { onUpgradeNeeded : Just (onUpgradeNeeded var)
332335 , onBlocked : Nothing
333336 }
@@ -339,16 +342,15 @@ main = runMocha do
339342 describe "IDBObjectStore" do
340343 let
341344 tearDown db = do
342− liftEff $ IDBDatabase.close db
345+ IDBDatabase.close db
343346 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
344347 pure unit
345348
346349 setup { storeParams, onUpgradeNeeded } = do
347− let onUpgradeNeeded' var db _ = pure unit <* do
350+ let onUpgradeNeeded' var db _ = launchAff' do
348351 store <- IDBDatabase.createObjectStore db "store" storeParams
349− launchAff do
350− liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store)
351− putVar var { db, store }
352+ liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store)
353+ putVar var { db, store }
352354
353355 var <- makeVar
354356 db <- IDBFactory.open "db" Nothing
@@ -362,7 +364,7 @@ main = runMocha do
362364 date <- liftEff $ toDateTime <$> now
363365 { db } <- setup
364366 { storeParams: { autoIncrement: true, keyPath: [] }
365− , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do
367+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
366368 -- no key
367369 key <- IDBObjectStore.add store "patate" (Nothing :: Maybe Key)
368370 (toKey 1) `shouldEqual` key
@@ -386,48 +388,48 @@ main = runMocha do
386388 -- array key
387389 key <- IDBObjectStore.add store "patate" (Just $ toKey [14, 42])
388390 (toKey [14, 42]) `shouldEqual` key
389− )}
391+ }
390392 tearDown db
391393
392394 it "clear()" do
393395 { db } <- setup
394396 { storeParams: IDBObjectStore.defaultParameters
395− , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do
397+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
396398 key <- IDBObjectStore.add store "patate" (Just 14)
397399 _ <- IDBObjectStore.clear store
398400 val <- IDBObjectStore.get store (IDBKeyRange.only key)
399401 val `shouldEqual` (Nothing :: Maybe String)
400− )}
402+ }
401403 tearDown db
402404
403405 it "count()" do
404406 { db } <- setup
405407 { storeParams: IDBObjectStore.defaultParameters
406− , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do
408+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
407409 key <- IDBObjectStore.add store "patate" (Just 14)
408410 key <- IDBObjectStore.add store "autruche" (Just 42)
409411 n <- IDBObjectStore.count store Nothing
410412 n `shouldEqual` 2
411− )}
413+ }
412414 tearDown db
413415
414416 it "getKey()" do
415417 { db } <- setup
416418 { storeParams: IDBObjectStore.defaultParameters
417− , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do
419+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
418420 key <- IDBObjectStore.add store "patate" (Just 14)
419421 mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 14)
420422 mkey `shouldEqual` (Just key)
421423
422424 mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
423425 mkey `shouldEqual` (Nothing :: Maybe Key)
424− )}
426+ }
425427 tearDown db
426428
427429 it "getAllKeys()" do
428430 { db } <- setup
429431 { storeParams: IDBObjectStore.defaultParameters
430− , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do
432+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
431433 key1 <- IDBObjectStore.add store "patate" (Just 14)
432434 key2 <- IDBObjectStore.add store "autruche" (Just 42)
433435 key3 <- IDBObjectStore.add store 14 (Just 1337)
@@ -447,39 +449,39 @@ main = runMocha do
447449 -- count
448450 keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
449451 keys `shouldEqual` [key1, key2]
450− )}
452+ }
451453 tearDown db
452454
453455 it "openCursor()" do
454456 { db } <- setup
455457 { storeParams: IDBObjectStore.defaultParameters
456− , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do
458+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
457459 _ <- IDBObjectStore.openCursor store Nothing Next
458460 _ <- IDBObjectStore.openCursor store Nothing NextUnique
459461 _ <- IDBObjectStore.openCursor store Nothing Prev
460462 _ <- IDBObjectStore.openCursor store Nothing PrevUnique
461463 _ <- IDBObjectStore.openCursor store (Just $ IDBKeyRange.upperBound 1 true) Next
462464 pure unit
463− )}
465+ }
464466 tearDown db
465467
466468 it "openKeyCursor()" do
467469 { db } <- setup
468470 { storeParams: IDBObjectStore.defaultParameters
469− , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do
471+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
470472 _ <- IDBObjectStore.openKeyCursor store Nothing Next
471473 _ <- IDBObjectStore.openKeyCursor store Nothing NextUnique
472474 _ <- IDBObjectStore.openKeyCursor store Nothing Prev
473475 _ <- IDBObjectStore.openKeyCursor store Nothing PrevUnique
474476 _ <- IDBObjectStore.openKeyCursor store (Just $ IDBKeyRange.lowerBound 1 true) Next
475477 pure unit
476− )}
478+ }
477479 tearDown db
478480
479481 describe "IDBIndex" do
480482 let
481483 tearDown db = do
482− liftEff $ IDBDatabase.close db
484+ IDBDatabase.close db
483485 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
484486 pure unit
485487
@@ -491,13 +493,12 @@ main = runMocha do
491493 , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
492494 } -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
493495 setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
494− let onUpgradeNeeded' var db tx = pure unit <* do
496+ let onUpgradeNeeded' var db tx = launchAff' do
495497 store <- IDBDatabase.createObjectStore db "store" storeParams
496− launchAff do
497− _ <- traverse (uncurry (IDBObjectStore.add store)) values
498− index <- liftEff $ IDBObjectStore.createIndex store "index" keyPath indexParams
499− liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
500− putVar var { db, index, store }
498+ _ <- traverse (uncurry (IDBObjectStore.add store)) values
499+ index <- IDBObjectStore.createIndex store "index" keyPath indexParams
500+ liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
501+ putVar var { db, index, store }
501502
502503 var <- makeVar
503504 db <- IDBFactory.open "db" Nothing
@@ -522,7 +523,7 @@ main = runMocha do
522523 tearDown db
523524
524525 it "attempt to create an index that requires unique values on an object store already contains duplicates" do
525− let onAbort var = launchAff (putVar var true) *> pure unit
526+ let onAbort var = launchAff' (putVar var true)
526527 txVar <- makeVar
527528 dbVar <- makeVar
528529 res <- attempt $ setup
@@ -534,7 +535,7 @@ main = runMocha do
534535 , values : [ { indexedProperty: "bar" } :+: (Just $ toKey 1)
535536 , { indexedProperty: "bar" } :+: (Just $ toKey 2)
536537 ]
537− , onUpgradeNeeded : Just $ \db tx _ -> do
538+ , onUpgradeNeeded : Just $ \db tx _ -> launchAff' do
538539 IDBTransaction.onAbort tx (onAbort txVar)
539540 IDBDatabase.onAbort db (onAbort dbVar)
540541 }
@@ -557,7 +558,7 @@ main = runMocha do
557558 , { key: "key2", indexedProperty: "indexed_2" } :+: Nothing
558559 , { key: "key3", indexedProperty: "indexed_3" } :+: Nothing
559560 ]
560− , onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do
561+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
561562 val <- IDBIndex.get index (IDBKeyRange.only "indexed_2")
562563 ((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2")
563564 }
@@ -572,9 +573,9 @@ main = runMocha do
572573 , "object_2" :+: (Just $ toKey 2)
573574 , "object_3" :+: (Just $ toKey 3)
574575 ]
575− , onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do
576− val <- IDBIndex.get index (IDBKeyRange.only "object_3")
577− val `shouldEqual` (Just "object_3")
576+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
577+ val <- IDBIndex.get index (IDBKeyRange.only "object_3")
578+ val `shouldEqual` (Just "object_3")
578579 }
579580 tearDown db
580581
@@ -588,9 +589,9 @@ main = runMocha do
588589 , keyPath : ["i"]
589590 , values : [ { key: "date", i: (toKey date) } :+: Nothing
590591 ]
591− , onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do
592− val <- IDBIndex.get index (IDBKeyRange.only date)
593− ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
592+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
593+ val <- IDBIndex.get index (IDBKeyRange.only date)
594+ ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
594595 }
595596 tearDown db
596597
@@ -604,9 +605,9 @@ main = runMocha do
604605 , keyPath : ["i"]
605606 , values : [ { key: "num", i: (toKey num) } :+: Nothing
606607 ]
607− , onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do
608− val <- IDBIndex.get index (IDBKeyRange.only num)
609− ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
608+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
609+ val <- IDBIndex.get index (IDBKeyRange.only num)
610+ ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
610611 }
611612 tearDown db
612613
@@ -620,8 +621,28 @@ main = runMocha do
620621 , keyPath : ["i"]
621622 , values : [ { key: "array", i: (toKey array) } :+: Nothing
622623 ]
623− , onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do
624− val <- IDBIndex.get index (IDBKeyRange.only array)
625− ((\r -> r.key) <$> val) `shouldEqual` (Just "array")
624+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
625+ val <- IDBIndex.get index (IDBKeyRange.only array)
626+ ((\r -> r.key) <$> val) `shouldEqual` (Just "array")
627+ }
628+ tearDown db
629+
630+ it "multiEntry - adding keys" do
631+ { db } <- setup
632+ { storeParams : IDBObjectStore.defaultParameters
633+ , indexParams : { unique: false, multiEntry: true }
634+ , keyPath : ["name"]
635+ , values : [ { name: ["patate", "autruche"] } :+: (Just $ toKey 1)
636+ , { name: ["bob"] } :+: (Just $ toKey 2)
637+ ]
638+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
639+ key <- IDBIndex.getKey index (IDBKeyRange.only "patate")
640+ key `shouldEqual` (Just $ toKey 1)
641+
642+ key <- IDBIndex.getKey index (IDBKeyRange.only "autruche")
643+ key `shouldEqual` (Just $ toKey 1)
644+
645+ key <- IDBIndex.getKey index (IDBKeyRange.only "bob")
646+ key `shouldEqual` (Just $ toKey 2)
626647 }
627648 tearDown db