简体   繁体   English

Observable.create() function 的参数是否必须明确定义观察者?

[英]Does the parameter of Observable.create() function have to define explicitely an observer?

I came across this code from here: https://medium.com/@mohandere/rxjs-5-in-5-minutes-1c3b4ed0d8cc我从这里遇到了这段代码: https://medium.com/@mohandere/rxjs-5-in-5-minutes-1c3b4ed0d8cc

function multiplyByTen(input) {
  var output = Rx.Observable.create(function subscribe(observer) {
    input.subscribe({
      next: (v) => observer.next(10 * v),
      error: (err) => observer.error(err),
      complete: () => observer.complete()
    });
  });
  return output;
}

var input = Rx.Observable.from([1, 2, 3, 4]);
var output = multiplyByTen(input);
output.subscribe(x => console.log(x));
// Result In:
// 10 
// 20 
// 30 
// 40

I just do not see where the "observer" parameter for the subscribe function inside the multiplyByTen function comes from?我只是看不到 multiplyByTen function 内的订阅 function 的“观察者”参数来自哪里? Does it have to be explicitely defined or is it just some "default" object that gets passed to the create function in case it was not defined before?它是否必须明确定义,或者只是一些“默认” object 被传递给创建 function 以防之前未定义? And why is it called function subscribe(observer) inside the function?为什么在function里面叫function subscribe(observer)? Does it override the default.subscribe() function or could it also be an anonymous function?它会覆盖 default.subscribe() function 还是匿名 function?

It is just an RxJS construct to allow you to dispatch new values to the subscriber functions.它只是一个 RxJS 构造,允许您向订阅者函数分派新值。 Basically, this is how RxJS have designed the library to work.基本上,这就是 RxJS 设计库的工作方式。 This allows you to control the dispatch of new values.这允许您控制新值的分派。 One example of usage is when you have some asynchronous operation and you want to send a new value to the subscribers when it has resolved/completed.使用的一个例子是当你有一些异步操作并且你想在它已经解决/完成时向订阅者发送一个新值。

You can read more about how it works here: https://www.learnrxjs.io/operators/creation/create.html您可以在此处阅读有关其工作原理的更多信息: https://www.learnrxjs.io/operators/creation/create.html

Also, check out this simple synchronous example in JsBin (taken from the docs link above)另外,在 JsBin 中查看这个简单的同步示例(取自上面的文档链接)

/*
  Create an observable that emits 'Hello' and 'World' on  
  subscription.
*/
const hello = Rx.Observable.create(function(observer) {
  observer.next('Hello');
  observer.next('World');
});

const subscribe = hello.subscribe(val => console.log(val));
// prints.. the follwoing:
// Hello
// World

The asynchronous case I mentioned above is portrayed here: (from official RxJS jsBin examples - https://jsbin.com/lodilohate/1/edit?js,console ):我上面提到的异步情况在这里描述:(来自官方 RxJS jsBin 示例 - https://jsbin.com/lodilohate/1/edit?js,console ):

// RxJS v6+
import { Observable } from 'rxjs';

/*
  Increment value every 1s, emit even numbers.
*/
const evenNumbers = Observable.create(function(observer) {
  let value = 0;
  const interval = setInterval(() => {
    if (value % 2 === 0) {
      observer.next(value);
    }
    value++;
  }, 1000);

  return () => clearInterval(interval);
});
//output: 0...2...4...6...8
const subscribe = evenNumbers.subscribe(val => console.log(val));
//unsubscribe after 10 seconds
setTimeout(() => {
  subscribe.unsubscribe();
}, 10000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM