Comments (3)
Hiya @Haroenv! Sorry for the slow response.
mitt()
accepts an object listener mapping as an argument, which you can keep a reference to and clear using delete
later on.
Here's a few implementations.
1. Function Composition
import mitt from 'mitt';
function EventEmitter() {
const events = Object.create(null);
const emitter = mitt(events);
emitter.removeAllListeners = () => {
for (let i in events) delete events[i];
};
return emitter;
}
2. ES5 class version
import mitt from 'mitt';
function EventEmitter() {
this._events = Object.create(null);
Object.assign(this, mitt(this._events));
}
EventEmitter.prototype.removeAllListeners = function() {
for (let i in this._events) delete this._events[i];
};
3. Pure ES Classes version
import Mitt from 'mitt';
class EventEmitter extends Mitt {
constructor() {
const events = Object.create(null);
super(events);
this._events = events;
}
removeAllListeners() {
for (let i in this._events) delete this._events[i];
}
}
I'd recommend going with Option #3 if you're also patching in methods like addListener
and removeListener
.
from mitt.
before this used to be:
var util = require('util');
var events = require('events');
function DerivedHelper(mainHelper, fn) {
}
util.inherits(DerivedHelper, events.EventEmitter);
/**
* Detach this helper from the main helper
* @return {undefined}
* @throws Error if the derived helper is already detached
*/
DerivedHelper.prototype.detach = function() {
this.removeAllListeners();
};
from mitt.
This looks really cool, thanks! I'll compare later if this is smaller or not than the current events
package which we use now. It likely is, but it might be better to fork and avoid the extra wrapper functions.
I'll let you know when I next work on this. In the mean time I'll close this issue, since this very likely solves the issue!
from mitt.
Related Issues (20)
- Nuxt3 issues ? HOT 2
- Persist events that not yet have a handler HOT 1
- Is it possible to use mitt across two different tabs? Or via an iframe? HOT 10
- [TypeScript] define events interface HOT 3
- Mitt - some problems HOT 1
- Typescript issue with `moduleResolution: NodeNext` HOT 1
- `dist/mitt.mjs` must not have a sourcemap HOT 5
- mitt extension
- Unpkg CDN Version is down HOT 1
- Using Ag-grid cell renderer with Mitt HOT 1
- Bug: type issue in [email protected]+ with esm HOT 2
- Could we have a minor update? HOT 1
- Doesn't work with typescript 5 moduleResolution set to bundler HOT 11
- vue3 options methods use mitt Invalid HOT 1
- why use sllice & map HOT 2
- Can't pass multiple arguments with emit HOT 2
- How to declare type of event parameter when listen for event HOT 1
- error TS2349: This expression is not callable when using "module": "NodeNext". HOT 5
- Uncaught TypeError: (0 , mitt__WEBPACK_IMPORTED_MODULE_5__.default)(...) is not a function
- Emit issue on Vue 3. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mitt.