首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >理解Concurrent.unicast的论点

理解Concurrent.unicast的论点
EN

Stack Overflow用户
提问于 2014-04-29 17:38:29
回答 1查看 460关注 0票数 2

我使用Play Framework2.2

为了实现WebSocket连接,我使用了适合我需要的Concurrent.unicast

代码语言:javascript
复制
val enumerator = Concurrent.unicast[JsValue] {
    channel => userIdWithChannelMap += u.id -> channel
}

但是,源代码 of Concurrent.unicast显示了几个参数的需要:

代码语言:javascript
复制
def unicast[E](
    onStart: Channel[E] => Unit,
    onComplete: => Unit = (),
    onError: (String, Input[E]) => Unit = (_: String, _: Input[E]) => ())(implicit ec: ExecutionContext)

据我所知,onComplete是在IterateeDone时执行的。

然而,onComplete回调与Iterateemap方法有什么区别?

代码语言:javascript
复制
/**
   *
   * Uses the provided function to transform the Iteratee's computed result when the Iteratee is done.
   *
   * @param f a function for transforming the computed result
   * $paramEcSingle
   */
  def map[B](f: A => B)(implicit ec: ExecutionContext): Iteratee[E, B] = this.flatMap(a => Done(f(a), Input.Empty))(ec)

此外,Enumerator#onDoneEnumerating的需求是什么,出现在源代码中。

实际上,我看到了WebSocket的一些实现,涉及:

代码语言:javascript
复制
Concurrent.unicast{...}.onDoneEnumerating{...}

我和onCompleteonDoneEnumeratingIteratee#map混在一起。

有人能解释一下差异吗?

特别是,为什么Concurrent#broadcast不像unicast那样提出onComplete参数。

很难找到一些关于Iteratee世界的好文档。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-30 01:00:22

每一种情况都是不同的,但差别是微妙的。

onComplete参数的unicast允许您为应用它的Iteratee成功完成时设置一个回调。单独的onError回调用于当它失败时使用。

onDoneEnumerating方法的Enumerator允许您附加一个回调,如果Iteratee成功完成或失败,将调用该回调。

Iteratee.map允许您将回调附加到成功完成的Iteratee,并更改它使用它完成的值。

Concurrent.broadcast不接受onCompleteonError回调,因为它可以应用于多个Iteratee实例,而这些实例的行为方式不一定相同。一个实例可能会因错误而死亡,而另一个实例可能会在稍后成功完成,而另一个实例则永远不会完成。因此,运行枚举数的代码不太可能对这些事件作出反应。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23371586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档