1

我想创建一个聊天,具有类似 facebook 的“xyz 正在输入”功能。

onKeydownSource.subscribe(function() {
  var typing = true;
}.bind(this));

onKeydownSource.throttle(500).subscribe(function(e) {
  var typing = false;
}.bind(this));

当用户停止输入时,我使用此代码段进行注册。

现在想象 e 是

{
    userId: 13,
    conversationId: 23
}

我的流/订阅者是

onKeydownSource.subscribe(function(e) {
  typingInConversations[e.conversationId][e.userId] = true;
}.bind(this));

onKeydownSource
// What should be here?
.subscribe(function(e) {
  typingInConversations[e.conversationId][e.userId] = false;
}.bind(this));

这意味着对于每个 keydown 我注册用户和用户正在输入的对话。我的问题是,我如何才能仅限制具有相同 userId+conversationId 的“事件”?

4

1 回答 1

1

GroupBy可能是您正在寻找的。它创建了一系列共享相同密钥的可观察组。在您的情况下,为对话中的每个打字用户创建新的可观察组,然后限制这些可观察组中的事件。

onKeydownSource.groupBy(
    function (e) { return e.conversationId + '-' + e.userId; },
    function (e) { return e; }
)
.subscribe(function(obs) {
    obs.throttle(500).subscribe(function (e) {
        typingInConversations[e.conversationId][e.userId] = false;
    });
});

第一个参数是提取每个元素的键的函数。你可能想在那里找到更好的。第二个参数是将每个源元素映射到可观察组中的元素的函数。在您的情况下,按原样传递元素。

于 2014-09-27T13:30:39.473 回答