Git Product home page Git Product logo

Comments (3)

developit avatar developit commented on August 18, 2024 2

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.

Haroenv avatar Haroenv commented on August 18, 2024

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.

Haroenv avatar Haroenv commented on August 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.