-
-
Notifications
You must be signed in to change notification settings - Fork 642
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hopefully resolves #315 by introducing an alternate tranx method that…
… replaces window.Promise withing the transaction scope only. NOTE: It replaces window.Promise temporarly only for code that executes within the transaction scope, so it won't disturb neither Zone.js or other libraries that depend on window.Promise. This will only affect your DB-facing code and code you call from withing your transaction scope.
- Loading branch information
1 parent
95a7ddc
commit 203143f
Showing
4 changed files
with
89 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import Dexie from 'dexie'; | ||
import {module, stop, start, asyncTest, equal, ok} from 'QUnit'; | ||
import {resetDatabase, spawnedTest} from './dexie-unittest-utils'; | ||
|
||
"use strict"; | ||
|
||
var db = new Dexie("TestDBTranx"); | ||
db.version(1).stores({ | ||
items: "id" | ||
}); | ||
|
||
module("tranx", { | ||
setup: function () { | ||
stop(); | ||
resetDatabase(db).catch(function (e) { | ||
ok(false, "Error resetting database: " + e.stack); | ||
}).finally(start); | ||
}, | ||
teardown: function () { | ||
} | ||
}); | ||
|
||
asyncTest("Should be able to use global Promise within transaction scopes", function(){ | ||
db.tranx('rw', db.items, trans => { | ||
return window.Promise.resolve().then(()=> { | ||
ok(Dexie.currentTransaction == trans, "Transaction scopes should persist through Promise.resolve()"); | ||
return db.items.add({ id: "foobar" }); | ||
}).then(()=>{ | ||
return Promise.resolve(); | ||
}).then(()=>{ | ||
ok(Dexie.currentTransaction == trans, "Transaction scopes should persist through Promise.resolve()"); | ||
return db.items.get('foobar'); | ||
}); | ||
}).then (function(foobar){ | ||
equal(foobar.id, 'foobar', "Transaction should have lived throughout the Promise.resolve() chain"); | ||
}).catch (e => { | ||
ok(false, `Error: ${e.stack || e}`); | ||
}).finally(start); | ||
}); | ||
|
||
|
||
spawnedTest("Should use Promise.all where applicable", function* (){ | ||
yield db.tranx('rw', db.items, function* () { | ||
let x = yield Promise.resolve(3); | ||
yield db.items.bulkAdd([{id: 'a'}, {id: 'b'}]); | ||
let all = yield Promise.all(db.items.get('a'), db.items.get('b')); | ||
equal (all.length, 2); | ||
equal (all[0].id, 'a'); | ||
equal (all[1].id, 'b'); | ||
}); | ||
}); |