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

event.off is not removing the event #123

Closed
bci24 opened this issue Jan 2, 2021 · 8 comments
Closed

event.off is not removing the event #123

bci24 opened this issue Jan 2, 2021 · 8 comments

Comments

@bci24
Copy link

bci24 commented Jan 2, 2021

It seems that off is not working or I am missing something.

I am using mitt it in Vue 3

`import mitt from "mitt";
const Event = mitt();

const app = createApp(App);
app.config.globalProperties.Event = Event;
app.mount('#app');`

I have 2 components Category and CategoryDetails and when changing from one to another it seems that the off is not "destroying" the event.

In CategoryDetails component:

mounted() { const vm = this; this.Event.on('category.save', function(){ vm.formSubmit(); }); },

// it should clear the event - to no longer emit
unmounted() { this.Event.off('category.save'); }

Entering again in CateegoryDetails and emits the save, it make 2 request and so on....

I have tried to use in unmounted:

this.Event.all.clear();

and seems to work.

Why is not working if I specify the event name to off ???

Am i missing something ?

sealice added a commit to sealice/mitt that referenced this issue Jan 4, 2021
If mitt.off provides only the event, remove all listeners for that event.
@littlekie
Copy link

How to solve this problem now?

@littlekie
Copy link

image
I have solved it that add all.delete(type) line in off function.

@Yrobot
Copy link

Yrobot commented Jan 21, 2021

.off(name, handler) func means mitt will remove handler from the name handler map.
check the Usage on https://www.npmjs.com/package/mitt
image

@acuD1
Copy link

acuD1 commented Feb 22, 2021

Manually deleting event : this.emitter.all.delete('eventName')

@redfox05
Copy link

redfox05 commented Mar 17, 2021

I'm assuming the above solution is not ideal / might be messing with internal logic, so is it not possible to make the .off() function take an optional handler instead of required, and if handler is missing, delete all handlers with that label? Then it would work the same as @acuD1's workaround.

Update: That seems to be exactly what has been implemented in #124 but not yet an approved/merged PR.

@workforbala
Copy link

if both handler having same call back function name how we can differentiate between which one is on and which one is off ?

@mariusa
Copy link

mariusa commented Jun 16, 2021

Our usecase is defining a handler inline, not as a separate function, eg

bus.on('charge', payload => {
            //do stuff
        })

off(type) should remove all handlers for the given type, be it inline or with specific functions.

.off(name, handler) func means mitt will remove handler from the name handler map.

This isn't enough. Doesn't work for inline listeners.

@Yrobot
Copy link

Yrobot commented Jun 17, 2021

Our usecase is defining a handler inline, not as a separate function, eg

bus.on('charge', payload => {
            //do stuff
        })

off(type) should remove all handlers for the given type, be it inline or with specific functions.

.off(name, handler) func means mitt will remove handler from the name handler map.

This isn't enough. Doesn't work for inline listeners.

yes, we'd better not use inline listener, cus .off(name,handler) func works based on the handler func address(inner logic: remove the same address listener from listener list) , if u use inline listener, then u cant pass the same address handler to .off func.

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

7 participants