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

sse.once(event,fn), fn triggered multiple times #37

Open
lgCode opened this issue Mar 20, 2022 · 0 comments
Open

sse.once(event,fn), fn triggered multiple times #37

lgCode opened this issue Mar 20, 2022 · 0 comments

Comments

@lgCode
Copy link

lgCode commented Mar 20, 2022

Use this.$sse.once(), it will be triggered continuously instead of only once.

sorry,I won't use jest to write this test (-。-!)

I made the following modifications to test the feasibility:

path: src/sse-client.js

api: once

`once(event, handler) {

//old   
-    this.on(event, (e) => {
-     this.off(event, handler);
-     handler(e);
-    });


//new change-1: start
 const once = e => {
   handler(e);
   this.off(event, handler);
 };
 once.initialhandle = handler;//
 this.on(event, once);

//change-1: end
return this;

}`

api: off

`off(event, handler) {
if (!this._handlers[event]) {
// no handlers registered for event
return this;
}

  //old
  const idx = this._handlers[event].indexOf(handler);
     if (idx === -1) {
     // handler not registered for event
     return this;
   }
   // remove handler from event
   this._handlers[event].splice(idx, 1);

  //new  change-2 start
  // if handler is null ,remove all handler from event
  if (!handler) {
    delete this._handlers[event];
    return this;
  }

  //initialhandle 标识出 once 的handler
  this._handlers[event] = this._handlers[event].filter(
    item => item !== handler && item.initialhandle != handler
  );
  //change-2 end


  if (this._handlers[event].length === 0) {
    // remove listener since no handlers exist
    this._source.removeEventListener(event, this._listeners[event]);
    delete this._handlers[event];
    delete this._listeners[event];
  }
  return this;


}

`

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

1 participant