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

about single parameter listener error, (.apply is not a function error) #1539

Open
9ssi7 opened this issue May 9, 2022 · 2 comments
Open
Labels
question Further information is requested

Comments

@9ssi7
Copy link

9ssi7 commented May 9, 2022

Describe the bug
Socketio on function requests event name as 1st parameter and callback as 2nd parameter. For this, a check is done under the hood, but it is not enough. We need to improve the control a little more. Because if the callback is not sent, we get the following error.

Screen Shot 2022-05-09 at 14 08 43

To Reproduce

Please fill the following code example:

Socket.IO server version: 4.4.1

Server

// it doesn't matter what

Socket.IO client version: 4.4.1

Client

import { io } from "socket.io-client";

const socket = io("ws://localhost:3000/", {});

socket.on("connect", () => {
  console.log(`connect ${socket.id}`);

  socket.on("mybestevent.v1") // causes an error.
});

socket.on("disconnect", () => {
  console.log("disconnect");
});

Expected behavior
The code under the hood is as follows:

Screen Shot 2022-05-09 at 14 08 55

I think we can update the code here as follows. We even issue a warning like in the else block, telling the user that he should send a callback. I think it would be a better use.

Emitter.prototype.emit = function(event) {
   this._callbacks = this._callbacks || {};
   
   var args = new Array(arguments.length - 1), callbacks = this._callbacks['$' + event];
  
   for (var i = 1; i < arguments.length; i++) {
      args[i - 1] = arguments[i];
   }

   if(callbacks) {
     callbacks = callbacks.slice(0);
     for (var i = 0, len = callbacks.length; i < len; i++) {
         if(!!callbacks[i] && typeof callbacks[i] === 'function') {
              callbacks[i].apply(this, args);
         }else {
              console.warn("Looks like you forgot to add a callback to a listener. Please check your listeners.")
         }
     }
   }
}

Platform:

  • Device: any
  • OS: any

Additional context
I would be glad if you consider it.

@9ssi7 9ssi7 added the to triage Waiting to be triaged by a member of the team label May 9, 2022
@darrachequesne
Copy link
Member

Hi! I guess we could add a check here:

Emitter.prototype.on =
Emitter.prototype.addEventListener = function(event, fn){
  if (typeof fn !== "function") {
    throw new Error("please provide a function");
  }
  this._callbacks = this._callbacks || {};
  (this._callbacks['$' + event] = this._callbacks['$' + event] || [])
    .push(fn);
  return this;
};

What do you think?

That being said, we could also add a warning if there is no matching callback here:

Emitter.prototype.emit = function(event){
  this._callbacks = this._callbacks || {};

  var args = new Array(arguments.length - 1)
    , callbacks = this._callbacks['$' + event];

  for (var i = 1; i < arguments.length; i++) {
    args[i - 1] = arguments[i];
  }

  if (callbacks) {
    callbacks = callbacks.slice(0);
    for (var i = 0, len = callbacks.length; i < len; ++i) {
      callbacks[i].apply(this, args);
    }
  } else {
    console.warn(`no callback registered for ${event}`);
  }

  return this;
};

(note: we'd have to remove these console.warn in production)

@darrachequesne darrachequesne added question Further information is requested and removed to triage Waiting to be triaged by a member of the team labels May 11, 2022
@9ssi7
Copy link
Author

9ssi7 commented May 11, 2022

Thank you for your callback. It looks so much better this way!

In terms of production, there is no warning given in most of the codes. We can skip it for now to adapt to the project. But I think we should definitely show a warning based on an env value to be determined by the user. Because finding this solution required extra focus.

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

No branches or pull requests

2 participants