mitt icon indicating copy to clipboard operation
mitt copied to clipboard

Request to Implement Unsubscribe Functionality in the Mitt Framework

Open wcldyx opened this issue 1 year ago • 1 comments

Request to Implement Unsubscribe Functionality in the Mitt Framework

Description: The current version of the Mitt framework lacks an unsubscribe feature, making it inconvenient to manage event listeners, especially when components are unmounted or no longer need to listen for events.

Suggested Feature: Implement an unsubscribe method that allows users to easily remove specific event listeners.

Example:

// Current usage in Mitt
const mitt = require('mitt');
const emitter = mitt();

const handler = () => console.log('Event triggered');
emitter.on('event', handler);
emitter.off('event', handler); 
// After adding the unsubscribe feature
const mitt = require('mitt');
const emitter = mitt();

// Proposed usage
const unsubscribe = emitter.on('event', () => console.log('Event triggered'));
unsubscribe(); // Now we can easily unsubscribe

Conclusion: Adding the unsubscribe functionality would greatly improve the usability of the Mitt framework and align it with best practices in event handling. Thank you for considering this enhancement!

wcldyx avatar Sep 05 '24 03:09 wcldyx

@wcldyx Hey! Why not just wrapping .on('event', ...) and return () => .off('event', ...)?

mvrlin avatar Sep 25 '25 13:09 mvrlin