Giter VIP home page Giter VIP logo

resources's Issues

aaaaa

釈迦に説法ですが Source[T,M]は[T]型のelementをpushします。

で、もう一つSourceの型に[M]が入っているのでこれはMaterialized Valueの型[M]ですね。
Materialized Valueはrun()して初めて取り出せるわけです。

とりだすというのは以下のようにrunの返り値になることですね

type T = ...
type M = ...
val source: Source[T, M] = ...
val sink: Sink[T, M]
val runnableGraph = source.toMat(sink)(Keep.Left) //Keep.LeftはSource側のmat valueをとってSinkがわのmat valueを捨てる
val materalizedValue = runnableGraph.run()

ここまではよくある話ですが、Source.asSubscriberは

  /**
   * Creates a `Source` that is materialized as a [[org.reactivestreams.Subscriber]]
   */
  def asSubscriber[T]: Source[T, Subscriber[T]] =
    fromGraph(new SubscriberSource[T](DefaultAttributes.subscriberSource, shape("SubscriberSource")))

というシグネチャで(Subscriber[T]に注目)

val runnableGraph = Source.asSubscriber[T].toMat(sink, Keep.Left)
val subscriber = runnableGraph.run()

という形でrunした結果取り出せるmaterialize valueがsubscriberです。
このsubscriberはReactive Streamの(Akka Streamより下位のAPIの)Subscriberです。

で、このSubscriberにElementを送ると

そのまま同じElementがSourceを通じてAkka Streamを流れます。

で、これをSink側とこのようなコードで組み合わせると

val runnableGraph: RunnableGraph[Subscriber, Publisher] = 
  Source.asSubscriber[Mat2]
      .toMat(Sink.asPublisher[Mat2](fanout = false))(Keep.both)

のような形になるわけです。このまま走らせると

上のsubscriber, publisherのtupleがmaterialized valueになってしまいます。

val matValue: (Subscriber[T], Publisher[T]) = runnableGraph.run()

Akka Streamを走らせたのに、低レベルAPIであるReactive Streamのsubscriber, publisherが返ってくるのがうれしくないですね。

というわけでmaterialized valueをAkka StreamのSinkとSourceにアップグレードするのが以下です

.mapMaterializedValue {
        case (sub, pub)  (Sink.fromSubscriber(sub), Source.fromPublisher(pub))
      }

そして最終的に出来上がるのが、下の図の3段目にあるやつです。さいしょのSink.asSubscriberとSource.asPublisherをrunした後に新たにSourceとSinkが作られるという不思議な仕組みになっています。

で、これどういうときに使うの???

さあwwww

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.