-
Notifications
You must be signed in to change notification settings - Fork 481
Return unsub function from on() #42
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
Conversation
255e9d6 to
ad3d596
Compare
src/index.js
Outdated
| * @memberOf mitt | ||
| */ | ||
| off(type: string, handler: EventHandler) { | ||
| let e = all[type] || (all[type] = []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that's intentional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the assignment isn't needed, just it produced a smaller output size because it is duplicated and thus cheap when gzipped. I'd be curious to see the size with & without this change.
|
We still not consider #1, so this will be delayed too... I'm still strongly against that. Since we promote to be "compat" with Node EE. We still are totally not compat in any terms, but yea.. |
src/index.js
Outdated
| on(type: string, handler: EventHandler) { | ||
| (all[type] || (all[type] = [])).push(handler); | ||
| var arr = all[type] = all[type] || []; | ||
| return arr.splice.bind(arr, arr.push(handler) - 1, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this fails if you remove an earlier-inserted event handler, because it shifts the indices:
let e = mitt(), a=()=>{}, b=()=>{};
e.on('a', a);
e.on('a', b);
e.off('a', a);
e.off('a', b); // attempts to remove all['a'][1], index is incorrectAFAIK there is no way around this except using a closure, which is why the other PR was too large.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, my "late night coding" brain missed how brittle the splice solution was.
76bda65 to
445a581
Compare
|
Alright, I just pushed. if we replace Regarding this not being compatible with EE.. This library isn't at all compatible with EventEmitter at the moment. |
fa64368 to
71ada07
Compare
Forked from developit#1.
71ada07 to
a09c7b5
Compare
|
object create is intentional. and yea exactly wat i mean |
|
I'm guessing the purpose of Object.create(null) here is to allow event names using object prototype names? Is that correct? |
|
Yes. Wanted and merged. In reality we can go down to ~160-170 if we drop edit: If we are more strict we even can remove the method names and be a single method :D |
|
What about this one then function dush (all) {
all = all || Object.create(null)
return function onOrEmit (name, handler) {
if (typeof handler === 'function') {
var e = (all[name] || (all[name] = []))
e.push(handler)
return function off (name) {
(name && (all[name] = [])) || e.splice(e.indexOf(handler) >>> 0, 1)
}
}
all[name].map((f) => { f(handler) })
}
}example var emitter = dush()
// .on
emitter('foo', (data) => console.log('one', data))
var offBar = emitter('bar', (data) => console.log('bar1', data))
emitter('bar', (data) => console.log('bar2', data))
var offFoo = emitter('foo', (data) => console.log('two', data))
// .emit
emitter('foo', 123)
// off all foo
offFoo('foo')
emitter('foo', 'not emitted')
emitter('bar', 'woohoo')
// off all bar
offBar('bar')
emitter('bar', 33)
emitter('foo', 33) |
|
That looks a lot like the observed value syntax from Mithril. Separate lib tho :P Think we should rename off? You're right about EE compat, I took that name from Wildemitter |
|
Returning an When having a lot of handlers subscriptions/unsubscriptions this implementation is apparently heavier in terms of memory consumption. Probably because a function is allocated every time a subscription is created. This has been reported to the @developit what do you think? |
|
^ Yes |
195b. Forked from #1.
all[type]in.off()all[type]in.on()(no parens)..on().The one thing I can't figure out is the why the "off() should normalize case" test is failing now. In fact, I don't see how it ever worked.