replace all Eff with Aff for consistency and ease of use

This commit is contained in:
KtorZ 2017-06-30 21:15:20 +02:00
parent 8dcfdda43b
commit a2bf21df62
No known key found for this signature in database
GPG Key ID: 3F72E8BC2894C015
9 changed files with 172 additions and 135 deletions

View File

@ -12,31 +12,32 @@ const successHandler = function successHandler(cb) {
exports._advance = function _advance(cursor, count) { exports._advance = function _advance(cursor, count) {
return function eff() { return function aff(success, error) {
try { try {
cursor.advance(count); cursor.advance(count);
success();
} catch (e) { } catch (e) {
throw new Error(e.name); error(new Error(e.name));
} }
}; };
}; };
exports._continue = function _continue(cursor, key) { exports._continue = function _continue(cursor, key) {
return function eff() { return function aff(success, error) {
try { try {
cursor.continue(key || undefined); cursor.continue(key || undefined);
} catch (e) { } catch (e) {
throw new Error(e.name); error(new Error(e.name));
} }
}; };
}; };
exports._continuePrimaryKey = function _continuePrimaryKey(cursor, key, primaryKey) { exports._continuePrimaryKey = function _continuePrimaryKey(cursor, key, primaryKey) {
return function eff() { return function aff(success, error) {
try { try {
cursor.continuePrimaryKey(key, primaryKey); cursor.continuePrimaryKey(key, primaryKey);
} catch (e) { } catch (e) {
throw new Error(e.name); error(new Error(e.name));
} }
}; };
}; };

View File

@ -14,8 +14,6 @@ module Database.IndexedDB.IDBCursor
import Prelude (Unit, (>>>)) import Prelude (Unit, (>>>))
import Control.Monad.Aff (Aff) 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 as Fn
import Data.Function.Uncurried (Fn2, Fn3) import Data.Function.Uncurried (Fn2, Fn3)
import Data.Maybe (Maybe) import Data.Maybe (Maybe)
@ -29,11 +27,11 @@ import Database.IndexedDB.Core
-- INTERFACES -- INTERFACES
-- --
class IDBCursor cursor where class IDBCursor cursor where
advance :: forall e. cursor -> Int -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit advance :: forall e. cursor -> Int -> Aff (idb :: IDB | e) Unit
continue :: forall e. cursor -> Maybe Key -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit continue :: forall e. cursor -> Maybe Key -> Aff (idb :: IDB | e) Unit
continuePrimaryKey :: forall e. cursor -> Key -> Key -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit continuePrimaryKey :: forall e. cursor -> Key -> Key -> Aff (idb :: IDB | e) Unit
delete :: forall e. cursor -> Aff (idb ::IDB, exception :: EXCEPTION | e) Unit delete :: forall e. cursor -> Aff (idb ::IDB | e) Unit
update :: forall val e. cursor -> val -> Aff (idb :: IDB, exception :: EXCEPTION | e) Key update :: forall val e. cursor -> val -> Aff (idb :: IDB | e) Key
-------------------- --------------------
@ -110,16 +108,16 @@ instance valueCursorKeyCursor :: IDBCursor ValueCursor where
-------------------- --------------------
-- FFI -- 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 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 _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 foreign import _value :: forall cursor val. cursor -> val

View File

@ -4,17 +4,18 @@ const toArray = $Core.toArray;
exports._close = function _close(db) { exports._close = function _close(db) {
return function eff() { return function aff(success, error) {
try { try {
db.close(); db.close();
success();
} catch (e) { } catch (e) {
throw new Error(e.name); error(new Error(e.name));
} }
}; };
}; };
exports._createObjectStore = function _createObjectStore(db, name, opts) { exports._createObjectStore = function _createObjectStore(db, name, opts) {
return function eff() { return function aff(success, error) {
var keyPath; var keyPath;
try { try {
@ -31,22 +32,25 @@ exports._createObjectStore = function _createObjectStore(db, name, opts) {
default: default:
keyPath = opts.keyPath; keyPath = opts.keyPath;
} }
return db.createObjectStore(name, {
const store = db.createObjectStore(name, {
autoIncrement: opts.autoIncrement, autoIncrement: opts.autoIncrement,
keyPath: keyPath, keyPath: keyPath,
}); });
success(store);
} catch (e) { } catch (e) {
throw new Error(e.name); error(new Error(e.name));
} }
}; };
}; };
exports._deleteObjectStore = function _deleteObjectStore(db, name) { exports._deleteObjectStore = function _deleteObjectStore(db, name) {
return function eff() { return function aff(success, error) {
try { try {
db.deleteObjectStore(name); db.deleteObjectStore(name);
success();
} catch (e) { } 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) { exports._onAbort = function _onAbort(db, f) {
return function eff() { return function aff(success) {
db.onabort = function onabort() { db.onabort = function onabort() {
f(); f();
}; };
success();
}; };
}; };
exports._onClose = function _onClose(db, f) { exports._onClose = function _onClose(db, f) {
return function eff() { return function aff(success) {
db.onclose = function onclose() { db.onclose = function onclose() {
f(); f();
}; };
success();
}; };
}; };
exports._onError = function _onError(db, f) { exports._onError = function _onError(db, f) {
return function eff() { return function aff(success) {
db.onerror = function onerror(e) { db.onerror = function onerror(e) {
f(new Error(e.target.error.name))(); f(new Error(e.target.error.name))();
}; };
success();
}; };
}; };
exports._onVersionChange = function _onVersionChange(db, f) { exports._onVersionChange = function _onVersionChange(db, f) {
return function eff() { return function aff(success) {
db.onversionchange = function onversionchange(e) { db.onversionchange = function onversionchange(e) {
f({ oldVersion: e.oldVersion, newVersion: e.newVersion })(); f({ oldVersion: e.oldVersion, newVersion: e.newVersion })();
}; };
success();
}; };
}; };
exports._transaction = function _transaction(show, db, stores, mode) { exports._transaction = function _transaction(show, db, stores, mode) {
return function eff() { return function aff(success, error) {
try { try {
return db.transaction(stores, show(mode)); const transaction = db.transaction(stores, show(mode));
success(transaction);
} catch (e) { } catch (e) {
throw new Error(e.name); error(new Error(e.name));
} }
}; };
}; };

View File

@ -12,8 +12,9 @@ module Database.IndexedDB.IDBDatabase
import Prelude (Unit, show) import Prelude (Unit, show)
import Control.Monad.Aff (Aff)
import Control.Monad.Eff (Eff) 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 as Fn
import Data.Function.Uncurried (Fn2, Fn3, Fn4) import Data.Function.Uncurried (Fn2, Fn3, Fn4)
@ -25,10 +26,10 @@ import Database.IndexedDB.IDBObjectStore (IDBObjectStoreParameters)
-- INTERFACE -- INTERFACE
-- --
class IDBDatabase db where class IDBDatabase db where
close :: forall e. db -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit close :: forall e. db -> Aff (idb :: IDB | e) Unit
createObjectStore :: forall e. db -> StoreName -> IDBObjectStoreParameters -> Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore createObjectStore :: forall e. db -> StoreName -> IDBObjectStoreParameters -> Aff (idb :: IDB | e) ObjectStore
deleteObjectStore :: forall e. db -> StoreName -> Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore deleteObjectStore :: forall e. db -> StoreName -> Aff (idb :: IDB | e) ObjectStore
transaction :: forall e. db -> KeyPath -> TransactionMode -> Eff (idb :: IDB, exception :: EXCEPTION | e) Transaction transaction :: forall e. db -> KeyPath -> TransactionMode -> Aff (idb :: IDB | e) Transaction
type StoreName = String type StoreName = String
@ -55,22 +56,22 @@ version =
-------------------- --------------------
-- EVENT HANDLERS -- 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 = onAbort db f =
Fn.runFn2 _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 = onClose db f =
Fn.runFn2 _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 = onError db f =
Fn.runFn2 _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 = onVersionChange db f =
Fn.runFn2 _onVersionChange db f Fn.runFn2 _onVersionChange db f
@ -96,13 +97,13 @@ instance idbDatabaseDatabase :: IDBDatabase Database where
-------------------- --------------------
-- FFI -- 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 foreign import _name :: Database -> String
@ -111,19 +112,19 @@ foreign import _name :: Database -> String
foreign import _objectStoreNames :: Database -> Array 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 foreign import _version :: Database -> Int

View File

@ -36,7 +36,7 @@ exports._clear = function _clear(store) {
}; };
exports._createIndex = function _createIndex(store, name, path, params) { exports._createIndex = function _createIndex(store, name, path, params) {
return function eff() { return function aff(success, error) {
var keyPath; var keyPath;
try { try {
@ -53,19 +53,21 @@ exports._createIndex = function _createIndex(store, name, path, params) {
keyPath = path; keyPath = path;
} }
return store.createIndex(name, keyPath, params); const index = store.createIndex(name, keyPath, params);
success(index);
} catch (e) { } catch (e) {
throw new Error(e.name); error(new Error(e.name));
} }
}; };
}; };
exports._deleteIndex = function _deleteIndex(store, name) { exports._deleteIndex = function _deleteIndex(store, name) {
return function eff() { return function aff(success, error) {
try { try {
store.deleteIndex(name); store.deleteIndex(name);
success();
} catch (e) { } 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) { exports._index = function _index(store, name) {
return function eff() { return function aff(success, error) {
try { try {
return store.index(name); const index = store.index(name);
success(index);
} catch (e) { } catch (e) {
throw new Error(e.name); error(new Error(e.name));
} }
}; };
}; };

View File

@ -14,8 +14,6 @@ module Database.IndexedDB.IDBObjectStore
import Prelude (Unit, ($), (<$>), (>>>)) import Prelude (Unit, ($), (<$>), (>>>))
import Control.Monad.Aff (Aff) import Control.Monad.Aff (Aff)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Exception (EXCEPTION)
import Data.Foreign (Foreign) import Data.Foreign (Foreign)
import Data.Function.Uncurried as Fn import Data.Function.Uncurried as Fn
import Data.Function.Uncurried (Fn2, Fn3, Fn4) 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 class IDBObjectStore store where
add :: forall v k e. (IDBKey k) => store -> v -> Maybe k -> Aff (idb :: IDB | e) Key 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 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 delete :: forall e. store -> KeyRange -> Aff (idb :: IDB | e) Unit
deleteIndex :: forall e. store -> IndexName -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit deleteIndex :: forall e. store -> IndexName -> Aff (idb :: IDB | e) Unit
index :: forall e. store -> IndexName -> Eff (idb :: IDB, exception :: EXCEPTION | e) Index 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 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 _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 _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 foreign import _indexNames :: ObjectStore -> Array String

View File

@ -1,9 +1,10 @@
exports._abort = function _abort(tx) { exports._abort = function _abort(tx) {
return function eff() { return function aff(success, error) {
try { try {
tx.abort(); tx.abort();
success();
} catch (e) { } 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) { exports._objectStore = function _objectStore(tx, name) {
return function eff() { return function aff(success, error) {
try { try {
return tx.objectStore(name); const store = tx.objectStore(name);
success(store);
} catch (e) { } catch (e) {
throw new Error(e.name); error(new Error(e.name));
} }
}; };
}; };
exports._onAbort = function _onAbort(tx, f) { exports._onAbort = function _onAbort(tx, f) {
return function eff() { return function aff(success) {
tx.onabort = function onabort() { tx.onabort = function onabort() {
f(); f();
}; };
success();
}; };
}; };
exports._onComplete = function _onComplete(tx, f) { exports._onComplete = function _onComplete(tx, f) {
return function eff() { return function aff(success) {
tx.oncomplete = function oncomplete() { tx.oncomplete = function oncomplete() {
f(); f();
}; };
success();
}; };
}; };
exports._onError = function _onError(tx, f) { exports._onError = function _onError(tx, f) {
return function eff() { return function aff(success) {
tx.onerror = function onerror(e) { tx.onerror = function onerror(e) {
f(new Error(e.target.error.name))(); f(new Error(e.target.error.name))();
}; };
success();
}; };
}; };

View File

@ -9,8 +9,9 @@ module Database.IndexedDB.IDBTransaction
import Prelude (Unit, (>>>)) import Prelude (Unit, (>>>))
import Control.Monad.Aff (Aff)
import Control.Monad.Eff (Eff) 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 as Fn
import Data.Function.Uncurried (Fn2, Fn4) import Data.Function.Uncurried (Fn2, Fn4)
import Data.Maybe (Maybe) import Data.Maybe (Maybe)
@ -23,8 +24,8 @@ import Database.IndexedDB.Core (IDB, ObjectStore, Transaction, TransactionMo
-- INTERFACES -- INTERFACES
-- --
class IDBTransaction tx where class IDBTransaction tx where
abort :: forall e. tx -> Eff (idb :: IDB, exception :: EXCEPTION | e) Unit abort :: forall e. tx -> Aff (idb :: IDB | e) Unit
objectStore :: forall e. tx -> String -> Eff (idb :: IDB, exception :: EXCEPTION | e) ObjectStore objectStore :: forall e. tx -> String -> Aff (idb :: IDB | e) ObjectStore
-------------------- --------------------
@ -43,17 +44,17 @@ mode =
-------------------- --------------------
-- EVENT HANDLERS -- 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 = onAbort db f =
Fn.runFn2 _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 = onComplete db f =
Fn.runFn2 _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 = onError db f =
Fn.runFn2 _onError db f Fn.runFn2 _onError db f
@ -72,7 +73,7 @@ instance idbTransactionTransaction :: IDBTransaction Transaction where
-------------------- --------------------
-- FFI -- 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) 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 _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)

View File

@ -34,12 +34,16 @@ import Database.IndexedDB.IDBTransaction as IDBTransaction
infixr 7 Tuple as :+: 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 :: forall eff. Eff (now :: NOW, mocha :: MOCHA, idb :: IDB, exception :: EXCEPTION, avar :: AVAR | eff) Unit
main = runMocha do main = runMocha do
describe "IDBFactory" do describe "IDBFactory" do
let let
tearDown name version db = do tearDown name version db = do
liftEff $ IDBDatabase.close db IDBDatabase.close db
version' <- IDBFactory.deleteDatabase name version' <- IDBFactory.deleteDatabase name
version' `shouldEqual` version version' `shouldEqual` version
@ -73,7 +77,7 @@ main = runMocha do
, onBlocked : Nothing , onBlocked : Nothing
} }
IDBDatabase.name db `shouldEqual` name IDBDatabase.name db `shouldEqual` name
liftEff $ IDBDatabase.close db IDBDatabase.close db
db <- IDBFactory.open name Nothing db <- IDBFactory.open name Nothing
{ onUpgradeNeeded : Nothing { onUpgradeNeeded : Nothing
, onBlocked : Nothing , onBlocked : Nothing
@ -111,7 +115,7 @@ main = runMocha do
} }
_ <- forkAff do _ <- forkAff do
delay (Milliseconds 100.0) delay (Milliseconds 100.0)
liftEff $ IDBDatabase.close db01 IDBDatabase.close db01
db02 <- IDBFactory.open name (Just version) db02 <- IDBFactory.open name (Just version)
{ onUpgradeNeeded : Nothing { onUpgradeNeeded : Nothing
@ -277,14 +281,14 @@ main = runMocha do
describe "IDBDatabase" do describe "IDBDatabase" do
let let
tearDown db = do tearDown db = do
liftEff $ IDBDatabase.close db IDBDatabase.close db
_ <- IDBFactory.deleteDatabase (IDBDatabase.name db) _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
pure unit pure unit
setup storeParams = do setup storeParams = do
let onUpgradeNeeded var db _ = pure unit <* do let onUpgradeNeeded var db _ = launchAff' do
store <- IDBDatabase.createObjectStore db "store" storeParams store <- IDBDatabase.createObjectStore db "store" storeParams
_ <- (launchAff $ putVar var { db, store }) _ <- putVar var { db, store }
pure unit pure unit
var <- makeVar var <- makeVar
@ -320,14 +324,13 @@ main = runMocha do
tearDown db tearDown db
it "deleteObjectStore" do it "deleteObjectStore" do
let onUpgradeNeeded var db _ = do let onUpgradeNeeded var db _ = launchAff' do
_ <- IDBDatabase.deleteObjectStore db "store" _ <- IDBDatabase.deleteObjectStore db "store"
_ <- (launchAff $ putVar var true) putVar var true
pure unit
var <- makeVar var <- makeVar
{ db, store } <- setup IDBObjectStore.defaultParameters { db, store } <- setup IDBObjectStore.defaultParameters
liftEff $ IDBDatabase.close db IDBDatabase.close db
db <- IDBFactory.open "db" (Just 999) { onUpgradeNeeded : Just (onUpgradeNeeded var) db <- IDBFactory.open "db" (Just 999) { onUpgradeNeeded : Just (onUpgradeNeeded var)
, onBlocked : Nothing , onBlocked : Nothing
} }
@ -339,16 +342,15 @@ main = runMocha do
describe "IDBObjectStore" do describe "IDBObjectStore" do
let let
tearDown db = do tearDown db = do
liftEff $ IDBDatabase.close db IDBDatabase.close db
_ <- IDBFactory.deleteDatabase (IDBDatabase.name db) _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
pure unit pure unit
setup { storeParams, onUpgradeNeeded } = do setup { storeParams, onUpgradeNeeded } = do
let onUpgradeNeeded' var db _ = pure unit <* do let onUpgradeNeeded' var db _ = launchAff' do
store <- IDBDatabase.createObjectStore db "store" storeParams store <- IDBDatabase.createObjectStore db "store" storeParams
launchAff do liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store)
liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store) putVar var { db, store }
putVar var { db, store }
var <- makeVar var <- makeVar
db <- IDBFactory.open "db" Nothing db <- IDBFactory.open "db" Nothing
@ -362,7 +364,7 @@ main = runMocha do
date <- liftEff $ toDateTime <$> now date <- liftEff $ toDateTime <$> now
{ db } <- setup { db } <- setup
{ storeParams: { autoIncrement: true, keyPath: [] } { storeParams: { autoIncrement: true, keyPath: [] }
, onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do , onUpgradeNeeded: Just $ \_ store -> launchAff' do
-- no key -- no key
key <- IDBObjectStore.add store "patate" (Nothing :: Maybe Key) key <- IDBObjectStore.add store "patate" (Nothing :: Maybe Key)
(toKey 1) `shouldEqual` key (toKey 1) `shouldEqual` key
@ -386,48 +388,48 @@ main = runMocha do
-- array key -- array key
key <- IDBObjectStore.add store "patate" (Just $ toKey [14, 42]) key <- IDBObjectStore.add store "patate" (Just $ toKey [14, 42])
(toKey [14, 42]) `shouldEqual` key (toKey [14, 42]) `shouldEqual` key
)} }
tearDown db tearDown db
it "clear()" do it "clear()" do
{ db } <- setup { db } <- setup
{ storeParams: IDBObjectStore.defaultParameters { 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 "patate" (Just 14)
_ <- IDBObjectStore.clear store _ <- IDBObjectStore.clear store
val <- IDBObjectStore.get store (IDBKeyRange.only key) val <- IDBObjectStore.get store (IDBKeyRange.only key)
val `shouldEqual` (Nothing :: Maybe String) val `shouldEqual` (Nothing :: Maybe String)
)} }
tearDown db tearDown db
it "count()" do it "count()" do
{ db } <- setup { db } <- setup
{ storeParams: IDBObjectStore.defaultParameters { 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 "patate" (Just 14)
key <- IDBObjectStore.add store "autruche" (Just 42) key <- IDBObjectStore.add store "autruche" (Just 42)
n <- IDBObjectStore.count store Nothing n <- IDBObjectStore.count store Nothing
n `shouldEqual` 2 n `shouldEqual` 2
)} }
tearDown db tearDown db
it "getKey()" do it "getKey()" do
{ db } <- setup { db } <- setup
{ storeParams: IDBObjectStore.defaultParameters { 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 "patate" (Just 14)
mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 14) mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 14)
mkey `shouldEqual` (Just key) mkey `shouldEqual` (Just key)
mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 42) mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
mkey `shouldEqual` (Nothing :: Maybe Key) mkey `shouldEqual` (Nothing :: Maybe Key)
)} }
tearDown db tearDown db
it "getAllKeys()" do it "getAllKeys()" do
{ db } <- setup { db } <- setup
{ storeParams: IDBObjectStore.defaultParameters { storeParams: IDBObjectStore.defaultParameters
, onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do , onUpgradeNeeded: Just $ \_ store -> launchAff' do
key1 <- IDBObjectStore.add store "patate" (Just 14) key1 <- IDBObjectStore.add store "patate" (Just 14)
key2 <- IDBObjectStore.add store "autruche" (Just 42) key2 <- IDBObjectStore.add store "autruche" (Just 42)
key3 <- IDBObjectStore.add store 14 (Just 1337) key3 <- IDBObjectStore.add store 14 (Just 1337)
@ -447,39 +449,39 @@ main = runMocha do
-- count -- count
keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2) keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
keys `shouldEqual` [key1, key2] keys `shouldEqual` [key1, key2]
)} }
tearDown db tearDown db
it "openCursor()" do it "openCursor()" do
{ db } <- setup { db } <- setup
{ storeParams: IDBObjectStore.defaultParameters { storeParams: IDBObjectStore.defaultParameters
, onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do , onUpgradeNeeded: Just $ \_ store -> launchAff' do
_ <- IDBObjectStore.openCursor store Nothing Next _ <- IDBObjectStore.openCursor store Nothing Next
_ <- IDBObjectStore.openCursor store Nothing NextUnique _ <- IDBObjectStore.openCursor store Nothing NextUnique
_ <- IDBObjectStore.openCursor store Nothing Prev _ <- IDBObjectStore.openCursor store Nothing Prev
_ <- IDBObjectStore.openCursor store Nothing PrevUnique _ <- IDBObjectStore.openCursor store Nothing PrevUnique
_ <- IDBObjectStore.openCursor store (Just $ IDBKeyRange.upperBound 1 true) Next _ <- IDBObjectStore.openCursor store (Just $ IDBKeyRange.upperBound 1 true) Next
pure unit pure unit
)} }
tearDown db tearDown db
it "openKeyCursor()" do it "openKeyCursor()" do
{ db } <- setup { db } <- setup
{ storeParams: IDBObjectStore.defaultParameters { storeParams: IDBObjectStore.defaultParameters
, onUpgradeNeeded: Just $ \_ store -> pure unit <* (launchAff do , onUpgradeNeeded: Just $ \_ store -> launchAff' do
_ <- IDBObjectStore.openKeyCursor store Nothing Next _ <- IDBObjectStore.openKeyCursor store Nothing Next
_ <- IDBObjectStore.openKeyCursor store Nothing NextUnique _ <- IDBObjectStore.openKeyCursor store Nothing NextUnique
_ <- IDBObjectStore.openKeyCursor store Nothing Prev _ <- IDBObjectStore.openKeyCursor store Nothing Prev
_ <- IDBObjectStore.openKeyCursor store Nothing PrevUnique _ <- IDBObjectStore.openKeyCursor store Nothing PrevUnique
_ <- IDBObjectStore.openKeyCursor store (Just $ IDBKeyRange.lowerBound 1 true) Next _ <- IDBObjectStore.openKeyCursor store (Just $ IDBKeyRange.lowerBound 1 true) Next
pure unit pure unit
)} }
tearDown db tearDown db
describe "IDBIndex" do describe "IDBIndex" do
let let
tearDown db = do tearDown db = do
liftEff $ IDBDatabase.close db IDBDatabase.close db
_ <- IDBFactory.deleteDatabase (IDBDatabase.name db) _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
pure unit pure unit
@ -491,13 +493,12 @@ main = runMocha do
, onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit) , 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 } } -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do 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 store <- IDBDatabase.createObjectStore db "store" storeParams
launchAff do _ <- traverse (uncurry (IDBObjectStore.add store)) values
_ <- traverse (uncurry (IDBObjectStore.add store)) values index <- IDBObjectStore.createIndex store "index" keyPath indexParams
index <- liftEff $ IDBObjectStore.createIndex store "index" keyPath indexParams liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index) putVar var { db, index, store }
putVar var { db, index, store }
var <- makeVar var <- makeVar
db <- IDBFactory.open "db" Nothing db <- IDBFactory.open "db" Nothing
@ -522,7 +523,7 @@ main = runMocha do
tearDown db tearDown db
it "attempt to create an index that requires unique values on an object store already contains duplicates" do 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 txVar <- makeVar
dbVar <- makeVar dbVar <- makeVar
res <- attempt $ setup res <- attempt $ setup
@ -534,7 +535,7 @@ main = runMocha do
, values : [ { indexedProperty: "bar" } :+: (Just $ toKey 1) , values : [ { indexedProperty: "bar" } :+: (Just $ toKey 1)
, { indexedProperty: "bar" } :+: (Just $ toKey 2) , { indexedProperty: "bar" } :+: (Just $ toKey 2)
] ]
, onUpgradeNeeded : Just $ \db tx _ -> do , onUpgradeNeeded : Just $ \db tx _ -> launchAff' do
IDBTransaction.onAbort tx (onAbort txVar) IDBTransaction.onAbort tx (onAbort txVar)
IDBDatabase.onAbort db (onAbort dbVar) IDBDatabase.onAbort db (onAbort dbVar)
} }
@ -557,7 +558,7 @@ main = runMocha do
, { key: "key2", indexedProperty: "indexed_2" } :+: Nothing , { key: "key2", indexedProperty: "indexed_2" } :+: Nothing
, { key: "key3", indexedProperty: "indexed_3" } :+: 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") val <- IDBIndex.get index (IDBKeyRange.only "indexed_2")
((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2") ((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2")
} }
@ -572,9 +573,9 @@ main = runMocha do
, "object_2" :+: (Just $ toKey 2) , "object_2" :+: (Just $ toKey 2)
, "object_3" :+: (Just $ toKey 3) , "object_3" :+: (Just $ toKey 3)
] ]
, onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
val <- IDBIndex.get index (IDBKeyRange.only "object_3") val <- IDBIndex.get index (IDBKeyRange.only "object_3")
val `shouldEqual` (Just "object_3") val `shouldEqual` (Just "object_3")
} }
tearDown db tearDown db
@ -588,9 +589,9 @@ main = runMocha do
, keyPath : ["i"] , keyPath : ["i"]
, values : [ { key: "date", i: (toKey date) } :+: Nothing , values : [ { key: "date", i: (toKey date) } :+: Nothing
] ]
, onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
val <- IDBIndex.get index (IDBKeyRange.only date) val <- IDBIndex.get index (IDBKeyRange.only date)
((\r -> r.key) <$> val) `shouldEqual` (Just "date") ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
} }
tearDown db tearDown db
@ -604,9 +605,9 @@ main = runMocha do
, keyPath : ["i"] , keyPath : ["i"]
, values : [ { key: "num", i: (toKey num) } :+: Nothing , values : [ { key: "num", i: (toKey num) } :+: Nothing
] ]
, onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
val <- IDBIndex.get index (IDBKeyRange.only num) val <- IDBIndex.get index (IDBKeyRange.only num)
((\r -> r.key) <$> val) `shouldEqual` (Just "num") ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
} }
tearDown db tearDown db
@ -620,8 +621,28 @@ main = runMocha do
, keyPath : ["i"] , keyPath : ["i"]
, values : [ { key: "array", i: (toKey array) } :+: Nothing , values : [ { key: "array", i: (toKey array) } :+: Nothing
] ]
, onUpgradeNeeded : Just $ \_ _ index -> const unit <$> launchAff do , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
val <- IDBIndex.get index (IDBKeyRange.only array) val <- IDBIndex.get index (IDBKeyRange.only array)
((\r -> r.key) <$> val) `shouldEqual` (Just "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 tearDown db