-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
355 additions
and
80 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
5 changes: 5 additions & 0 deletions
5
src/commonMain/kotlin/uk/gibby/driver/rpc/exception/LiveQueryKilledException.kt
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,5 @@ | ||
package uk.gibby.driver.rpc.exception | ||
|
||
import kotlinx.coroutines.CancellationException | ||
|
||
object LiveQueryKilledException: CancellationException("Live query has been killed") |
10 changes: 10 additions & 0 deletions
10
src/commonMain/kotlin/uk/gibby/driver/rpc/functions/Kill.kt
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,10 @@ | ||
package uk.gibby.driver.rpc.functions | ||
|
||
import kotlinx.serialization.json.add | ||
import kotlinx.serialization.json.buildJsonArray | ||
import uk.gibby.driver.Surreal | ||
|
||
suspend fun Surreal.kill(liveQueryId: String) { | ||
sendRequest("kill", buildJsonArray { add(liveQueryId) }) | ||
} | ||
|
56 changes: 56 additions & 0 deletions
56
src/commonMain/kotlin/uk/gibby/driver/rpc/functions/Live.kt
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,56 @@ | ||
package uk.gibby.driver.rpc.functions | ||
|
||
import io.ktor.utils.io.core.* | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.add | ||
import kotlinx.serialization.json.buildJsonArray | ||
import kotlinx.serialization.json.decodeFromJsonElement | ||
import uk.gibby.driver.Surreal | ||
import uk.gibby.driver.rpc.model.LiveQueryAction | ||
import uk.gibby.driver.rpc.model.asType | ||
import uk.gibby.driver.surrealJson | ||
import kotlin.jvm.JvmName | ||
|
||
|
||
suspend fun Surreal.live(table: String): String { | ||
val response = sendRequest("live", buildJsonArray { add(table) }) | ||
return surrealJson.decodeFromJsonElement(response) | ||
} | ||
|
||
|
||
@JvmName("observeJson") | ||
suspend fun Surreal.observeAsJson(liveQueryId: String): LiveQueryFlow<JsonElement> { | ||
val id = live(liveQueryId) | ||
return LiveQueryFlow( | ||
flow = subscribe(id), | ||
id = id, | ||
connection = this | ||
) | ||
} | ||
|
||
suspend inline fun <reified T>Surreal.observe(table: String): LiveQueryFlow<T> { | ||
val jsonFlow = observeAsJson(table) | ||
return jsonFlow.map { it.asType<T>() } | ||
} | ||
|
||
class LiveQueryFlow<T>( | ||
private val flow: Flow<LiveQueryAction<T>>, | ||
val id: String, | ||
private val connection: Surreal | ||
): Flow<LiveQueryAction<T>> by flow, Closeable { | ||
override fun close() { | ||
connection.unsubscribe(id) | ||
connection.triggerKill(id) | ||
} | ||
|
||
fun <R>map(transform: (LiveQueryAction<T>) -> LiveQueryAction<R>): LiveQueryFlow<R> { | ||
return LiveQueryFlow( | ||
flow = flow.map { transform(it) }, | ||
id = id, | ||
connection = connection | ||
) | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
src/commonMain/kotlin/uk/gibby/driver/rpc/functions/LiveQuery.kt
This file was deleted.
Oops, something went wrong.
67 changes: 63 additions & 4 deletions
67
src/commonMain/kotlin/uk/gibby/driver/rpc/model/LiveQueryAction.kt
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 |
---|---|---|
@@ -1,7 +1,66 @@ | ||
package uk.gibby.driver.rpc.model | ||
|
||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.* | ||
import kotlinx.serialization.builtins.serializer | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor | ||
import kotlinx.serialization.encoding.CompositeDecoder | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.decodeFromJsonElement | ||
import uk.gibby.driver.surrealJson | ||
|
||
@Serializable | ||
data class LiveQueryAction(val action: String, val id: String, val result: JsonObject? = null) | ||
@Serializable(with = LiveQueryActionSerializer::class) | ||
sealed class LiveQueryAction<out T>(val id: String) { | ||
class Create<T>(id: String, val result: T): LiveQueryAction<T>(id) | ||
class Update<T>(id: String, val result: T): LiveQueryAction<T>(id) | ||
class Delete(id: String, val deletedId: String): LiveQueryAction<Nothing>(id) | ||
|
||
} | ||
|
||
|
||
inline fun <reified T>LiveQueryAction<JsonElement>.asType(): LiveQueryAction<T> { | ||
return when(this) { | ||
is LiveQueryAction.Delete -> this | ||
is LiveQueryAction.Create -> LiveQueryAction.Create(id, surrealJson.decodeFromJsonElement(result)) | ||
is LiveQueryAction.Update -> LiveQueryAction.Update(id, surrealJson.decodeFromJsonElement(result)) | ||
} | ||
} | ||
|
||
class LiveQueryActionSerializer<T: Any>(private val resultSerializer: KSerializer<T>): KSerializer<LiveQueryAction<T>> { | ||
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("LiveQueryAction") { | ||
element("action", String.serializer().descriptor) | ||
element("id", String.serializer().descriptor) | ||
element("result", resultSerializer.descriptor) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): LiveQueryAction<T> { | ||
val input = decoder.beginStructure(descriptor) | ||
var action: String? = null | ||
var id: String? = null | ||
var result: Thing<T>? = null | ||
loop@ while (true) { | ||
when (val i = input.decodeElementIndex(descriptor)) { | ||
CompositeDecoder.DECODE_DONE -> break@loop | ||
0 -> action = input.decodeStringElement(descriptor, i) | ||
1 -> id = input.decodeStringElement(descriptor, i) | ||
2 -> result = input.decodeSerializableElement(descriptor, i, ThingSerializer(resultSerializer)) | ||
else -> throw SerializationException("Unknown index $i") | ||
} | ||
} | ||
input.endStructure(descriptor) | ||
if (action == null || id == null || result == null) throw SerializationException("Missing fields") | ||
return when(action) { | ||
"CREATE" -> LiveQueryAction.Create(id, (result as Thing.Record<T>).result) | ||
"UPDATE" -> LiveQueryAction.Update(id, (result as Thing.Record<T>).result) | ||
"DELETE" -> LiveQueryAction.Delete(id, (result as Thing.Reference).id) | ||
else -> throw SerializationException("Unknown action $action") | ||
} | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: LiveQueryAction<T>) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
} |
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,21 @@ | ||
import kotlinx.coroutines.test.runTest | ||
import uk.gibby.driver.Surreal | ||
import uk.gibby.driver.rpc.functions.kill | ||
import uk.gibby.driver.rpc.functions.live | ||
import uk.gibby.driver.rpc.functions.signin | ||
import uk.gibby.driver.rpc.functions.use | ||
import utils.cleanDatabase | ||
import kotlin.test.Test | ||
|
||
class KillTest { | ||
@Test | ||
fun testKill() = runTest { | ||
cleanDatabase() | ||
val connection = Surreal("localhost", 8000) | ||
connection.connect() | ||
connection.signin("root", "root") | ||
connection.use("test", "test") | ||
val liveQueryId = connection.live("test") | ||
connection.kill(liveQueryId) | ||
} | ||
} |
Oops, something went wrong.