These are chat archives for sirthias/swave

26th
Sep 2016
Felix Palludan Hargreaves
@hejfelix
Sep 26 2016 13:10
hello guys
I wanted to create a simple mutable queue to feed a Spout
and I thought: That must be a very common use case so it must already exist in the library
but I don’t see anything that works as my usecase
the example I neded up with using Akka streams to feed a Spout is here:

package controllers

import akka.actor.Actor.Receive
import akka.actor.{ActorLogging, ActorRef, ActorSystem, Props, Stash}
import akka.stream.actor.ActorPublisher
import controllers.PublishingActor.PushInt
import org.reactivestreams.Publisher
import swave.core._

import scala.util.Random
import concurrent.duration._
import scala.collection.mutable
import scala.concurrent.{ExecutionContext, ExecutionContextExecutor}
object PublishingActor {
  case class PushInt(i: Int)
}
class PublishingActor extends ActorPublisher[Int] with ActorLogging with Stash {

  override def receive: Receive = {
    case _ if totalDemand > 0 =>
      stash()
      log.info("becoming...")
      context.become(publishingReaction)
      unstashAll
    case _ =>
      stash()
  }

  def publishingReaction: PartialFunction[Any, Unit] = {
    case _ if totalDemand == 0 =>
      stash()
      context.unbecome()
      log.info("unbecoming...")
    case PushInt(i) =>
      log.info(s"pushing $i")
      onNext(i)
  }

}


object MySwaveQueueApp extends App {

  implicit val system                       = ActorSystem()
  implicit val executor                     = system.dispatcher
  implicit val dispatcher: ExecutionContext = system.dispatcher.prepare()
  implicit val env                          = StreamEnv()

  val dataPublisherRef: ActorRef    = system.actorOf(Props[PublishingActor])
  val dataPublisher: Publisher[Int] = ActorPublisher[Int](dataPublisherRef)


  system.scheduler.schedule(0.seconds, 1.seconds) {
    dataPublisherRef ! PushInt( Random.nextInt)
  }

  val spout: Spout[Int] = Spout.fromPublisher(dataPublisher)
  spout.take(3).foreach(println)

}
(sorry for the brickwall)
This prints:
[INFO] [09/26/2016 15:11:59.901] [default-akka.actor.default-dispatcher-2] [akka://default/user/$a] becoming...
-1880577538
[INFO] [09/26/2016 15:11:59.903] [default-akka.actor.default-dispatcher-2] [akka://default/user/$a] pushing -1880577538
2137174713
[INFO] [09/26/2016 15:12:00.875] [default-akka.actor.default-dispatcher-2] [akka://default/user/$a] pushing 2137174713
-1118198918
[INFO] [09/26/2016 15:12:01.875] [default-akka.actor.default-dispatcher-2] [akka://default/user/$a] pushing -1118198918
[INFO] [09/26/2016 15:12:01.877] [default-akka.actor.default-dispatcher-4] [akka://default/user/$a] unbecoming...
do I really need to a) add akka-streams as a dependency and b) do all this boiler plate to turn some push events into a Spout?
Mathias
@sirthias
Sep 26 2016 14:21
Hi Felix, it appears what you want is a PushSpout which was already requested by ticket #13: sirthias/swave#13
It is already implemented but not yet released.
If you run a local build then you can use it already.
Does this look it would solve your problem?
Felix Palludan Hargreaves
@hejfelix
Sep 26 2016 14:47
Yes that looks very promising. Is there an ETA on a release including this?
Thanks for the quick response btw πŸ˜€
Mathias
@sirthias
Sep 26 2016 14:52
Great, no problem. I'm in the process of polishing the API for the first proper release anyway...
Let me know if you have any other concerns/ideas/whatever
Concerning a release: I could release an M4 right now if you want
doesn't really take long
Felix Palludan Hargreaves
@hejfelix
Sep 26 2016 14:56
That would be great for our team. We would like to proceed with this exploration tomorrow πŸ™‚
Mathias
@sirthias
Sep 26 2016 14:56
Ok, M4 will be out in about 10 minutes
Felix Palludan Hargreaves
@hejfelix
Sep 26 2016 14:56
Super cool πŸ™‚
Mathias
@sirthias
Sep 26 2016 15:09
Ok, 0.5-M4 released and pushed to sonatype. Should appear on Maven Central soon. Let me know how it works for you.
(and congrats for being brave enough to play with the seriously bleeding edge... :) )
Felix Palludan Hargreaves
@hejfelix
Sep 26 2016 15:12
Yes we'll see how the rest of the team likes that idea xD. Anyway -- you have until January to make it sane for use. That's when this small product goes into production
Mathias
@sirthias
Sep 26 2016 15:19
ok, gotcha.
Felix Palludan Hargreaves
@hejfelix
Sep 26 2016 15:20
Also: it's worth it simply for the ascii rendrrings xD
Renderings*
Mathias
@sirthias
Sep 26 2016 15:21
Yeah, these are cool. But there is still some work to be done to make them truly awesome