zone.js/dist/zone-patch-rxjs的用途 [英] Purpose of zone.js/dist/zone-patch-rxjs

查看:68
本文介绍了zone.js/dist/zone-patch-rxjs的用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能我对这个问题已经太迟了,但是无论如何.

在什么情况下需要导入区域补丁- zone.js/dist/zone-patch-rxjs ,有人可以向我解释一下.据我所知,补丁是在 PR 中添加的(这一个).

我在 Angular 项目中使用 zone RxJs ,尽管看到 make rxjs在正确的区域中运行我对PR的描述不完全了解,它何时能为我提供帮助或应该为我解决什么问题.

我会喜欢一些之前/之后的代码示例.

谢谢.

解决方案

您可以在此处进行检查,拉请求843 .下面的示例代码描述了这个想法.

  const constructorZone = Zone.current.fork({name:'constructor'});const subscriptionZone = Zone.current.fork({name:'subscription'});;const operatorZone = Zone.current.fork({name:'operator'});可以观察;让订户;constructorZone.run(()=> {observable =新的Observable((_ subscriber)=> {订阅者= _订阅者;console.log('构造可观察时的当前区域:',Zone.current.name);//将输出构造函数.return()=>{console.log('可取消订阅时的当前区域:',Zone.current.name);//将输出构造函数.}});});subscriptionZone.run(()=> {observable.subscribe(()=> {console.log('下一个订阅时的当前区域',Zone.current.name);//将输出订阅.},()=>{console.log('订阅错误时的当前区域',dZone.current.name);//将输出订阅.},()=>{console.log(订阅完成后的当前区域",Zone.current.name);//将输出订阅.});});operatorZone.run(()=> {observable.map(()=> {console.log('地图操作员时的当前区域',Zone.current.name);//将输出运算符.});}); 

目前基本上rxjs API包含的所有内容

  • 可观察
  • 订阅
  • 订户
  • 操作员
  • 调度程序

已打补丁,因此每个异步调用都将在正确的区域中运行.

回答您的评论问题.

否,这是不正确的.当前,没有补丁,每个回调将在角度区域的内部或外部运行,具体取决于发射器.与创建回调时无关.

例如.

  let sub;ngZone.runOutsideAngular(()=> {const observable =新的Observable(订户=> sub =订户));observable.subscribe(()=> {//在ngzone中});});ngZone.run(()=> {sub.next(1);}); 

在这种情况下,可观察对象是在角度区域之外创建的,但是subscriber.next在角度区域内部被调用,因此最终,回调仍将在角度区域内.

有了补丁程序,回调将在ngzone之外,因为它是在ngzone之外创建的.

Probably I am too late with the question but anyway.

Could someone explain me in what cases I need to import zone's patch - zone.js/dist/zone-patch-rxjs. As far as I know the patch was added in this PR (successor of this one).

I use zone and RxJs in my Angular project and despite seeing make rxjs run in correct zone in PR's description I do not fully understand when it can help me or what problems it should resolve for me.

I would appreciate some code examples like before/after.

Thanks in advance.

解决方案

You can check it here, https://github.com/angular/angular/blob/master/packages/zone.js/NON-STANDARD-APIS.md

The idea is to let rxjs run into correct zone in different cases.

zone.js also provide a rxjs patch to make sure rxjs Observable/Subscription/Operator run in correct zone. For details please refer to pull request 843. The following sample code describes the idea.

const constructorZone = Zone.current.fork({name: 'constructor'});
const subscriptionZone = Zone.current.fork({name: 'subscription'});
const operatorZone = Zone.current.fork({name: 'operator'});

let observable;
let subscriber;
constructorZone.run(() => {
  observable = new Observable((_subscriber) => {
    subscriber = _subscriber;
    console.log('current zone when construct observable:', 
      Zone.current.name); // will output constructor.
    return () => {
      console.log('current zone when unsubscribe observable:', 
      Zone.current.name); // will output constructor.
    }
  });
}); 

subscriptionZone.run(() => {
  observable.subscribe(() => {
    console.log('current zone when subscription next', 
      Zone.current.name); // will output subscription. 
  }, () => {
    console.log('current zone when subscription error', d 
      Zone.current.name); // will output subscription. 
  }, () => {
    console.log('current zone when subscription complete', 
      Zone.current.name); // will output subscription. 
  });
});

operatorZone.run(() => {
  observable.map(() => {
    console.log('current zone when map operator', Zone.current.name); 
    // will output operator. 
  });
});

Currently basically everything the rxjs API includes

  • Observable
  • Subscription
  • Subscriber
  • Operators
  • Scheduler

is patched, so each asynchronous call will run in the correct zone.

To answer your comment question.

No, it is not correct. Currently, without the patch, every callback will run inside or outside of angular zone depends on the emitter. It will have nothing to do with when the callback was created.

for example.

let sub;
ngZone.runOutsideAngular(() => {
   const observable = new Observable(subscriber => sub = subscriber));
   observable.subscribe(() => {
      // in ngzone
   });
});

ngZone.run(() => {
  sub.next(1);
});

in this case, the observable was created outside of angular zone, but the subscriber.next was called inside angular zone, so finally, the callback will still in angular zone.

with the patch, the callback will be out side of ngzone because it is created outside of ngzone.

这篇关于zone.js/dist/zone-patch-rxjs的用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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