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) {
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));
}

View File

@ -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