@@ -2,8 +2,8 @@ module Test.Main where
22
33import Prelude
44
5−import Control.Monad.Aff (Aff, launchAff, forkAff, delay, attempt)
6−import Control.Monad.Aff.AVar (AVAR, makeVar, makeVar', modifyVar, peekVar, putVar, takeVar)
5+import Control.Monad.Aff (Aff, launchAff, launchAff_, forkAff, delay, attempt)
6+import Control.Monad.Aff.AVar (AVAR, AVar, makeVar, makeEmptyVar, readVar, putVar, takeVar)
77import Control.Monad.Aff.Console (log)
88import Control.Monad.Eff (Eff)
99import Control.Monad.Eff.Class (liftEff)
@@ -37,9 +37,16 @@ import Database.IndexedDB.IDBTransaction as IDBTransaction
3737
3838infixr 7 Tuple as :+:
3939
40−launchAff' :: forall a e. Aff e a -> Eff (exception :: EXCEPTION | e) Unit
41−launchAff' aff =
42− pure unit <* (launchAff aff)
40+launchAff' :: forall a e. Aff e a -> Eff e Unit
41+launchAff' =
42+ launchAff_
43+
44+
45+modifyVar :: forall eff a. (a -> a) -> AVar a -> Aff (avar :: AVAR | eff) Unit
46+modifyVar fn v = do
47+ val <- takeVar v
48+ putVar (fn val) v
49+
4350
4451-- main :: forall eff. Eff (now :: NOW, mocha :: MOCHA, idb :: IDB, exception :: EXCEPTION, avar :: AVAR | eff) Unit
4552main = runMocha do
@@ -75,17 +82,17 @@ main = runMocha do
7582 it "open specific version -> close -> open latest" do
7683 let name = "db-latest"
7784 version = 14
78− db <- IDBFactory.open name (Just version)
85+ db01 <- IDBFactory.open name (Just version)
7986 { onUpgradeNeeded : Nothing
8087 , onBlocked : Nothing
8188 }
82− IDBDatabase.name db `shouldEqual` name
83− IDBDatabase.close db
84− db <- IDBFactory.open name Nothing
89+ IDBDatabase.name db01 `shouldEqual` name
90+ IDBDatabase.close db01
91+ db02 <- IDBFactory.open name Nothing
8592 { onUpgradeNeeded : Nothing
8693 , onBlocked : Nothing
8794 }
88− tearDown name version db
95+ tearDown name version db02
8996
9097 it "open + onUpgradeNeed" do
9198 let name = "db-upgrade-needed"
@@ -94,14 +101,14 @@ main = runMocha do
94101 _ <- launchAff $ modifyVar (const $ IDBDatabase.name db) varName
95102 _ <- launchAff $ modifyVar (const $ oldVersion) varVersion
96103 pure unit
97− varName <- makeVar' "-"
98− varVersion <- makeVar' (-1)
104+ varName <- makeVar "-"
105+ varVersion <- makeVar (-1)
99106 db <- IDBFactory.open name Nothing
100107 { onUpgradeNeeded : Just (callback (Tuple varName varVersion))
101108 , onBlocked : Nothing
102109 }
103− name' <- peekVar varName
104− version' <- peekVar varVersion
110+ name' <- readVar varName
111+ version' <- readVar varVersion
105112 name' `shouldEqual` name
106113 version' `shouldEqual` 0
107114 tearDown name version db
@@ -113,7 +120,7 @@ main = runMocha do
113120 _ <- launchAff $ modifyVar (const $ "db-blocked") var
114121 pure unit
115122
116− var <- makeVar' "-"
123+ var <- makeVar "-"
117124 db01 <- IDBFactory.open name Nothing
118125 { onUpgradeNeeded : Nothing
119126 , onBlocked : Nothing
@@ -126,7 +133,7 @@ main = runMocha do
126133 { onUpgradeNeeded : Nothing
127134 , onBlocked : Just (callback var)
128135 }
129− name' <- peekVar var
136+ name' <- readVar var
130137 name' `shouldEqual` name
131138 tearDown name version db02
132139
@@ -293,10 +300,10 @@ main = runMocha do
293300 setup storeParams = do
294301 let onUpgradeNeeded var db _ _ = launchAff' do
295302 store <- IDBDatabase.createObjectStore db "store" storeParams
296− _ <- putVar var { db, store }
303+ _ <- putVar { db, store } var
297304 pure unit
298305
299− var <- makeVar
306+ var <- makeEmptyVar
300307 db <- IDBFactory.open "db" Nothing
301308 { onUpgradeNeeded : Just (onUpgradeNeeded var)
302309 , onBlocked : Nothing
@@ -331,17 +338,17 @@ main = runMocha do
331338 it "deleteObjectStore" do
332339 let onUpgradeNeeded var db _ _ = launchAff' do
333340 _ <- IDBDatabase.deleteObjectStore db "store"
334− putVar var true
341+ putVar true var
335342
336− var <- makeVar
343+ var <- makeEmptyVar
337344 { db, store } <- setup IDBDatabase.defaultParameters
338345 IDBDatabase.close db
339− db <- IDBFactory.open "db" (Just 999) { onUpgradeNeeded : Just (onUpgradeNeeded var)
346+ db' <- IDBFactory.open "db" (Just 999) { onUpgradeNeeded : Just (onUpgradeNeeded var)
340347 , onBlocked : Nothing
341348 }
342349 deleted <- takeVar var
343350 deleted `shouldEqual` true
344− tearDown db
351+ tearDown db'
345352
346353
347354 describe "IDBObjectStore" do
@@ -355,9 +362,9 @@ main = runMocha do
355362 let onUpgradeNeeded' var db _ _ = launchAff' do
356363 store <- IDBDatabase.createObjectStore db "store" storeParams
357364 liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store)
358− putVar var { db, store }
365+ putVar { db, store } var
359366
360− var <- makeVar
367+ var <- makeEmptyVar
361368 db <- IDBFactory.open "db" Nothing
362369 { onUpgradeNeeded : Just (onUpgradeNeeded' var)
363370 , onBlocked : Nothing
@@ -375,24 +382,24 @@ main = runMocha do
375382 (toKey 1) `shouldEqual` key
376383
377384 -- int key
378− key <- IDBObjectStore.add store "patate" (Just 14)
379− (toKey 14) `shouldEqual` key
385+ key' <- IDBObjectStore.add store "patate" (Just 14)
386+ (toKey 14) `shouldEqual` key'
380387
381388 -- number key
382− key <- IDBObjectStore.add store "patate" (Just 14.42)
383− (toKey 14.42) `shouldEqual` key
389+ key'' <- IDBObjectStore.add store "patate" (Just 14.42)
390+ (toKey 14.42) `shouldEqual` key''
384391
385392 -- string key
386− key <- IDBObjectStore.add store "patate" (Just "key")
387− (toKey "key") `shouldEqual` key
393+ key''' <- IDBObjectStore.add store "patate" (Just "key")
394+ (toKey "key") `shouldEqual` key'''
388395
389396 -- date key
390− key <- IDBObjectStore.add store "patate" (Just date)
391− (toKey date) `shouldEqual` key
397+ key'''' <- IDBObjectStore.add store "patate" (Just date)
398+ (toKey date) `shouldEqual` key''''
392399
393400 -- array key
394− key <- IDBObjectStore.add store "patate" (Just $ toKey [14, 42])
395− (toKey [14, 42]) `shouldEqual` key
401+ key''''' <- IDBObjectStore.add store "patate" (Just $ toKey [14, 42])
402+ (toKey [14, 42]) `shouldEqual` key'''''
396403 }
397404 tearDown db
398405
@@ -411,8 +418,8 @@ main = runMocha do
411418 { db } <- setup
412419 { storeParams: IDBDatabase.defaultParameters
413420 , onUpgradeNeeded: Just $ \_ store -> launchAff' do
414− key <- IDBObjectStore.add store "patate" (Just 14)
415− key <- IDBObjectStore.add store "autruche" (Just 42)
421+ _ <- IDBObjectStore.add store "patate" (Just 14)
422+ _ <- IDBObjectStore.add store "autruche" (Just 42)
416423 n <- IDBObjectStore.count store Nothing
417424 n `shouldEqual` 2
418425 }
@@ -426,8 +433,8 @@ main = runMocha do
426433 mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 14)
427434 mkey `shouldEqual` (Just key)
428435
429− mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
430− mkey `shouldEqual` none
436+ mkey' <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
437+ mkey' `shouldEqual` none
431438 }
432439 tearDown db
433440
@@ -444,16 +451,16 @@ main = runMocha do
444451 keys `shouldEqual` [key1, key2, key3]
445452
446453 -- lower bound
447− keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 14 true) Nothing
448− keys `shouldEqual` [key2, key3]
454+ keys' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 14 true) Nothing
455+ keys' `shouldEqual` [key2, key3]
449456
450457 -- upper bound
451− keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.upperBound 42 false) Nothing
452− keys `shouldEqual` [key1, key2]
458+ keys'' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.upperBound 42 false) Nothing
459+ keys'' `shouldEqual` [key1, key2]
453460
454461 -- count
455− keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
456− keys `shouldEqual` [key1, key2]
462+ keys''' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
463+ keys''' `shouldEqual` [key1, key2]
457464 }
458465 tearDown db
459466
@@ -513,9 +520,9 @@ main = runMocha do
513520 _ <- traverse (uncurry (IDBObjectStore.add store)) values
514521 index <- IDBObjectStore.createIndex store "index" keyPath indexParams
515522 liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
516− putVar var { db, index, store }
523+ putVar { db, index, store } var
517524
518− var <- makeVar
525+ var <- makeEmptyVar
519526 db <- IDBFactory.open "db" Nothing
520527 { onUpgradeNeeded : Just (onUpgradeNeeded' var)
521528 , onBlocked : Nothing
@@ -538,9 +545,9 @@ main = runMocha do
538545 tearDown db
539546
540547 it "attempt to create an index that requires unique values on an object store already contains duplicates" do
541− let onAbort var = launchAff' (putVar var true)
542− txVar <- makeVar
543− dbVar <- makeVar
548+ let onAbort var = launchAff' (putVar true var)
549+ txVar <- makeEmptyVar
550+ dbVar <- makeEmptyVar
544551 res <- attempt $ setup
545552 { storeParams : IDBDatabase.defaultParameters
546553 , indexParams : { unique : true
@@ -802,11 +809,11 @@ main = runMocha do
802809 key <- IDBIndex.getKey index (IDBKeyRange.only "patate")
803810 key `shouldEqual` (Just $ toKey 1)
804811
805− key <- IDBIndex.getKey index (IDBKeyRange.only "autruche")
806− key `shouldEqual` (Just $ toKey 1)
812+ key' <- IDBIndex.getKey index (IDBKeyRange.only "autruche")
813+ key' `shouldEqual` (Just $ toKey 1)
807814
808− key <- IDBIndex.getKey index (IDBKeyRange.only "bob")
809− key `shouldEqual` (Just $ toKey 2)
815+ key'' <- IDBIndex.getKey index (IDBKeyRange.only "bob")
816+ key'' `shouldEqual` (Just $ toKey 2)
810817 }
811818 tearDown db
812819
@@ -851,9 +858,9 @@ main = runMocha do
851858 _ <- traverse (uncurry (IDBObjectStore.add store)) values
852859 index <- IDBObjectStore.createIndex store "index" keyPath indexParams
853860 liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
854− putVar var { db, index, store }
861+ putVar { db, index, store } var
855862
856− var <- makeVar
863+ var <- makeEmptyVar
857864 db <- IDBFactory.open "db" Nothing
858865 { onUpgradeNeeded : Just (onUpgradeNeeded' var)
859866 , onBlocked : Nothing
@@ -874,7 +881,7 @@ main = runMocha do
874881 }
875882 let cb vdone vvals =
876883 { onComplete: launchAff' do
877− putVar vdone unit
884+ putVar unit vdone
878885
879886 , onError: \error -> launchAff' do
880887 fail $ "unexpected error: " <> show error
@@ -883,11 +890,11 @@ main = runMocha do
883890 vals <- takeVar vvals
884891 pure (IDBCursor.value cursor) >>= shouldEqual (maybe "" _.v $ head vals)
885892 IDBCursor.primaryKey cursor >>= shouldEqual (maybe (toKey 0) _.k $ head vals)
886− putVar vvals (drop 1 vals)
893+ putVar (drop 1 vals) vvals
887894 IDBCursor.continue cursor none
888895 }
889− vdone <- makeVar
890− vvals <- makeVar'
896+ vdone <- makeEmptyVar
897+ vvals <- makeVar
891898 [ { v: "pie" , k: toKey 1 }
892899 , { v: "pancake", k: toKey 2 }
893900 , { v: "pie" , k: toKey 3 }
@@ -923,11 +930,11 @@ main = runMocha do
923930 case res of
924931 Left err -> do
925932 name err `shouldEqual` "DataError"
926− putVar vdone unit
933+ putVar unit vdone
927934 Right _ -> do
928935 fail "expected continue to fail"
929936 }
930− vdone <- makeVar
937+ vdone <- makeEmptyVar
931938 tx <- IDBDatabase.transaction db ["store"] ReadOnly
932939 store <- IDBTransaction.objectStore tx "store"
933940 IDBObjectStore.openCursor store Nothing Next (cb vdone)
@@ -949,7 +956,7 @@ main = runMocha do
949956 }
950957 let cb vdone vjump =
951958 { onComplete: launchAff' do
952− putVar vdone unit
959+ putVar unit vdone
953960
954961 , onError: \error -> launchAff' do
955962 fail $ "unexpected error: " <> show error
@@ -964,10 +971,10 @@ main = runMocha do
964971 value.pKey `shouldEqual` "pkey_3"
965972 value.iKey `shouldEqual` "ikey_3"
966973 IDBCursor.continue cursor none
967− putVar vjump false
974+ putVar false vjump
968975 }
969− vdone <- makeVar
970− vjump <- makeVar' true
976+ vdone <- makeEmptyVar
977+ vjump <- makeVar true
971978 tx <- IDBDatabase.transaction db ["store"] ReadOnly
972979 store <- IDBTransaction.objectStore tx "store"
973980 IDBObjectStore.openCursor store Nothing Next (cb vdone vjump)
@@ -990,7 +997,7 @@ main = runMocha do
990997 { onComplete: launchAff' do
991998 mval <- map _.pKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
992999 mval `shouldEqual` (Nothing :: Maybe String)
993− putVar vdone unit
1000+ putVar unit vdone
9941001
9951002 , onError: \error -> launchAff' do
9961003 fail $ "unexpected error: " <> show error
@@ -1001,7 +1008,7 @@ main = runMocha do
10011008 IDBCursor.delete cursor
10021009 IDBCursor.advance cursor 4
10031010 }
1004− vdone <- makeVar
1011+ vdone <- makeEmptyVar
10051012 tx <- IDBDatabase.transaction db ["store"] ReadWrite
10061013 store <- IDBTransaction.objectStore tx "store"
10071014 IDBObjectStore.openCursor store Nothing Next (cb vdone store)
@@ -1021,7 +1028,7 @@ main = runMocha do
10211028 { onComplete: launchAff' do
10221029 mval <- map _.iKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
10231030 mval `shouldEqual` (Just "patate")
1024− putVar vdone unit
1031+ putVar unit vdone
10251032
10261033 , onError: \error -> launchAff' do
10271034 fail $ "unexpected error: " <> show error
@@ -1033,7 +1040,7 @@ main = runMocha do
10331040 key `shouldEqual` toKey "pkey_0"
10341041 IDBCursor.advance cursor 4
10351042 }
1036− vdone <- makeVar
1043+ vdone <- makeEmptyVar
10371044 tx <- IDBDatabase.transaction db ["store"] ReadWrite
10381045 store <- IDBTransaction.objectStore tx "store"
10391046 IDBObjectStore.openCursor store Nothing Next (cb vdone store)
@@ -1061,11 +1068,11 @@ main = runMocha do
10611068 case res of
10621069 Left err -> do
10631070 name err `shouldEqual` "ReadOnlyError"
1064− putVar vdone unit
1071+ putVar unit vdone
10651072 Right _ ->
10661073 fail $ "expected ReadOnlyError"
10671074 }
1068− vdone <- makeVar
1075+ vdone <- makeEmptyVar
10691076 tx <- IDBDatabase.transaction db ["store"] ReadOnly
10701077 store <- IDBTransaction.objectStore tx "store"
10711078 IDBObjectStore.openCursor store Nothing Next (cb vdone)