-
Notifications
You must be signed in to change notification settings - Fork 593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
asap: add support for zone.js with microtasks #113
Conversation
Is the desire here to run promise jobs after other jobs, even if they're scheduled within the same job? Eg: setImmediate(_ => console.log(1));
Promise.resolve().then(_ => console.log(2));
setImmediate(_ => console.log(3)); The above will currently log @stefanpenner I guess this should go in RSVP rather than here anyway. What's your take on it? Should Angular fork here, or should there be a way to change the job queuing stuff? |
This PR does not intend to change the order (ie it should be 1, 2, 3). Nonetheless I need to update zone.js to enqueue a microtask in such a case, thanks for the pointer ! |
RSVP actually makes the https://github.com/emberjs/ember.js/blob/master/packages/ember-runtime/lib/ext/rsvp.js#L23-L32 |
my further thoughts:
note: in ember, we essentially do this, because the "whole world" abides by the run.loop. I believe this is the same in angular via zone.js and more or less reasonable. cc @IgorMinar |
@stefanpenner a more generic way to override |
@stefanpenner there would be no ordering changes. The purpose of this change is only to detect the end of VM turns (macrotask + microtasks). |
The way do this in ember, actually defers all of I suspect something that is both compatible with ember run.loop and zone.js would allow for a reasonable scheduler API to be derived. I will also be sure to review @jakearchibald something like @vicb you are correct, I forgot that zone.js is merely for detection/control. Can i assume, it correctly stub native promises? |
@stefanpenner I'm not sure if there is a way to make zone.js work with native The "issue" with native |
ah ok. |
@stefanpenner some more thinking about this (let me know if you prefer that I open an issue and copy the text there instead of discussing on this PR). In Angular2:
I have realized that letting <script>
// This code is not wrapped into zone.run()
Promise.resolve(null).then(function() {
// will only be executed after the call to zone.run') :(
});
</script> One way I can think of to solve this is to use JS for scheduling microtasks by using:
For this to work we would need:
The zone.js implementation od function scheduleMicrotasks(fn) {
pendingMicrotasks++;
asap(function() { // asap() being either the es6-promise polyfill or a native Promise based impl
try {
fn();
} finally {
pendingMicrotask--;
}
} item 2 was already discussed (ability to override This is my current thinking, I'll try to build a prototype and update here with my results. |
This sounds reasonable. I think making As, I mentioned RSVP.js has this feature. Although it uses more bytes to accomplish it, then I would like in this library (Typically byte sensitive people prefer es6-promise to RSVP) |
@stefanpenner I have a prototype working - I can point you to the repos/branches if you are interested in the detail but the basic idea is: // Override es6-promise asap implementation
export default function asap(callback, arg) {
window.zone.scheduleMicrotask(function() {
callback(arg);
});
}
// zone.js scheduleMicrotask
var resolvedPromise = Promise.resolve(void 0);
function _asap(fn) {
resolvedPromise.then(fn);
}
function scheduleMicrotask(fn) {
// The idea is to use any of the es6-promise fallbacks when native Promise are not available
_asap(this.bind(fn));
} This implementation is not using the queue from the es6-promise implementation. I think there should be no reason why it would not work but may be it's better if you allow overriding this as well. Let me know if I can help you port this from RSVP. |
a better solution is implemented in #116 |
Would you please consider integrated this small patch into the master branch ?
Context:
This patch is required by Angular2.
In Angular2, we need to execute the change detection (check what properties have changed) at the end of each VM turn. A VM turn being one macrotask followed by 0+ microtasks.
By using this patch in combination with zone.js, we will be able to correctly detect VM turns - we need a custom version of zone.js, I'll submit the PR shortly.
Anything else I could do before this can get merged ?
Thanks.
/cc @mhevery