@@ -2,1060 +2,995 @@ module Test.Main where
3import Prelude
3import Prelude
5−import Data.Array (head, drop)
5+import Control.Monad.Aff (Aff, launchAff, forkAff, delay, attempt)
6+import Control.Monad.Aff.AVar (AVAR, makeVar, makeVar', modifyVar, peekVar, putVar, takeVar)
7+import Control.Monad.Aff.Console (log)
8+import Control.Monad.Eff (Eff)
9+import Control.Monad.Eff.Class (liftEff)
10+import Control.Monad.Eff.Exception (EXCEPTION, message)
11+import Control.Monad.Eff.Now (NOW, now)
6−import Data.Date as Date
12+import Data.Date as Date
7−import Data.DateTime (DateTime(..), Time(..))
13+import Data.DateTime as DateTime
14+import Data.DateTime (DateTime(..), Time(..))
8−import Data.DateTime.Instant (toDateTime)
15+import Data.DateTime.Instant (toDateTime)
9−import Data.Either (Either(..))
16+import Data.Either (Either(..))
10−import Data.Enum (toEnum)
17+import Data.Enum (toEnum)
11−import Data.Maybe (Maybe(..), isNothing, maybe)
18+import Data.Maybe (Maybe(..), isNothing, maybe)
19+import Data.Array (head, drop)
12−import Data.Time.Duration (Milliseconds(..))
20+import Data.Time.Duration (Milliseconds(..))
13−import Data.Traversable (traverse)
21+import Data.Traversable (traverse)
14−import Data.Tuple (Tuple(..), uncurry)
22+import Data.Tuple (Tuple(..), uncurry)
15−import Database.IndexedDB.Core (CursorDirection(..), Database, Index, ObjectStore, Transaction, TransactionMode(..))
16−import Database.IndexedDB.IDBCursor as IDBCursor
17−import Database.IndexedDB.IDBDatabase as IDBDatabase
18−import Database.IndexedDB.IDBFactory as IDBFactory
19−import Database.IndexedDB.IDBIndex as IDBIndex
20−import Database.IndexedDB.IDBKey (Key, none, toKey)
21−import Database.IndexedDB.IDBKeyRange as IDBKeyRange
22−import Database.IndexedDB.IDBObjectStore as IDBObjectStore
23−import Database.IndexedDB.IDBTransaction as IDBTransaction
24−import Effect (Effect)
25−import Effect.Aff (Aff, attempt, delay, forkAff, launchAff, launchAff_)
26−import Effect.Aff.AVar (AVar)
27−import Effect.Aff.AVar as AVar
28−import Effect.Class (liftEffect)
29−import Effect.Console (log)
30−import Effect.Exception (name)
31−import Effect.Now (now)
32−import Test.Spec (describe, it)
23+import Test.Spec (describe, describeOnly, it)
33−import Test.Spec.Assertions (shouldEqual, fail)
24+import Test.Spec.Assertions (shouldEqual, fail)
34−import Test.Spec.Reporter (consoleReporter)
25+import Test.Spec.Mocha (MOCHA, runMocha)
35−import Test.Spec.Runner (runSpec)
26+
27+import Database.IndexedDB.Core
28+import Database.IndexedDB.IDBKey
29+import Database.IndexedDB.IDBCursor as IDBCursor
30+import Database.IndexedDB.IDBDatabase as IDBDatabase
31+import Database.IndexedDB.IDBFactory as IDBFactory
32+import Database.IndexedDB.IDBIndex as IDBIndex
33+import Database.IndexedDB.IDBKeyRange as IDBKeyRange
34+import Database.IndexedDB.IDBObjectStore as IDBObjectStore
35+import Database.IndexedDB.IDBTransaction as IDBTransaction
39−launchAff' :: forall a. Aff a -> Effect Unit
38+infixr 7 Tuple as :+:
43−modifyAVar :: forall a. (a -> a) -> AVar a -> Aff Unit
40+launchAff' :: forall a e. Aff e a -> Eff (exception :: EXCEPTION | e) Unit
44−modifyAVar fn v = do
41+launchAff' aff =
45− val <- AVar.take v
42+ pure unit <* (launchAff aff)
48−main :: Effect Unit
44+-- main :: forall eff. Eff (now :: NOW, mocha :: MOCHA, idb :: IDB, exception :: EXCEPTION, avar :: AVAR | eff) Unit
49−main = do
45+main = runMocha do
50− launchAff_ $ runSpec [ consoleReporter ] $ do
51− describe "IDBFactory" do
46+ describe "IDBFactory" do
53 tearDown name version db = do
48 tearDown name version db = do
54 IDBDatabase.close db
49 IDBDatabase.close db
55 version' <- IDBFactory.deleteDatabase name
50 version' <- IDBFactory.deleteDatabase name
56− liftEffect $ log $ show version'
57 version' `shouldEqual` version
51 version' `shouldEqual` version
59− it "open default" do
53+ it "open default" do
61− name = "db-default"
54+ let name = "db-default"
62 version = 1
55 version = 1
63− db <- IDBFactory.open name Nothing
56+ db <- IDBFactory.open name Nothing
64− { onUpgradeNeeded: Nothing
57+ { onUpgradeNeeded : Nothing
65− , onBlocked: Nothing
58+ , onBlocked : Nothing
67− IDBDatabase.name db `shouldEqual` name
60+ IDBDatabase.name db `shouldEqual` name
68− tearDown name version db
61+ tearDown name version db
70− it "open specific version" do
74− db <- IDBFactory.open name (Just version)
75− { onUpgradeNeeded: Nothing
78− IDBDatabase.name db `shouldEqual` name
79− tearDown name version db
81− it "open specific version -> close -> open latest" do
64+ it "open specific version" do
83− name = "db-latest"
65+ let name = "db-specific"
84 version = 14
66 version = 14
85− db01 <- IDBFactory.open name (Just version)
67+ db <- IDBFactory.open name (Just version)
86− { onUpgradeNeeded: Nothing
68+ { onUpgradeNeeded : Nothing
87− , onBlocked: Nothing
69+ , onBlocked : Nothing
89− IDBDatabase.name db01 `shouldEqual` name
71+ IDBDatabase.name db `shouldEqual` name
90− IDBDatabase.close db01
91− db02 <- IDBFactory.open name Nothing
92− { onUpgradeNeeded: Nothing
95− tearDown name version db02
72+ tearDown name version db
75+ it "open specific version -> close -> open latest" do
76+ let name = "db-latest"
78+ db <- IDBFactory.open name (Just version)
79+ { onUpgradeNeeded : Nothing
80+ , onBlocked : Nothing
82+ IDBDatabase.name db `shouldEqual` name
84+ db <- IDBFactory.open name Nothing
85+ { onUpgradeNeeded : Nothing
86+ , onBlocked : Nothing
88+ tearDown name version db
97− it "open + onUpgradeNeed" do
91+ it "open + onUpgradeNeed" do
99− name = "db-upgrade-needed"
92+ let name = "db-upgrade-needed"
100 version = 1
93 version = 1
101− callback (Tuple varName varVersion) db _ { oldVersion } = do
94+ callback var db _ = do
102− _ <- launchAff $ modifyAVar (const $ IDBDatabase.name db) varName
95+ _ <- launchAff $ modifyVar (const $ IDBDatabase.name db) var
103− _ <- launchAff $ modifyAVar (const $ oldVersion) varVersion
104 pure unit
96 pure unit
105− varName <- AVar.new "-"
97+ var <- makeVar' "-"
106− varVersion <- AVar.new (-1)
107− db <- IDBFactory.open name Nothing
98+ db <- IDBFactory.open name Nothing
108− { onUpgradeNeeded: Just (callback (Tuple varName varVersion))
99+ { onUpgradeNeeded : Just (callback var)
109− , onBlocked: Nothing
100+ , onBlocked : Nothing
111− name' <- AVar.read varName
102+ name <- peekVar var
112− version' <- AVar.read varVersion
113− name' `shouldEqual` name
103+ name `shouldEqual` name
114− version' `shouldEqual` 0
115− tearDown name version db
104+ tearDown name version db
117− it "open + onBlocked" do
107+ it "open + onBlocked" do
119− name = "db-blocked"
108+ let name = "db-blocked"
120 version = 14
109 version = 14
121 callback var = do
110 callback var = do
122− _ <- launchAff $ modifyAVar (const $ "db-blocked") var
111+ _ <- launchAff $ modifyVar (const $ "db-blocked") var
123 pure unit
112 pure unit
125− var <- AVar.new "-"
114+ var <- makeVar' "-"
126− db01 <- IDBFactory.open name Nothing
115+ db01 <- IDBFactory.open name Nothing
127− { onUpgradeNeeded: Nothing
116+ { onUpgradeNeeded : Nothing
128− , onBlocked: Nothing
117+ , onBlocked : Nothing
130− _ <- forkAff do
119+ _ <- forkAff do
131− delay (Milliseconds 100.0)
120+ delay (Milliseconds 100.0)
132− IDBDatabase.close db01
121+ IDBDatabase.close db01
134− db02 <- IDBFactory.open name (Just version)
123+ db02 <- IDBFactory.open name (Just version)
135− { onUpgradeNeeded: Nothing
124+ { onUpgradeNeeded : Nothing
136− , onBlocked: Just (callback var)
125+ , onBlocked : Just (callback var)
138− name' <- AVar.read var
127+ name <- peekVar var
139− name' `shouldEqual` name
128+ name `shouldEqual` name
140− tearDown name version db02
129+ tearDown name version db02
142− describe "IDBKeyRange" do
131+ describe "IDBKeyRange" do
143− it "only(int)" do
132+ it "only(int)" do
145− key = 14
133+ let key = 14
146 range = IDBKeyRange.only key
134 range = IDBKeyRange.only key
147− IDBKeyRange.includes range (toKey key) `shouldEqual` true
135+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
149− it "only(string)" do
137+ it "only(string)" do
151− key = "patate"
138+ let key = "patate"
152 range = IDBKeyRange.only key
139 range = IDBKeyRange.only key
153− IDBKeyRange.includes range (toKey key) `shouldEqual` true
140+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
155− it "only(float)" do
142+ it "only(float)" do
157− key = 14.42
143+ let key = 14.42
158 range = IDBKeyRange.only key
144 range = IDBKeyRange.only key
159− IDBKeyRange.includes range (toKey key) `shouldEqual` true
145+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
161− it "only(date)" do
147+ it "only(date)" do
163− mkey = DateTime
148+ let mkey = DateTime
164 <$> (Date.canonicalDate <$> toEnum 2017 <*> toEnum 6 <*> toEnum 23)
149 <$> (Date.canonicalDate <$> toEnum 2017 <*> toEnum 6 <*> toEnum 23)
165 <*> (Time <$> toEnum 17 <*> toEnum 59 <*> toEnum 34 <*> toEnum 42)
150 <*> (Time <$> toEnum 17 <*> toEnum 59 <*> toEnum 34 <*> toEnum 42)
167− case mkey of
152+ case mkey of
168− Nothing ->
153+ Nothing ->
169− fail "unable to create datetime"
154+ fail "unable to create datetime"
170− Just key -> do
155+ Just key -> do
171− let range = IDBKeyRange.only key
156+ let range = IDBKeyRange.only key
172− IDBKeyRange.includes range (toKey key) `shouldEqual` true
157+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
174− it "only([int])" do
159+ it "only([int])" do
176− key = [ 14, 42 ]
160+ let key = [14, 42]
177 range = IDBKeyRange.only key
161 range = IDBKeyRange.only key
178− IDBKeyRange.includes range (toKey key) `shouldEqual` true
162+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
180− it "only([string])" do
164+ it "only([string])" do
182− key = [ "patate", "autruche" ]
165+ let key = ["patate", "autruche"]
183 range = IDBKeyRange.only key
166 range = IDBKeyRange.only key
184− IDBKeyRange.includes range (toKey key) `shouldEqual` true
167+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
186− it "lowerBound(14, open)" do
169+ it "lowerBound(14, open)" do
188− key = 14
170+ let key = 14
189− open = true
171+ open = true
190 range = IDBKeyRange.lowerBound key open
172 range = IDBKeyRange.lowerBound key open
191− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
173+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
192− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
174+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
193− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
175+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
195− it "lowerBound(14, close)" do
177+ it "lowerBound(14, close)" do
197− key = 14
178+ let key = 14
198− open = false
179+ open = false
199 range = IDBKeyRange.lowerBound key open
180 range = IDBKeyRange.lowerBound key open
200− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
181+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
201− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
182+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
202− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
183+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
204− it "upperBound(14, open)" do
185+ it "upperBound(14, open)" do
206− key = 14
186+ let key = 14
207− open = true
187+ open = true
208 range = IDBKeyRange.upperBound key open
188 range = IDBKeyRange.upperBound key open
209− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
189+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
210− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
190+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
211− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
191+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
213− it "upperBound(14, close)" do
193+ it "upperBound(14, close)" do
215− key = 14
194+ let key = 14
216− open = false
195+ open = false
217 range = IDBKeyRange.upperBound key open
196 range = IDBKeyRange.upperBound key open
218− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
197+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
219− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
198+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
220− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
199+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
222− it "bound(42, 14, open, open) => Nothing" do
201+ it "bound(42, 14, open, open) => Nothing" do
224− lower = 42
202+ let lower = 42
225− upper = 14
203+ upper = 14
226 lowerOpen = true
204 lowerOpen = true
227 upperOpen = true
205 upperOpen = true
228− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
206+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
229− isNothing mrange `shouldEqual` true
207+ isNothing mrange `shouldEqual` true
231− it "bound(14, 42, open, open)" do
209+ it "bound(14, 42, open, open)" do
233− lower = 14
210+ let lower = 14
234− upper = 42
211+ upper = 42
235 lowerOpen = true
212 lowerOpen = true
236 upperOpen = true
213 upperOpen = true
237− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
214+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
238− case mrange of
215+ case mrange of
239− Nothing ->
216+ Nothing ->
240− fail "invalid range provided"
217+ fail "invalid range provided"
241− Just range -> do
218+ Just range -> do
242− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
219+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
243− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
220+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
244− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
221+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
245− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
222+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
246− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
223+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
247− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
224+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
249− it "bound(14, 42, open, close)" do
226+ it "bound(14, 42, open, close)" do
251− lower = 14
227+ let lower = 14
252− upper = 42
228+ upper = 42
253 lowerOpen = true
229 lowerOpen = true
254 upperOpen = false
230 upperOpen = false
255− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
231+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
256− case mrange of
232+ case mrange of
257− Nothing ->
233+ Nothing ->
258− fail "invalid range provided"
234+ fail "invalid range provided"
259− Just range -> do
235+ Just range -> do
260− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
236+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
261− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
237+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
262− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
238+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
263− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
239+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
264− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
240+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
265− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
241+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
267− it "bound(14, 42, close, open)" do
243+ it "bound(14, 42, close, open)" do
269− lower = 14
244+ let lower = 14
270− upper = 42
245+ upper = 42
271 lowerOpen = false
246 lowerOpen = false
272 upperOpen = true
247 upperOpen = true
273− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
248+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
274− case mrange of
249+ case mrange of
275− Nothing ->
250+ Nothing ->
276− fail "invalid range provided"
251+ fail "invalid range provided"
277− Just range -> do
252+ Just range -> do
278− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
253+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
279− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
254+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
280− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
255+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
281− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
256+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
282− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
257+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
283− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
258+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
285− it "bound(14, 42, close, close)" do
260+ it "bound(14, 42, close, close)" do
287− lower = 14
261+ let lower = 14
288− upper = 42
262+ upper = 42
289 lowerOpen = false
263 lowerOpen = false
290 upperOpen = false
264 upperOpen = false
291− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
265+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
292− case mrange of
266+ case mrange of
293− Nothing ->
267+ Nothing ->
294− fail "invalid range provided"
268+ fail "invalid range provided"
295− Just range -> do
269+ Just range -> do
296− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
270+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
297− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
271+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
298− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
272+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
299− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
273+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
300− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
274+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
301− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
275+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
303− it "can access attributes of a range" do
277+ it "can access attributes of a range" do
304− let range = IDBKeyRange.lowerBound 14 false
278+ let range = IDBKeyRange.lowerBound 14 false
305− IDBKeyRange.lower range `shouldEqual` (Just $ toKey 14)
279+ IDBKeyRange.lower range `shouldEqual` (Just $ toKey 14)
306− IDBKeyRange.upper range `shouldEqual` none
280+ IDBKeyRange.upper range `shouldEqual` none
307− IDBKeyRange.lowerOpen range `shouldEqual` false
281+ IDBKeyRange.lowerOpen range `shouldEqual` false
308− IDBKeyRange.upperOpen range `shouldEqual` true
282+ IDBKeyRange.upperOpen range `shouldEqual` true
310− describe "IDBDatabase" do
284+ describe "IDBDatabase" do
312 tearDown db = do
286 tearDown db = do
313 IDBDatabase.close db
287 IDBDatabase.close db
314 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
288 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
315 pure unit
289 pure unit
317 setup storeParams = do
291 setup storeParams = do
319− onUpgradeNeeded var db _ _ = launchAff' do
292+ let onUpgradeNeeded var db _ = launchAff' do
320− store <- IDBDatabase.createObjectStore db "store" storeParams
293+ store <- IDBDatabase.createObjectStore db "store" storeParams
321− _ <- AVar.put { db, store } var
294+ _ <- putVar var { db, store }
322− pure unit
295+ pure unit
324− var <- AVar.empty
297+ var <- makeVar
325− _ <- IDBFactory.open "db" Nothing
298+ db <- IDBFactory.open "db" Nothing
326− { onUpgradeNeeded: Just (onUpgradeNeeded var)
299+ { onUpgradeNeeded : Just (onUpgradeNeeded var)
327− , onBlocked: Nothing
300+ , onBlocked : Nothing
330− AVar.take var
303+ takeVar var
332− it "createObjectStore (keyPath: [], autoIncrement: true)" do
305+ it "createObjectStore (keyPath: [], autoIncrement: true)" do
333− { db, store } <- setup { keyPath: [], autoIncrement: true }
306+ { db, store } <- setup { keyPath: [], autoIncrement: true }
334− IDBObjectStore.name store `shouldEqual` "store"
307+ IDBObjectStore.name store `shouldEqual` "store"
335− IDBObjectStore.keyPath store `shouldEqual` []
308+ IDBObjectStore.keyPath store `shouldEqual` []
336− IDBObjectStore.autoIncrement store `shouldEqual` true
309+ IDBObjectStore.autoIncrement store `shouldEqual` true
337− IDBObjectStore.indexNames store `shouldEqual` []
310+ IDBObjectStore.indexNames store `shouldEqual` []
338− tearDown db
311+ tearDown db
340− it "createObjectStore (keyPath: [\"patate\"], autoIncrement: true)" do
313+ it "createObjectStore (keyPath: [\"patate\"], autoIncrement: true)" do
341− { db, store } <- setup { keyPath: [ "patate" ], autoIncrement: true }
314+ { db, store } <- setup { keyPath: ["patate"], autoIncrement: true }
342− IDBObjectStore.name store `shouldEqual` "store"
315+ IDBObjectStore.name store `shouldEqual` "store"
343− IDBObjectStore.keyPath store `shouldEqual` [ "patate" ]
316+ IDBObjectStore.keyPath store `shouldEqual` ["patate"]
344− IDBObjectStore.autoIncrement store `shouldEqual` true
317+ IDBObjectStore.autoIncrement store `shouldEqual` true
345− IDBObjectStore.indexNames store `shouldEqual` []
318+ IDBObjectStore.indexNames store `shouldEqual` []
346− tearDown db
319+ tearDown db
348− it "createObjectStore (keyPath: [\"a\", \"b\"], autoIncrement: false)" do
321+ it "createObjectStore (keyPath: [\"a\", \"b\"], autoIncrement: false)" do
349− { db, store } <- setup { keyPath: [ "patate", "autruche" ], autoIncrement: false }
322+ { db, store } <- setup { keyPath: ["patate", "autruche"], autoIncrement: false }
350− IDBObjectStore.name store `shouldEqual` "store"
323+ IDBObjectStore.name store `shouldEqual` "store"
351− IDBObjectStore.keyPath store `shouldEqual` [ "patate", "autruche" ]
324+ IDBObjectStore.keyPath store `shouldEqual` ["patate", "autruche"]
352− IDBObjectStore.autoIncrement store `shouldEqual` false
325+ IDBObjectStore.autoIncrement store `shouldEqual` false
353− IDBObjectStore.indexNames store `shouldEqual` []
326+ IDBObjectStore.indexNames store `shouldEqual` []
354− tearDown db
327+ tearDown db
356− it "deleteObjectStore" do
329+ it "deleteObjectStore" do
358− onUpgradeNeeded var db _ _ = launchAff' do
330+ let onUpgradeNeeded var db _ = launchAff' do
359 _ <- IDBDatabase.deleteObjectStore db "store"
331 _ <- IDBDatabase.deleteObjectStore db "store"
360− AVar.put true var
332+ putVar var true
362− var <- AVar.empty
334+ var <- makeVar
363− { db } <- setup IDBDatabase.defaultParameters
335+ { db, store } <- setup IDBDatabase.defaultParameters
364− IDBDatabase.close db
336+ IDBDatabase.close db
365− db' <- IDBFactory.open "db" (Just 999)
366− { onUpgradeNeeded: Just (onUpgradeNeeded var)
337+ db <- IDBFactory.open "db" (Just 999) { onUpgradeNeeded : Just (onUpgradeNeeded var)
367− , onBlocked: Nothing
338+ , onBlocked : Nothing
369− deleted <- AVar.take var
340+ deleted <- takeVar var
370− deleted `shouldEqual` true
341+ deleted `shouldEqual` true
371− tearDown db'
342+ tearDown db
373− describe "IDBObjectStore" do
345+ describe "IDBObjectStore" do
375 tearDown db = do
347 tearDown db = do
376 IDBDatabase.close db
348 IDBDatabase.close db
377 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
349 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
378 pure unit
350 pure unit
380 setup { storeParams, onUpgradeNeeded } = do
352 setup { storeParams, onUpgradeNeeded } = do
382− onUpgradeNeeded' var db _ _ = launchAff' do
353+ let onUpgradeNeeded' var db _ = launchAff' do
383− store <- IDBDatabase.createObjectStore db "store" storeParams
354+ store <- IDBDatabase.createObjectStore db "store" storeParams
384− liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure store)
355+ liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store)
385− AVar.put { db, store } var
356+ putVar var { db, store }
387− var <- AVar.empty
358+ var <- makeVar
388− _ <- IDBFactory.open "db" Nothing
359+ db <- IDBFactory.open "db" Nothing
389− { onUpgradeNeeded: Just (onUpgradeNeeded' var)
360+ { onUpgradeNeeded : Just (onUpgradeNeeded' var)
390− , onBlocked: Nothing
361+ , onBlocked : Nothing
393− AVar.take var
364+ takeVar var
395− it "add()" do
366+ it "add()" do
396− date <- liftEffect $ toDateTime <$> now
367+ date <- liftEff $ toDateTime <$> now
397− { db } <- setup
368+ { db } <- setup
398− { storeParams: { autoIncrement: true, keyPath: [] }
369+ { storeParams: { autoIncrement: true, keyPath: [] }
399− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
370+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
400− -- no key
371+ -- no key
401− key <- IDBObjectStore.add store "patate" none
372+ key <- IDBObjectStore.add store "patate" none
402− (toKey 1) `shouldEqual` key
373+ (toKey 1) `shouldEqual` key
404− -- int key
375+ -- int key
405− key' <- IDBObjectStore.add store "patate" (Just 14)
376+ key <- IDBObjectStore.add store "patate" (Just 14)
406− (toKey 14) `shouldEqual` key'
377+ (toKey 14) `shouldEqual` key
408− -- number key
379+ -- number key
409− key'' <- IDBObjectStore.add store "patate" (Just 14.42)
380+ key <- IDBObjectStore.add store "patate" (Just 14.42)
410− (toKey 14.42) `shouldEqual` key''
381+ (toKey 14.42) `shouldEqual` key
412− -- string key
383+ -- string key
413− key''' <- IDBObjectStore.add store "patate" (Just "key")
384+ key <- IDBObjectStore.add store "patate" (Just "key")
414− (toKey "key") `shouldEqual` key'''
385+ (toKey "key") `shouldEqual` key
416− -- date key
387+ -- date key
417− key'''' <- IDBObjectStore.add store "patate" (Just date)
388+ key <- IDBObjectStore.add store "patate" (Just date)
418− (toKey date) `shouldEqual` key''''
389+ (toKey date) `shouldEqual` key
420− -- array key
391+ -- array key
421− key''''' <- IDBObjectStore.add store "patate" (Just $ toKey [ 14, 42 ])
392+ key <- IDBObjectStore.add store "patate" (Just $ toKey [14, 42])
422− (toKey [ 14, 42 ]) `shouldEqual` key'''''
393+ (toKey [14, 42]) `shouldEqual` key
424− tearDown db
395+ tearDown db
426− it "clear()" do
397+ it "clear()" do
427− { db } <- setup
398+ { db } <- setup
428− { storeParams: IDBDatabase.defaultParameters
399+ { storeParams: IDBDatabase.defaultParameters
429− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
400+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
430− key <- IDBObjectStore.add store "patate" (Just 14)
401+ key <- IDBObjectStore.add store "patate" (Just 14)
431− _ <- IDBObjectStore.clear store
402+ _ <- IDBObjectStore.clear store
432− val <- IDBObjectStore.get store (IDBKeyRange.only key)
403+ val <- IDBObjectStore.get store (IDBKeyRange.only key)
433− val `shouldEqual` (Nothing :: Maybe String)
404+ val `shouldEqual` (Nothing :: Maybe String)
410+ { storeParams: IDBDatabase.defaultParameters
411+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
412+ key <- IDBObjectStore.add store "patate" (Just 14)
413+ key <- IDBObjectStore.add store "autruche" (Just 42)
414+ n <- IDBObjectStore.count store Nothing
421+ { storeParams: IDBDatabase.defaultParameters
422+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
423+ key <- IDBObjectStore.add store "patate" (Just 14)
424+ mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 14)
425+ mkey `shouldEqual` (Just key)
427+ mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
428+ mkey `shouldEqual` none
439− { storeParams: IDBDatabase.defaultParameters
440− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
441− _ <- IDBObjectStore.add store "patate" (Just 14)
442− _ <- IDBObjectStore.add store "autruche" (Just 42)
443− n <- IDBObjectStore.count store Nothing
446− tearDown db
430+ tearDown db
448− it "getKey()" do
432+ it "getAllKeys()" do
449− { db } <- setup
433+ { db } <- setup
450− { storeParams: IDBDatabase.defaultParameters
434+ { storeParams: IDBDatabase.defaultParameters
451− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
452− key <- IDBObjectStore.add store "patate" (Just 14)
453− mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 14)
454− mkey `shouldEqual` (Just key)
456− mkey' <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
457− mkey' `shouldEqual` none
461− it "getAllKeys()" do
463− { storeParams: IDBDatabase.defaultParameters
464− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
435+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
465− key1 <- IDBObjectStore.add store "patate" (Just 14)
436+ key1 <- IDBObjectStore.add store "patate" (Just 14)
466− key2 <- IDBObjectStore.add store "autruche" (Just 42)
437+ key2 <- IDBObjectStore.add store "autruche" (Just 42)
467− key3 <- IDBObjectStore.add store 14 (Just 1337)
438+ key3 <- IDBObjectStore.add store 14 (Just 1337)
469− -- no bounds
440+ -- no bounds
470− keys <- IDBObjectStore.getAllKeys store Nothing Nothing
441+ keys <- IDBObjectStore.getAllKeys store Nothing Nothing
471− keys `shouldEqual` [ key1, key2, key3 ]
442+ keys `shouldEqual` [key1, key2, key3]
473− -- lower bound
444+ -- lower bound
474− keys' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 14 true) Nothing
445+ keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 14 true) Nothing
475− keys' `shouldEqual` [ key2, key3 ]
446+ keys `shouldEqual` [key2, key3]
477− -- upper bound
448+ -- upper bound
478− keys'' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.upperBound 42 false) Nothing
449+ keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.upperBound 42 false) Nothing
479− keys'' `shouldEqual` [ key1, key2 ]
450+ keys `shouldEqual` [key1, key2]
481− -- count
452+ -- count
482− keys''' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
453+ keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
483− keys''' `shouldEqual` [ key1, key2 ]
454+ keys `shouldEqual` [key1, key2]
485− tearDown db
456+ tearDown db
487− it "openCursor()" do
458+ it "openCursor()" do
490− { onSuccess: const $ pure unit
460+ { onSuccess : const $ pure unit
491− , onError: show >>> fail >>> launchAff'
461+ , onError : show >>> fail >>> launchAff'
492 , onComplete: pure unit
462 , onComplete: pure unit
494− { db } <- setup
464+ { db } <- setup
495− { storeParams: IDBDatabase.defaultParameters
465+ { storeParams: IDBDatabase.defaultParameters
496− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
466+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
497− _ <- IDBObjectStore.openCursor store Nothing Next cb
467+ _ <- IDBObjectStore.openCursor store Nothing Next cb
498− _ <- IDBObjectStore.openCursor store Nothing NextUnique cb
468+ _ <- IDBObjectStore.openCursor store Nothing NextUnique cb
499− _ <- IDBObjectStore.openCursor store Nothing Prev cb
469+ _ <- IDBObjectStore.openCursor store Nothing Prev cb
500− _ <- IDBObjectStore.openCursor store Nothing PrevUnique cb
470+ _ <- IDBObjectStore.openCursor store Nothing PrevUnique cb
501− _ <- IDBObjectStore.openCursor store (Just $ IDBKeyRange.upperBound 1 true) Next cb
471+ _ <- IDBObjectStore.openCursor store (Just $ IDBKeyRange.upperBound 1 true) Next cb
502− pure unit
472+ pure unit
504− tearDown db
474+ tearDown db
506− it "openKeyCursor()" do
476+ it "openKeyCursor()" do
509− { onSuccess: const $ pure unit
478+ { onSuccess : const $ pure unit
510− , onError: show >>> fail >>> launchAff'
479+ , onError : show >>> fail >>> launchAff'
511 , onComplete: pure unit
480 , onComplete: pure unit
513− { db } <- setup
482+ { db } <- setup
514− { storeParams: IDBDatabase.defaultParameters
483+ { storeParams: IDBDatabase.defaultParameters
515− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
484+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
516− _ <- IDBObjectStore.openKeyCursor store Nothing Next cb
485+ _ <- IDBObjectStore.openKeyCursor store Nothing Next cb
517− _ <- IDBObjectStore.openKeyCursor store Nothing NextUnique cb
486+ _ <- IDBObjectStore.openKeyCursor store Nothing NextUnique cb
518− _ <- IDBObjectStore.openKeyCursor store Nothing Prev cb
487+ _ <- IDBObjectStore.openKeyCursor store Nothing Prev cb
519− _ <- IDBObjectStore.openKeyCursor store Nothing PrevUnique cb
488+ _ <- IDBObjectStore.openKeyCursor store Nothing PrevUnique cb
520− _ <- IDBObjectStore.openKeyCursor store (Just $ IDBKeyRange.lowerBound 1 true) Next cb
489+ _ <- IDBObjectStore.openKeyCursor store (Just $ IDBKeyRange.lowerBound 1 true) Next cb
521− pure unit
490+ pure unit
523− tearDown db
492+ tearDown db
525− describe "IDBIndex" do
494+ describe "IDBIndex" do
527 tearDown db = do
496 tearDown db = do
528 IDBDatabase.close db
497 IDBDatabase.close db
529 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
498 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
530 pure unit
499 pure unit
533− :: forall value
501+ setup :: forall value e e'.
534− . { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
502+ { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
535− , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
503+ , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
536− , values :: Array (Tuple value (Maybe Key))
504+ , values :: Array (Tuple value (Maybe Key))
537− , keyPath :: Array String
505+ , keyPath :: Array String
538− , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Effect Unit)
506+ , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
540− -> Aff { db :: Database, index :: Index, store :: ObjectStore }
507+ } -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
541 setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
508 setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
543− onUpgradeNeeded' var db tx _ = launchAff' do
509+ let onUpgradeNeeded' var db tx = launchAff' do
544− store <- IDBDatabase.createObjectStore db "store" storeParams
510+ store <- IDBDatabase.createObjectStore db "store" storeParams
545− _ <- traverse (uncurry (IDBObjectStore.add store)) values
511+ _ <- traverse (uncurry (IDBObjectStore.add store)) values
546− index <- IDBObjectStore.createIndex store "index" keyPath indexParams
512+ index <- IDBObjectStore.createIndex store "index" keyPath indexParams
547− liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
513+ liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
548− AVar.put { db, index, store } var
514+ putVar var { db, index, store }
550− var <- AVar.empty
516+ var <- makeVar
551− _ <- IDBFactory.open "db" Nothing
517+ db <- IDBFactory.open "db" Nothing
552− { onUpgradeNeeded: Just (onUpgradeNeeded' var)
518+ { onUpgradeNeeded : Just (onUpgradeNeeded' var)
553− , onBlocked: Nothing
519+ , onBlocked : Nothing
555− AVar.take var
521+ takeVar var
557− it "returns an IDBIndex and the properties are set correctly" do
523+
558− { db, index } <- setup
524+ it "returns an IDBIndex and the properties are set correctly" do
559− { storeParams: IDBDatabase.defaultParameters
525+ { db, index } <- setup
560− , indexParams: IDBObjectStore.defaultParameters
526+ { storeParams : IDBDatabase.defaultParameters
561− , onUpgradeNeeded: Nothing
527+ , indexParams : IDBObjectStore.defaultParameters
562− , keyPath: []
528+ , onUpgradeNeeded : Nothing
563− , values: []
529+ , keyPath : []
565− IDBIndex.name index `shouldEqual` "index"
531+ }
566− IDBIndex.keyPath index `shouldEqual` []
532+ IDBIndex.name index `shouldEqual` "index"
567− IDBIndex.unique index `shouldEqual` IDBObjectStore.defaultParameters.unique
533+ IDBIndex.keyPath index `shouldEqual` []
568− IDBIndex.multiEntry index `shouldEqual` IDBObjectStore.defaultParameters.multiEntry
534+ IDBIndex.unique index `shouldEqual` IDBObjectStore.defaultParameters.unique
569− tearDown db
535+ IDBIndex.multiEntry index `shouldEqual` IDBObjectStore.defaultParameters.multiEntry
571− it "attempt to create an index that requires unique values on an object store already contains duplicates" do
537+
572− let onAbort var = launchAff' (AVar.put true var)
538+ it "attempt to create an index that requires unique values on an object store already contains duplicates" do
573− txVar <- AVar.empty
539+ let onAbort var = launchAff' (putVar var true)
574− dbVar <- AVar.empty
540+ txVar <- makeVar
575− res <- attempt $ setup
541+ dbVar <- makeVar
576− { storeParams: IDBDatabase.defaultParameters
542+ res <- attempt $ setup
577− , indexParams:
543+ { storeParams : IDBDatabase.defaultParameters
578− { unique: true
544+ , indexParams : { unique : true
579− , multiEntry: false
545+ , multiEntry : false
581− , keyPath: [ "indexedProperty" ]
547+ , keyPath : ["indexedProperty"]
582− , values:
548+ , values : [ { indexedProperty: "bar" } :+: (Just $ toKey 1)
583− [ { indexedProperty: "bar" } :+: (Just $ toKey 1)
549+ , { indexedProperty: "bar" } :+: (Just $ toKey 2)
584− , { indexedProperty: "bar" } :+: (Just $ toKey 2)
550+ ]
585− ]
551+ , onUpgradeNeeded : Just $ \db tx _ -> launchAff' do
586− , onUpgradeNeeded: Just $ \db tx _ -> launchAff' do
552+ IDBTransaction.onAbort tx (onAbort txVar)
587− IDBTransaction.onAbort tx (onAbort txVar)
553+ IDBDatabase.onAbort db (onAbort dbVar)
588− IDBDatabase.onAbort db (onAbort dbVar)
554+ }
590− case res of
556+ Right _ ->
591− Right _ ->
557+ fail "expected abort"
592− fail "expected abort"
558+ _ -> do
593− _ -> do
559+ shouldEqual true =<< takeVar txVar
594− shouldEqual true =<< AVar.take txVar
560+ shouldEqual true =<< takeVar dbVar
595− shouldEqual true =<< AVar.take dbVar
561+
597− it "the index is usable right after being made" do
563+ it "the index is usable right after being made" do
598− { db } <- setup
564+ { db } <- setup
599− { storeParams:
565+ { storeParams : { keyPath : ["key"]
600− { keyPath: [ "key" ]
566+ , autoIncrement : false
601− , autoIncrement: false
567+ }
602− }
568+ , indexParams : IDBObjectStore.defaultParameters
603− , indexParams: IDBObjectStore.defaultParameters
569+ , keyPath : ["indexedProperty"]
604− , keyPath: [ "indexedProperty" ]
570+ , values : [ { key: "key1", indexedProperty: "indexed_1" } :+: Nothing
605− , values:
571+ , { key: "key2", indexedProperty: "indexed_2" } :+: Nothing
606− [ { key: "key1", indexedProperty: "indexed_1" } :+: Nothing
572+ , { key: "key3", indexedProperty: "indexed_3" } :+: Nothing
607− , { key: "key2", indexedProperty: "indexed_2" } :+: Nothing
573+ ]
608− , { key: "key3", indexedProperty: "indexed_3" } :+: Nothing
574+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
609− ]
575+ val <- IDBIndex.get index (IDBKeyRange.only "indexed_2")
610− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
576+ ((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2")
611− val <- IDBIndex.get index (IDBKeyRange.only "indexed_2")
577+ }
612− ((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2")
578+ tearDown db
614− tearDown db
580+ it "empty keyPath" do
616− it "empty keyPath" do
582+ { storeParams : IDBDatabase.defaultParameters
617− { db } <- setup
583+ , indexParams : IDBObjectStore.defaultParameters
618− { storeParams: IDBDatabase.defaultParameters
584+ , keyPath : []
619− , indexParams: IDBObjectStore.defaultParameters
585+ , values : [ "object_1" :+: (Just $ toKey 1)
620− , keyPath: []
586+ , "object_2" :+: (Just $ toKey 2)
621− , values:
587+ , "object_3" :+: (Just $ toKey 3)
622− [ "object_1" :+: (Just $ toKey 1)
588+ ]
623− , "object_2" :+: (Just $ toKey 2)
589+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
624− , "object_3" :+: (Just $ toKey 3)
590+ val <- IDBIndex.get index (IDBKeyRange.only "object_3")
625− ]
591+ val `shouldEqual` (Just "object_3")
626− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
592+ }
627− val <- IDBIndex.get index (IDBKeyRange.only "object_3")
593+ tearDown db
628− val `shouldEqual` (Just "object_3")
594+
629− }
595+ it "index can be valid keys [date]" do
630− tearDown db
596+ date <- liftEff $ toDateTime <$> now
632− it "index can be valid keys [date]" do
598+ { storeParams : { keyPath: ["key"]
633− date <- liftEffect $ toDateTime <$> now
599+ , autoIncrement: false
634− { db } <- setup
600+ }
635− { storeParams:
601+ , indexParams : IDBObjectStore.defaultParameters
636− { keyPath: [ "key" ]
602+ , keyPath : ["i"]
637− , autoIncrement: false
603+ , values : [ { key: "date", i: (toKey date) } :+: Nothing
639− , indexParams: IDBObjectStore.defaultParameters
605+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
640− , keyPath: [ "i" ]
606+ val <- IDBIndex.get index (IDBKeyRange.only date)
641− , values:
607+ ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
642− [ { key: "date", i: (toKey date) } :+: Nothing
608+ }
644− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
610+
645− val <- IDBIndex.get index (IDBKeyRange.only date)
611+ it "index can be valid keys [num]" do
646− ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
612+ let num = 14
647− }
613+ { db } <- setup
648− tearDown db
614+ { storeParams : { keyPath: ["key"]
649−
615+ , autoIncrement: false
650− it "index can be valid keys [num]" do
616+ }
651− let num = 14
617+ , indexParams : IDBObjectStore.defaultParameters
652− { db } <- setup
618+ , keyPath : ["i"]
653− { storeParams:
619+ , values : [ { key: "num", i: (toKey num) } :+: Nothing
654− { keyPath: [ "key" ]
620+ ]
655− , autoIncrement: false
621+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
656− }
622+ val <- IDBIndex.get index (IDBKeyRange.only num)
657− , indexParams: IDBObjectStore.defaultParameters
623+ ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
658− , keyPath: [ "i" ]
624+ }
659− , values:
625+ tearDown db
660− [ { key: "num", i: (toKey num) } :+: Nothing
626+
661− ]
627+ it "index can be valid keys [array]" do
662− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
628+ let array = ["patate", "autruche"]
663− val <- IDBIndex.get index (IDBKeyRange.only num)
629+ { db } <- setup
664− ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
630+ { storeParams : { keyPath: ["key"]
665− }
631+ , autoIncrement: false
667−
633+ , indexParams : IDBObjectStore.defaultParameters
668− it "index can be valid keys [array]" do
634+ , keyPath : ["i"]
669− let array = [ "patate", "autruche" ]
635+ , values : [ { key: "array", i: (toKey array) } :+: Nothing
670− { db } <- setup
636+ ]
671− { storeParams:
637+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
672− { keyPath: [ "key" ]
638+ val <- IDBIndex.get index (IDBKeyRange.only array)
673− , autoIncrement: false
639+ ((\r -> r.key) <$> val) `shouldEqual` (Just "array")
675− , indexParams: IDBObjectStore.defaultParameters
641+ tearDown db
676− , keyPath: [ "i" ]
642+
677− , values:
643+ it "openKeyCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
678− [ { key: "array", i: (toKey array) } :+: Nothing
644+ res <- attempt $ setup
679− ]
645+ { storeParams : { keyPath: ["key"], autoIncrement: false }
680− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
646+ , indexParams : IDBObjectStore.defaultParameters
681− val <- IDBIndex.get index (IDBKeyRange.only array)
647+ , keyPath : ["indexedProperty"]
682− ((\r -> r.key) <$> val) `shouldEqual` (Just "array")
648+ , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
684− tearDown db
650+ , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
686− it "openKeyCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
652+ { onSuccess : const $ pure unit
687− res <- attempt $ setup
653+ , onError : show >>> fail >>> launchAff'
688− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
689− , indexParams: IDBObjectStore.defaultParameters
690− , keyPath: [ "indexedProperty" ]
692− [ { key: 14, indexedProperty: "patate" } :+: Nothing
694− , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
697− { onSuccess: const $ pure unit
698− , onError: show >>> fail >>> launchAff'
699 , onComplete: pure unit
654 , onComplete: pure unit
701− IDBTransaction.onAbort tx (pure unit)
656+ IDBTransaction.onAbort tx (pure unit)
702− IDBDatabase.onAbort db (pure unit)
657+ IDBDatabase.onAbort db (pure unit)
703− IDBTransaction.abort tx
658+ IDBTransaction.abort tx
704− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
659+ cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
705− case cursor of
660+ case cursor of
706− Right _ -> fail "expected InvalidStateError"
661+ Right _ -> fail "expected InvalidStateError"
707− Left err -> name err `shouldEqual` "InvalidStateError"
662+ Left err -> message err `shouldEqual` "InvalidStateError"
709− case res of
664+ case res of
710− Right _ -> fail "expected InvalidStateError"
665+ Right _ -> fail "expected InvalidStateError"
711− _ -> pure unit
666+ _ -> pure unit
713− it "openKeyCursor() - throw TransactionInactiveError on aborted transaction" do
668+ it "openKeyCursor() - throw TransactionInactiveError on aborted transaction" do
714− { db } <- setup
669+ { db } <- setup
715− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
670+ { storeParams : { keyPath: ["key"], autoIncrement: false }
716− , indexParams: IDBObjectStore.defaultParameters
671+ , indexParams : IDBObjectStore.defaultParameters
717− , keyPath: [ "indexedProperty" ]
672+ , keyPath : ["indexedProperty"]
718− , values:
673+ , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
719− [ { key: 14, indexedProperty: "patate" } :+: Nothing
674+ ]
720− ]
675+ , onUpgradeNeeded : Nothing
721− , onUpgradeNeeded: Nothing
676+ }
723− let
678+ { onSuccess : const $ pure unit
724− cb =
679+ , onError : show >>> fail >>> launchAff'
725− { onSuccess: const $ pure unit
726− , onError: show >>> fail >>> launchAff'
727 , onComplete: pure unit
680 , onComplete: pure unit
729− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
682+ tx <- IDBDatabase.transaction db ["store"] ReadOnly
730− store <- IDBTransaction.objectStore tx "store"
683+ store <- IDBTransaction.objectStore tx "store"
731− index <- IDBObjectStore.index store "index"
684+ index <- IDBObjectStore.index store "index"
732− IDBTransaction.onAbort tx (pure unit)
685+ IDBTransaction.onAbort tx (pure unit)
733− IDBTransaction.abort tx
686+ IDBTransaction.abort tx
734− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
687+ cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
735− case cursor of
688+ case cursor of
736− Right _ -> fail "expected TransactionInactiveError"
689+ Right _ -> fail "expected TransactionInactiveError"
737− Left err -> name err `shouldEqual` "TransactionInactiveError"
690+ Left err -> message err `shouldEqual` "TransactionInactiveError"
739− tearDown db
692+ tearDown db
741− it "openKeyCursor() - throw InvalidStateError when the index is deleted" do
694+ it "openKeyCursor() - throw InvalidStateError when the index is deleted" do
742− { db } <- setup
695+ { db } <- setup
743− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
696+ { storeParams : { keyPath: ["key"], autoIncrement: false }
744− , indexParams: IDBObjectStore.defaultParameters
697+ , indexParams : IDBObjectStore.defaultParameters
745− , keyPath: [ "indexedProperty" ]
698+ , keyPath : ["indexedProperty"]
746− , values:
699+ , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
747− [ { key: 14, indexedProperty: "patate" } :+: Nothing
700+ ]
748− ]
701+ , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
749− , onUpgradeNeeded: Just $ \_ tx index -> launchAff' do
702+ let cb =
750− let
703+ { onSuccess : const $ pure unit
751− cb =
704+ , onError : show >>> fail >>> launchAff'
752− { onSuccess: const $ pure unit
753− , onError: show >>> fail >>> launchAff'
754 , onComplete: pure unit
705 , onComplete: pure unit
756− store <- IDBTransaction.objectStore tx "store"
707+ store <- IDBTransaction.objectStore tx "store"
757− IDBObjectStore.deleteIndex store "index"
708+ IDBObjectStore.deleteIndex store "index"
758− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
709+ cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
759− case cursor of
710+ case cursor of
760− Right _ -> fail "expected InvalidStateError"
711+ Right _ -> fail "expected InvalidStateError"
761− Left err -> name err `shouldEqual` "InvalidStateError"
712+ Left err -> message err `shouldEqual` "InvalidStateError"
764− tearDown db
715+ tearDown db
766− it "openCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
717+ it "openCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
767− res <- attempt $ setup
718+ res <- attempt $ setup
768− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
719+ { storeParams : { keyPath: ["key"], autoIncrement: false }
769− , indexParams: IDBObjectStore.defaultParameters
720+ , indexParams : IDBObjectStore.defaultParameters
770− , keyPath: [ "indexedProperty" ]
721+ , keyPath : ["indexedProperty"]
771− , values:
722+ , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
772− [ { key: 14, indexedProperty: "patate" } :+: Nothing
723+ ]
773− ]
724+ , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
774− , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
725+ let cb =
775− let
726+ { onSuccess : const $ pure unit
776− cb =
727+ , onError : show >>> fail >>> launchAff'
777− { onSuccess: const $ pure unit
778− , onError: show >>> fail >>> launchAff'
779 , onComplete: pure unit
728 , onComplete: pure unit
781− IDBTransaction.onAbort tx (pure unit)
730+ IDBTransaction.onAbort tx (pure unit)
782− IDBDatabase.onAbort db (pure unit)
731+ IDBDatabase.onAbort db (pure unit)
783− IDBTransaction.abort tx
732+ IDBTransaction.abort tx
784− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
733+ cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
785− case cursor of
734+ case cursor of
786− Right _ -> fail "expected InvalidStateError"
735+ Right _ -> fail "expected InvalidStateError"
787− Left err -> name err `shouldEqual` "InvalidStateError"
736+ Left err -> message err `shouldEqual` "InvalidStateError"
789− case res of
738+ case res of
790− Right _ -> fail "expected InvalidStateError"
739+ Right _ -> fail "expected InvalidStateError"
791− _ -> pure unit
740+ _ -> pure unit
793− it "openCursor() - throw TransactionInactiveError on aborted transaction" do
742+ it "openCursor() - throw TransactionInactiveError on aborted transaction" do
794− { db } <- setup
743+ { db } <- setup
795− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
744+ { storeParams : { keyPath: ["key"], autoIncrement: false }
796− , indexParams: IDBObjectStore.defaultParameters
745+ , indexParams : IDBObjectStore.defaultParameters
797− , keyPath: [ "indexedProperty" ]
746+ , keyPath : ["indexedProperty"]
798− , values:
747+ , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
799− [ { key: 14, indexedProperty: "patate" } :+: Nothing
748+ ]
800− ]
749+ , onUpgradeNeeded : Nothing
801− , onUpgradeNeeded: Nothing
750+ }
803− let
752+ { onSuccess : const $ pure unit
804− cb =
753+ , onError : show >>> fail >>> launchAff'
805− { onSuccess: const $ pure unit
806− , onError: show >>> fail >>> launchAff'
807 , onComplete: pure unit
754 , onComplete: pure unit
809− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
756+ tx <- IDBDatabase.transaction db ["store"] ReadOnly
810− store <- IDBTransaction.objectStore tx "store"
757+ store <- IDBTransaction.objectStore tx "store"
811− index <- IDBObjectStore.index store "index"
758+ index <- IDBObjectStore.index store "index"
812− IDBTransaction.onAbort tx (pure unit)
759+ IDBTransaction.onAbort tx (pure unit)
813− IDBTransaction.abort tx
760+ IDBTransaction.abort tx
814− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
761+ cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
815− case cursor of
762+ case cursor of
816− Right _ -> fail "expected TransactionInactiveError"
763+ Right _ -> fail "expected TransactionInactiveError"
817− Left err -> name err `shouldEqual` "TransactionInactiveError"
764+ Left err -> message err `shouldEqual` "TransactionInactiveError"
819− tearDown db
766+ tearDown db
821− it "openCursor() - throw InvalidStateError when the index is deleted" do
768+ it "openCursor() - throw InvalidStateError when the index is deleted" do
822− { db } <- setup
769+ { db } <- setup
823− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
770+ { storeParams : { keyPath: ["key"], autoIncrement: false }
824− , indexParams: IDBObjectStore.defaultParameters
771+ , indexParams : IDBObjectStore.defaultParameters
825− , keyPath: [ "indexedProperty" ]
772+ , keyPath : ["indexedProperty"]
826− , values:
773+ , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
827− [ { key: 14, indexedProperty: "patate" } :+: Nothing
774+ ]
828− ]
775+ , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
829− , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
776+ let cb =
830− let
777+ { onSuccess : const $ pure unit
831− cb =
778+ , onError : show >>> fail >>> launchAff'
832− { onSuccess: const $ pure unit
833− , onError: show >>> fail >>> launchAff'
834 , onComplete: pure unit
779 , onComplete: pure unit
836− store <- IDBTransaction.objectStore tx "store"
781+ store <- IDBTransaction.objectStore tx "store"
837− IDBObjectStore.deleteIndex store "index"
782+ IDBObjectStore.deleteIndex store "index"
838− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
783+ cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
839− case cursor of
784+ case cursor of
840− Right _ -> fail "expected InvalidStateError"
785+ Right _ -> fail "expected InvalidStateError"
841− Left err -> name err `shouldEqual` "InvalidStateError"
786+ Left err -> message err `shouldEqual` "InvalidStateError"
844− tearDown db
789+ tearDown db
846− it "getKey() - multiEntry - adding keys" do
791+ it "getKey() - multiEntry - adding keys" do
847− { db } <- setup
792+ { db } <- setup
848− { storeParams: IDBDatabase.defaultParameters
793+ { storeParams : IDBDatabase.defaultParameters
849− , indexParams: { unique: false, multiEntry: true }
794+ , indexParams : { unique: false, multiEntry: true }
850− , keyPath: [ "name" ]
795+ , keyPath : ["name"]
851− , values:
796+ , values : [ { name: ["patate", "autruche"] } :+: (Just $ toKey 1)
852− [ { name: [ "patate", "autruche" ] } :+: (Just $ toKey 1)
797+ , { name: ["bob"] } :+: (Just $ toKey 2)
853− , { name: [ "bob" ] } :+: (Just $ toKey 2)
798+ ]
854− ]
799+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
855− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
800+ key <- IDBIndex.getKey index (IDBKeyRange.only "patate")
856− key <- IDBIndex.getKey index (IDBKeyRange.only "patate")
801+ key `shouldEqual` (Just $ toKey 1)
857− key `shouldEqual` (Just $ toKey 1)
802+
858−
803+ key <- IDBIndex.getKey index (IDBKeyRange.only "autruche")
859− key' <- IDBIndex.getKey index (IDBKeyRange.only "autruche")
804+ key `shouldEqual` (Just $ toKey 1)
860− key' `shouldEqual` (Just $ toKey 1)
805+
861−
806+ key <- IDBIndex.getKey index (IDBKeyRange.only "bob")
862− key'' <- IDBIndex.getKey index (IDBKeyRange.only "bob")
807+ key `shouldEqual` (Just $ toKey 2)
863− key'' `shouldEqual` (Just $ toKey 2)
808+ }
867− it "get() - returns the record with the first key in the range" do
812+ it "get() - returns the record with the first key in the range" do
868− { db } <- setup
813+ { db } <- setup
869− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
814+ { storeParams : { keyPath: ["key"], autoIncrement: false }
870− , indexParams: IDBObjectStore.defaultParameters
815+ , indexParams : IDBObjectStore.defaultParameters
871− , keyPath: [ "indexedProperty" ]
816+ , keyPath : ["indexedProperty"]
872− , values:
817+ , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
873− [ { key: 14, indexedProperty: "patate" } :+: Nothing
818+ , { key: 42, indexedProperty: "autruche" } :+: Nothing
874− , { key: 42, indexedProperty: "autruche" } :+: Nothing
819+ , { key: 1337, indexedProperty: "baguette" } :+: Nothing
875− , { key: 1337, indexedProperty: "baguette" } :+: Nothing
820+ ]
876− ]
821+ , onUpgradeNeeded : Nothing
877− , onUpgradeNeeded: Nothing
822+ }
879−
824+ tx <- IDBDatabase.transaction db ["store"] ReadOnly
880− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
825+ store <- IDBTransaction.objectStore tx "store"
881− store <- IDBTransaction.objectStore tx "store"
826+ index <- IDBObjectStore.index store "index"
882− index <- IDBObjectStore.index store "index"
827+ val <- IDBIndex.get index (IDBKeyRange.lowerBound "autruche" false)
883− val <- IDBIndex.get index (IDBKeyRange.lowerBound "autruche" false)
828+ ((\r -> r.key) <$> val) `shouldEqual` (Just 42)
884− ((\r -> r.key) <$> val) `shouldEqual` (Just 42)
829+
887−
832+ describe "IDBCursor" do
888− describe "IDBCursor" do
833+ let
890 tearDown db = do
834 tearDown db = do
891 IDBDatabase.close db
835 IDBDatabase.close db
892 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
836 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
893 pure unit
837 pure unit
895− setup
839+ setup :: forall value e e'.
896− :: forall value
840+ { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
897− . { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
841+ , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
898− , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
842+ , values :: Array (Tuple value (Maybe Key))
899− , values :: Array (Tuple value (Maybe Key))
843+ , keyPath :: Array String
900− , keyPath :: Array String
844+ , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
901− , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Effect Unit)
845+ } -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
903− -> Aff { db :: Database, index :: Index, store :: ObjectStore }
904 setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
846 setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
905− let
847+ let onUpgradeNeeded' var db tx = launchAff' do
906− onUpgradeNeeded' var db tx _ = launchAff' do
848+ store <- IDBDatabase.createObjectStore db "store" storeParams
907− store <- IDBDatabase.createObjectStore db "store" storeParams
849+ _ <- traverse (uncurry (IDBObjectStore.add store)) values
908− _ <- traverse (uncurry (IDBObjectStore.add store)) values
850+ index <- IDBObjectStore.createIndex store "index" keyPath indexParams
909− index <- IDBObjectStore.createIndex store "index" keyPath indexParams
851+ liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
910− liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
852+ putVar var { db, index, store }
911− AVar.put { db, index, store } var
853+
913− var <- AVar.empty
855+ db <- IDBFactory.open "db" Nothing
914− db <- IDBFactory.open "db" Nothing
856+ { onUpgradeNeeded : Just (onUpgradeNeeded' var)
915− { onUpgradeNeeded: Just (onUpgradeNeeded' var)
857+ , onBlocked : Nothing
916− , onBlocked: Nothing
918− AVar.take var
859+ takeVar var
920− it "continue() - iterate to the next record" do
861+ it "continue() - iterate to the next record" do
921− { db } <- setup
862+ { db } <- setup
922− { storeParams: IDBDatabase.defaultParameters
863+ { storeParams : IDBDatabase.defaultParameters
923− , indexParams: IDBObjectStore.defaultParameters
864+ , indexParams : IDBObjectStore.defaultParameters
924− , keyPath: []
865+ , keyPath : []
925− , values:
866+ , values : [ "cupcake" :+: (Just $ toKey 4)
926− [ "cupcake" :+: (Just $ toKey 4)
867+ , "pancake" :+: (Just $ toKey 2)
927− , "pancake" :+: (Just $ toKey 2)
868+ , "pie" :+: (Just $ toKey 1)
928− , "pie" :+: (Just $ toKey 1)
869+ , "pie" :+: (Just $ toKey 3)
929− , "pie" :+: (Just $ toKey 3)
870+ ]
930− ]
871+ , onUpgradeNeeded: Nothing
931− , onUpgradeNeeded: Nothing
872+ }
932− }
873+ let cb vdone vvals =
935 { onComplete: launchAff' do
874 { onComplete: launchAff' do
936− AVar.put unit vdone
875+ putVar vdone unit
938− , onError: \error -> launchAff' do
877+ , onError: \error -> launchAff' do
939 fail $ "unexpected error: " <> show error
878 fail $ "unexpected error: " <> show error
941 , onSuccess: \cursor -> launchAff' do
880 , onSuccess: \cursor -> launchAff' do
942− vals <- AVar.take vvals
881+ vals <- takeVar vvals
943 pure (IDBCursor.value cursor) >>= shouldEqual (maybe "" _.v $ head vals)
882 pure (IDBCursor.value cursor) >>= shouldEqual (maybe "" _.v $ head vals)
944− IDBCursor.primaryKey cursor >>= shouldEqual (maybe (toKey 0) _.k $ head vals)
883+ IDBCursor.primaryKey cursor >>= shouldEqual (maybe (toKey 0) _.k $ head vals)
945− AVar.put (drop 1 vals) vvals
884+ putVar vvals (drop 1 vals)
946 IDBCursor.continue cursor none
885 IDBCursor.continue cursor none
948− vdone <- AVar.empty
887+ vdone <- makeVar
949− vvals <- AVar.new
888+ vvals <- makeVar'
950− [ { v: "pie", k: toKey 1 }
889+ [ { v: "pie" , k: toKey 1 }
951− , { v: "pancake", k: toKey 2 }
890+ , { v: "pancake", k: toKey 2 }
952− , { v: "pie", k: toKey 3 }
891+ , { v: "pie" , k: toKey 3 }
953− , { v: "cupcake", k: toKey 4 }
892+ , { v: "cupcake", k: toKey 4 }
955− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
894+ tx <- IDBDatabase.transaction db ["store"] ReadOnly
956− store <- IDBTransaction.objectStore tx "store"
895+ store <- IDBTransaction.objectStore tx "store"
957− IDBObjectStore.openCursor store Nothing Next (cb vdone vvals)
896+ IDBObjectStore.openCursor store Nothing Next (cb vdone vvals)
958− AVar.take vdone
897+ takeVar vdone
959− tearDown db
898+ tearDown db
961− it "continue() - attempt to iterate in the wrong direction" do
900+ it "continue() - attempt to iterate in the wrong direction" do
962− { db } <- setup
901+ { db } <- setup
963− { storeParams: IDBDatabase.defaultParameters
902+ { storeParams : IDBDatabase.defaultParameters
964− , indexParams: IDBObjectStore.defaultParameters
903+ , indexParams : IDBObjectStore.defaultParameters
965− , keyPath: []
904+ , keyPath : []
966− , values:
905+ , values : [ "cupcake" :+: (Just $ toKey 4)
967− [ "cupcake" :+: (Just $ toKey 4)
906+ , "pancake" :+: (Just $ toKey 2)
968− , "pancake" :+: (Just $ toKey 2)
907+ , "pie" :+: (Just $ toKey 1)
969− , "pie" :+: (Just $ toKey 1)
908+ , "pie" :+: (Just $ toKey 3)
970− , "pie" :+: (Just $ toKey 3)
909+ ]
971− ]
910+ , onUpgradeNeeded: Nothing
972− , onUpgradeNeeded: Nothing
911+ }
973− }
912+ let cb vdone =
976 { onComplete: launchAff' do
913 { onComplete: launchAff' do
977 fail $ "shouldn't complete"
914 fail $ "shouldn't complete"
979− , onError: \error -> launchAff' do
916+ , onError: \error -> launchAff' do
980 fail $ "unexpected error: " <> show error
917 fail $ "unexpected error: " <> show error
982 , onSuccess: \cursor -> launchAff' do
919 , onSuccess: \cursor -> launchAff' do
983 res <- attempt $ IDBCursor.continue cursor (Just 1)
920 res <- attempt $ IDBCursor.continue cursor (Just 1)
984 case res of
921 case res of
985− Left err -> do
922+ Left err -> do
986− name err `shouldEqual` "DataError"
923+ message err `shouldEqual` "DataError"
987− AVar.put unit vdone
924+ putVar vdone unit
988 Right _ -> do
925 Right _ -> do
989 fail "expected continue to fail"
926 fail "expected continue to fail"
991− vdone <- AVar.empty
928+ vdone <- makeVar
992− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
929+ tx <- IDBDatabase.transaction db ["store"] ReadOnly
993− store <- IDBTransaction.objectStore tx "store"
930+ store <- IDBTransaction.objectStore tx "store"
994− IDBObjectStore.openCursor store Nothing Next (cb vdone)
931+ IDBObjectStore.openCursor store Nothing Next (cb vdone)
995− AVar.take vdone
932+ takeVar vdone
996− tearDown db
933+ tearDown db
998− it "advance() - iterate cursor number of times specified by count" do
935+
999− { db } <- setup
936+ it "advance() - iterate cursor number of times specified by count" do
1000− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
937+ { db } <- setup
1001− , indexParams: IDBObjectStore.defaultParameters
938+ { storeParams : { keyPath: ["pKey"], autoIncrement: false }
1002− , keyPath: []
939+ , indexParams : IDBObjectStore.defaultParameters
1003− , values:
940+ , keyPath : []
1004− [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
941+ , values : [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1005− , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
942+ , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
1006− , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
943+ , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
1007− , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
944+ , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
1009− , onUpgradeNeeded: Nothing
946+ , onUpgradeNeeded: Nothing
1011− let
948+ let cb vdone vjump =
1013 { onComplete: launchAff' do
949 { onComplete: launchAff' do
1014− AVar.put unit vdone
950+ putVar vdone unit
1016− , onError: \error -> launchAff' do
952+ , onError: \error -> launchAff' do
1017 fail $ "unexpected error: " <> show error
953 fail $ "unexpected error: " <> show error
1019 , onSuccess: \cursor -> launchAff' do
955 , onSuccess: \cursor -> launchAff' do
1020− jump <- AVar.take vjump
956+ jump <- takeVar vjump
1021− if jump then do
957+ if jump
1022− IDBCursor.advance cursor 3
958+ then do
1023− else do
959+ IDBCursor.advance cursor 3
1024− let value = IDBCursor.value cursor
960+ else do
1025− value.pKey `shouldEqual` "pkey_3"
961+ let value = IDBCursor.value cursor
1026− value.iKey `shouldEqual` "ikey_3"
962+ value.pKey `shouldEqual` "pkey_3"
1027− IDBCursor.continue cursor none
963+ value.iKey `shouldEqual` "ikey_3"
1028− AVar.put false vjump
964+ IDBCursor.continue cursor none
1030− vdone <- AVar.empty
967+ vdone <- makeVar
1031− vjump <- AVar.new true
968+ vjump <- makeVar' true
1032− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
969+ tx <- IDBDatabase.transaction db ["store"] ReadOnly
1033− store <- IDBTransaction.objectStore tx "store"
970+ store <- IDBTransaction.objectStore tx "store"
1034− IDBObjectStore.openCursor store Nothing Next (cb vdone vjump)
971+ IDBObjectStore.openCursor store Nothing Next (cb vdone vjump)
1035− AVar.take vdone
972+ takeVar vdone
1036− tearDown db
973+ tearDown db
1038− it "delete() - remove a record from the object store" do
975+ it "delete() - remove a record from the object store" do
1039− { db } <- setup
976+ { db } <- setup
1040− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
977+ { storeParams : { keyPath: ["pKey"], autoIncrement: false }
1041− , indexParams: IDBObjectStore.defaultParameters
978+ , indexParams : IDBObjectStore.defaultParameters
1042− , keyPath: []
979+ , keyPath : []
1043− , values:
980+ , values : [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1044− [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
981+ , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
1045− , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
982+ , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
1046− , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
983+ , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
1047− , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
984+ ]
1048− ]
985+ , onUpgradeNeeded: Nothing
1049− , onUpgradeNeeded: Nothing
986+ }
1050− }
987+ let cb vdone store =
1053 { onComplete: launchAff' do
988 { onComplete: launchAff' do
1054 mval <- map _.pKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
989 mval <- map _.pKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
1055 mval `shouldEqual` (Nothing :: Maybe String)
990 mval `shouldEqual` (Nothing :: Maybe String)
1056− AVar.put unit vdone
991+ putVar vdone unit
1058− , onError: \error -> launchAff' do
993+ , onError: \error -> launchAff' do
1059 fail $ "unexpected error: " <> show error
994 fail $ "unexpected error: " <> show error
1061 , onSuccess: \cursor -> launchAff' do
996 , onSuccess: \cursor -> launchAff' do
@@ -1064,31 +999,29 @@ main = do
1064 IDBCursor.delete cursor
999 IDBCursor.delete cursor
1065 IDBCursor.advance cursor 4
1000 IDBCursor.advance cursor 4
1067− vdone <- AVar.empty
1002+ vdone <- makeVar
1068− tx <- IDBDatabase.transaction db [ "store" ] ReadWrite
1003+ tx <- IDBDatabase.transaction db ["store"] ReadWrite
1069− store <- IDBTransaction.objectStore tx "store"
1004+ store <- IDBTransaction.objectStore tx "store"
1070− IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1005+ IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1071− AVar.take vdone
1006+ takeVar vdone
1072− tearDown db
1007+ tearDown db
1074− it "update() - modify a record in the object store" do
1009+ it "update() - modify a record in the object store" do
1075− { db } <- setup
1010+ { db } <- setup
1076− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1011+ { storeParams : { keyPath: ["pKey"], autoIncrement: false }
1077− , indexParams: IDBObjectStore.defaultParameters
1012+ , indexParams : IDBObjectStore.defaultParameters
1078− , keyPath: []
1013+ , keyPath : []
1079− , values:
1014+ , values : [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1080− [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1015+ ]
1081− ]
1016+ , onUpgradeNeeded: Nothing
1082− , onUpgradeNeeded: Nothing
1017+ }
1083− }
1018+ let cb vdone store =
1086 { onComplete: launchAff' do
1019 { onComplete: launchAff' do
1087 mval <- map _.iKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
1020 mval <- map _.iKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
1088 mval `shouldEqual` (Just "patate")
1021 mval `shouldEqual` (Just "patate")
1089− AVar.put unit vdone
1022+ putVar vdone unit
1091− , onError: \error -> launchAff' do
1024+ , onError: \error -> launchAff' do
1092 fail $ "unexpected error: " <> show error
1025 fail $ "unexpected error: " <> show error
1094 , onSuccess: \cursor -> launchAff' do
1027 , onSuccess: \cursor -> launchAff' do
@@ -1098,44 +1031,41 @@ main = do
1098 key `shouldEqual` toKey "pkey_0"
1031 key `shouldEqual` toKey "pkey_0"
1099 IDBCursor.advance cursor 4
1032 IDBCursor.advance cursor 4
1101− vdone <- AVar.empty
1034+ vdone <- makeVar
1102− tx <- IDBDatabase.transaction db [ "store" ] ReadWrite
1035+ tx <- IDBDatabase.transaction db ["store"] ReadWrite
1103− store <- IDBTransaction.objectStore tx "store"
1036+ store <- IDBTransaction.objectStore tx "store"
1104− IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1037+ IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1105− AVar.take vdone
1038+ takeVar vdone
1106− tearDown db
1039+ tearDown db
1108− it "update() - throw ReadOnlyError after update on ReadOnly transaction" do
1041+ it "update() - throw ReadOnlyError after update on ReadOnly transaction" do
1109− { db } <- setup
1042+ { db } <- setup
1110− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1043+ { storeParams : { keyPath: ["pKey"], autoIncrement: false }
1111− , indexParams: IDBObjectStore.defaultParameters
1044+ , indexParams : IDBObjectStore.defaultParameters
1112− , keyPath: []
1045+ , keyPath : []
1113− , values:
1046+ , values : [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1114− [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1047+ ]
1115− ]
1048+ , onUpgradeNeeded: Nothing
1116− , onUpgradeNeeded: Nothing
1049+ }
1117− }
1050+ let cb vdone =
1120 { onComplete: launchAff' do
1051 { onComplete: launchAff' do
1121 fail $ "shouldn't complete"
1052 fail $ "shouldn't complete"
1123− , onError: \error -> launchAff' do
1054+ , onError: \error -> launchAff' do
1124 fail $ "unexpected error: " <> show error
1055 fail $ "unexpected error: " <> show error
1126 , onSuccess: \cursor -> launchAff' do
1057 , onSuccess: \cursor -> launchAff' do
1127 res <- attempt $ IDBCursor.update cursor "patate"
1058 res <- attempt $ IDBCursor.update cursor "patate"
1128 case res of
1059 case res of
1129 Left err -> do
1060 Left err -> do
1130− name err `shouldEqual` "ReadOnlyError"
1061+ message err `shouldEqual` "ReadOnlyError"
1131− AVar.put unit vdone
1062+ putVar vdone unit
1132 Right _ ->
1063 Right _ ->
1133 fail $ "expected ReadOnlyError"
1064 fail $ "expected ReadOnlyError"
1135− vdone <- AVar.empty
1066+ vdone <- makeVar
1136− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
1067+ tx <- IDBDatabase.transaction db ["store"] ReadOnly
1137− store <- IDBTransaction.objectStore tx "store"
1068+ store <- IDBTransaction.objectStore tx "store"
1138− IDBObjectStore.openCursor store Nothing Next (cb vdone)
1069+ IDBObjectStore.openCursor store Nothing Next (cb vdone)
1139− AVar.take vdone
1070+ takeVar vdone
1140− tearDown db
1071+ tearDown db