From d719af07def6a74d8edb18d265b06eb2a795ead4 Mon Sep 17 00:00:00 2001 From: Francisco Aguilera Date: Thu, 10 Aug 2023 09:33:44 -0700 Subject: [PATCH] Add RibCoroutineWorker.bind receiving multiple workers --- .../ribworkerselection/RibWorkerBindTypeClickType.kt | 3 ++- .../RibWorkerSelectionInteractor.kt | 9 +++++++-- .../main/ribworkerselection/RibWorkerSelectionView.kt | 10 ++++++++-- .../kotlin/com/uber/rib/core/RibCoroutineWorker.kt | 11 +++++++++++ 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/android/demos/rib-workers/src/main/kotlin/com/uber/rib/workers/root/main/ribworkerselection/RibWorkerBindTypeClickType.kt b/android/demos/rib-workers/src/main/kotlin/com/uber/rib/workers/root/main/ribworkerselection/RibWorkerBindTypeClickType.kt index e0b14d72..dfac9f0d 100644 --- a/android/demos/rib-workers/src/main/kotlin/com/uber/rib/workers/root/main/ribworkerselection/RibWorkerBindTypeClickType.kt +++ b/android/demos/rib-workers/src/main/kotlin/com/uber/rib/workers/root/main/ribworkerselection/RibWorkerBindTypeClickType.kt @@ -18,7 +18,8 @@ package com.uber.rib.workers.root.main.ribworkerselection enum class RibWorkerBindTypeClickType { SINGLE_WORKER_BIND_CALLER_THREAD, SINGLE_WORKER_BIND_BACKGROUND_THREAD, - BIND_MULTIPLE_WORKERS, + BIND_MULTIPLE_DEPRECATED_WORKERS, + BIND_MULTIPLE_RIB_COROUTINE_WORKERS, BIND_RIB_COROUTINE_WORKER, WORKER_TO_RIB_COROUTINE_WORKER, RIB_COROUTINE_WORKER_TO_WORKER, diff --git a/android/demos/rib-workers/src/main/kotlin/com/uber/rib/workers/root/main/ribworkerselection/RibWorkerSelectionInteractor.kt b/android/demos/rib-workers/src/main/kotlin/com/uber/rib/workers/root/main/ribworkerselection/RibWorkerSelectionInteractor.kt index 01276d04..4a94dfa0 100644 --- a/android/demos/rib-workers/src/main/kotlin/com/uber/rib/workers/root/main/ribworkerselection/RibWorkerSelectionInteractor.kt +++ b/android/demos/rib-workers/src/main/kotlin/com/uber/rib/workers/root/main/ribworkerselection/RibWorkerSelectionInteractor.kt @@ -63,11 +63,16 @@ class RibWorkerSelectionInteractor( updateViewModel(ioWorker::class.simpleName) WorkerBinder.bind(this, ioWorker) } - RibWorkerBindTypeClickType.BIND_MULTIPLE_WORKERS -> { + RibWorkerBindTypeClickType.BIND_MULTIPLE_DEPRECATED_WORKERS -> { val workers = listOf(backgroundWorker, defaultWorker, ioWorker, uiWorker) - updateViewModel("Multiple workers ") + updateViewModel("Multiple deprecated workers ") WorkerBinder.bind(this, workers) } + RibWorkerBindTypeClickType.BIND_MULTIPLE_RIB_COROUTINE_WORKERS -> { + val workers = listOf(defaultRibCoroutineWorker, defaultRibCoroutineWorker) + updateViewModel("Multiple RibCoroutineWorkers ") + coroutineScope.bind(workers) + } RibWorkerBindTypeClickType.BIND_RIB_COROUTINE_WORKER -> { updateViewModel(defaultRibCoroutineWorker::class.simpleName) coroutineScope.bind(defaultRibCoroutineWorker) diff --git a/android/demos/rib-workers/src/main/kotlin/com/uber/rib/workers/root/main/ribworkerselection/RibWorkerSelectionView.kt b/android/demos/rib-workers/src/main/kotlin/com/uber/rib/workers/root/main/ribworkerselection/RibWorkerSelectionView.kt index 72a3a5d1..a1b55db5 100644 --- a/android/demos/rib-workers/src/main/kotlin/com/uber/rib/workers/root/main/ribworkerselection/RibWorkerSelectionView.kt +++ b/android/demos/rib-workers/src/main/kotlin/com/uber/rib/workers/root/main/ribworkerselection/RibWorkerSelectionView.kt @@ -57,8 +57,14 @@ fun RibWorkerSelectionView( AddButton( eventStream, - RibWorkerBindTypeClickType.BIND_MULTIPLE_WORKERS, - "Bind multiple workers", + RibWorkerBindTypeClickType.BIND_MULTIPLE_DEPRECATED_WORKERS, + "Bind multiple Deprecated Workers", + ) + + AddButton( + eventStream, + RibWorkerBindTypeClickType.BIND_MULTIPLE_RIB_COROUTINE_WORKERS, + "Bind multiple RibCoroutineWorkers", ) AddButton( diff --git a/android/libraries/rib-base/src/main/kotlin/com/uber/rib/core/RibCoroutineWorker.kt b/android/libraries/rib-base/src/main/kotlin/com/uber/rib/core/RibCoroutineWorker.kt index ddeb57e1..b4cbfe05 100644 --- a/android/libraries/rib-base/src/main/kotlin/com/uber/rib/core/RibCoroutineWorker.kt +++ b/android/libraries/rib-base/src/main/kotlin/com/uber/rib/core/RibCoroutineWorker.kt @@ -107,6 +107,17 @@ public fun CoroutineScope.bind( return BindWorkerHandleImpl(bindJob, unbindJob) } +/** Binds a list of [worker]'s in a scope that is a child of the [CoroutineScope] receiver. */ +@JvmOverloads +public fun CoroutineScope.bind( + workers: Iterable, + coroutineContext: CoroutineContext = RibDispatchers.Default, +) { + for (worker in workers) { + bind(worker, coroutineContext) + } +} + /** * Guarantees to run synchronous [init] block exactly once in an undispatched manner. *