Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement Source.futureSource operator #33

Merged
merged 1 commit into from
Nov 8, 2023
Merged

Conversation

geminicaprograms
Copy link
Contributor

Creates a source that emits elements from future source when it completes or fails otherwise. The future completion is performed on the provided ExecutionContext whereas elements are emitted through Supervised. Note that when Future fails with ExecutionException then its cause is returned as source failure.

Examples:

  Source
    .futureSource(Future.failed(new RuntimeException("future failed")))
    .receive()                                                           // ChannelClosed.Error(Some(java.lang.RuntimeException: future failed))
  Source.futureSource(Future.successful(Source.fromValues(1, 2))).toList // List(1, 2)

Base automatically changed from feat_future to master November 2, 2023 10:34
def futureSource[T](from: Future[Source[T]])(using StageCapacity, ExecutionContext): Source[T] =
val c = StageCapacity.newChannel[T]
from.onComplete {
case Success(source) => supervised { source.pipeTo(c) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we block in onComplete, which probably shouldn't happen - the pipe should only complete once all elements have been transmitted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's correct, 🤔 the other option would be to somehow pass the received (from onComplete -> Success) Source to another fork and process it there so that emission doesn't happen in the ExecutionContext. Let me think and propose sth there...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I think adding a source-transferring channel (with a buffer size of 1) would do the trick (plus a fork awaiting for the channel)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT?

Creates a source that emits elements from future source when it completes or
fails otherwise. The future completion is performed on the provided
`ExecutionContext` whereas elements are emitted through Ox.
Note that when Future fails with `ExecutionException` then its cause is
returned as source failure.

Examples:
  Source
    .futureSource(Future.failed(new RuntimeException("future failed")))
    .receive()                                                           // ChannelClosed.Error(Some(java.lang.RuntimeException: future failed))
  Source.futureSource(Future.successful(Source.fromValues(1, 2))).toList // List(1, 2)
@adamw adamw merged commit c6e02bb into master Nov 8, 2023
@adamw
Copy link
Member

adamw commented Nov 8, 2023

Thanks :)

@geminicaprograms geminicaprograms deleted the feat_futureSource branch November 15, 2023 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants