@@ -828,6 +828,45 @@ final class Flow[In, Out, Mat](delegate: scaladsl.Flow[In, Out, Mat]) extends Gr
828
828
(resource, out) => f(resource, out),
829
829
resource => close.apply(resource).toScala))
830
830
831
+ /**
832
+ * Transform each stream element with the help of a [[AutoCloseable ]] resource and close it.
833
+ *
834
+ * The resource creation function is invoked once when the stream is materialized and the returned resource is passed to
835
+ * the mapping function for mapping the first element. The mapping function returns a mapped element to emit
836
+ * downstream. The returned `T` MUST NOT be `null` as it is illegal as stream element - according to the Reactive Streams specification.
837
+ *
838
+ * The [[AutoCloseable ]] resource is closed only once when the upstream or downstream finishes or fails.
839
+ *
840
+ * Early completion can be done with combination of the [[takeWhile ]] operator.
841
+ *
842
+ * Adheres to the [[ActorAttributes.SupervisionStrategy ]] attribute.
843
+ *
844
+ * You can configure the default dispatcher for this Source by changing the `akka.stream.materializer.blocking-io-dispatcher` or
845
+ * set it for a given Source by using [[ActorAttributes ]].
846
+ *
847
+ * '''Emits when''' the mapping function returns an element and downstream is ready to consume it
848
+ *
849
+ * '''Backpressures when''' downstream backpressures
850
+ *
851
+ * '''Completes when''' upstream completes
852
+ *
853
+ * '''Cancels when''' downstream cancels
854
+ *
855
+ * @tparam R the type of the resource
856
+ * @tparam T the type of the output elements
857
+ * @param create function that creates the resource
858
+ * @param f function that transforms the upstream element and the resource to output element
859
+ * @since 1.1.0
860
+ */
861
+ def mapWithResource [R <: AutoCloseable , T ](
862
+ create : function.Creator [R ],
863
+ f : function.Function2 [R , Out , T ]): javadsl.Flow [In , T , Mat ] =
864
+ mapWithResource(create, f,
865
+ (resource : AutoCloseable ) => {
866
+ resource.close()
867
+ Optional .empty()
868
+ })
869
+
831
870
/**
832
871
* Transform each input element into an `Iterable` of output elements that is
833
872
* then flattened into the output stream. The transformation is meant to be stateful,
0 commit comments