使用forEach代替使用可观察到的键盘输入事件进行订阅有好处吗? [英] Is there a benefit to using forEach instead of subscribe with a keyboard input event observable?

查看:37
本文介绍了使用forEach代替使用可观察到的键盘输入事件进行订阅有好处吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想听Angular的 FormControl.valueChanges时,我是否想确定是否更喜欢使用 .forEach 而不是 .subscribe .可以观察到. FormControl API

在Angular的表单文档中,他们使用 .forEach 而不真正解释原因,并说您并不需要真正理解它.文档链接

我还从答案知道, .forEach 将捆绑一定的持续时间事件/传入的价值变成诺言.

所以我自己尝试测试 .forEach .subscribe ,我认为 .forEach 在捆绑键盘输入事件时会表现得更好.用户将在其上输入疯狂的猴子.

  constructor(私有fb:FormBuilder){this.createForm();}私人createForm(){this.myForm = this.fb.group({姓名: '',});}私人forEach = this.myForm.get('name').valueChanges.forEach(value => {console.log(value,'forEach');});私人订阅= this.myForm.get('name').valueChanges.subscribe(value => {console.log(value,'subscribe');}); 

在我的测试中,手动在html输入中快速键入并没有像我期望的那样捆绑 .forEach 值.取而代之的是 .subscribe .forEach 一次发出一个字符的值.

在这种情况下, .forEach .subscribe 有什么好处吗?如果使用<在 .subscribe 上通过 .forEach ?

解决方案

尽管经常可以同时使用它们,但您需要了解 forEach 的工作方式类似于发出多个值的Promise./p>

这也意味着您不能退订 forEach ,因此对于无法完成的Observable,您很可能会造成内存泄漏(例如 valueChanges 或Router事件)./p>

说实话,我不确定 valueChanges 是否完成.我在中检查了源代码https://github.com/angular/angular/blob/5.0.0/packages/forms/src/model.ts#L636-L867 ,他们从不发送 complete 通知.我没有找到可确保发送正确的 complete 通知的任何测试( https://github.com/angular/angular/blob/5.0.x/packages/forms/test/form_control_spec.ts ).所以我不知道他们为什么在Angular文档中使用 forEach .我认为他们应该改用 subscribe .

我什至看不到为什么 valueChanges 仍然会完成.仅当您破坏表格但自己无法控制时.

如果您想测试任何东西,则需要知道结果.我不明白您希望在编写测试后看到哪些好处.

I'm trying to find confirmation if I should prefer to use .forEach instead of .subscribe when I want to listen in on Angular's FormControl.valueChanges observable. FormControl API

In Angular's Documentation for forms they use .forEach without really explaining why and say you don't really need to understand it. Document Link

I also know from this answer that .forEach will bundle a sort of finite duration of events/incoming values into a promise.

So I tried to test both .forEach and .subscribe myself thinking that .forEach will be more performant in bundling keyboard input events if the user were to type monkey crazy on it.

constructor(private fb: FormBuilder) {
    this.createForm();
}
private createForm() {
    this.myForm = this.fb.group({
      name: '',
    });
}

private forEach = this.myForm.get('name').valueChanges.forEach(value => {
  console.log(value, 'forEach');
});

private subscription = this.myForm.get('name').valueChanges.subscribe(value => {
  console.log(value, 'subscribe');
});

In my test manually typing fast in the html input did not bundle .forEach values as I expected. Instead both .subscribe and .forEach emited values one character at a time.

Is there any benefit to .forEach over .subscribe in this situation and how can I create a test that can show the difference if there is a benefit to using .forEach over .subscribe?

解决方案

Although it's often possible to use both of them you need to be aware that forEach works similarly to Promises that emit multiple values.

This also means you can't unsubscribe from forEach so with Observables that never complete you are very likely to create memory leaks (like valueChanges or Router events).

I'm not sure if valueChanges ever completes to be honest. I checked the source code at https://github.com/angular/angular/blob/5.0.0/packages/forms/src/model.ts#L636-L867 and they never send the complete notification. I didn't find any test that makes sure proper complete notification is sent (https://github.com/angular/angular/blob/5.0.x/packages/forms/test/form_control_spec.ts). So I don't know why they are using forEach in Angular docs. I think they should be using subscribe instead.

I don't even see why would valueChanges complete anyway. Only when you destroy the form but you don't control this yourself.

If you want to test anything you need to know what you expect as a result. I don't understand what benefits you want to see after writing tests.

这篇关于使用forEach代替使用可观察到的键盘输入事件进行订阅有好处吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆