-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for transaction rollbacks #178
Merged
+102
−4
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import kotlinx.coroutines.channels.awaitClose | |
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import org.w3c.dom.events.Event | ||
import kotlin.js.Json | ||
|
||
public open class Transaction internal constructor( | ||
internal val transaction: IDBTransaction, | ||
|
@@ -23,6 +24,26 @@ public open class Transaction internal constructor( | |
} | ||
} | ||
|
||
internal suspend fun awaitFailure() { | ||
transaction.onNextEvent("complete", "abort", "error") { event -> | ||
when (event.type) { | ||
"abort" -> Unit | ||
"error" -> Unit | ||
else -> Unit | ||
} | ||
} | ||
} | ||
internal fun abort() { | ||
transaction.abort() | ||
} | ||
|
||
internal fun commit() { | ||
// Check if function exists before calling it. | ||
if (jsTypeOf(transaction.asDynamic().commit) === "function") { | ||
transaction.commit() | ||
} | ||
} | ||
|
||
public fun objectStore(name: String): ObjectStore = | ||
ObjectStore(transaction.objectStore(name)) | ||
|
||
|
@@ -132,6 +153,7 @@ public open class Transaction internal constructor( | |
): Flow<T> = callbackFlow { | ||
var cursorStartAction = cursorStart | ||
val request = open(query, direction).request | ||
var finished = false | ||
val onSuccess: (Event) -> Unit = { event -> | ||
@Suppress("UNCHECKED_CAST") | ||
val cursor = (event.target as IDBRequest<U?>).result | ||
|
@@ -141,7 +163,7 @@ public open class Transaction internal constructor( | |
} else if (cursor != null) { | ||
val result = trySend(wrap(cursor, channel)) | ||
when { | ||
result.isSuccess -> if (autoContinue) cursor.`continue`() | ||
result.isSuccess -> if (autoContinue && !finished) cursor.`continue`() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. info: I've added this check, so Otherwise in the following code: val returnedValue = database.transaction("customers") {
objectStore("customers")
.index("name")
.openCursor(autoContinue = true)
.first()
} Cursor might try to return new value after |
||
result.isFailure -> channel.close(IllegalStateException("Send failed. Did you suspend illegally?")) | ||
result.isClosed -> channel.close() | ||
} | ||
|
@@ -153,6 +175,7 @@ public open class Transaction internal constructor( | |
request.addEventListener("success", onSuccess) | ||
request.addEventListener("error", onError) | ||
awaitClose { | ||
finished = true | ||
request.removeEventListener("success", onSuccess) | ||
request.removeEventListener("error", onError) | ||
} | ||
|
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,68 @@ | ||
package com.juul.indexeddb | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
|
||
class TransactionTest { | ||
@Test | ||
fun readWithinTransaction() = runTest { | ||
val database = openDatabase("read-within-transaction", 1) { database, oldVersion, newVersion -> | ||
if (oldVersion < 1) { | ||
database.createObjectStore("users", KeyPath("id")) | ||
} | ||
} | ||
onCleanup { | ||
database.close() | ||
deleteDatabase("read-within-transaction") | ||
} | ||
|
||
val user = database.writeTransaction("users") { | ||
objectStore("users").add( | ||
jso { | ||
id = "7740f7c4-f889-498a-bc6d-f88dabdcfb9a" | ||
username = "Username" | ||
}, | ||
) | ||
objectStore("users") | ||
.get(Key("7740f7c4-f889-498a-bc6d-f88dabdcfb9a")) | ||
} | ||
|
||
assertEquals("Username", user.username) | ||
} | ||
|
||
@Test | ||
fun whenExceptionIsThrowWithinTransaction_transactionIsAborted() = runTest { | ||
val database = openDatabase("abort-transaction", 1) { database, oldVersion, newVersion -> | ||
if (oldVersion < 1) { | ||
database.createObjectStore("users", KeyPath("id")) | ||
} | ||
} | ||
onCleanup { | ||
database.close() | ||
deleteDatabase("abort-transaction") | ||
} | ||
|
||
assertFailsWith<ExceptionToAbortTransaction> { | ||
database.writeTransaction("users") { | ||
objectStore("users").add( | ||
jso { | ||
id = "7740f7c4-f889-498a-bc6d-f88dabdcfb9a" | ||
username = "Username" | ||
}, | ||
) | ||
|
||
// Abort transaction | ||
throw ExceptionToAbortTransaction() | ||
} | ||
} | ||
|
||
// because transaction is aborted, new values shouldn't be stored | ||
val users = database.transaction("users") { | ||
objectStore("users").getAll().toList() | ||
} | ||
|
||
assertEquals(listOf(), users) | ||
} | ||
} | ||
private class ExceptionToAbortTransaction : Exception() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: commit was added later to browser, so it might not be always available
https://developer.mozilla.org/en-US/docs/Web/API/IDBTransaction#browser_compatibility
note: Also from the commit documentation we can read:
So calling it is optional.