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

feat: added checks on creating events and added method for unregister… #144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Mohmn
Copy link
Member

@Mohmn Mohmn commented Nov 23, 2022

…ing events"

fixes #134

@dadiorchen
Copy link
Collaborator

@Mohmn did you test this locally?

Copy link
Member

@RubenSmn RubenSmn left a comment

Choose a reason for hiding this comment

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

Maybe instead of a function to remove methods, we can return a unsubscribe method when subscribing on

@Mohmn
Copy link
Member Author

Mohmn commented Nov 24, 2022

@Mohmn did you test this locally?

Yep i have tested it locally

@Mohmn
Copy link
Member Author

Mohmn commented Nov 24, 2022

Maybe instead of a function to remove methods, we can return a unsubscribe method when subscribing on

I donot see how it will be helpful, can u state the benefits of it

@dadiorchen
Copy link
Collaborator

@RubenSmn you mean like the way of useEffect returning function? if that's the goal, I think the event's once is a good go, just run it one time, then unregiste the lisnter.

@RubenSmn
Copy link
Member

I mean these days its a very common pattern to get back a unsubscribe method when subscribing to something.

@Mohmn
Copy link
Member Author

Mohmn commented Nov 25, 2022

I mean these days its a very common pattern to get back a unsubscribe method when subscribing to something.

Should I do both things
1An explicit function for unsubscribing events
2 returning the unsubscribe function from the subscriber

@dadiorchen @RubenSmn

@RubenSmn
Copy link
Member

I feel like its weird to have to call an "extra" method and pass the eventName and the correct handler which means the reference needs to be the same, so I think we can assume that we only add and remove the event in one place in the code if that makes sense.

I think that for the developer experience it is more logical to just receive an unsubscribe when subscribing.

@dadiorchen
Copy link
Collaborator

But maybe there are some cases we need to do the unscribe separately?

@RubenSmn
Copy link
Member

But maybe there are some cases we need to do the unsubscribe separately?

Our subscriptions are based on the reference of the function, this means we'll always need to provide the reference of the function we ran on a event.

Seperate Method (off)

// Parent
const Parent = () => {
  const myHandler = () => {
    // do stuff
  };

  map.on("this-event", myHandler);

  return (
    <Child handler={myHandler} />
  );
};

// Child
const Child = ({ handler }) => {
  // needs the exact instance of the "handler" function.
  // needs an extra "map" instance
  // needs the "this-event" name
  const onClick = () => {
    map.off("this-event", handler);
  };

  return (
    <button onClick={onClick}>Remove the handler from the map</button>
  );
};

Unsubscribe Method (callback)

// Parent
const Parent = () => {
  const myHandler = () => {
    // do stuff
  };

  const unSubscribe = map.on("this-event", myHandler);

  return (
    <Child unSubscribe={unSubscribe} />
  );
};

// Child
const Child = ({ unSubscribe }) => {
  // only cares about unsubscribing
  const onClick = () => {
    unSubscribe();
  };

  return (
    <button onClick={onClick}>Remove the handler from the map</button>
  );
};

@dadiorchen
Copy link
Collaborator

@RubenSmn I'm confused, what's your preference? callback?

@Mohmn
Copy link
Member Author

Mohmn commented Dec 1, 2022

@dadiorchen i think that @RubenSmn here is trying to imply that whether we use handleoff(implicit unsubscribe method) or callback (subscribe returning unsubscribe), we need to store the reference for it.

@Mohmn
Copy link
Member Author

Mohmn commented Dec 1, 2022

Ruben's point is now making sense to me , so which way should we go now

@dadiorchen
Copy link
Collaborator

@Mohmn let me take some time to understand this

@RubenSmn
Copy link
Member

RubenSmn commented Dec 2, 2022

@dadiorchen yes my preference is the callback. I think it will reduce the possibility for errors and provides a better developer experience!

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.

add method for removing custom event listeners
3 participants