Skip to content
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

fix: Uncaught TypeError: Illegal invocation in chrome 51 #290

Merged
merged 2 commits into from
May 15, 2019

Conversation

qinyang912
Copy link
Contributor

in chrome 51, will throw error

image

in chrome 51, will throw error
Uncaught TypeError: Illegal invocation
@coveralls
Copy link

coveralls commented May 9, 2019

Coverage Status

Coverage increased (+0.005%) to 95.914% when pulling 7c729d7 on qinyang912:master into ca4816f on share:master.

@@ -15,7 +15,7 @@ Logger.prototype.setMethods = function (overrides) {

SUPPORTED_METHODS.forEach(function (method) {
if (typeof overrides[method] === 'function') {
logger[method] = overrides[method];
logger[method] = overrides[method].bind(overrides);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, but shouldn't this be:

Suggested change
logger[method] = overrides[method].bind(overrides);
logger[method] = overrides[method].bind(overrides[method]);

And by that point, maybe we can tidy up the loop a bit:

SUPPORTED_METHODS.forEach(function (method) {
  var override = overrides[method];
  if (typeof override === 'function') {
    logger[method] = override.bind(override);
  }
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be this:

SUPPORTED_METHODS.forEach(function (method) {
  var override = overrides[method];
  if (typeof override === 'function') {
    logger[method] = override.bind(overrides);
  }
});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why bind to the object of overrides rather than the function itself? Or actually, maybe more accurate would be:

logger[method] = override.bind(logger);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can try these code in chrome 51:

var logger = {};
var override = console.log;
logger.log = override.bind(override);
logger.log('test')

and

var logger = {}
var override = console.log
logger.log = override.bind(logger)
logger.log('test')

both throw error
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this is successful:

var logger = {};
var override = console.log;
logger.log = override.bind(console);
logger.log('test');

image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're getting that error because of this issue with Chrome. The correct thing to do is:

var logger = {};
var override = console.log.bind(console);
logger.log = override;
logger.log('test');

@alecgibson
Copy link
Collaborator

I'm closing this, because the correct usage should be:

ShareDB.logger.setMethods({
  log: console.log.bind(console),
});

@alecgibson alecgibson closed this May 10, 2019
@alecgibson
Copy link
Collaborator

Sorry, I just realised you mean this happens with the default Logger?

I'm not sure that arbitrarily setting the this argument to the provided object is valid. Instead, we should change how we set the default:

function Logger() {
  var defaultMethods = {};
  SUPPORTED_METHODS.forEach(function (method) {
    // Deal with Chrome issue: https://bugs.chromium.org/p/chromium/issues/detail?id=179628
    defaultMethods[method] = console[method].bind(console);
  });
  this.setMethods(defaultMethods);
}

@alecgibson alecgibson reopened this May 10, 2019
@qinyang912
Copy link
Contributor Author

@alecgibson Yes, I mean the default logger.. 😄

@ericyhwang ericyhwang merged commit b0d4277 into share:master May 15, 2019
@ericyhwang
Copy link
Contributor

Thanks! Published in [email protected]

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

Successfully merging this pull request may close these issues.

4 participants