From a2bf21df62479d83d135e5df6a1ad3380b51e2d5 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Fri, 30 Jun 2017 21:15:20 +0200 Subject: [PATCH] replace all Eff with Aff for consistency and ease of use --- src/Database/IndexedDB/IDBCursor.js | 13 ++- src/Database/IndexedDB/IDBCursor.purs | 22 ++-- src/Database/IndexedDB/IDBDatabase.js | 37 ++++--- src/Database/IndexedDB/IDBDatabase.purs | 35 +++--- src/Database/IndexedDB/IDBObjectStore.js | 19 ++-- src/Database/IndexedDB/IDBObjectStore.purs | 14 +-- src/Database/IndexedDB/IDBTransaction.js | 21 ++-- src/Database/IndexedDB/IDBTransaction.purs | 23 ++-- test/Main.purs | 123 ++++++++++++--------- 9 files changed, 172 insertions(+), 135 deletions(-) diff --git a/src/Database/IndexedDB/IDBCursor.js b/src/Database/IndexedDB/IDBCursor.js index e56a04b..2bf688d 100644 --- a/src/Database/IndexedDB/IDBCursor.js +++ b/src/Database/IndexedDB/IDBCursor.js @@ -12,31 +12,32 @@ const successHandler = function successHandler(cb) { exports._advance = function _advance(cursor, count) { - return function eff() { + return function aff(success, error) { try { cursor.advance(count); + success(); } catch (e) { - throw new Error(e.name); + error(new Error(e.name)); } }; }; exports._continue = function _continue(cursor, key) { - return function eff() { + return function aff(success, error) { try { cursor.continue(key || undefined); } catch (e) { - throw new Error(e.name); + error(new Error(e.name)); } }; }; exports._continuePrimaryKey = function _continuePrimaryKey(cursor, key, primaryKey) { - return function eff() { + return function aff(success, error) { try { cursor.continuePrimaryKey(key, primaryKey); } catch (e) { - throw new Error(e.name); + error(new Error(e.name)); } }; }; diff --git a/src/Database/IndexedDB/IDBCursor.purs b/src/Database/IndexedDB/IDBCursor.purs index 3b99264..42442ca 100644 --- a/src/Database/IndexedDB/IDBCursor.purs +++ b/src/Database/IndexedDB/IDBCursor.purs @@ -14,8 +14,6 @@ module Database.IndexedDB.IDBCursor import Prelude (Unit, (>>>)) import Control.Monad.Aff (Aff) -import Control.Monad.Eff (Eff) -import Control.Monad.Eff.Exception (EXCEPTION) import Data.Function.Uncurried as Fn import Data.Function.Uncurried (Fn2, Fn3) import Data.Maybe (Maybe) @@ -29,11 +27,11 @@ import Database.IndexedDB.Core -- INTERFACES -- class IDBCursor cursor where - advance :: forall e. cursor -> Int -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit - continue :: forall e. cursor -> Maybe Key -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit - continuePrimaryKey :: forall e. cursor -> Key -> Key -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit - delete :: forall e. cursor -> Aff (idb ::IDB, exception :: EXCEPTION | e) Unit - update :: forall val e. cursor -> val -> Aff (idb :: IDB, exception :: EXCEPTION | e) Key + advance :: forall e. cursor -> Int -> Aff (idb :: IDB | e) Unit + continue :: forall e. cursor -> Maybe Key -> Aff (idb :: IDB | e) Unit + continuePrimaryKey :: forall e. cursor -> Key -> Key -> Aff (idb :: IDB | e) Unit + delete :: forall e. cursor -> Aff (idb ::IDB | e) Unit + update :: forall val e. cursor -> val -> Aff (idb :: IDB | e) Key -------------------- @@ -110,16 +108,16 @@ instance valueCursorKeyCursor :: IDBCursor ValueCursor where -------------------- -- FFI -- -foreign import _advance :: forall cursor e. Fn2 cursor Int (Eff (idb :: IDB, exception :: EXCEPTION | e) Unit) +foreign import _advance :: forall cursor e. Fn2 cursor Int (Aff (idb :: IDB | e) Unit) -foreign import _continue :: forall cursor e. Fn2 cursor (Nullable Key) (Eff (idb :: IDB, exception :: EXCEPTION | e) Unit) +foreign import _continue :: forall cursor e. Fn2 cursor (Nullable Key) (Aff (idb :: IDB | e) Unit) -foreign import _continuePrimaryKey :: forall cursor e. Fn3 cursor Key Key (Eff (idb :: IDB, exception :: EXCEPTION | e) Unit) +foreign import _continuePrimaryKey :: forall cursor e. Fn3 cursor Key Key (Aff (idb :: IDB | e) Unit) -foreign import _delete :: forall cursor e. cursor -> (Aff (idb ::IDB, exception :: EXCEPTION | e) Unit) +foreign import _delete :: forall cursor e. cursor -> (Aff (idb ::IDB | e) Unit) foreign import _direction :: forall cursor. Fn2 (String -> Nullable CursorDirection) cursor CursorDirection @@ -134,7 +132,7 @@ foreign import _primaryKey :: forall cursor. cursor -> Key foreign import _source :: forall cursor. Fn3 (ObjectStore -> CursorSource) (Index -> CursorSource) cursor CursorSource -foreign import _update :: forall cursor e. Fn2 cursor Foreign (Aff (idb :: IDB, exception :: EXCEPTION | e) Key) +foreign import _update :: forall cursor e. Fn2 cursor Foreign (Aff (idb :: IDB | e) Key) foreign import _value :: forall cursor val. cursor -> val diff --git a/src/Database/IndexedDB/IDBDatabase.js b/src/Database/IndexedDB/IDBDatabase.js index 4dfea48..855112d 100644 --- a/src/Database/IndexedDB/IDBDatabase.js +++ b/src/Database/IndexedDB/IDBDatabase.js @@ -4,17 +4,18 @@ const toArray = $Core.toArray; exports._close = function _close(db) { - return function eff() { + return function aff(success, error) { try { db.close(); + success(); } catch (e) { - throw new Error(e.name); + error(new Error(e.name)); } }; }; exports._createObjectStore = function _createObjectStore(db, name, opts) { - return function eff() { + return function aff(success, error) { var keyPath; try { @@ -31,22 +32,25 @@ exports._createObjectStore = function _createObjectStore(db, name, opts) { default: keyPath = opts.keyPath; } - return db.createObjectStore(name, { + + const store = db.createObjectStore(name, { autoIncrement: opts.autoIncrement, keyPath: keyPath, }); + success(store); } catch (e) { - throw new Error(e.name); + error(new Error(e.name)); } }; }; exports._deleteObjectStore = function _deleteObjectStore(db, name) { - return function eff() { + return function aff(success, error) { try { db.deleteObjectStore(name); + success(); } catch (e) { - throw new Error(e.name); + error(new Error(e.name)); } }; }; @@ -60,43 +64,48 @@ exports._objectStoreNames = function _objectStoreNames(db) { }; exports._onAbort = function _onAbort(db, f) { - return function eff() { + return function aff(success) { db.onabort = function onabort() { f(); }; + success(); }; }; exports._onClose = function _onClose(db, f) { - return function eff() { + return function aff(success) { db.onclose = function onclose() { f(); }; + success(); }; }; exports._onError = function _onError(db, f) { - return function eff() { + return function aff(success) { db.onerror = function onerror(e) { f(new Error(e.target.error.name))(); }; + success(); }; }; exports._onVersionChange = function _onVersionChange(db, f) { - return function eff() { + return function aff(success) { db.onversionchange = function onversionchange(e) { f({ oldVersion: e.oldVersion, newVersion: e.newVersion })(); }; + success(); }; }; exports._transaction = function _transaction(show, db, stores, mode) { - return function eff() { + return function aff(success, error) { try { - return db.transaction(stores, show(mode)); + const transaction = db.transaction(stores, show(mode)); + success(transaction); } catch (e) { - throw new Error(e.name); + error(new Error(e.name)); } }; }; diff --git a/src/Database/IndexedDB/IDBDatabase.purs b/src/Database/IndexedDB/IDBDatabase.purs index 92a6cde..923b819 100644 --- a/src/Database/IndexedDB/IDBDatabase.purs +++ b/src/Database/IndexedDB/IDBDatabase.purs @@ -12,8 +12,9 @@ module Database.IndexedDB.IDBDatabase import Prelude (Unit, show) +import Control.Monad.Aff (Aff) import Control.Monad.Eff (Eff) -import Control.Monad.Eff.Exception (EXCEPTION, Error) +import Control.Monad.Eff.Exception (Error) import Data.Function.Uncurried as Fn import Data.Function.Uncurried (Fn2, Fn3, Fn4) @@ -25,10 +26,10 @@ import Database.IndexedDB.IDBObjectStore (IDBObjectStoreParameters) -- INTERFACE -- class IDBDatabase db where - close :: forall e. db -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit - createObjectStore :: forall e. db -> StoreName -> IDBObjectStoreParameters -> Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore - deleteObjectStore :: forall e. db -> StoreName -> Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore - transaction :: forall e. db -> KeyPath -> TransactionMode -> Eff (idb :: IDB, exception :: EXCEPTION | e) Transaction + close :: forall e. db -> Aff (idb :: IDB | e) Unit + createObjectStore :: forall e. db -> StoreName -> IDBObjectStoreParameters -> 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 type StoreName = String @@ -55,22 +56,22 @@ version = -------------------- -- EVENT HANDLERS -- -onAbort :: forall e e'. Database -> Eff ( | e') Unit -> Eff (idb :: IDB | e) Unit +onAbort :: forall e e'. Database -> Eff ( | e') Unit -> Aff (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 :: forall e e'. Database -> Eff ( | e') Unit -> Aff (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 :: forall e e'. Database -> (Error -> Eff ( | e') Unit) -> Aff (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 :: forall e e'. Database -> ({ oldVersion :: Int, newVersion :: Int } -> Eff ( | e') Unit) -> Aff (idb :: IDB | e) Unit onVersionChange db f = Fn.runFn2 _onVersionChange db f @@ -96,13 +97,13 @@ instance idbDatabaseDatabase :: IDBDatabase Database where -------------------- -- FFI -- -foreign import _close :: forall db e. db -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit +foreign import _close :: forall db e. db -> Aff (idb :: IDB | e) Unit -foreign import _createObjectStore :: forall db e. Fn3 db String { keyPath :: Array String, autoIncrement :: Boolean } (Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore) +foreign import _createObjectStore :: forall db e. Fn3 db String { keyPath :: Array String, autoIncrement :: Boolean } (Aff (idb :: IDB | e) ObjectStore) -foreign import _deleteObjectStore :: forall db e. Fn2 db String (Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore) +foreign import _deleteObjectStore :: forall db e. Fn2 db String (Aff (idb :: IDB | e) ObjectStore) foreign import _name :: Database -> String @@ -111,19 +112,19 @@ 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 _onAbort :: forall db e e'. Fn2 db (Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit) -foreign import _onClose :: 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) (Aff (idb :: IDB | e) Unit) -foreign import _onError :: forall db e e'. Fn2 db (Error -> Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit) +foreign import _onError :: forall db e e'. Fn2 db (Error -> Eff ( | e') Unit) (Aff (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 _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 (Eff (idb :: IDB, exception :: EXCEPTION | e) Transaction) +foreign import _transaction :: forall db e. Fn4 (db -> String) db (Array String) TransactionMode (Aff (idb :: IDB | e) Transaction) foreign import _version :: Database -> Int diff --git a/src/Database/IndexedDB/IDBObjectStore.js b/src/Database/IndexedDB/IDBObjectStore.js index 8f839ee..c49969e 100644 --- a/src/Database/IndexedDB/IDBObjectStore.js +++ b/src/Database/IndexedDB/IDBObjectStore.js @@ -36,7 +36,7 @@ exports._clear = function _clear(store) { }; exports._createIndex = function _createIndex(store, name, path, params) { - return function eff() { + return function aff(success, error) { var keyPath; try { @@ -53,19 +53,21 @@ exports._createIndex = function _createIndex(store, name, path, params) { keyPath = path; } - return store.createIndex(name, keyPath, params); + const index = store.createIndex(name, keyPath, params); + success(index); } catch (e) { - throw new Error(e.name); + error(new Error(e.name)); } }; }; exports._deleteIndex = function _deleteIndex(store, name) { - return function eff() { + return function aff(success, error) { try { store.deleteIndex(name); + success(); } catch (e) { - throw new Error(e.name); + error(new Error(e.name)); } }; }; @@ -79,11 +81,12 @@ exports._delete = function _delete(store, query) { }; exports._index = function _index(store, name) { - return function eff() { + return function aff(success, error) { try { - return store.index(name); + const index = store.index(name); + success(index); } catch (e) { - throw new Error(e.name); + error(new Error(e.name)); } }; }; diff --git a/src/Database/IndexedDB/IDBObjectStore.purs b/src/Database/IndexedDB/IDBObjectStore.purs index 9d9a494..2c35aa0 100644 --- a/src/Database/IndexedDB/IDBObjectStore.purs +++ b/src/Database/IndexedDB/IDBObjectStore.purs @@ -14,8 +14,6 @@ module Database.IndexedDB.IDBObjectStore import Prelude (Unit, ($), (<$>), (>>>)) import Control.Monad.Aff (Aff) -import Control.Monad.Eff (Eff) -import Control.Monad.Eff.Exception (EXCEPTION) import Data.Foreign (Foreign) import Data.Function.Uncurried as Fn import Data.Function.Uncurried (Fn2, Fn3, Fn4) @@ -33,10 +31,10 @@ import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key(Key), extractFore class IDBObjectStore store where add :: forall v k e. (IDBKey k) => store -> v -> Maybe k -> Aff (idb :: IDB | e) Key clear :: forall e. store -> Aff (idb :: IDB | e) Unit - createIndex :: forall e. store -> IndexName -> KeyPath -> IDBIndexParameters -> Eff (idb :: IDB, exception :: EXCEPTION | e) Index + createIndex :: forall e. store -> IndexName -> KeyPath -> IDBIndexParameters -> Aff (idb :: IDB | e) Index delete :: forall e. store -> KeyRange -> Aff (idb :: IDB | e) Unit - deleteIndex :: forall e. store -> IndexName -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit - index :: forall e. store -> IndexName -> Eff (idb :: IDB, exception :: EXCEPTION | e) Index + deleteIndex :: forall e. store -> IndexName -> Aff (idb :: IDB | e) Unit + index :: forall e. store -> IndexName -> Aff (idb :: IDB | e) Index put :: forall v k e. (IDBKey k) => store -> v -> Maybe k -> Aff (idb :: IDB | e) Key @@ -122,16 +120,16 @@ foreign import _autoIncrement :: ObjectStore -> Boolean foreign import _clear :: forall e. ObjectStore -> Aff (idb :: IDB | e) Unit -foreign import _createIndex :: forall e. Fn4 ObjectStore String (Array String) { unique :: Boolean, multiEntry :: Boolean } (Eff (idb :: IDB | e) Index) +foreign import _createIndex :: forall e. Fn4 ObjectStore String (Array String) { unique :: Boolean, multiEntry :: Boolean } (Aff (idb :: IDB | e) Index) foreign import _delete :: forall e. Fn2 ObjectStore KeyRange (Aff (idb :: IDB | e) Unit) -foreign import _deleteIndex :: forall e. Fn2 ObjectStore String (Eff (idb :: IDB | e) Unit) +foreign import _deleteIndex :: forall e. Fn2 ObjectStore String (Aff (idb :: IDB | e) Unit) -foreign import _index :: forall e. Fn2 ObjectStore String (Eff (idb :: IDB | e) Index) +foreign import _index :: forall e. Fn2 ObjectStore String (Aff (idb :: IDB | e) Index) foreign import _indexNames :: ObjectStore -> Array String diff --git a/src/Database/IndexedDB/IDBTransaction.js b/src/Database/IndexedDB/IDBTransaction.js index 0e60a7f..404ee33 100644 --- a/src/Database/IndexedDB/IDBTransaction.js +++ b/src/Database/IndexedDB/IDBTransaction.js @@ -1,9 +1,10 @@ exports._abort = function _abort(tx) { - return function eff() { + return function aff(success, error) { try { tx.abort(); + success(); } catch (e) { - throw new Error(e.name); + error(new Error(e.name)); } }; }; @@ -25,35 +26,39 @@ exports._mode = function _mode(ReadOnly, ReadWrite, VersionChange, tx) { }; exports._objectStore = function _objectStore(tx, name) { - return function eff() { + return function aff(success, error) { try { - return tx.objectStore(name); + const store = tx.objectStore(name); + success(store); } catch (e) { - throw new Error(e.name); + error(new Error(e.name)); } }; }; exports._onAbort = function _onAbort(tx, f) { - return function eff() { + return function aff(success) { tx.onabort = function onabort() { f(); }; + success(); }; }; exports._onComplete = function _onComplete(tx, f) { - return function eff() { + return function aff(success) { tx.oncomplete = function oncomplete() { f(); }; + success(); }; }; exports._onError = function _onError(tx, f) { - return function eff() { + return function aff(success) { tx.onerror = function onerror(e) { f(new Error(e.target.error.name))(); }; + success(); }; }; diff --git a/src/Database/IndexedDB/IDBTransaction.purs b/src/Database/IndexedDB/IDBTransaction.purs index 4055968..9d9a14c 100644 --- a/src/Database/IndexedDB/IDBTransaction.purs +++ b/src/Database/IndexedDB/IDBTransaction.purs @@ -9,8 +9,9 @@ module Database.IndexedDB.IDBTransaction import Prelude (Unit, (>>>)) +import Control.Monad.Aff (Aff) import Control.Monad.Eff (Eff) -import Control.Monad.Eff.Exception (EXCEPTION, Error) +import Control.Monad.Eff.Exception (Error) import Data.Function.Uncurried as Fn import Data.Function.Uncurried (Fn2, Fn4) import Data.Maybe (Maybe) @@ -23,8 +24,8 @@ import Database.IndexedDB.Core (IDB, ObjectStore, Transaction, TransactionMo -- INTERFACES -- class IDBTransaction tx where - abort :: forall e. tx -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit - objectStore :: forall e. tx -> String -> Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore + abort :: forall e. tx -> Aff (idb :: IDB | e) Unit + objectStore :: forall e. tx -> String -> Aff (idb :: IDB | e) ObjectStore -------------------- @@ -43,17 +44,17 @@ mode = -------------------- -- EVENT HANDLERS -- -onAbort :: forall e e'. Transaction -> Eff ( | e') Unit -> Eff (idb :: IDB | e) Unit +onAbort :: forall e e'. Transaction -> Eff ( | e') Unit -> Aff (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 :: forall e e'. Transaction -> Eff ( | e') Unit -> Aff (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 :: forall e e'. Transaction -> (Error -> Eff ( | e') Unit) -> Aff (idb :: IDB | e) Unit onError db f = Fn.runFn2 _onError db f @@ -72,7 +73,7 @@ instance idbTransactionTransaction :: IDBTransaction Transaction where -------------------- -- FFI -- -foreign import _abort :: forall tx e. tx -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit +foreign import _abort :: forall tx e. tx -> Aff (idb :: IDB | e) Unit foreign import _error :: Transaction -> (Nullable Error) @@ -81,13 +82,13 @@ foreign import _error :: Transaction -> (Nullable Error) foreign import _mode :: Fn4 TransactionMode TransactionMode TransactionMode Transaction TransactionMode -foreign import _objectStore :: forall tx e. Fn2 tx String (Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore) +foreign import _objectStore :: forall tx e. Fn2 tx String (Aff (idb :: IDB | e) ObjectStore) -foreign import _onAbort :: forall tx e e'. Fn2 tx (Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit) +foreign import _onAbort :: forall tx e e'. Fn2 tx (Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit) -foreign import _onComplete :: 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) (Aff (idb :: IDB | e) Unit) -foreign import _onError :: forall tx e e'. Fn2 tx (Error -> Eff ( | e') Unit) (Eff (idb :: IDB | e) Unit) +foreign import _onError :: forall tx e e'. Fn2 tx (Error -> Eff ( | e') Unit) (Aff (idb :: IDB | e) Unit) diff --git a/test/Main.purs b/test/Main.purs index 9fe0676..efa5a08 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -34,12 +34,16 @@ import Database.IndexedDB.IDBTransaction as IDBTransaction infixr 7 Tuple as :+: +launchAff' :: forall a e. Aff e a -> Eff (exception :: EXCEPTION | e) Unit +launchAff' aff = + pure unit <* (launchAff aff) + main :: forall eff. Eff (now :: NOW, mocha :: MOCHA, idb :: IDB, exception :: EXCEPTION, avar :: AVAR | eff) Unit main = runMocha do describe "IDBFactory" do let tearDown name version db = do - liftEff $ IDBDatabase.close db + IDBDatabase.close db version' <- IDBFactory.deleteDatabase name version' `shouldEqual` version @@ -73,7 +77,7 @@ main = runMocha do , onBlocked : Nothing } IDBDatabase.name db `shouldEqual` name - liftEff $ IDBDatabase.close db + IDBDatabase.close db db <- IDBFactory.open name Nothing { onUpgradeNeeded : Nothing , onBlocked : Nothing @@ -111,7 +115,7 @@ main = runMocha do } _ <- forkAff do delay (Milliseconds 100.0) - liftEff $ IDBDatabase.close db01 + IDBDatabase.close db01 db02 <- IDBFactory.open name (Just version) { onUpgradeNeeded : Nothing @@ -277,14 +281,14 @@ main = runMocha do describe "IDBDatabase" do let tearDown db = do - liftEff $ IDBDatabase.close db + IDBDatabase.close db _ <- IDBFactory.deleteDatabase (IDBDatabase.name db) pure unit setup storeParams = do - let onUpgradeNeeded var db _ = pure unit <* do + let onUpgradeNeeded var db _ = launchAff' do store <- IDBDatabase.createObjectStore db "store" storeParams - _ <- (launchAff $ putVar var { db, store }) + _ <- putVar var { db, store } pure unit var <- makeVar @@ -320,14 +324,13 @@ main = runMocha do tearDown db it "deleteObjectStore" do - let onUpgradeNeeded var db _ = do + let onUpgradeNeeded var db _ = launchAff' do _ <- IDBDatabase.deleteObjectStore db "store" - _ <- (launchAff $ putVar var true) - pure unit + putVar var true var <- makeVar { db, store } <- setup IDBObjectStore.defaultParameters - liftEff $ IDBDatabase.close db + IDBDatabase.close db db <- IDBFactory.open "db" (Just 999) { onUpgradeNeeded : Just (onUpgradeNeeded var) , onBlocked : Nothing } @@ -339,16 +342,15 @@ main = runMocha do describe "IDBObjectStore" do let tearDown db = do - liftEff $ IDBDatabase.close db + IDBDatabase.close db _ <- IDBFactory.deleteDatabase (IDBDatabase.name db) pure unit setup { storeParams, onUpgradeNeeded } = do - let onUpgradeNeeded' var db _ = pure unit <* do + let onUpgradeNeeded' var db _ = launchAff' do store <- IDBDatabase.createObjectStore db "store" storeParams - launchAff do - liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store) - putVar var { db, store } + liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store) + putVar var { db, store } var <- makeVar db <- IDBFactory.open "db" Nothing @@ -362,7 +364,7 @@ main = runMocha do date <- liftEff $ toDateTime <$> now { db } <- setup { storeParams: { autoIncrement: true, keyPath: [] } - , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do + , onUpgradeNeeded: Just $ \_ store -> launchAff' do -- no key key <- IDBObjectStore.add store "patate" (Nothing :: Maybe Key) (toKey 1) `shouldEqual` key @@ -386,48 +388,48 @@ main = runMocha do -- array key key <- IDBObjectStore.add store "patate" (Just $ toKey [14, 42]) (toKey [14, 42]) `shouldEqual` key - )} + } tearDown db it "clear()" do { db } <- setup { storeParams: IDBObjectStore.defaultParameters - , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do + , onUpgradeNeeded: Just $ \_ store -> launchAff' do key <- IDBObjectStore.add store "patate" (Just 14) _ <- IDBObjectStore.clear store val <- IDBObjectStore.get store (IDBKeyRange.only key) val `shouldEqual` (Nothing :: Maybe String) - )} + } tearDown db it "count()" do { db } <- setup { storeParams: IDBObjectStore.defaultParameters - , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do + , onUpgradeNeeded: Just $ \_ store -> launchAff' do key <- IDBObjectStore.add store "patate" (Just 14) key <- IDBObjectStore.add store "autruche" (Just 42) n <- IDBObjectStore.count store Nothing n `shouldEqual` 2 - )} + } tearDown db it "getKey()" do { db } <- setup { storeParams: IDBObjectStore.defaultParameters - , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do + , onUpgradeNeeded: Just $ \_ store -> launchAff' do key <- IDBObjectStore.add store "patate" (Just 14) mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 14) mkey `shouldEqual` (Just key) mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 42) mkey `shouldEqual` (Nothing :: Maybe Key) - )} + } tearDown db it "getAllKeys()" do { db } <- setup { storeParams: IDBObjectStore.defaultParameters - , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do + , onUpgradeNeeded: Just $ \_ store -> launchAff' do key1 <- IDBObjectStore.add store "patate" (Just 14) key2 <- IDBObjectStore.add store "autruche" (Just 42) key3 <- IDBObjectStore.add store 14 (Just 1337) @@ -447,39 +449,39 @@ main = runMocha do -- count keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2) keys `shouldEqual` [key1, key2] - )} + } tearDown db it "openCursor()" do { db } <- setup { storeParams: IDBObjectStore.defaultParameters - , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do + , onUpgradeNeeded: Just $ \_ store -> launchAff' do _ <- IDBObjectStore.openCursor store Nothing Next _ <- IDBObjectStore.openCursor store Nothing NextUnique _ <- IDBObjectStore.openCursor store Nothing Prev _ <- IDBObjectStore.openCursor store Nothing PrevUnique _ <- IDBObjectStore.openCursor store (Just $ IDBKeyRange.upperBound 1 true) Next pure unit - )} + } tearDown db it "openKeyCursor()" do { db } <- setup { storeParams: IDBObjectStore.defaultParameters - , onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do + , onUpgradeNeeded: Just $ \_ store -> launchAff' do _ <- IDBObjectStore.openKeyCursor store Nothing Next _ <- IDBObjectStore.openKeyCursor store Nothing NextUnique _ <- IDBObjectStore.openKeyCursor store Nothing Prev _ <- IDBObjectStore.openKeyCursor store Nothing PrevUnique _ <- IDBObjectStore.openKeyCursor store (Just $ IDBKeyRange.lowerBound 1 true) Next pure unit - )} + } tearDown db describe "IDBIndex" do let tearDown db = do - liftEff $ IDBDatabase.close db + IDBDatabase.close db _ <- IDBFactory.deleteDatabase (IDBDatabase.name db) pure unit @@ -491,13 +493,12 @@ main = runMocha do , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit) } -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore } setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do - let onUpgradeNeeded' var db tx = pure unit <* do + let onUpgradeNeeded' var db tx = launchAff' do store <- IDBDatabase.createObjectStore db "store" storeParams - launchAff do - _ <- traverse (uncurry (IDBObjectStore.add store)) values - index <- liftEff $ IDBObjectStore.createIndex store "index" keyPath indexParams - liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index) - putVar var { db, index, store } + _ <- traverse (uncurry (IDBObjectStore.add store)) values + index <- IDBObjectStore.createIndex store "index" keyPath indexParams + liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index) + putVar var { db, index, store } var <- makeVar db <- IDBFactory.open "db" Nothing @@ -522,7 +523,7 @@ main = runMocha do tearDown db it "attempt to create an index that requires unique values on an object store already contains duplicates" do - let onAbort var = launchAff (putVar var true) *> pure unit + let onAbort var = launchAff' (putVar var true) txVar <- makeVar dbVar <- makeVar res <- attempt $ setup @@ -534,7 +535,7 @@ main = runMocha do , values : [ { indexedProperty: "bar" } :+: (Just $ toKey 1) , { indexedProperty: "bar" } :+: (Just $ toKey 2) ] - , onUpgradeNeeded : Just $ \db tx _ -> do + , onUpgradeNeeded : Just $ \db tx _ -> launchAff' do IDBTransaction.onAbort tx (onAbort txVar) IDBDatabase.onAbort db (onAbort dbVar) } @@ -557,7 +558,7 @@ main = runMocha do , { key: "key2", indexedProperty: "indexed_2" } :+: Nothing , { key: "key3", indexedProperty: "indexed_3" } :+: Nothing ] - , onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do + , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do val <- IDBIndex.get index (IDBKeyRange.only "indexed_2") ((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2") } @@ -572,9 +573,9 @@ main = runMocha do , "object_2" :+: (Just $ toKey 2) , "object_3" :+: (Just $ toKey 3) ] - , onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do - val <- IDBIndex.get index (IDBKeyRange.only "object_3") - val `shouldEqual` (Just "object_3") + , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do + val <- IDBIndex.get index (IDBKeyRange.only "object_3") + val `shouldEqual` (Just "object_3") } tearDown db @@ -588,9 +589,9 @@ main = runMocha do , keyPath : ["i"] , values : [ { key: "date", i: (toKey date) } :+: Nothing ] - , onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do - val <- IDBIndex.get index (IDBKeyRange.only date) - ((\r -> r.key) <$> val) `shouldEqual` (Just "date") + , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do + val <- IDBIndex.get index (IDBKeyRange.only date) + ((\r -> r.key) <$> val) `shouldEqual` (Just "date") } tearDown db @@ -604,9 +605,9 @@ main = runMocha do , keyPath : ["i"] , values : [ { key: "num", i: (toKey num) } :+: Nothing ] - , onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do - val <- IDBIndex.get index (IDBKeyRange.only num) - ((\r -> r.key) <$> val) `shouldEqual` (Just "num") + , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do + val <- IDBIndex.get index (IDBKeyRange.only num) + ((\r -> r.key) <$> val) `shouldEqual` (Just "num") } tearDown db @@ -620,8 +621,28 @@ main = runMocha do , keyPath : ["i"] , values : [ { key: "array", i: (toKey array) } :+: Nothing ] - , onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do - val <- IDBIndex.get index (IDBKeyRange.only array) - ((\r -> r.key) <$> val) `shouldEqual` (Just "array") + , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do + val <- IDBIndex.get index (IDBKeyRange.only array) + ((\r -> r.key) <$> val) `shouldEqual` (Just "array") + } + tearDown db + + it "multiEntry - adding keys" do + { db } <- setup + { storeParams : IDBObjectStore.defaultParameters + , indexParams : { unique: false, multiEntry: true } + , keyPath : ["name"] + , values : [ { name: ["patate", "autruche"] } :+: (Just $ toKey 1) + , { name: ["bob"] } :+: (Just $ toKey 2) + ] + , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do + key <- IDBIndex.getKey index (IDBKeyRange.only "patate") + key `shouldEqual` (Just $ toKey 1) + + key <- IDBIndex.getKey index (IDBKeyRange.only "autruche") + key `shouldEqual` (Just $ toKey 1) + + key <- IDBIndex.getKey index (IDBKeyRange.only "bob") + key `shouldEqual` (Just $ toKey 2) } tearDown db