Skip to content

Commit 0ba9a43

Browse files
committed
feat(light-model-client): option to automatically filter non-loaded nodes
1 parent d69e626 commit 0ba9a43

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

light-model-client/src/commonMain/kotlin/org/modelix/client/light/LightModelClient.kt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import kotlin.time.Duration.Companion.seconds
1616

1717
private const val TEMP_ID_PREFIX = "tmp-"
1818

19-
class LightModelClient(
19+
class LightModelClient internal constructor(
2020
val connection: IConnection,
2121
val transactionManager: ITransactionManager,
22+
val autoFilterNonLoadedNodes: Boolean,
2223
val debugName: String = ""
2324
) {
2425

@@ -331,11 +332,17 @@ class LightModelClient(
331332
get() = requiresRead { getData().parent?.let { getNodeAdapter(it) } }
332333

333334
override fun getChildren(role: String?): List<NodeAdapter> {
334-
return requiresRead { getData().children[role]?.map { getNodeAdapter(it) } ?: emptyList() }
335+
return requiresRead {
336+
val children = getData().children[role]?.map { getNodeAdapter(it) } ?: emptyList()
337+
if (autoFilterNonLoadedNodes) children.filterLoaded() else children
338+
}
335339
}
336340

337341
override val allChildren: List<NodeAdapter>
338-
get() = requiresRead { getData().children.flatMap { it.value }.map { getNodeAdapter(it) } }
342+
get() = requiresRead {
343+
val children = getData().children.flatMap { it.value }.map { getNodeAdapter(it) }
344+
if (autoFilterNonLoadedNodes) children.filterLoaded() else children
345+
}
339346

340347
override fun getConceptReference(): IConceptReference? {
341348
return requiresRead { getData().concept?.let { ConceptReference(it) } }
@@ -459,6 +466,7 @@ class LightModelClient(
459466

460467
override fun getReferenceTarget(role: String): NodeAdapter? {
461468
return getReferenceTargetRef(role)?.let { getNode(it.nodeId) }
469+
?.takeIf { !autoFilterNonLoadedNodes || it.isLoaded() }
462470
}
463471

464472
override fun getReferenceTargetRef(role: String): LightClientNodeReference? {
@@ -692,6 +700,7 @@ abstract class LightModelClientBuilder {
692700
private var httpEngineFactory: HttpClientEngineFactory<*>? = null
693701
private var debugName: String = ""
694702
private var transactionManager: ITransactionManager = ReadWriteLockTransactionManager()
703+
private var autoFilterNonLoadedNodes: Boolean = false
695704

696705
protected abstract fun getDefaultEngineFactory(): HttpClientEngineFactory<*>
697706

@@ -708,9 +717,14 @@ abstract class LightModelClientBuilder {
708717
))
709718
),
710719
transactionManager,
711-
debugName
720+
autoFilterNonLoadedNodes = autoFilterNonLoadedNodes,
721+
debugName = debugName
712722
)
713723
}
724+
fun autoFilterNonLoadedNodes(value: Boolean = true): LightModelClientBuilder {
725+
autoFilterNonLoadedNodes = value
726+
return this
727+
}
714728
fun autoTransactions(): LightModelClientBuilder {
715729
transactionManager = AutoTransactions(transactionManager)
716730
return this
@@ -796,5 +810,5 @@ fun NodeData.asUpdateData(): NodeUpdateData {
796810
}
797811

798812
fun INode.isLoaded() = isValid
799-
fun Iterable<INode>.filterLoaded() = filter { it.isLoaded() }
800-
fun Sequence<INode>.filterLoaded() = filter { it.isLoaded() }
813+
fun <T : INode> Iterable<T>.filterLoaded() = filter { it.isLoaded() }
814+
fun <T : INode> Sequence<T>.filterLoaded() = filter { it.isLoaded() }

0 commit comments

Comments
 (0)