FlowBus icon indicating copy to clipboard operation
FlowBus copied to clipboard

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

Open brys0 opened this issue 3 years ago • 4 comments

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 avatar Apr 25 '22 13:04 brys0

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

brys0 avatar Apr 25 '22 13:04 brys0

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

brys0 avatar Apr 25 '22 14:04 brys0

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

vunder avatar Jul 16 '23 08:07 vunder

-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()
)

vunder avatar Jul 16 '23 09:07 vunder