Make Coroutine Actor prevent duplicated value

It’s possible to make Coroutine Actor prevent that I send duplicated value?

Eg:
I use this sealed class to send values to actor

sealed class LoginCommand {
    class LOGIN(val email: String, val password: String) : LoginCommand()
    class LOGIN_VALIDATE(val email: String, val password: String) : LoginCommand()
    class SAVE_USER_DATA(val email: String, val password: String) : LoginCommand()
    object USER_IV : LoginCommand()
}

And the actor call:

private val eventActor = viewModelScope.actor<LoginCommand>(Dispatchers.Main, Channel.RENDEZVOUS) {
        for (command in channel) {
            when (command) {
                is LoginCommand.LOGIN -> {
                    loadingStateLiveData.postValue(true)
                    loginLiveData.postValue(loginRepository.getLogin(Login(command.email, command.password)))
                    loadingStateLiveData.postValue(false)
                }
                is LoginCommand.SAVE_USER_DATA -> {
                    loginSavedRepository.saveUserData(command.email, command.password)
                }
            }
        }
    }

I need to prevent that can send LoginCommand.LOGIN and LoginCommand.SAVE_USER_DATA two times, but permit to send LoginCommand.LOGIN and LoginCommand.SAVE_USER_DATA at the same same time… Only block the duplicated. It’s possible?

(I know that Channel.RENDEZVOUS keep just one call at time, it’s just a sample, because I implemented that and now I will need to implemented a new actor that accept many calls but block duplicated calls…)

You can use a MutableSet to deduplicate data, for example

for (command in commands) {
  when(command)
    is LoginCommand.LOGIN -> {
      if(commandSet.add(command)) {
1 Like

I need to add this values in a MutableCollection?
If I do it, then I think that I will need to remove this values after the call is finished, because I need to accept the call again if the previous call are finished, I will only recuse if the call are duplicated in the queue…

Actor/Channel don’t have a method to check if the “command” are already in the queue?

Sample in real world:

I need to use coroutine to control download calls, in the app has 10 files to download, the user can download all the files at the same time, but I need to prevent the user to download the same file at the same file, if the user click to download the file 1, then the user can’t click to download this file again once the file are completed download, when the file 1 are download completed then the user can download again, (but remember, the user can’t download file 1 at the same time as file 1 again, but can download file 1, 2, 3, 4, 5… at the same time).

(It’s just a sample, my app are not for download files!!)

The actor/channel don’t have this control internal?
If I use MutableCollection, then I need this control (keep the collection, remove the file from collection when download is complete…)

No, I never talked about a MutableCollection.

No, it don’t.

No, it works as documented.