当第二个可观察到的变化时,combinateLatest不发光吗?

时间:2019-11-16 04:12:50

标签: javascript typescript rxjs

This is a stackblitz demo of the below code

有两个观测值-from lstm_tutorial import load_data train_data, valid_data, test_data, vocabulary, reversed_dictionary = load_data() usage: [-h] [--data_path DATA_PATH] run_opt : error: the following arguments are required: run_opt An exception has occurred, use %tb to see the full traceback. SystemExit: 2 D:\TProgramFiles\Anaconda3\envs\keras-gpu\lib\site-packages\IPython\core\interactiveshell.py:3334: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1) -Add Attachments

active$

collection$通知时,如以下语句中所述:

const central:EStore<Todo> = new EStore();
const collection:EStore<Todo> = new EStore();
const active$ = central.observeActive();
const collection$ = collection.observe();

collection$函数不会触发:

collection$.subscribe(v=>console.log('Todo added to collection'));

但是,如果我们导致combineLatest观察对象发出通知,则const isActiveTodoInCollection$:Observable<boolean> = combineLatest(active$, collection$, (a, c)=>{ console.log("Firing"); if (getMapValue(central.active)) { return collection.containsById(getMapValue(central.active)); } return false; }); 观察对象将会触发。

为什么active$可观察到的通知不触发?

另一个奇怪的是,当isActiveTodoInCollection$通知以下内容时,它将产生一条日志记录语句:

collection$

但是,如果我们将collection$观测值添加到参数中,它将停止通知。好像const t$ = combineLatest(collection$, (c)=>console.log("collection$ performing a notification")); t$.subscribe(); 必须先通知...

1 个答案:

答案 0 :(得分:2)

combineLatest仅在每个可观察对象发出至少一次值时起作用。

central.post(todo)并没有真正完成,因为在您的案例中,observeActive()实际上仅在调用central.addActive(todo)时才观察,因此没有任何可组合的东西。

示例以说明问题,在下面的代码中注释two $并取消注释以下行,您将看到区别

import { combineLatest, Observable } from "rxjs";

const one$ = new Observable(subscriber => {
  setTimeout(() => {
    console.log("set");
    subscriber.next(42);
  }, 1000);
});

const two$ = new Observable(subscriber => {});

// comment above two$ and uncomment this
/*const two$ = new Observable(subscriber => {
  subscriber.next(56);
});*/

combineLatest(one$, two$).subscribe(([one, two]) => {
  console.log(
    ` One Latest: ${one},
      Two Latest: ${two}`
  );
});

https://codesandbox.io/s/modest-vaughan-1t9k0