-
Notifications
You must be signed in to change notification settings - Fork 275
Migration guide: ChatObserver and events() APIs
Márton Braun edited this page Feb 22, 2021
·
3 revisions
The API for handling events from Chat has been updated to make it simpler to use - and harder to misuse. We strongly suggest migrating to the new APIs.
The main change is that the intermediate ChatObservable
class is being removed, and instead of chaining operators onto that and then subscribing to it, you can call subscribe
-like methods on the ChatClient
or ChannelController
objects directly.
Here's how calls to the old API are replaced with the new API.
Please note that while the examples here are for the
ChatClient
, the API changes and migration steps are exactly the same forChannelController
.
Old API
client.events().subscribe { event: ChatEvent ->
if (event is NewMessageEvent) {
val message = event.message
}
}
New API
client.subscribe { event: ChatEvent ->
if (event is NewMessageEvent) {
val message = event.message
}
}
Old API
val subscription: Subscription = client.events().subscribe { /* ... */ }
subscription.unsubscribe()
New API
val disposable: Disposable = client.subscribe { /* ... */ }
disposable.dispose()
Old API
client.events()
.filter("message.new")
.filter("message.updated")
.filter("message.deleted")
.subscribe { event: ChatEvent ->
/* ... */
}
client.events()
.filter("message.new")
.filter("message.updated")
.filter("message.deleted")
.subscribe((ChatEvent event) -> {
/* ... */
return Unit.INSTANCE;
});
New API
client.subscribeFor(
"message.new",
"message.updated",
"message.deleted"
) { event: ChatEvent ->
/* ... */
}
client.subscribeFor(
new String[]{"message.new", "message.updated", "message.deleted"},
(ChatEvent event) -> {
/* ... */
return Unit.INSTANCE;
});
Old API
client.events()
.filter(EventType.MESSAGE_NEW)
.subscribe { event: ChatEvent ->
/* ... */
}
New API
client.subscribeFor(EventType.MESSAGE_NEW) { event: ChatEvent ->
/* ... */
}
Old API
client.events()
.filter(NewMessageEvent::class.java)
.subscribe { event: ChatEvent ->
/* ... */
}
client.events()
.filter(NewMessageEvent.class)
.subscribe((ChatEvent event) -> {
/* ... */
return Unit.INSTANCE;
});
New API
client.subscribeFor(NewMessageEvent::class) { event: ChatEvent ->
/* ... */
}
client.subscribeFor<NewMessageEvent> { newMessageEvent: NewMessageEvent ->
/* ... */
}
client.subscribeFor(
new Class[]{NewMessageEvent.class},
(ChatEvent event) -> {
/* ... */
});
Old API
client.events()
.filter { it.type == "my.custom.event" && it.createdAt < someDate }
.subscribe { event: ChatEvent ->
/* ... */
}
New API
client.subscribe { event: ChatEvent ->
if (event.type == "my.custom.event" && event.createdAt < someDate) {
/* ... */
}
}
Old API
client.events()
.filter(ConnectedEvent::class.java)
.first()
.subscribe { event: ChatEvent
Toast.makeText(this, "First connection", Toast.LENGTH_SHORT).show()
}
client.events()
.filter(ConnectedEvent.class)
.first()
.subscribe((ChatEvent event) -> {
/* ... */
return Unit.INSTANCE;
})
New API
client.subscribeForSingle(ConnectedEvent::class.java) { event: ConnectedEvent ->
Toast.makeText(this, "First connection", Toast.LENGTH_SHORT).show()
}
client.subscribeForSingle<ConnectedEvent> { event: ConnectedEvent
Toast.makeText(this, "First connection", Toast.LENGTH_SHORT).show()
}
client.subscribeForSingle(ConnectedEvent.class,
(ConnectedEvent event) -> {
/* ... */
});