|
18 | 18 |
|
19 | 19 | @Volatile private var mainHandlerInternal: Handler? = null |
20 | 20 |
|
21 | | - /** |
22 | | - * Handler associated with the main (UI) thread. |
23 | | - * Exposed for Java interop as `getUiThreadHandler()`. |
24 | | - */ |
| 21 | + |
25 | 22 | private val mainHandler: Handler |
26 | 23 | get() { |
27 | 24 | if (mainHandlerInternal == null) { |
|
35 | 32 | } |
36 | 33 |
|
37 | 34 | /** |
38 | | - * Java-compatible static getter for the UI thread handler. |
39 | | - */ |
| 35 | + * Returns the handler associated with the main (UI) thread. |
| 36 | + * @return The handler for the main thread |
| 37 | + */ |
40 | 38 | @JvmStatic |
41 | 39 | public fun getUiThreadHandler(): Handler = mainHandler |
| 40 | + |
| 41 | + |
42 | 42 | /** |
43 | | - * Checks if the current thread is the UI thread. |
44 | | - * |
45 | | - * @return `true` if the current thread is the UI thread, `false` otherwise |
46 | | - */ |
| 43 | + * @return `true` if the current thread is the UI thread. |
| 44 | + */ |
47 | 45 | @JvmStatic |
48 | 46 | public fun isOnUiThread(): Boolean = Looper.getMainLooper().thread == Thread.currentThread() |
49 | 47 |
|
| 48 | + |
50 | 49 | /** |
51 | 50 | * Throws an [AssertionException] if the current thread is not the UI thread. |
52 | | - * This method is only active in debug mode and is a no-op in production. |
53 | | - */ |
| 51 | + * This is a no-op in production and is only meant to run in debug mode! |
| 52 | + * If you need to check for incorrect-thread issues in production, duplicate this code and call it elsewhere. |
| 53 | + */ |
54 | 54 | @JvmStatic |
55 | 55 | public fun assertOnUiThread() { |
56 | 56 | if (ReactBuildConfig.DEBUG) { |
57 | 57 | SoftAssertions.assertCondition(isOnUiThread(), "Expected to run on UI thread!") |
58 | 58 | } |
59 | 59 | } |
60 | 60 |
|
| 61 | + |
61 | 62 | /** |
62 | | - * Throws an [AssertionException] if the current thread is the UI thread. |
63 | | - * This method is only active in debug mode and is a no-op in production. |
64 | | - */ |
| 63 | + * Throws an [AssertionException] if the current thread is the UI thread. This is a noop in |
| 64 | + * production, and is only meant to run in debug mode! If you need to check for incorrect-thread |
| 65 | + * issues in production, duplicate this code and call it elsewhere. |
| 66 | + */ |
65 | 67 | @JvmStatic |
66 | 68 | public fun assertNotOnUiThread() { |
67 | 69 | if (ReactBuildConfig.DEBUG) { |
68 | 70 | SoftAssertions.assertCondition(!isOnUiThread(), "Expected not to run on UI thread!") |
69 | 71 | } |
70 | 72 | } |
71 | | - |
72 | | - /** |
73 | | - * Runs the given [Runnable] on the UI thread. |
74 | | - * |
75 | | - * @param runnable the task to run |
76 | | - * @return `true` if the runnable was successfully placed in the message queue |
77 | | - */ |
| 73 | + |
| 74 | + /** Runs the given [Runnable] on the UI thread. */ |
78 | 75 | @JvmStatic |
79 | 76 | public fun runOnUiThread(runnable: Runnable): Boolean = |
80 | 77 | mainHandler.postDelayed(runnable, 0) |
81 | 78 |
|
82 | | - /** |
83 | | - * Posts a [Runnable] to run on the UI thread after a specified delay. |
84 | | - * |
85 | | - * @param runnable the task to run |
86 | | - * @param delayInMs the delay in milliseconds before the task is executed |
87 | | - * @return `true` if the runnable was successfully placed in the message queue |
88 | | - */ |
| 79 | + /** Runs the given [Runnable] on the UI thread after the specified delay. */ |
89 | 80 | @JvmStatic |
90 | 81 | public fun runOnUiThread(runnable: Runnable, delayInMs: Long): Boolean = |
91 | 82 | mainHandler.postDelayed(runnable, delayInMs) |
92 | 83 |
|
93 | | - /** |
94 | | - * Removes the given [Runnable] from the UI thread queue. |
95 | | - * |
96 | | - * @param runnable the task to remove |
97 | | - */ |
| 84 | + /** Removes the given [Runnable] on the UI thread. */ |
98 | 85 | @JvmStatic |
99 | 86 | public fun removeOnUiThread(runnable: Runnable?) { |
100 | 87 | runnable?.let { mainHandler.removeCallbacks(it) } |
|
0 commit comments