ubu
01/19/2022, 3:31 PMfun loadObject(id: Id) : Flow<Object>
fun subscribe(id: Id) : Flow<List<SubscriptionEvent>>
First, I tried to use combine
operator in the following manner:
loadObject(id).combine(subscribe(id)) { obj, events ->
var result = obj
events.forEach { event ->
result = when (event) {
is Init -> {
obj
}
is Amend -> {
result.amend(event.diff)
}
is Set -> {
event.data
}
is SubscriptionEvent.Unset -> {
result.unset(event.keys)
}
}
}
result
}
But this doesn’t work, obviously, since in the flow, there is always initial object (emitted only once after loading), which is combined with latest events / transformations, whereas what I need is something like scan
operator, where I could first initialize (load) my object, then start consuming events, which lead to the transformation of this object. Observers will receive something like: “object in initial state, after loaded”, “object with the first sets of transformations applied”, “object with the first and second set of transformations applied”.
Is there something like:
transformations.scan(initialObjectFlow) { latestVersionOfTheObject, events ->
// apply new events / transformations
// emit new version of the object
}
Thank you!bezrukov
01/19/2022, 3:42 PMloadObject(id).flatMapLatest { object ->
subscribe(id).scan(object) { prev, transform ->
prev.applyTransform(transform)
}
}
but technically you can miss some transforms when a new object from loadObject emittedubu
01/19/2022, 3:49 PMephemient
01/19/2022, 4:34 PMsuspend fun
than Flow
, and then none of these concerns would ariseubu
01/19/2022, 6:26 PMephemient
01/19/2022, 6:28 PMflow {
val obj = loadObject()
emitAll(subscribe(id)...)
}
ubu
01/19/2022, 6:33 PMJoffrey
01/20/2022, 11:09 PMwhat if the client code wants to consume a flow?@ubu Why would they want that if only one value is expected anyway? Do you have an example of use case where the client knows there is a single value to get in an async way, and yet still wants to consume it as a flow? IMO those flow APIs with single item are mistakes and misdocument the methods