Skip to content

Add Flow.lazyInitAsync operator for lazy flow materialization #3131

Description

@He-Pin

Motivation

Currently, pekko provides Sink.lazyInit for lazily creating a sink based on the first element, and Source.lazySource/Source.lazyFutureSource for lazy source creation. However, there is no equivalent operator for Flow — the ability to lazily create a flow based on the first element that passes through it.

This is useful when:

  • The flow's behavior depends on properties of the first element (e.g., detecting format/encoding from the first message)
  • Expensive resources should only be initialized when data actually starts flowing
  • The flow factory is asynchronous (e.g., needs to query a service before deciding how to process elements)

Proposed Enhancement

Add a Flow.lazyInitAsync operator that:

  1. Accepts a factory function (firstElement: T) => Future[Flow[T, U, M]]
  2. On the first element, invokes the factory asynchronously
  3. Materializes the returned flow and feeds all elements (including the first) through it
  4. Returns the materialized value of the lazily created flow

References

This operator was implemented in the Akka.NET project: akkadotnet/akka.net#5476

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions