将rxjs中的多个可观察对象合并到一个缓冲区中

时间:2019-06-05 03:21:13

标签: javascript rxjs

我有2个websocket连接(如果有区别,请使用LiveQuery中的Parse)。

对于每个网络套接字,我有3个我要捕获的事件,"create""update""delete"

可能会同时进行大量更新,因此对于单个事件,我使用缓冲区捕获一次进行的一系列更新:

const base = fromEvent(listener, type);
const triggerBuffer = base.pipe(debounceTime(500));
const buffered = base.pipe(buffer(triggerBuffer));

那很好。当一堆更新来自服务器时,它们将被缓冲并全部发出。

我想要的是将来自两个websocket的所有事件合并到一个更新中。我尝试使用zip运算符来实现这一点,就像这样:

const setupServerObservable = (listener, listenerName) => type => {
  const base = fromEvent(listener, type);
  return base.pipe(map(x => ({ [listenerName]: { [type]: x } })));
};

const setupServerObservables = subscriptions => {
  const joins = setupServerObservable(subscriptions.joins, "joins");
  const containers = setupServerObservable(
    subscriptions.containers,
    "containers"
  );

  const eventTypes = ["update", "create", "delete"];

  const allJoins = eventTypes.map(joins);
  const allContainers = eventTypes.map(containers);
  const all = allJoins.concat(allContainers);
  const zippedObservables = zip(all);
  console.log(zippedObservables);
  const triggerBuffer = zippedObservables.pipe(debounceTime(500));
  const buffered = zippedObservables.pipe(buffer(triggerBuffer));
  return buffered;
};

但是当我尝试订阅这个新创建的Observable时,它不会从服务器发出任何更新。 console.log(zippedObservables)产生以下输出:

Observable {_isScalar: false, source: Observable, operator: ZipOperator}
operator: ZipOperator
    resultSelector: Array(6)
     0: Observable {_isScalar: false, source: Observable, operator: MapOperator}
     1: Observable {_isScalar: false, source: Observable, operator: MapOperator}
     2: Observable {_isScalar: false, source: Observable, operator: MapOperator}
     3: Observable {_isScalar: false, source: Observable, operator: MapOperator}
     4: Observable {_isScalar: false, source: Observable, operator: MapOperator}
     5: Observable {_isScalar: false, source: Observable, operator: MapOperator}
length: 6
__proto__: Array(0)
__proto__: Object
source: Observable {_isScalar: false, _subscribe: ƒ}
_isScalar: false
__proto__: Object

我认为我可能误解了zip运算符的功能,但是我也无法使其与mergeconcat一起使用。

1 个答案:

答案 0 :(得分:0)

看完评论中的教程后,我意识到zip不是我想要的。

我用merge尝试了同样的方法,但是没有用。似乎merge不接受数组,它必须接受每个Observable才能合并为一个单独的参数,如下所示:

const merged = merge(all[0], all[1], all[2], all[3], all[4], all[5]);

所以最终的代码是:

const setupServerObservable = (listener, listenerName) => type => {
  const base = fromEvent(listener, type);
  return base.pipe(map(x => ({ [listenerName]: { [type]: x } })));
};

const setupServerObservables = subscriptions => {
  const joins = setupServerObservable(subscriptions.joins, "joins");
  const containers = setupServerObservable(
    subscriptions.containers,
    "containers"
  );

  const eventTypes = ["update", "create", "delete"];

  const allJoins = eventTypes.map(joins);
  const allContainers = eventTypes.map(containers);
  const all = allJoins.concat(allContainers);
  const merged = merge(all[0], all[1], all[2], all[3], all[4], all[5]);
  const triggerBuffer = merged.pipe(debounceTime(500));
  const buffered = merged.pipe(buffer(triggerBuffer));
  return buffered;
};

如果有一种方法可以对数组执行此操作,那很好,但似乎没有。