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

I have to call GlobalBus.dropAll() to keep receiving events after the first one. #1

Open
brys0 opened this issue Apr 25, 2022 · 4 comments

Comments

@brys0
Copy link

brys0 commented Apr 25, 2022

My event subscribers will not continue to work after the same event once it is repeated. I've tried using skipRetained and retained. Event drop event but the only thing that seems to work is the dropAll() command.

Here is a portion of my code.

Poster

GlobalBus.post(FriendUpdatePayload(userSession.user.toPublic(), userSession.user.identifier, "state", UserState.ONLINE.ordinal))
GlobalBus.post(SelfUpdatePayload(userSession.user.toPublic(), "state", UserState.ONLINE.ordinal))

Subscriber

  events.subscribe<FriendUpdatePayload> { payload ->
            logger.info("Update Payload -> ${payload.id} ${payload.name} = ${payload.value}")
            connections.values.forEach { v -> println(v.user.identifier) }
            val friends = connections.values.find { u -> u.user.identifier== payload.id }!!.user.getFriends()
            for (friend in friends.friends) {
                val friendSession = connections.values.find { f -> f.user.identifier == friend.id }!!
                send(friendSession.ws.session, friendSession.ws.type, jsonStr = payload.toJSON())
            }
            GlobalBus.dropAll() // Doesn't get another event without this.
            return@subscribe
        }
        events.subscribe<SelfUpdatePayload> { payload ->
            logger.info("Self Update -> ${payload.self.id} ${payload.name} = ${payload.value}")
            val selfSocket = connections.values.find { u -> u.user.identifier == payload.self.id }
            if (selfSocket != null) {
                send(selfSocket.ws.session, selfSocket.ws.type, jsonStr = payload.toJSON())
            }
            GlobalBus.dropAll() // Doesn't get another event without this.
            return@subscribe
        }
@brys0
Copy link
Author

brys0 commented Apr 25, 2022

I've also tried to use older versions of Kotlin and JVM (JVM Down to 16) and none seem to fix it.

@brys0
Copy link
Author

brys0 commented Apr 25, 2022

It also appears kotlin version 1.6.20 and anything above completely renders flowbus inoperable

@vunder
Copy link

vunder commented Jul 16, 2023

+1
Must call GlobalBus.dropEvent(Foo::java.class) to make events raise second time

@vunder
Copy link

vunder commented Jul 16, 2023

-1 for me
It turned out that it is an issue in my code. Or to be precise - forgot about equality of data class
My notification class looks like this

data class Notification(val payload: String)

Sometimes, payload value could be the same within several sequential notifications (e.g. Notification("foo"), Notification("foo"), Notification("goo")). Since it is a data class, second notification with payload="foo" will be ignored by the MutableStateFlow, even though my app expect to receive all that notifications

@brys0 My advice to you is to check whether your notifications are data classes and have the same payload. If so - you can chande it to a regular class or add default unique property. E.g.

data class Notification(
    val payload: String,
    val unique: String = UUID.randomUUID().toString()
)

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

2 participants