I can understand addListener(callback) pretty well, but removeListener(listener) and hasListener(listener) is kinda annoying to me. I totally have no idea what actually are. So here is some questions keep wondering in my mind:
What is listener? Is it a promise object or id string of a notification?
Where do we get it (the listener)? It cannot a vague callback function right?
How to use these functions? The idea of remove functions is hard to understand to me? Why would we need to remove it? A thing that we try so hard to add it to an event? Moreover, isnât it will be get rid of event after the object destroyed? Notifications donât last forever right. And if we really need to change the content of notification, we can just use browser.notifications.update().
Iâve checked Chrome document but it doesnât seem they have these functions.
listener is a function. A function listening to the event. You make that function yourself.
The idea of has and remove is that you pass the same function that you passed to add listener if you want to do these actions. You will not have to manage your listeners with remove/has listeners in most situations. It behaves pretty much the same as on the web. You need these methods if you want to explicitly stop receiving events, or find out if something is receiving events.
But how do has and remove know that the function we pass to is the same function we used for add? I donât think it could see functions as object because there are some returned functions as well.
If you pass the same function reference, it can compare the function references. So to use these methods, itâs important to pass a function reference and not a function literal (which is a reference too, just that you donât store the reference).
I think I not fully get it well. I still have some trouble in understanding the âfunction referencesâ. So, I hope you donât mind if I ask for a simple example of function references? Iâm sorry, but Iâm just an amateur who start from HTML to JS and struggling with C++ at pointers soâŠ