Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

WIP(core): Proposal: can unload, reload patch modules #1159

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

JiaLiPassion
Copy link
Collaborator

@JiaLiPassion JiaLiPassion commented Nov 4, 2018

The same idea with #1073.

In 1073, there is some performance concern, I used a more simple logic to implement this in this PR, every time only need to do a small loop and just reset reference. I did some basic benchmark.
It will only cost 0.000022ms every time unload/reload all the patches, so I think basically no cost

With this PR, we can run zone.js only inside Angular Elements, and don't impact outside of Angular Elements.

@mhevery, @robwormald, please review. Thank you!

@JiaLiPassion
Copy link
Collaborator Author

The idea of #1073 is,
This is a proposal to support unload, reload patch.

Use case: only run specified code into zone.

const zone = Zone.current.fork({name: 'zone'});
zone.run(() => {setTimeout(() => {
  console.log('zone', Zone.current.name);  // will run into zone
})});

setTimeout(() => {
  console.log('zone', Zone.current); // will run into root zone in current version.
}, 100);

for example, in current version, the code above, the 1st setTimeout will run into zone, the 2nd will run into root Zone. But even root Zone still have performance impact. So we may want the 2nd totally run in native setTimeout.

in this PR proposal, only patch all modules such as setTimeout, XHR, promise in zone.run.
and unPatch them when in root zone or by calling some method like 'runOutsideOfZone` explicitly.

Use case: such as angular elements to implement WebComponent. Only run logic with zone.js patched inside `angular elements without impact outside world.

Proposal. Auto patch when zone.run, Auto unpatch after zone.run. Auto repatch when zone.run again.

const zone = Zone.current.fork({name: 'zone'});
zone.run(() => {setTimeout(() => {
  console.log('zone', Zone.current.name);  // will run into zone
})});

setTimeout(() => {
  console.log('zone', Zone.current); // will not in root zone, this is native setTimeout
}, 100);

zone.run(() => {setTimeout(() => {
  console.log('zone', Zone.current.name);  // will run into zone again by rePatch
})});

To make unPatch and rePatch faster, keep patched delegate and native delegate reference when call Zone.__load_patch, so we only reset delegate when unPatch and rePatch.

@mleibman
Copy link
Contributor

mleibman commented Dec 4, 2018

Can this behavior be configurable?
Just like in #1138 (comment), this would break some of our existing latency instrumentation that relies on monkey-patching Zone.runTask/Zone.scheduleTask/Zone.cancelTask to be able to track all tasks including the ones run in the root zone.

@JiaLiPassion
Copy link
Collaborator Author

@mleibman, yes, this new behavior is totally configurable, by default, zone.js will work just like before. This new behavior supposes to just work for Angular Elements.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants