make Key polymorphic in IDBCursor

This commit is contained in:
KtorZ 2017-07-07 11:37:21 +02:00
parent 0f4af3ae25
commit c589d8cfbb
No known key found for this signature in database
GPG Key ID: 3F72E8BC2894C015
2 changed files with 70 additions and 47 deletions

View File

@ -26,6 +26,7 @@ exports._continue = function _continue(cursor, key) {
return function aff(success, error) { return function aff(success, error) {
try { try {
cursor.continue(key || undefined); cursor.continue(key || undefined);
success();
} catch (e) { } catch (e) {
error(new Error(e.name)); error(new Error(e.name));
} }
@ -36,6 +37,7 @@ exports._continuePrimaryKey = function _continuePrimaryKey(cursor, key, primaryK
return function aff(success, error) { return function aff(success, error) {
try { try {
cursor.continuePrimaryKey(key, primaryKey); cursor.continuePrimaryKey(key, primaryKey);
success();
} catch (e) { } catch (e) {
error(new Error(e.name)); error(new Error(e.name));
} }

View File

@ -13,16 +13,17 @@ module Database.IndexedDB.IDBCursor
, value , value
) where ) where
import Prelude (Unit, (>>>)) import Prelude (Unit, ($), (>>>), map)
import Control.Monad.Aff (Aff) import Control.Monad.Aff (Aff)
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)
import Data.Nullable (Nullable, toNullable) import Data.Nullable (Nullable, toNullable)
import Data.Foreign (Foreign, toForeign, unsafeFromForeign) import Data.Foreign (Foreign, toForeign, unsafeFromForeign)
import Database.IndexedDB.Core 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. -- | There is no limit on how many cursors can be used at the same time.
class IDBCursor cursor where class IDBCursor cursor where
-- | Advances the cursor through the next count records in range. -- | 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. -- | 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. -- | 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 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. -- | Update the record pointed at by the cursor with a new value.
-- | -- |
-- | Throws a "DataError" DOMException if the effective object store uses -- | Throws a "DataError" DOMException if the effective object store uses
-- | in-line keys and the key would have changed. -- | 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 -- ATTRIBUTES
-- --
-- | Returns the direction (Next|NextUnique|Prev|PrevUnique) of the cursor. -- | Returns the direction (Next|NextUnique|Prev|PrevUnique) of the cursor.
direction direction'
:: KeyCursor :: KeyCursor
-> CursorDirection -> CursorDirection
direction = direction' =
Fn.runFn2 _direction (parse >>> toNullable) Fn.runFn2 _direction (parse >>> toNullable)
-- | Returns the key of the cursor. Throws a "InvalidStateError" DOMException -- | Returns the key of the cursor. Throws a "InvalidStateError" DOMException
-- | if the cursor is advancing or is finished. -- | if the cursor is advancing or is finished.
key key'
:: forall e :: forall e
. KeyCursor . KeyCursor
-> Aff (idb :: IDB | e) Key -> Aff (idb :: IDB | e) Key
key = key' =
_key _key >>> map Key
-- | Returns the effective key of the cursor. Throws a "InvalidStateError" DOMException -- | Returns the effective key of the cursor. Throws a "InvalidStateError" DOMException
-- | if the cursor is advancing or is finished. -- | if the cursor is advancing or is finished.
primaryKey primaryKey'
:: forall e :: forall e
. KeyCursor . KeyCursor
-> Aff (idb :: IDB | e) Key -> Aff (idb :: IDB | e) Key
primaryKey = primaryKey' =
_primaryKey _primaryKey >>> map Key
-- | Returns the IDBObjectStore or IDBIndex the cursor was opened from. -- | Returns the IDBObjectStore or IDBIndex the cursor was opened from.
source source'
:: KeyCursor :: KeyCursor
-> CursorSource -> CursorSource
source = source' =
Fn.runFn3 _source ObjectStore Index Fn.runFn3 _source ObjectStore Index
@ -97,38 +118,38 @@ source =
-- | Returns the direction (Next|NextUnique|Prev|PrevUnique) of the cursor. -- | Returns the direction (Next|NextUnique|Prev|PrevUnique) of the cursor.
direction' direction
:: ValueCursor :: ValueCursor
-> CursorDirection -> CursorDirection
direction' = direction =
Fn.runFn2 _direction (parse >>> toNullable) Fn.runFn2 _direction (parse >>> toNullable)
-- | Returns the key of the cursor. Throws a "InvalidStateError" DOMException -- | Returns the key of the cursor. Throws a "InvalidStateError" DOMException
-- | if the cursor is advancing or is finished. -- | if the cursor is advancing or is finished.
key' key
:: forall e :: forall e
. ValueCursor . ValueCursor
-> Aff (idb :: IDB | e) Key -> Aff (idb :: IDB | e) Key
key' = key =
_key _key >>> map Key
-- | Returns the effective key of the cursor. Throws a "InvalidStateError" DOMException -- | Returns the effective key of the cursor. Throws a "InvalidStateError" DOMException
-- | if the cursor is advancing or is finished. -- | if the cursor is advancing or is finished.
primaryKey' primaryKey
:: forall e :: forall e
. ValueCursor . ValueCursor
-> Aff (idb :: IDB | e) Key -> Aff (idb :: IDB | e) Key
primaryKey' = primaryKey =
_primaryKey _primaryKey >>> map Key
-- | Returns the IDBObjectStore or IDBIndex the cursor was opened from. -- | Returns the IDBObjectStore or IDBIndex the cursor was opened from.
source' source
:: ValueCursor :: ValueCursor
-> CursorSource -> CursorSource
source' = source =
Fn.runFn3 _source ObjectStore Index Fn.runFn3 _source ObjectStore Index
@ -144,19 +165,19 @@ value =
-- INSTANCES -- INSTANCES
-- --
instance keyCursorKeyCursor :: IDBCursor KeyCursor where instance keyCursorKeyCursor :: IDBCursor KeyCursor where
advance = Fn.runFn2 _advance advance = Fn.runFn2 _advance
continue c mk = Fn.runFn2 _continue c (toNullable mk) continue c mk = Fn.runFn2 _continue c (toNullable $ map (toKey >>> extractForeign) mk)
continuePrimaryKey = Fn.runFn3 _continuePrimaryKey continuePrimaryKey c k1 k2 = Fn.runFn3 _continuePrimaryKey c (extractForeign $ toKey k1) (extractForeign $ toKey k2)
delete = _delete delete = _delete
update c = toForeign >>> Fn.runFn2 _update c update c = toForeign >>> Fn.runFn2 _update c >>> map Key
instance valueCursorKeyCursor :: IDBCursor ValueCursor where instance valueCursorKeyCursor :: IDBCursor ValueCursor where
advance = Fn.runFn2 _advance advance = Fn.runFn2 _advance
continue c mk = Fn.runFn2 _continue c (toNullable mk) continue c mk = Fn.runFn2 _continue c (toNullable $ map (toKey >>> extractForeign) mk)
continuePrimaryKey = Fn.runFn3 _continuePrimaryKey continuePrimaryKey c k1 k2 = Fn.runFn3 _continuePrimaryKey c (extractForeign $ toKey k1) (extractForeign $ toKey k2)
delete = _delete delete = _delete
update c = toForeign >>> Fn.runFn2 _update c update c = toForeign >>> Fn.runFn2 _update c >>> map Key
-------------------- --------------------
@ -169,12 +190,12 @@ foreign import _advance
foreign import _continue foreign import _continue
:: forall cursor e :: forall cursor e
. Fn2 cursor (Nullable Key) (Aff (idb :: IDB | e) Unit) . Fn2 cursor (Nullable Foreign) (Aff (idb :: IDB | e) Unit)
foreign import _continuePrimaryKey foreign import _continuePrimaryKey
:: forall cursor e :: forall cursor e
. Fn3 cursor Key Key (Aff (idb :: IDB | e) Unit) . Fn3 cursor Foreign Foreign (Aff (idb :: IDB | e) Unit)
foreign import _delete foreign import _delete
@ -191,13 +212,13 @@ foreign import _direction
foreign import _key foreign import _key
:: forall cursor e :: forall cursor e
. cursor . cursor
-> Aff (idb :: IDB | e) Key -> Aff (idb :: IDB | e) Foreign
foreign import _primaryKey foreign import _primaryKey
:: forall cursor e :: forall cursor e
. cursor . cursor
-> Aff (idb :: IDB | e) Key -> Aff (idb :: IDB | e) Foreign
foreign import _source foreign import _source
@ -207,7 +228,7 @@ foreign import _source
foreign import _update foreign import _update
:: forall cursor e :: forall cursor e
. Fn2 cursor Foreign (Aff (idb :: IDB | e) Key) . Fn2 cursor Foreign (Aff (idb :: IDB | e) Foreign)
foreign import _value foreign import _value