Skip to content
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

Use flows instead of interface listeners #29

Open
Burtan opened this issue Feb 6, 2023 · 2 comments
Open

Use flows instead of interface listeners #29

Burtan opened this issue Feb 6, 2023 · 2 comments

Comments

@Burtan
Copy link
Contributor

Burtan commented Feb 6, 2023

Hi,

I think it would be more idiomatic for kotlin to use flows for midi inputs instead of interface listeners. At least lambdas should be provided. What do you think?

@Burtan Burtan changed the title Use flows instead of custom listeners Use flows instead of interface listeners Feb 6, 2023
@atsushieno
Copy link
Owner

atsushieno commented Feb 6, 2023

Probably...! I have to say it's quite awkward right now. On lambdas I wonder if that is not going to shut out Java language users unnecessarily though (current setXxxListener() would work for them). It is a Kotlin library and if lambdas improve our experience a lot then I'd go for it, bu I thought SAM works for our listener. If you come up with a PR that'd be great.

@sed0
Copy link
Contributor

sed0 commented May 2, 2023

It's possible to add Flows alongside listeners like this for example:

interface MidiInput : MidiPort {
    val messageReceivedFlow: Flow<MidiMessageReceivedEvent>
        get() = callbackFlow {
            setMessageReceivedListener { data, start, length, timestampInNanoseconds ->
                trySend(MidiMessageReceivedEvent(data, start, length, timestampInNanoseconds))
            }

            awaitClose {
                setMessageReceivedListener(null)
            }
        }

    fun setMessageReceivedListener(listener: OnMidiReceivedEventListener?)
}

data class MidiMessageReceivedEvent(
    val data: ByteArray,
    val start: Int,
    val length: Int,
    val timestampInNanoseconds: Long,
)

Not the best solution I guess, but it's easy, and it doesn't eliminate Java support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants