Skip to content

Commit

Permalink
Use IDBCursor methods in the beginning of reading a cursor (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
YulimitBreak authored Oct 20, 2022
1 parent b3ab7a4 commit e923c35
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
26 changes: 26 additions & 0 deletions core/src/jsMain/kotlin/CursorStart.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.juul.indexeddb

import com.juul.indexeddb.external.IDBCursor

public sealed class CursorStart {

internal abstract fun apply(cursor: IDBCursor)

public data class Advance(val count: Int) : CursorStart() {
override fun apply(cursor: IDBCursor) {
cursor.advance(count)
}
}

public data class Continue(val key: Key) : CursorStart() {
override fun apply(cursor: IDBCursor) {
cursor.`continue`(key.toJs())
}
}

public data class ContinuePrimaryKey(val key: Key, val primaryKey: Key) : CursorStart() {
override fun apply(cursor: IDBCursor) {
cursor.continuePrimaryKey(key.toJs(), primaryKey.toJs())
}
}
}
11 changes: 10 additions & 1 deletion core/src/jsMain/kotlin/Transaction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,23 @@ public open class Transaction internal constructor(
public suspend fun Queryable.openCursor(
query: Key? = null,
direction: Cursor.Direction = Cursor.Direction.Next,
cursorStart: CursorStart? = null,
): Flow<CursorWithValue> = openCursorImpl(
query,
direction,
cursorStart,
open = this::requestOpenCursor,
wrap = ::CursorWithValue,
)

public suspend fun Queryable.openKeyCursor(
query: Key? = null,
direction: Cursor.Direction = Cursor.Direction.Next,
cursorStart: CursorStart? = null,
): Flow<Cursor> = openCursorImpl(
query,
direction,
cursorStart,
open = this::requestOpenKeyCursor,
wrap = ::Cursor,
)
Expand All @@ -70,14 +74,19 @@ public open class Transaction internal constructor(
private suspend fun <T : Cursor, U : IDBCursor> openCursorImpl(
query: Key?,
direction: Cursor.Direction,
cursorStart: CursorStart?,
open: (Key?, Cursor.Direction) -> Request<U?>,
wrap: (U) -> T,
): Flow<T> = callbackFlow {
var cursorStartAction = cursorStart
val request = open(query, direction).request
val onSuccess: (Event) -> Unit = { event ->
@Suppress("UNCHECKED_CAST")
val cursor = (event.target as IDBRequest<U?>).result
if (cursor != null) {
if (cursorStartAction != null && cursor != null) {
cursorStartAction?.apply(cursor)
cursorStartAction = null
} else if (cursor != null) {
val result = trySend(wrap(cursor))
when {
result.isSuccess -> cursor.`continue`()
Expand Down
21 changes: 21 additions & 0 deletions core/src/jsTest/kotlin/Samples.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.juul.indexeddb

import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.toList
import kotlin.test.Test
import kotlin.test.assertEquals

Expand Down Expand Up @@ -68,5 +69,25 @@ class Samples {
objectStore("customers").index("age").count(upperBound(32))
}
assertEquals(2, countBelowThirtyTwo)

val skipTwoYoungest = database.transaction("customers") {
objectStore("customers")
.index("age")
.openCursor(cursorStart = CursorStart.Advance(2))
.map { it.value as Customer }
.map { it.name }
.toList()
}
assertEquals(listOf("Alice", "Bill"), skipTwoYoungest)

val skipUntil33 = database.transaction("customers") {
objectStore("customers")
.index("age")
.openCursor(cursorStart = CursorStart.Continue(Key(33)))
.map { it.value as Customer }
.map { it.name }
.toList()
}
assertEquals(listOf("Alice", "Bill"), skipUntil33)
}
}
6 changes: 6 additions & 0 deletions external/src/jsMain/kotlin/IDBCursor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ public external interface IDBCursor {
public val primaryKey: dynamic

public fun `continue`()

public fun advance(count: Int)

public fun `continue`(key: dynamic)

public fun continuePrimaryKey(key: dynamic, primaryKey: dynamic)
}

/** https://developer.mozilla.org/en-US/docs/Web/API/IDBCursorWithValue */
Expand Down

0 comments on commit e923c35

Please sign in to comment.