From c589d8cfbbf76788ba0e2cb8039f90400cab0ab5 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Fri, 7 Jul 2017 11:37:21 +0200 Subject: [PATCH] make Key polymorphic in IDBCursor --- src/Database/IndexedDB/IDBCursor.js | 2 + src/Database/IndexedDB/IDBCursor.purs | 115 +++++++++++++++----------- 2 files changed, 70 insertions(+), 47 deletions(-) diff --git a/src/Database/IndexedDB/IDBCursor.js b/src/Database/IndexedDB/IDBCursor.js index 740e9f6..8845888 100644 --- a/src/Database/IndexedDB/IDBCursor.js +++ b/src/Database/IndexedDB/IDBCursor.js @@ -26,6 +26,7 @@ exports._continue = function _continue(cursor, key) { return function aff(success, error) { try { cursor.continue(key || undefined); + success(); } catch (e) { error(new Error(e.name)); } @@ -36,6 +37,7 @@ exports._continuePrimaryKey = function _continuePrimaryKey(cursor, key, primaryK return function aff(success, error) { try { cursor.continuePrimaryKey(key, primaryKey); + success(); } catch (e) { error(new Error(e.name)); } diff --git a/src/Database/IndexedDB/IDBCursor.purs b/src/Database/IndexedDB/IDBCursor.purs index ccd96c0..76a4611 100644 --- a/src/Database/IndexedDB/IDBCursor.purs +++ b/src/Database/IndexedDB/IDBCursor.purs @@ -13,16 +13,17 @@ module Database.IndexedDB.IDBCursor , value ) where -import Prelude (Unit, (>>>)) +import Prelude (Unit, ($), (>>>), map) -import Control.Monad.Aff (Aff) -import Data.Function.Uncurried as Fn -import Data.Function.Uncurried (Fn2, Fn3) -import Data.Maybe (Maybe) -import Data.Nullable (Nullable, toNullable) -import Data.Foreign (Foreign, toForeign, unsafeFromForeign) +import Control.Monad.Aff (Aff) +import Data.Function.Uncurried as Fn +import Data.Function.Uncurried (Fn2, Fn3) +import Data.Maybe (Maybe) +import Data.Nullable (Nullable, toNullable) +import Data.Foreign (Foreign, toForeign, unsafeFromForeign) import Database.IndexedDB.Core +import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key(..), toKey, extractForeign) -------------------- @@ -33,60 +34,80 @@ import Database.IndexedDB.Core -- | There is no limit on how many cursors can be used at the same time. class IDBCursor cursor where -- | Advances the cursor through the next count records in range. - advance :: forall e. cursor -> Int -> Aff (idb :: IDB | e) Unit + advance + :: forall e + . cursor + -> Int + -> Aff (idb :: IDB | e) Unit -- | Advances the cursor to the next record in range matching or after key. - continue :: forall e. cursor -> Maybe Key -> Aff (idb :: IDB | e) Unit + continue + :: forall e k. (IDBKey k) + => cursor + -> Maybe k + -> Aff (idb :: IDB | e) Unit -- | Advances the cursor to the next record in range matching or after key and primaryKey. Throws an "InvalidAccessError" DOMException if the source is not an index. - continuePrimaryKey :: forall e. cursor -> Key -> Key -> Aff (idb :: IDB | e) Unit + continuePrimaryKey + :: forall e k. (IDBKey k) + => cursor + -> k + -> k + -> Aff (idb :: IDB | e) Unit -- | Delete the record pointed at by the cursor with a new value. - delete :: forall e. cursor -> Aff (idb :: IDB | e) Unit + delete + :: forall e + . cursor + -> Aff (idb :: IDB | e) Unit -- | Update the record pointed at by the cursor with a new value. -- | -- | Throws a "DataError" DOMException if the effective object store uses -- | in-line keys and the key would have changed. - update :: forall val e. cursor -> val -> Aff (idb :: IDB | e) Key + update + :: forall val e + . cursor + -> val + -> Aff (idb :: IDB | e) Key -------------------- -- ATTRIBUTES -- -- | Returns the direction (Next|NextUnique|Prev|PrevUnique) of the cursor. -direction +direction' :: KeyCursor -> CursorDirection -direction = +direction' = Fn.runFn2 _direction (parse >>> toNullable) -- | Returns the key of the cursor. Throws a "InvalidStateError" DOMException -- | if the cursor is advancing or is finished. -key +key' :: forall e . KeyCursor -> Aff (idb :: IDB | e) Key -key = - _key +key' = + _key >>> map Key -- | Returns the effective key of the cursor. Throws a "InvalidStateError" DOMException -- | if the cursor is advancing or is finished. -primaryKey +primaryKey' :: forall e . KeyCursor -> Aff (idb :: IDB | e) Key -primaryKey = - _primaryKey +primaryKey' = + _primaryKey >>> map Key -- | Returns the IDBObjectStore or IDBIndex the cursor was opened from. -source +source' :: KeyCursor -> CursorSource -source = +source' = Fn.runFn3 _source ObjectStore Index @@ -97,38 +118,38 @@ source = -- | Returns the direction (Next|NextUnique|Prev|PrevUnique) of the cursor. -direction' +direction :: ValueCursor -> CursorDirection -direction' = +direction = Fn.runFn2 _direction (parse >>> toNullable) -- | Returns the key of the cursor. Throws a "InvalidStateError" DOMException -- | if the cursor is advancing or is finished. -key' +key :: forall e . ValueCursor -> Aff (idb :: IDB | e) Key -key' = - _key +key = + _key >>> map Key -- | Returns the effective key of the cursor. Throws a "InvalidStateError" DOMException -- | if the cursor is advancing or is finished. -primaryKey' +primaryKey :: forall e . ValueCursor -> Aff (idb :: IDB | e) Key -primaryKey' = - _primaryKey +primaryKey = + _primaryKey >>> map Key -- | Returns the IDBObjectStore or IDBIndex the cursor was opened from. -source' +source :: ValueCursor -> CursorSource -source' = +source = Fn.runFn3 _source ObjectStore Index @@ -144,19 +165,19 @@ value = -- INSTANCES -- instance keyCursorKeyCursor :: IDBCursor KeyCursor where - advance = Fn.runFn2 _advance - continue c mk = Fn.runFn2 _continue c (toNullable mk) - continuePrimaryKey = Fn.runFn3 _continuePrimaryKey - delete = _delete - update c = toForeign >>> Fn.runFn2 _update c + advance = Fn.runFn2 _advance + continue c mk = Fn.runFn2 _continue c (toNullable $ map (toKey >>> extractForeign) mk) + continuePrimaryKey c k1 k2 = Fn.runFn3 _continuePrimaryKey c (extractForeign $ toKey k1) (extractForeign $ toKey k2) + delete = _delete + update c = toForeign >>> Fn.runFn2 _update c >>> map Key instance valueCursorKeyCursor :: IDBCursor ValueCursor where - advance = Fn.runFn2 _advance - continue c mk = Fn.runFn2 _continue c (toNullable mk) - continuePrimaryKey = Fn.runFn3 _continuePrimaryKey - delete = _delete - update c = toForeign >>> Fn.runFn2 _update c + advance = Fn.runFn2 _advance + continue c mk = Fn.runFn2 _continue c (toNullable $ map (toKey >>> extractForeign) mk) + continuePrimaryKey c k1 k2 = Fn.runFn3 _continuePrimaryKey c (extractForeign $ toKey k1) (extractForeign $ toKey k2) + delete = _delete + update c = toForeign >>> Fn.runFn2 _update c >>> map Key -------------------- @@ -169,12 +190,12 @@ foreign import _advance foreign import _continue :: forall cursor e - . Fn2 cursor (Nullable Key) (Aff (idb :: IDB | e) Unit) + . Fn2 cursor (Nullable Foreign) (Aff (idb :: IDB | e) Unit) foreign import _continuePrimaryKey :: forall cursor e - . Fn3 cursor Key Key (Aff (idb :: IDB | e) Unit) + . Fn3 cursor Foreign Foreign (Aff (idb :: IDB | e) Unit) foreign import _delete @@ -191,13 +212,13 @@ foreign import _direction foreign import _key :: forall cursor e . cursor - -> Aff (idb :: IDB | e) Key + -> Aff (idb :: IDB | e) Foreign foreign import _primaryKey :: forall cursor e . cursor - -> Aff (idb :: IDB | e) Key + -> Aff (idb :: IDB | e) Foreign foreign import _source @@ -207,7 +228,7 @@ foreign import _source foreign import _update :: forall cursor e - . Fn2 cursor Foreign (Aff (idb :: IDB | e) Key) + . Fn2 cursor Foreign (Aff (idb :: IDB | e) Foreign) foreign import _value