Skip to content

Commit 6b6a767

Browse files
authored
Merge pull request #964 from arkivanov/main-thread-check-setting
Added mainThreadCheckEnabled setting
2 parents 5c4742b + 6d687b8 commit 6b6a767

File tree

7 files changed

+28
-14
lines changed

7 files changed

+28
-14
lines changed

decompose/api/android/decompose.api

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ public final class com/arkivanov/decompose/DecomposeExperimentFlags {
7171
public final class com/arkivanov/decompose/DecomposeSettings {
7272
public static final field Companion Lcom/arkivanov/decompose/DecomposeSettings$Companion;
7373
public fun <init> ()V
74-
public fun <init> (Z)V
75-
public synthetic fun <init> (ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
74+
public fun <init> (ZZ)V
75+
public synthetic fun <init> (ZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
7676
public final fun component1 ()Z
77-
public final fun copy (Z)Lcom/arkivanov/decompose/DecomposeSettings;
78-
public static synthetic fun copy$default (Lcom/arkivanov/decompose/DecomposeSettings;ZILjava/lang/Object;)Lcom/arkivanov/decompose/DecomposeSettings;
77+
public final fun component2 ()Z
78+
public final fun copy (ZZ)Lcom/arkivanov/decompose/DecomposeSettings;
79+
public static synthetic fun copy$default (Lcom/arkivanov/decompose/DecomposeSettings;ZZILjava/lang/Object;)Lcom/arkivanov/decompose/DecomposeSettings;
7980
public fun equals (Ljava/lang/Object;)Z
8081
public final fun getDuplicateConfigurationsEnabled ()Z
82+
public final fun getMainThreadCheckEnabled ()Z
8183
public fun hashCode ()I
8284
public fun toString ()Ljava/lang/String;
8385
}

decompose/api/decompose.klib.api

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,13 +483,16 @@ final class <#A: out kotlin/Any> com.arkivanov.decompose.router.pages/Pages { //
483483
}
484484

485485
final class com.arkivanov.decompose/DecomposeSettings { // com.arkivanov.decompose/DecomposeSettings|null[0]
486-
constructor <init>(kotlin/Boolean = ...) // com.arkivanov.decompose/DecomposeSettings.<init>|<init>(kotlin.Boolean){}[0]
486+
constructor <init>(kotlin/Boolean = ..., kotlin/Boolean = ...) // com.arkivanov.decompose/DecomposeSettings.<init>|<init>(kotlin.Boolean;kotlin.Boolean){}[0]
487487

488488
final val duplicateConfigurationsEnabled // com.arkivanov.decompose/DecomposeSettings.duplicateConfigurationsEnabled|{}duplicateConfigurationsEnabled[0]
489489
final fun <get-duplicateConfigurationsEnabled>(): kotlin/Boolean // com.arkivanov.decompose/DecomposeSettings.duplicateConfigurationsEnabled.<get-duplicateConfigurationsEnabled>|<get-duplicateConfigurationsEnabled>(){}[0]
490+
final val mainThreadCheckEnabled // com.arkivanov.decompose/DecomposeSettings.mainThreadCheckEnabled|{}mainThreadCheckEnabled[0]
491+
final fun <get-mainThreadCheckEnabled>(): kotlin/Boolean // com.arkivanov.decompose/DecomposeSettings.mainThreadCheckEnabled.<get-mainThreadCheckEnabled>|<get-mainThreadCheckEnabled>(){}[0]
490492

491493
final fun component1(): kotlin/Boolean // com.arkivanov.decompose/DecomposeSettings.component1|component1(){}[0]
492-
final fun copy(kotlin/Boolean = ...): com.arkivanov.decompose/DecomposeSettings // com.arkivanov.decompose/DecomposeSettings.copy|copy(kotlin.Boolean){}[0]
494+
final fun component2(): kotlin/Boolean // com.arkivanov.decompose/DecomposeSettings.component2|component2(){}[0]
495+
final fun copy(kotlin/Boolean = ..., kotlin/Boolean = ...): com.arkivanov.decompose/DecomposeSettings // com.arkivanov.decompose/DecomposeSettings.copy|copy(kotlin.Boolean;kotlin.Boolean){}[0]
493496
final fun equals(kotlin/Any?): kotlin/Boolean // com.arkivanov.decompose/DecomposeSettings.equals|equals(kotlin.Any?){}[0]
494497
final fun hashCode(): kotlin/Int // com.arkivanov.decompose/DecomposeSettings.hashCode|hashCode(){}[0]
495498
final fun toString(): kotlin/String // com.arkivanov.decompose/DecomposeSettings.toString|toString(){}[0]

decompose/api/jvm/decompose.api

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ public final class com/arkivanov/decompose/DecomposeExperimentFlags {
7171
public final class com/arkivanov/decompose/DecomposeSettings {
7272
public static final field Companion Lcom/arkivanov/decompose/DecomposeSettings$Companion;
7373
public fun <init> ()V
74-
public fun <init> (Z)V
75-
public synthetic fun <init> (ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
74+
public fun <init> (ZZ)V
75+
public synthetic fun <init> (ZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
7676
public final fun component1 ()Z
77-
public final fun copy (Z)Lcom/arkivanov/decompose/DecomposeSettings;
78-
public static synthetic fun copy$default (Lcom/arkivanov/decompose/DecomposeSettings;ZILjava/lang/Object;)Lcom/arkivanov/decompose/DecomposeSettings;
77+
public final fun component2 ()Z
78+
public final fun copy (ZZ)Lcom/arkivanov/decompose/DecomposeSettings;
79+
public static synthetic fun copy$default (Lcom/arkivanov/decompose/DecomposeSettings;ZZILjava/lang/Object;)Lcom/arkivanov/decompose/DecomposeSettings;
7980
public fun equals (Ljava/lang/Object;)Z
8081
public final fun getDuplicateConfigurationsEnabled ()Z
82+
public final fun getMainThreadCheckEnabled ()Z
8183
public fun hashCode ()I
8284
public fun toString ()Ljava/lang/String;
8385
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package com.arkivanov.decompose.mainthread
22

33
import android.os.Looper
4+
import com.arkivanov.decompose.DecomposeSettings
45
import com.arkivanov.decompose.errorhandler.onDecomposeError
56

67
private val mainThreadId: Long? by lazy {
78
try {
89
Looper.getMainLooper().thread.id
9-
} catch (e: Exception) {
10+
} catch (_: Exception) {
1011
null
1112
}
1213
}
1314

1415
internal actual fun checkMainThread() {
15-
if ((mainThreadId != null) && (Thread.currentThread().id != mainThreadId)) {
16+
if (DecomposeSettings.settings.mainThreadCheckEnabled && (mainThreadId != null) && (Thread.currentThread().id != mainThreadId)) {
1617
onDecomposeError(NotOnMainThreadException(currentThreadName = Thread.currentThread().name))
1718
}
1819
}

decompose/src/commonMain/kotlin/com/arkivanov/decompose/DecomposeSettings.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ import kotlin.concurrent.Volatile
77
*
88
* @param duplicateConfigurationsEnabled controls whether duplicate configurations are enabled or not. Default value is `false`.
99
* Excludes the `Child Items` navigation model, which doesn't support duplicate configurations.
10+
* @param mainThreadCheckEnabled controls whether main thread checks are enabled or not.
11+
* If enabled, Decompose will log an error if it detects access from a background thread when it was expected from the main thread.
12+
* Default value is `true`.
1013
*/
1114
data class DecomposeSettings(
1215
val duplicateConfigurationsEnabled: Boolean = false,
16+
val mainThreadCheckEnabled: Boolean = true,
1317
) {
1418

1519
companion object {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.arkivanov.decompose.mainthread
22

3+
import com.arkivanov.decompose.DecomposeSettings
34
import com.arkivanov.decompose.errorhandler.onDecomposeError
45
import platform.Foundation.NSThread
56

67
internal actual fun checkMainThread() {
7-
if (!NSThread.isMainThread()) {
8+
if (DecomposeSettings.settings.mainThreadCheckEnabled && !NSThread.isMainThread()) {
89
onDecomposeError(NotOnMainThreadException(currentThreadName = NSThread.currentThread.description()))
910
}
1011
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.arkivanov.decompose.mainthread
22

3+
import com.arkivanov.decompose.DecomposeSettings
34
import com.arkivanov.decompose.errorhandler.onDecomposeError
45

56
internal actual fun checkMainThread() {
6-
if (mainThreadChecker?.isMainThread() == false) {
7+
if (DecomposeSettings.settings.mainThreadCheckEnabled && mainThreadChecker?.isMainThread() == false) {
78
onDecomposeError(NotOnMainThreadException(currentThreadName = Thread.currentThread().name))
89
}
910
}

0 commit comments

Comments
 (0)