@@ -13,16 +13,17 @@ module Database.IndexedDB.IDBCursor
1313 , value
1414 ) where
1515
16−import Prelude (Unit, (>>>))
16+import Prelude (Unit, ($), (>>>), map)
1717
18−import Control.Monad.Aff (Aff)
19−import Data.Function.Uncurried as Fn
20−import Data.Function.Uncurried (Fn2, Fn3)
21−import Data.Maybe (Maybe)
22−import Data.Nullable (Nullable, toNullable)
23−import Data.Foreign (Foreign, toForeign, unsafeFromForeign)
18+import Control.Monad.Aff (Aff)
19+import Data.Function.Uncurried as Fn
20+import Data.Function.Uncurried (Fn2, Fn3)
21+import Data.Maybe (Maybe)
22+import Data.Nullable (Nullable, toNullable)
23+import Data.Foreign (Foreign, toForeign, unsafeFromForeign)
2424
2525import Database.IndexedDB.Core
26+import Database.IndexedDB.IDBKey.Internal (class IDBKey, Key(..), toKey, extractForeign)
2627
2728
2829--------------------
@@ -33,60 +34,80 @@ import Database.IndexedDB.Core
3334-- | There is no limit on how many cursors can be used at the same time.
3435class IDBCursor cursor where
3536 -- | Advances the cursor through the next count records in range.
36− advance :: forall e. cursor -> Int -> Aff (idb :: IDB | e) Unit
37+ advance
38+ :: forall e
39+ . cursor
40+ -> Int
41+ -> Aff (idb :: IDB | e) Unit
3742
3843 -- | Advances the cursor to the next record in range matching or after key.
39− continue :: forall e. cursor -> Maybe Key -> Aff (idb :: IDB | e) Unit
44+ continue
45+ :: forall e k. (IDBKey k)
46+ => cursor
47+ -> Maybe k
48+ -> Aff (idb :: IDB | e) Unit
4049
4150 -- | 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.
42− continuePrimaryKey :: forall e. cursor -> Key -> Key -> Aff (idb :: IDB | e) Unit
51+ continuePrimaryKey
52+ :: forall e k. (IDBKey k)
53+ => cursor
54+ -> k
55+ -> k
56+ -> Aff (idb :: IDB | e) Unit
4357
4458 -- | Delete the record pointed at by the cursor with a new value.
45− delete :: forall e. cursor -> Aff (idb :: IDB | e) Unit
59+ delete
60+ :: forall e
61+ . cursor
62+ -> Aff (idb :: IDB | e) Unit
4663
4764 -- | Update the record pointed at by the cursor with a new value.
4865 -- |
4966 -- | Throws a "DataError" DOMException if the effective object store uses
5067 -- | in-line keys and the key would have changed.
51− update :: forall val e. cursor -> val -> Aff (idb :: IDB | e) Key
68+ update
69+ :: forall val e
70+ . cursor
71+ -> val
72+ -> Aff (idb :: IDB | e) Key
5273
5374
5475--------------------
5576-- ATTRIBUTES
5677--
5778-- | Returns the direction (Next|NextUnique|Prev|PrevUnique) of the cursor.
58−direction
79+direction'
5980 :: KeyCursor
6081 -> CursorDirection
61−direction =
82+direction' =
6283 Fn.runFn2 _direction (parse >>> toNullable)
6384
6485
6586-- | Returns the key of the cursor. Throws a "InvalidStateError" DOMException
6687-- | if the cursor is advancing or is finished.
67−key
88+key'
6889 :: forall e
6990 . KeyCursor
7091 -> Aff (idb :: IDB | e) Key
71−key =
72− _key
92+key' =
93+ _key >>> map Key
7394
7495
7596-- | Returns the effective key of the cursor. Throws a "InvalidStateError" DOMException
7697-- | if the cursor is advancing or is finished.
77−primaryKey
98+primaryKey'
7899 :: forall e
79100 . KeyCursor
80101 -> Aff (idb :: IDB | e) Key
81−primaryKey =
82− _primaryKey
102+primaryKey' =
103+ _primaryKey >>> map Key
83104
84105
85106-- | Returns the IDBObjectStore or IDBIndex the cursor was opened from.
86−source
107+source'
87108 :: KeyCursor
88109 -> CursorSource
89−source =
110+source' =
90111 Fn.runFn3 _source ObjectStore Index
91112
92113
@@ -97,38 +118,38 @@ source =
97118
98119
99120-- | Returns the direction (Next|NextUnique|Prev|PrevUnique) of the cursor.
100−direction'
121+direction
101122 :: ValueCursor
102123 -> CursorDirection
103−direction' =
124+direction =
104125 Fn.runFn2 _direction (parse >>> toNullable)
105126
106127
107128-- | Returns the key of the cursor. Throws a "InvalidStateError" DOMException
108129-- | if the cursor is advancing or is finished.
109−key'
130+key
110131 :: forall e
111132 . ValueCursor
112133 -> Aff (idb :: IDB | e) Key
113−key' =
114− _key
134+key =
135+ _key >>> map Key
115136
116137
117138-- | Returns the effective key of the cursor. Throws a "InvalidStateError" DOMException
118139-- | if the cursor is advancing or is finished.
119−primaryKey'
140+primaryKey
120141 :: forall e
121142 . ValueCursor
122143 -> Aff (idb :: IDB | e) Key
123−primaryKey' =
124− _primaryKey
144+primaryKey =
145+ _primaryKey >>> map Key
125146
126147
127148-- | Returns the IDBObjectStore or IDBIndex the cursor was opened from.
128−source'
149+source
129150 :: ValueCursor
130151 -> CursorSource
131−source' =
152+source =
132153 Fn.runFn3 _source ObjectStore Index
133154
134155
@@ -144,19 +165,19 @@ value =
144165-- INSTANCES
145166--
146167instance keyCursorKeyCursor :: IDBCursor KeyCursor where
147− advance = Fn.runFn2 _advance
148− continue c mk = Fn.runFn2 _continue c (toNullable mk)
149− continuePrimaryKey = Fn.runFn3 _continuePrimaryKey
150− delete = _delete
151− update c = toForeign >>> Fn.runFn2 _update c
168+ advance = Fn.runFn2 _advance
169+ continue c mk = Fn.runFn2 _continue c (toNullable $ map (toKey >>> extractForeign) mk)
170+ continuePrimaryKey c k1 k2 = Fn.runFn3 _continuePrimaryKey c (extractForeign $ toKey k1) (extractForeign $ toKey k2)
171+ delete = _delete
172+ update c = toForeign >>> Fn.runFn2 _update c >>> map Key
152173
153174
154175instance valueCursorKeyCursor :: IDBCursor ValueCursor where
155− advance = Fn.runFn2 _advance
156− continue c mk = Fn.runFn2 _continue c (toNullable mk)
157− continuePrimaryKey = Fn.runFn3 _continuePrimaryKey
158− delete = _delete
159− update c = toForeign >>> Fn.runFn2 _update c
176+ advance = Fn.runFn2 _advance
177+ continue c mk = Fn.runFn2 _continue c (toNullable $ map (toKey >>> extractForeign) mk)
178+ continuePrimaryKey c k1 k2 = Fn.runFn3 _continuePrimaryKey c (extractForeign $ toKey k1) (extractForeign $ toKey k2)
179+ delete = _delete
180+ update c = toForeign >>> Fn.runFn2 _update c >>> map Key
160181
161182
162183--------------------
@@ -169,12 +190,12 @@ foreign import _advance
169190
170191foreign import _continue
171192 :: forall cursor e
172− . Fn2 cursor (Nullable Key) (Aff (idb :: IDB | e) Unit)
193+ . Fn2 cursor (Nullable Foreign) (Aff (idb :: IDB | e) Unit)
173194
174195
175196foreign import _continuePrimaryKey
176197 :: forall cursor e
177− . Fn3 cursor Key Key (Aff (idb :: IDB | e) Unit)
198+ . Fn3 cursor Foreign Foreign (Aff (idb :: IDB | e) Unit)
178199
179200
180201foreign import _delete
@@ -191,13 +212,13 @@ foreign import _direction
191212foreign import _key
192213 :: forall cursor e
193214 . cursor
194− -> Aff (idb :: IDB | e) Key
215+ -> Aff (idb :: IDB | e) Foreign
195216
196217
197218foreign import _primaryKey
198219 :: forall cursor e
199220 . cursor
200− -> Aff (idb :: IDB | e) Key
221+ -> Aff (idb :: IDB | e) Foreign
201222
202223
203224foreign import _source
@@ -207,7 +228,7 @@ foreign import _source
207228
208229foreign import _update
209230 :: forall cursor e
210− . Fn2 cursor Foreign (Aff (idb :: IDB | e) Key)
231+ . Fn2 cursor Foreign (Aff (idb :: IDB | e) Foreign)
211232
212233
213234foreign import _value