insidegui / MultipeerKit

MultipeerConnectivity + Codable = ❤️
BSD 2-Clause "Simplified" License
1.1k stars 71 forks source link

Support for asynchronous message handlers #20

Closed DigitalSolomon closed 2 years ago

DigitalSolomon commented 2 years ago

Is there a way we can return a CLIResponse asynchronously, or do the message handling methods passed to transceiver.receive have to be synchronous calls which return a String?

insidegui commented 2 years ago

Not sure I fully understand the question.

The callback receive expects doesn't return anything, it's a Void closure.

If you're talking about async/await, you can have your callback perform asynchronous code by wrapping it in a Task, like shown here.

DigitalSolomon commented 2 years ago

Thanks for the quick response :) To clarify what I mean, let's take the start() method in CLIReceiver.swift for example.

func start() {
        transceiver.receive(FooCommand, using: response(handleFoo))
        transceiver.receive(BarCommand, using: response(handleBar))
        transceiver.resume()
    }

Right now the handleFoo method which I pass to response is a synchronous method which returns .message("sample-message"). I want to be able to make an asynchronous call upon receiving the command to populate the String response. Do you know how to achieve this? I'll check out the link you provided.

DigitalSolomon commented 2 years ago

Also, if a project doesn't use async/await yet, are there other elegant options to elegantly return .message asynchronously? Thanks

insidegui commented 2 years ago

Hmmmm, I'm a bit lost because the code you're sharing is not in the context of this repo. I assume that's one of your projects that's using the library, right?

There should be some way to plug an async method in there, or you can write your own little wrapper. That's out of the scope of Github issues for the library though, so I'm closing this issue.

John's concurrency discover page has lots of guides that could help you get started: https://www.swiftbysundell.com/discover/concurrency/

If that doesn't help, you may also ask your question in Stackoverflow.

I don't know of any other similar wrappers for MultipeerConnectivity. I have async/await support planned, but I won't have time to implement that any time soon.

DigitalSolomon commented 2 years ago

This is from the Listr sample app mentioned in this article here (repo here) . I thought this was also your project as well, but maybe I'm mistaken.

insidegui commented 2 years ago

Ah, it is. Sorry, I didn't make the connection based on your initial comment 😅

DigitalSolomon commented 2 years ago

No worries. I modified the code so that the handlers don't return a CLIResponse immediately, but instead return Void and receive an additional callback parameter which calls self?.transceiver.broadcast(CLIResponse.message(result)) after asynchronous activity has occurred. It's all working, just was curious if you had anything more elegant. Good work on your project overall! MultipeerKit is a nice wrapper around MultipeerConnectivity!

insidegui commented 2 years ago

Glad to know you found a solution :)