diff --git a/CHANGELOG b/CHANGELOG index af8d300..d07b54a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +Version 1.0.9 +- Improvements to using DispatchObservable + Version 1.0.8 - Added ErrorHandler for DispatchCallAdapterFactory diff --git a/README.md b/README.md index e29b472..fe41874 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[ ![Download](https://api.bintray.com/packages/tonyofrancis/maven/dispatch/images/download.svg?version=1.0.8) ](https://bintray.com/tonyofrancis/maven/dispatch/1.0.8/link) +[ ![Download](https://api.bintray.com/packages/tonyofrancis/maven/dispatch/images/download.svg?version=1.0.9) ](https://bintray.com/tonyofrancis/maven/dispatch/1.0.9/link) Overview -------- @@ -7,12 +7,12 @@ Dispatch is a simple and flexible work scheduler that schedulers work on a backg To use dispatch add the following to your app's build.gradle file ```java -implementation "com.tonyodev.dispatch:dispatch:1.0.8" +implementation "com.tonyodev.dispatch:dispatch:1.0.9" ``` To use with Retrofit add ```java - implementation "com.tonyodev.dispatch:dispatch-retrofit2-adapter:1.0.8" + implementation "com.tonyodev.dispatch:dispatch-retrofit2-adapter:1.0.9" ``` Example: diff --git a/build.gradle b/build.gradle index fc2454b..93b7567 100644 --- a/build.gradle +++ b/build.gradle @@ -2,8 +2,8 @@ buildscript { ext.kotlin_version = '1.3.21' - ext.library_version = '1.0.8' - ext.library_version_code = 9 + ext.library_version = '1.0.9' + ext.library_version_code = 10 ext.retrofit_version = '2.5.0' ext.gson_version = '2.8.5' ext.novoda_bintray_version = '0.9' diff --git a/dispatch/src/main/java/com/tonyodev/dispatch/Dispatch.kt b/dispatch/src/main/java/com/tonyodev/dispatch/Dispatch.kt index 069385e..8c09c16 100644 --- a/dispatch/src/main/java/com/tonyodev/dispatch/Dispatch.kt +++ b/dispatch/src/main/java/com/tonyodev/dispatch/Dispatch.kt @@ -16,7 +16,7 @@ import java.lang.IllegalArgumentException * queueId 88 -> dispatch(post) -> dispatch(async) * queueId 78595 -> dispatch(post) * */ -interface Dispatch: DispatchObservable> { +interface Dispatch { /** * The dispatch queue id. @@ -224,12 +224,6 @@ interface Dispatch: DispatchObservable> { * */ fun cancelOnComplete(cancel: Boolean): Dispatch - /** - * Gets the backing dispatch Observable. - * @return DispatchObservable - * */ - fun getDispatchObservable(): DispatchObservable> - /** * Sets the dispatch object id. Use this id to identify where errors occur in the dispatch queue. * @param id the dispatch object id. @@ -244,4 +238,38 @@ interface Dispatch: DispatchObservable> { * */ fun map(func: (R) -> T): Dispatch + /** + * Gets the backing dispatch Observable. + * @return DispatchObservable + * */ + fun getDispatchObservable(): DispatchObservable + + /** + * Adds a dispatch observer. + * @param dispatchObserver the observer. + * @return the dispatch. + * */ + fun addObserver(dispatchObserver: DispatchObserver): Dispatch + + /** + * Adds a list of dispatch observers. + * @param dispatchObservers the list of observers. + * @return the dispatch. + * */ + fun addObservers(dispatchObservers: List>): Dispatch + + /** + * Removes a dispatch observer. + * @param dispatchObserver the observer to be removed. + * @return the dispatch. + * */ + fun removeObserver(dispatchObserver: DispatchObserver): Dispatch + + /** + * Remove a list of dispatch observers. + * @param dispatchObservers the list of observers to be removed. + * @return the dispatch. + * */ + fun removeObservers(dispatchObservers: List>): Dispatch + } \ No newline at end of file diff --git a/dispatch/src/main/java/com/tonyodev/dispatch/DispatchObservable.kt b/dispatch/src/main/java/com/tonyodev/dispatch/DispatchObservable.kt index 9ef7367..b8b5bf0 100644 --- a/dispatch/src/main/java/com/tonyodev/dispatch/DispatchObservable.kt +++ b/dispatch/src/main/java/com/tonyodev/dispatch/DispatchObservable.kt @@ -3,34 +3,34 @@ package com.tonyodev.dispatch /** * Interface that allows for the attaching of dispatch observers. * */ -interface DispatchObservable { +interface DispatchObservable { /** * Adds a dispatch observer. * @param dispatchObserver the observer. - * @return the dispatch. + * @return the dispatchObservable. * */ - fun addObserver(dispatchObserver: DispatchObserver): T + fun addObserver(dispatchObserver: DispatchObserver): DispatchObservable /** * Adds a list of dispatch observers. * @param dispatchObservers the list of observers. - * @return the dispatch. + * @return the dispatchObservable. * */ - fun addObservers(dispatchObservers: List>): T + fun addObservers(dispatchObservers: List>): DispatchObservable /** * Removes a dispatch observer. * @param dispatchObserver the observer to be removed. - * @return the dispatch. + * @return the dispatchObservable. * */ - fun removeObserver(dispatchObserver: DispatchObserver): T + fun removeObserver(dispatchObserver: DispatchObserver): DispatchObservable /** * Remove a list of dispatch observers. * @param dispatchObservers the list of observers to be removed. - * @return the dispatch. + * @return the dispatchObservable. * */ - fun removeObservers(dispatchObservers: List>): T + fun removeObservers(dispatchObservers: List>): DispatchObservable } \ No newline at end of file diff --git a/dispatch/src/main/java/com/tonyodev/dispatch/Dispatcher.kt b/dispatch/src/main/java/com/tonyodev/dispatch/Dispatcher.kt index 99e3f6b..add5957 100644 --- a/dispatch/src/main/java/com/tonyodev/dispatch/Dispatcher.kt +++ b/dispatch/src/main/java/com/tonyodev/dispatch/Dispatcher.kt @@ -390,8 +390,7 @@ object Dispatcher { private val dispatchType: Int): Dispatch { private val dispatchSources = ArrayList?>(3) - private val dispatchObserversSet = mutableSetOf>() - + private val dispatchObservable = DispatchObservableInfo(handler) private var doOnErrorWorker: ((throwable: Throwable) -> R)? = null override val queueId: Int @@ -484,11 +483,7 @@ object Dispatcher { @Suppress("UNCHECKED_CAST") private fun notifyDispatchObservers() { if (!isCancelled) { - val iterator = dispatchObserversSet.iterator() - val result = results as R - while (iterator.hasNext()) { - iterator.next().onChanged(result) - } + dispatchObservable.notifyObservers(results as R) } } @@ -560,7 +555,7 @@ object Dispatcher { if (dispatchQueue.dispatchQueueController == null && enableWarnings && this == dispatchQueue.rootDispatch) { Log.w(TAG, "No DispatchQueueController set for dispatch queue with id: $queueId. " + - "Not setting a DispatchQueueController can cause memory leaks for long running tasks.") + "Not setting a DispatchQueueController can cause memory leaks for long running tasks.") } } } @@ -575,9 +570,9 @@ object Dispatcher { dispatchQueue.completedDispatchQueue = false dispatchQueue.rootDispatch.runDispatcher() } else { - if (enableWarnings) { - Log.d(TAG, "Start called on dispatch queue with id: $queueId after it has already been cancelled.") - } + if (enableWarnings) { + Log.d(TAG, "Start called on dispatch queue with id: $queueId after it has already been cancelled.") + } } return this } @@ -632,11 +627,7 @@ object Dispatcher { private fun removeDispatcher() { handler.removeCallbacks(dispatcher) - val iterator = dispatchObserversSet.iterator() - while (iterator.hasNext()) { - iterator.next() - iterator.remove() - } + dispatchObservable.removeAllObservers() if (closeHandler) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { handler.looper.quitSafely() @@ -898,12 +889,12 @@ object Dispatcher { delayInMillis = 0, worker = { if (!zipDispatchQueue.isCancelled) { - if (zipDispatchQueue.completedDispatchQueue) { - zipDispatchQueue.completedDispatchQueue = false - zipQueueDispatch.runDispatcher() - } else { - zipDispatchQueue.rootDispatch.runDispatcher() - } + if (zipDispatchQueue.completedDispatchQueue) { + zipDispatchQueue.completedDispatchQueue = false + zipQueueDispatch.runDispatcher() + } else { + zipDispatchQueue.rootDispatch.runDispatcher() + } } it }, @@ -1062,16 +1053,64 @@ object Dispatcher { } override fun addObserver(dispatchObserver: DispatchObserver): Dispatch { - dispatchObserversSet.add(dispatchObserver) + dispatchObservable.addObserver(dispatchObserver) return this } override fun addObservers(dispatchObservers: List>): Dispatch { - dispatchObserversSet.addAll(dispatchObservers) + dispatchObservable.addObservers(dispatchObservers) return this } override fun removeObserver(dispatchObserver: DispatchObserver): Dispatch { + dispatchObservable.removeObserver(dispatchObserver) + return this + } + + override fun removeObservers(dispatchObservers: List>): Dispatch { + dispatchObservable.removeObservers(dispatchObservers) + return this + } + + override fun getDispatchObservable(): DispatchObservable { + return dispatchObservable + } + + override fun setDispatchId(id: String): Dispatch { + this.dispatchId = id + return this + } + + } + + private class DispatchObservableInfo(private val handler: Handler): DispatchObservable { + + private val dispatchObserversSet = mutableSetOf>() + private var result: Any? = INVALID_RESULT + + override fun addObserver(dispatchObserver: DispatchObserver): DispatchObservable { + dispatchObserversSet.add(dispatchObserver) + if (result != INVALID_RESULT) { + handler.post { + notifyObserver(dispatchObserver) + } + } + return this + } + + override fun addObservers(dispatchObservers: List>): DispatchObservable { + dispatchObserversSet.addAll(dispatchObservers) + if (result != INVALID_RESULT) { + handler.post { + for (dispatchObserver in dispatchObservers) { + notifyObserver(dispatchObserver) + } + } + } + return this + } + + override fun removeObserver(dispatchObserver: DispatchObserver): DispatchObservable { val iterator = dispatchObserversSet.iterator() while (iterator.hasNext()) { if (dispatchObserver == iterator.next()) { @@ -1082,7 +1121,7 @@ object Dispatcher { return this } - override fun removeObservers(dispatchObservers: List>): Dispatch { + override fun removeObservers(dispatchObservers: List>): DispatchObservable { val iterator = dispatchObserversSet.iterator() var count = 0 while (iterator.hasNext()) { @@ -1097,13 +1136,28 @@ object Dispatcher { return this } - override fun getDispatchObservable(): DispatchObservable> { - return this + fun removeAllObservers() { + val iterator = dispatchObserversSet.iterator() + while (iterator.hasNext()) { + iterator.next() + iterator.remove() + } } - override fun setDispatchId(id: String): Dispatch { - this.dispatchId = id - return this + @Suppress("UNCHECKED_CAST") + private fun notifyObserver(dispatchObserver: DispatchObserver) { + val r = result + if (r != INVALID_RESULT) { + dispatchObserver.onChanged(r as R) + } + } + + fun notifyObservers(result: R) { + this.result = result + val iterator = dispatchObserversSet.iterator() + while (iterator.hasNext()) { + iterator.next().onChanged(result) + } } }