-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Define awaitCancel helper function to produce callback-induced streams #289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Why not use the fun produceXxx(): ReceiveChannel<Xxx> =
Channel<Xxx>(Channel.UNLIMITED).apply {
registerXxxCallback { offer(it) }
job.invokeOnCompletion { unregisterXxxCallback() }
} |
@fvasco Thanks. |
I correct myself, the right way is: fun produceXxx(): ReceiveChannel<Xxx> =
Channel<Xxx>(Channel.UNLIMITED).apply {
val callback = XxxCallback { offer(it) }
registerXxxCallback(callback)
job.invokeOnCompletion { unregisterXxxCallback(callback) }
} |
Is it planned to add this? Or shall we prefer to use Personally, I prefer the code style using Here's how one can open a click subscription in Swing using fun Component.openMouseClickedSubscription(): ReceiveChannel<MouseEvent> =
GlobalScope.produce(Dispatchers.Swing, Channel.UNLIMITED) {
val listener = object : MouseAdapter() {
override fun mouseClicked(e: MouseEvent) {
offer(e)
}
}
addMouseListener(listener) // called in EDT
try {
awaitCancel()
} finally {
removeMouseListener(listener) // called in EDT
}
} And the same example using fun Component.openMouseClickedSubscription(): ReceiveChannel<MouseEvent> {
val channel = Channel<MouseEvent>(Channel.UNLIMITED)
val listener = object : MouseAdapter() {
override fun mouseClicked(e: MouseEvent) {
channel.offer(e)
}
}
// this is needed if we want `openMouseClickedSubscription` to be callable from any thread
SwingUtilities.invokeLater {
addMouseListener(listener) // Has to be called in EDT
channel.invokeOnClose { // We cannot make any assumption about in which thread this will be executed
SwingUtilities.invokeLater {
removeMouseListener(listener) // Has to be called in EDT
}
}
}
return channel
} Of course, it is a matter of personal taste. But I prefer the first one (using |
The only point of invoking When the coroutines resumes, an exception will be thrown. So you need to wrap in try/finally block (which is better than Maybe the two should be combined into one function. |
I suggest we'd name it I like @Dico200 suggestion to combine the the two into one function:
|
I'd make onCompletion nullable in such a function, with a default value of
null.
…On Thu, Dec 6, 2018, 1:00 PM Anton Spaans ***@***.***> wrote:
I suggest we'd name it suspendForever :-)
I like @Dico200 <https://github.com/Dico200> suggestion to combine the
the two into one function:
suspend fun suspendForever(onCompletion: (Throwable?) -> Unit): Nothing
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#289 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGpvBWUYBfobbXrYxehsR2i7YGNNbs6sks5u2QbGgaJpZM4SsLkr>
.
|
|
The implementation is trivial:
The indented usage is to convert callback-based multi-fire APIs to channels like this:
The text was updated successfully, but these errors were encountered: