Allow controls magix endpoint to receive broadcast.

This commit is contained in:
Alexander Nozik 2024-02-05 14:08:15 +03:00
parent fa2414ef47
commit b1121d61cb
4 changed files with 20 additions and 16 deletions

View File

@ -32,12 +32,14 @@ internal fun generateId(request: MagixMessage): String = if (request.id != null)
/**
* Communicate with server in [Magix format](https://github.com/waltz-controls/rfc/tree/master/1)
*
* Accepts messages with target that equals [endpointID] or null (broadcast messages)
*/
public fun DeviceManager.launchMagixService(
endpoint: MagixEndpoint,
endpointID: String = controlsMagixFormat.defaultFormat,
): Job = context.launch {
endpoint.subscribe(controlsMagixFormat, targetFilter = listOf(endpointID)).onEach { (request, payload) ->
endpoint.subscribe(controlsMagixFormat, targetFilter = listOf(endpointID, null)).onEach { (request, payload) ->
val responsePayload = respondHubMessage(payload)
responsePayload.forEach {
endpoint.send(

View File

@ -26,7 +26,7 @@ public data class MagixFormat<T>(
public fun <T> MagixEndpoint.subscribe(
format: MagixFormat<T>,
originFilter: Collection<String>? = null,
targetFilter: Collection<String>? = null,
targetFilter: Collection<String?>? = null,
): Flow<Pair<MagixMessage, T>> = subscribe(
MagixMessageFilter(format = format.formats, source = originFilter, target = targetFilter)
).map {

View File

@ -11,7 +11,7 @@ import kotlinx.serialization.Serializable
public data class MagixMessageFilter(
val format: Collection<String>? = null,
val source: Collection<String>? = null,
val target: Collection<String>? = null,
val target: Collection<String?>? = null,
) {
public fun accepts(message: MagixMessage): Boolean =

View File

@ -39,9 +39,8 @@ public class XodusMagixHistory(private val store: PersistentEntityStore) : Write
setBlobString(MagixMessage::payload.name, magixJson.encodeToString(message.payload))
message.targetEndpoint?.let {
setProperty(MagixMessage::targetEndpoint.name, it)
}
setProperty(MagixMessage::targetEndpoint.name, (message.targetEndpoint ?: ""))
message.id?.let {
setProperty(MagixMessage::id.name, it)
}
@ -68,14 +67,14 @@ public class XodusMagixHistory(private val store: PersistentEntityStore) : Write
): Unit = store.executeInReadonlyTransaction { transaction ->
val all = transaction.getAll(XodusMagixStorage.MAGIC_MESSAGE_ENTITY_TYPE)
fun StoreTransaction.findAllIn(
fun findAllIn(
entityType: String,
field: String,
values: Collection<String>?,
values: Collection<String?>?,
): EntityIterable? {
var union: EntityIterable? = null
values?.forEach {
val filter = transaction.find(entityType, field, it)
val filter = transaction.find(entityType, field, it ?: "")
union = union?.union(filter) ?: filter
}
return union
@ -84,21 +83,24 @@ public class XodusMagixHistory(private val store: PersistentEntityStore) : Write
// filter by magix filter
val filteredByMagix: EntityIterable = magixFilter?.let { mf ->
var res = all
transaction.findAllIn(XodusMagixStorage.MAGIC_MESSAGE_ENTITY_TYPE, MagixMessage::format.name, mf.format)
?.let {
res = res.intersect(it)
}
transaction.findAllIn(
findAllIn(
XodusMagixStorage.MAGIC_MESSAGE_ENTITY_TYPE,
MagixMessage::format.name,
mf.format
)?.let {
res = res.intersect(it)
}
findAllIn(
XodusMagixStorage.MAGIC_MESSAGE_ENTITY_TYPE,
MagixMessage::sourceEndpoint.name,
mf.source
)?.let {
res = res.intersect(it)
}
transaction.findAllIn(
findAllIn(
XodusMagixStorage.MAGIC_MESSAGE_ENTITY_TYPE,
MagixMessage::targetEndpoint.name,
mf.target
mf.target?.filterNotNull()
)?.let {
res = res.intersect(it)
}