-
Notifications
You must be signed in to change notification settings - Fork 425
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
Support chaining #12
Comments
Interesting! Would be curious to see the filesize hit. |
I think the easiest fix is to make to exports. The default to be the "instance" (like invoking export default mitt()
export const mitt
function mitt(all) {
// code ... Something like that? So if users want to add initial events through import { mitt } from 'mit' otherwise they will have directly all the methods import emitter from 'mitt'
emitter.on().on() |
Oookey, okey, let me think a bit, i'll try. Coding at github issue isn't feels good haha |
Hmm - that'd be a singleton, which is generally not going to be a good thing (npm dupe issues, etc). I've used that pattern before with a similar EventEmitter, and that was actually one of the reasons I wrote this one - you can do this:
That's probably the better option if you're looking to then import the singleton methods later on. You get to use an instantiable library (mitt) but then work with your own created singleton in your codebase. Just my 2 cents :) |
Actually, I did it :) But is 212bytes (tests passing) ;/ All of my above snippets was wrong. Idea is different. We need to not just return an object with the methods but assign that object to export default function mitt(all) {
// Arrays of event handlers, keyed by type
all = all || {};
// Get or create a named handler list
function list(type) {
let t = type.toLowerCase();
return all[t] || (all[t] = []);
}
const ret = {
/** Register an event handler for the given type.
* @param {String} type Type of event to listen for, or `"*"` for all events
* @param {Function} handler Function to call in response to the given event
* @memberof mitt
*/
on(type, handler) {
list(type).push(handler);
return ret;
},
/** Remove an event handler for the given type.
* @param {String} type Type of event to unregister `handler` from, or `"*"`
* @param {Function} handler Handler function to remove
* @memberof mitt
*/
off(type, handler) {
let e = list(type),
i = e.indexOf(handler);
if (~i) e.splice(i, 1);
return ret;
},
/** Invoke all handlers for the given type.
* If present, `"*"` handlers are invoked prior to type-matched handlers.
* @param {String} type The event type to invoke
* @param {Any} [event] An event object, passed to each handler
* @memberof mitt
*/
emit(type, event) {
list('*').concat(list(type)).forEach( f => { f(event); });
return ret;
}
};
return ret;
} |
It will fit if we use #8. Damn.. that's freaking crazy haha |
Gotta get that size down somehow 😉 208 is pretty good though, basically just paying for one |
Seems like this issue and #1 are incompatible. |
Oooh - good point. I totally did not think of that. |
Yea, absolutely. But I believe this lib is meant to be with kind of compat with most common and to be small. Or at least it is promoted as that. I not mean that #1 will be with bigger size, but it is other thing and can be done in separate more smaller lib. 2c |
Instead of returning unsuscribe method (#1) we can add the |
@sospedra I'm not sure how viable a new method would be as the 200 B gzipped limit is quite a challenge to work around. @tunnckoCore has been doing some heroic with in #19. |
adding |
Using Closure Compiler, you end up with 197 bytes after including the
|
The source gets transpiled by Buble so those |
It would be cool and is already some habit for some devs.
The text was updated successfully, but these errors were encountered: