Skip to content

bug: zoneWrap applies broken arguments after bundled #3126

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

Closed
lacolaco opened this issue Jan 21, 2022 · 2 comments
Closed

bug: zoneWrap applies broken arguments after bundled #3126

lacolaco opened this issue Jan 21, 2022 · 2 comments

Comments

@lacolaco
Copy link
Contributor

lacolaco commented Jan 21, 2022

Version info

Angular: v13

Firebase: 9.6

AngularFire: 7.2.0

Other (e.g. Ionic/Cordova, Node, browser, operating system):

How to reproduce these conditions

Actual behavior

angularfire/src/zones.ts

Lines 162 to 175 in b60670c

return function() {
let macrotask: MacroTask | undefined;
// if this is a callback function, e.g, onSnapshot, we should create a microtask and invoke it
// only once one of the callback functions is tripped.
for (let i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'function') {
if (blockUntilFirst) {
macrotask ||= run(() => Zone.current.scheduleMacroTask('firebaseZoneBlock', noop, {}, noop, noop));
}
// TODO create a microtask to track callback functions
arguments[i] = zoneWrapFn(arguments[i], macrotask);
}
}
const ret = runOutsideAngular(() => (it as any).apply(this, arguments));

  return function() {
    let macrotask: MacroTask | undefined;
    // if this is a callback function, e.g, onSnapshot, we should create a microtask and invoke it
    // only once one of the callback functions is tripped.
    for (let i = 0; i < arguments.length; i++) {
      if (typeof arguments[i] === 'function') {
        if (blockUntilFirst) {
          macrotask ||= run(() => Zone.current.scheduleMacroTask('firebaseZoneBlock', noop, {}, noop, noop));
        }
        // TODO create a microtask to track callback functions
        arguments[i] = zoneWrapFn(arguments[i], macrotask);
      }
    }
    const ret = runOutsideAngular(() => (it as any).apply(this, arguments)); // <--- `arguments` of the top function

In this source file, arguments is used as an array that contains the arguments of the function at the beginning. In fact, this code works fine in builds targeting ES2015 and later.

If you look at the zoneWrap part of the UNPKG file, you will see that the callback for the runOutsideAngular function is transpiled to a function, so the internal arguments changes the arguments it refers to.

https://unpkg.com/@angular/[email protected]/bundles/angular-fire.umd.js

        return function () {
            var _this_1 = this;
            var macrotask;
            // if this is a callback function, e.g, onSnapshot, we should create a microtask and invoke it
            // only once one of the callback functions is tripped.
            for (var i = 0; i < arguments.length; i++) {
                if (typeof arguments[i] === 'function') {
                    if (blockUntilFirst) {
                        macrotask || (macrotask = run(function () { return Zone.current.scheduleMacroTask('firebaseZoneBlock', noop, {}, noop, noop); }));
                    }
                    // TODO create a microtask to track callback functions
                    arguments[i] = zoneWrapFn(arguments[i], macrotask);
                }
            }
            var ret = runOutsideAngular(function () { return it.apply(_this_1, arguments); });  // <--- `arguments` of the nested function

How to fix

  1. Upgrade ng-packagr to v13 and stop shiping UMD bundle (no ES5 transpliation)
  2. Stop direct referring to arguments in callback functions (fix(core): avoid referring to arguments in arrow functions #3127)
  3. Use (...args) => {}
@google-oss-bot
Copy link

This issue does not seem to follow the issue template. Make sure you provide all the required information.

@jamesdaniels
Copy link
Member

Cutting 7.2.1 with the fix now

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

No branches or pull requests

3 participants