Skip to content

Commit

Permalink
provide oldVersion metadata as part of the onUpgradeNeeded callback
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Nov 7, 2017
1 parent 51b48b6 commit c64384d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
6 changes: 5 additions & 1 deletion src/Database/IndexedDB/IDBFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const noOp2 = function noOp2() {
return noOp;
};

const noOp3 = function noOp3() {
return noOp2;
};

exports._deleteDatabase = function _deleteDatabase(name) {
return function aff(success, error) {
Expand Down Expand Up @@ -46,7 +49,8 @@ exports._open = function _open(fromMaybe, name, mver, req) {
};

request.onupgradeneeded = function onUpgradeNeeded(e) {
fromMaybe(noOp2)(req.onUpgradeNeeded)(e.target.result)(e.target.transaction)();
const meta = { oldVersion: e.oldVersion };
fromMaybe(noOp3)(req.onUpgradeNeeded)(e.target.result)(e.target.transaction)(meta)();
};

request.onerror = errorHandler(error);
Expand Down
2 changes: 1 addition & 1 deletion src/Database/IndexedDB/IDBFactory.purs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Database.IndexedDB.Core
-- Type alias for binding listeners to an initial open action.
type Callbacks e =
{ onBlocked :: Maybe (Eff (| e) Unit)
, onUpgradeNeeded :: Maybe (Database -> Transaction -> Eff (| e) Unit)
, onUpgradeNeeded :: Maybe (Database -> Transaction -> { oldVersion :: Int } -> Eff (| e) Unit)
}


Expand Down
34 changes: 18 additions & 16 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,25 @@ main = runMocha do
}
tearDown name version db


it "open + onUpgradeNeed" do
let name = "db-upgrade-needed"
version = 1
callback var db _ = do
_ <- launchAff $ modifyVar (const $ IDBDatabase.name db) var
callback (Tuple varName varVersion) db _ { oldVersion } = do
_ <- launchAff $ modifyVar (const $ IDBDatabase.name db) varName
_ <- launchAff $ modifyVar (const $ oldVersion) varVersion
pure unit
var <- makeVar' "-"
db <- IDBFactory.open name Nothing
{ onUpgradeNeeded : Just (callback var)
varName <- makeVar' "-"
varVersion <- makeVar' (-1)
db <- IDBFactory.open name Nothing
{ onUpgradeNeeded : Just (callback (Tuple varName varVersion))
, onBlocked : Nothing
}
name <- peekVar var
name `shouldEqual` name
name' <- peekVar varName
version' <- peekVar varVersion
name' `shouldEqual` name
version' `shouldEqual` 0
tearDown name version db


it "open + onBlocked" do
let name = "db-blocked"
version = 14
Expand All @@ -124,8 +126,8 @@ main = runMocha do
{ onUpgradeNeeded : Nothing
, onBlocked : Just (callback var)
}
name <- peekVar var
name `shouldEqual` name
name' <- peekVar var
name' `shouldEqual` name
tearDown name version db02

describe "IDBKeyRange" do
Expand Down Expand Up @@ -289,7 +291,7 @@ main = runMocha do
pure unit

setup storeParams = do
let onUpgradeNeeded var db _ = launchAff' do
let onUpgradeNeeded var db _ _ = launchAff' do
store <- IDBDatabase.createObjectStore db "store" storeParams
_ <- putVar var { db, store }
pure unit
Expand Down Expand Up @@ -327,7 +329,7 @@ main = runMocha do
tearDown db

it "deleteObjectStore" do
let onUpgradeNeeded var db _ = launchAff' do
let onUpgradeNeeded var db _ _ = launchAff' do
_ <- IDBDatabase.deleteObjectStore db "store"
putVar var true

Expand All @@ -350,7 +352,7 @@ main = runMocha do
pure unit

setup { storeParams, onUpgradeNeeded } = do
let onUpgradeNeeded' var db _ = launchAff' do
let onUpgradeNeeded' var db _ _ = launchAff' do
store <- IDBDatabase.createObjectStore db "store" storeParams
liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store)
putVar var { db, store }
Expand Down Expand Up @@ -506,7 +508,7 @@ main = runMocha do
, onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
} -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
let onUpgradeNeeded' var db tx = launchAff' do
let onUpgradeNeeded' var db tx _ = launchAff' do
store <- IDBDatabase.createObjectStore db "store" storeParams
_ <- traverse (uncurry (IDBObjectStore.add store)) values
index <- IDBObjectStore.createIndex store "index" keyPath indexParams
Expand Down Expand Up @@ -844,7 +846,7 @@ main = runMocha do
, onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
} -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
let onUpgradeNeeded' var db tx = launchAff' do
let onUpgradeNeeded' var db tx _ = launchAff' do
store <- IDBDatabase.createObjectStore db "store" storeParams
_ <- traverse (uncurry (IDBObjectStore.add store)) values
index <- IDBObjectStore.createIndex store "index" keyPath indexParams
Expand Down

0 comments on commit c64384d

Please sign in to comment.