diff --git a/instantsearch-core/src/commonMain/kotlin/com/algolia/instantsearch/core/connection/Connection.kt b/instantsearch-core/src/commonMain/kotlin/com/algolia/instantsearch/core/connection/Connection.kt index eddd8916d..1fe8e7a72 100644 --- a/instantsearch-core/src/commonMain/kotlin/com/algolia/instantsearch/core/connection/Connection.kt +++ b/instantsearch-core/src/commonMain/kotlin/com/algolia/instantsearch/core/connection/Connection.kt @@ -1,10 +1,26 @@ package com.algolia.instantsearch.core.connection +import com.algolia.instantsearch.ExperimentalInstantSearch + +/** + * Represents a link (connection) between two components. + */ public interface Connection { + /** True if the established connection is enabled, otherwise false */ public val isConnected: Boolean + /** Enable this connection */ public fun connect() + /** Disable this connection */ public fun disconnect() } + +/** + * Delegate the connection/disconnection operations to a [ConnectionHandler]. + */ +@ExperimentalInstantSearch +public fun Connection.handleBy(handler: ConnectionHandler) { + handler += this +} diff --git a/instantsearch-core/src/commonMain/kotlin/com/algolia/instantsearch/core/connection/ConnectionHandler.kt b/instantsearch-core/src/commonMain/kotlin/com/algolia/instantsearch/core/connection/ConnectionHandler.kt index 599dce804..62935ea00 100644 --- a/instantsearch-core/src/commonMain/kotlin/com/algolia/instantsearch/core/connection/ConnectionHandler.kt +++ b/instantsearch-core/src/commonMain/kotlin/com/algolia/instantsearch/core/connection/ConnectionHandler.kt @@ -1,5 +1,6 @@ package com.algolia.instantsearch.core.connection +/** Handles connect and disconnect operations of a set of [Connection]s */ public class ConnectionHandler( public val connections: MutableSet = mutableSetOf() ) { @@ -12,18 +13,22 @@ public class ConnectionHandler( connections.forEach { it.connect() } } + /** Disconnects all [Connection]s */ public fun disconnect() { connections.forEach { it.disconnect() } } + /** Adds a [Connection] to the handler */ public operator fun plusAssign(connection: Connection) { connections += connection.apply { connect() } } + /** Adds a collection of [Connection]s to the handler */ public operator fun plusAssign(connections: Collection) { - this.connections += connections.apply { forEach { it.connect() } } + this.connections += connections.onEach { it.connect() } } + /** Disconnects and clears all [Connection]s */ public fun clear() { disconnect() connections.clear()