forked from FreeTubeApp/FreeTube
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace jsCommunicator calls with new promise class
(benefits): allows for more concise passing of JSON back to the js side
- Loading branch information
1 parent
802aabd
commit 3a3059a
Showing
2 changed files
with
81 additions
and
7 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
android/app/src/main/java/io/freetubeapp/freetube/helpers/Promise.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 io.freetubeapp.freetube.helpers | ||
|
||
import io.freetubeapp.freetube.javascript.AsyncJSCommunicator | ||
import java.util.UUID.randomUUID | ||
import java.util.concurrent.ThreadPoolExecutor | ||
|
||
class Promise<T, G> { | ||
private val successListeners: MutableList<(T) -> Unit> = mutableListOf() | ||
private var successResult: T? = null | ||
private val errorListeners: MutableList<(G) -> Unit> = mutableListOf() | ||
private var errorResult: G? = null | ||
private val id = "${randomUUID()}" | ||
|
||
constructor(executor: ThreadPoolExecutor, runnable: ((T) -> Unit, (G) -> Unit) -> Unit) { | ||
executor.run { | ||
runnable.invoke({ | ||
result -> | ||
notifySuccess(result) | ||
}, { | ||
result -> | ||
notifyError(result) | ||
}) | ||
} | ||
} | ||
|
||
fun addJsCommunicator(communicator: AsyncJSCommunicator) : String { | ||
then { | ||
communicator.resolve(id, "$it") | ||
} | ||
catch { | ||
communicator.reject(id, "$it") | ||
} | ||
return id | ||
} | ||
|
||
fun notifySuccess(result: T) { | ||
successResult = result | ||
successListeners.forEach { | ||
listener -> | ||
listener.invoke(result) | ||
} | ||
} | ||
|
||
fun notifyError(result: G) { | ||
errorResult = result | ||
errorListeners.forEach { | ||
listener -> | ||
listener.invoke(result) | ||
} | ||
} | ||
|
||
fun then(listener: (T) -> Unit): Promise<T, G> { | ||
if (successResult != null) { | ||
// assume success result won't be unset | ||
listener(successResult!!) | ||
} else { | ||
successListeners.add(listener) | ||
} | ||
return this | ||
} | ||
|
||
fun catch(listener: (G) -> Unit): Promise<T, G> { | ||
if (errorResult != null) { | ||
// assume success result won't be unset | ||
listener(errorResult!!) | ||
} else { | ||
errorListeners.add(listener) | ||
} | ||
return this | ||
} | ||
} |
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