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.
map
operator inside then without patch operator's callback will be called outside angular zone but with patch it will be called inside. Am I right? – Dees