Skip to content
This repository was archived by the owner on Apr 4, 2026. It is now read-only.

Add off method#165

Merged
alessandrokonrad merged 5 commits into
input-output-hk:mainfrom
dasilvaluis:add-off-method
Dec 24, 2021
Merged

Add off method#165
alessandrokonrad merged 5 commits into
input-output-hk:mainfrom
dasilvaluis:add-off-method

Conversation

@dasilvaluis

@dasilvaluis dasilvaluis commented Dec 24, 2021

Copy link
Copy Markdown

Context

While following the thread cardano-foundation/CIPs#151 I noticed some work was done very recently to support the on() proposal. I also notice a couple of issues with it, being 1) using .name of the callback to identify which to clear out and 2) Support to remove the callback was removed, while a lonely removeListener function was left

Solution

  • Added off method, based on removeListener
  • Refactored the methods to use the callback reference, not the .name

About using .name:
This would only work when the given callback would not be an anonymous function.

Examples:

function normalFn() {}
normalFn.name // normalFn

const arrow = () => {}
arrow.name // arrow

(() => {}).name // empty string

In order to identify which callback is which, the function reference works fine.

About the off method:
When firing it, all the matching callbacks assigned to a given event are removed and cleared from the window event listener.

Other notes

  • the import of 'react-icons/Go' was breaking my build due to the uppercase char
  • tests were added to support the on/off methods
  • I was not really sure about injecting the on/off to window.cardano right away, so I decided to return { remove: () => off() } at the on method, which in the end is backwards compatable with the previous version
  • I noticed that after the very latest push, the events were not working anymore. But with this PR they are. I am not sure if it is something I fixed, or it I was doing something wrong after I pulled.

@alessandrokonrad

Copy link
Copy Markdown
Contributor

Great, thanks!

I was not really sure about injecting the on/off to window.cardano right away, so I decided to return { remove: () => off() } at the on method, which in the end is backwards compatable with the previous version

Yes, I was not even sure someone knew that you can remove the event with that function. That is why I removed it and the idea of having a seperate function in the API called removeListener or off is much better. I'm still waiting with implementing it until the discussion progresses a little bit more.

I noticed that after the very latest push, the events were not working anymore.

Events are still working for me? Or do you you mean only the removing part here?

@dasilvaluis

Copy link
Copy Markdown
Author

So, let me know if I should remove the remove method from the return.

I think exposing both on and off definitely makes more sense in the long term.

Yes, I was not even sure someone knew that you can remove the event with that function.

It was not obvious, but evident by reading the source code

Events are still working for me? Or do you you mean only the removing part here?

So probably I was doing something wrong, don't mind it.

@alessandrokonrad

Copy link
Copy Markdown
Contributor

So, let me know if I should remove the remove method from the return.

It's fine, we can leave it in for now, until the event listeners become part of CIP-0030

I'm gonna merge this PR now. Looks good👍🏻

@alessandrokonrad alessandrokonrad merged commit a14d269 into input-output-hk:main Dec 24, 2021
alessandrokonrad added a commit that referenced this pull request Jan 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants