So, I found one good way to solve my problem, but if you have another way, I would also like to see it.
def someSource(file: File) = {
val f = openFile(file)
Source
.fromPublisher(SomePublisher(f))
.transform(() => new PushStage[?, ?] {
override def onPush(elem: ?, ctx: Context[?]): SyncDirective = ctx.push(elem)
override def postStop(): Unit = {
f.close()
super.postStop()
}
}
}
def func(files: List[File]) =
Source(files)
.flatMapConcat(someSource)
.grouped(10)
.via(SomeFlow)
.runWith(Sink.ignore)
source
share