Coverage Summary for Class: MindboxWebPageRegistry (cloud.mindbox.mobile_sdk.inapp.presentation.view)
| Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
| MindboxWebPageRegistry |
100%
(5/5)
|
100%
(10/10)
|
100%
(22/22)
|
100%
(167/167)
|
| MindboxWebPageRegistry$sweep$1 |
100%
(1/1)
|
100%
(2/2)
|
100%
(1/1)
|
100%
(7/7)
|
| MindboxWebPageRegistry$unregister$count$1$1 |
100%
(1/1)
|
100%
(2/2)
|
100%
(1/1)
|
100%
(9/9)
|
| Total |
100%
(7/7)
|
100%
(14/14)
|
100%
(24/24)
|
100%
(183/183)
|
package cloud.mindbox.mobile_sdk.inapp.presentation.view
import cloud.mindbox.mobile_sdk.logger.mindboxLogI
import java.lang.ref.WeakReference
internal fun interface MindboxWebPage {
fun push(action: WebViewAction, payload: String)
}
internal class MindboxWebPageRegistry {
private val pages = mutableListOf<WeakReference<MindboxWebPage>>()
fun register(page: MindboxWebPage) {
val count = synchronized(pages) {
sweep()
if (pages.any { reference -> reference.get() === page }) return
pages.add(WeakReference(page))
pages.size
}
mindboxLogI("[WebView] Registry: page registered, $count live")
}
fun unregister(page: MindboxWebPage) {
val count = synchronized(pages) {
sweep()
if (!pages.removeAll { reference -> reference.get() === page }) return
pages.size
}
mindboxLogI("[WebView] Registry: page released, $count live")
}
fun broadcast(action: WebViewAction, payload: String, excludingAuthor: MindboxWebPage?) {
val receivers = synchronized(pages) {
sweep()
pages.mapNotNull { reference -> reference.get() }
.filter { page -> page !== excludingAuthor }
}
if (receivers.isEmpty()) {
mindboxLogI("[WebView] Registry: nobody to receive '$action'")
return
}
mindboxLogI("[WebView] Registry: broadcasting '$action' to ${receivers.size} page(s)")
receivers.forEach { page -> page.push(action, payload) }
}
private fun sweep() {
pages.removeAll { reference -> reference.get() == null }
}
}