-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android] Ensure main thread internally.
- Loading branch information
Showing
8 changed files
with
111 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...la-android/src/main/java/com/instacart/formula/android/internal/AndroidUpdateScheduler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.instacart.formula.android.internal | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean | ||
import java.util.concurrent.atomic.AtomicReference | ||
|
||
/** | ||
* Handles state update scheduling to the main thread. If update arrives on a background thread, | ||
* it will added it the main thread queue. It will throw away a pending update if a new update | ||
* arrives. | ||
*/ | ||
class AndroidUpdateScheduler<Value : Any>( | ||
private val update: (Value) -> Unit, | ||
) { | ||
/** | ||
* If not null, that means that we have an update pending. | ||
*/ | ||
private val pendingValue = AtomicReference<Value>() | ||
|
||
/** | ||
* Defines if an update is currently scheduled. | ||
*/ | ||
private val updateScheduled = AtomicBoolean(false) | ||
|
||
/** | ||
* To avoid re-entry, we track if [updateRunnable] is currently handling an update. | ||
*/ | ||
private var isUpdating = false | ||
|
||
private val updateRunnable = object :Runnable { | ||
override fun run() { | ||
updateScheduled.set(false) | ||
|
||
var localPending = pendingValue.getAndSet(null) | ||
while (localPending != null) { | ||
// Handle the update | ||
isUpdating = true | ||
update(localPending) | ||
isUpdating = false | ||
|
||
// Check if another update arrived while we were processing. | ||
localPending = pendingValue.getAndSet(null) | ||
|
||
if (localPending != null) { | ||
// We will take over processing, so let's clear the message | ||
Utils.mainThreadHandler.removeCallbacks(this) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun emitUpdate(value: Value) { | ||
// Set pending value | ||
pendingValue.set(value) | ||
|
||
if (Utils.isMainThread()) { | ||
if (isUpdating) { | ||
// Let's exit and let the [updateRunnable] to pick up the change | ||
return | ||
} else { | ||
// Since we are on main thread, let's force run it | ||
updateRunnable.run() | ||
} | ||
} else { | ||
// If no update is scheduled, schedule one | ||
if (updateScheduled.compareAndSet(false, true)) { | ||
|
||
Utils.mainThreadHandler.post(updateRunnable) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 16 additions & 1 deletion
17
formula-android/src/main/java/com/instacart/formula/android/internal/Utils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,26 @@ | ||
package com.instacart.formula.android.internal | ||
|
||
import android.os.Handler | ||
import android.os.Looper | ||
|
||
internal object Utils { | ||
internal val mainThreadHandler = Handler(Looper.getMainLooper()) | ||
|
||
fun assertMainThread() { | ||
if (Looper.getMainLooper() != Looper.myLooper()) { | ||
if (!isMainThread()) { | ||
throw IllegalStateException("should be called on main thread: ${Thread.currentThread()}") | ||
} | ||
} | ||
|
||
inline fun executeOnMainThread(crossinline runnable: () -> Unit) { | ||
if (isMainThread()) { | ||
runnable() | ||
} else { | ||
mainThreadHandler.post { runnable() } | ||
} | ||
} | ||
|
||
fun isMainThread(): Boolean { | ||
return Looper.getMainLooper() == Looper.myLooper() | ||
} | ||
} |