I have a collection of objects, call them obj
. They have an act()
method. The act()
method will eventually cause the event()
observable on o
to call onComplete
.
What is a good way to chain these?
That is, call o.act()
, wait for o.event().onComplete
and then call the next o2.act()
, and so on for indefinite number of o
in collection.
So the signature is like so:
public class Item {
final protected PublishSubject<Object> event = PublishSubject.create();
public Observable<ReturnType> event() {
return event;
}
public void act() {
// do a bunch of stuff
event.onComplete();
}
}
And then in the consuming code:
Collection<Item> items...
foreach item in items
item.act -> await item.event().onComplete() -> call next item.act() -> so on
If I understand correctly, your objects have this kind of signature:
So if they were filled out like so:
They could then be chained like so:
Result:
Then if you have an
Iterable
collection, you could useflatMap
:Alternative
An alternative that's more like your case might be:
usage:
But it does not use the
event()
on items 2 onwards.