Collected · mitt · MIT

Temporary exhibition · Hall of the Commons

Two Hundred Bytes

TypeScript·2017·123 lines·3722 bytes

Curator’s note

Most of this file is type declarations. The event emitter itself — the part that actually runs — is about twenty lines, and it holds no state beyond a single Map from event name to array of handlers.

What is worth standing in front of is how little it defends itself. on pushes. off splices. emit iterates a copy. There is no unsubscribe token, no once-only helper, no priority ordering, no error boundary around handlers, no check that you passed a function. Every one of those is a feature someone has certainly asked for, and every one of them is absent.

That absence is the design. The published bundle is around 200 bytes, and it stays there because each refusal was deliberate. The emit handler list is copied with .slice() before iteration — the one piece of defensiveness that survived the cull, because without it a handler that unsubscribes during dispatch corrupts the loop it is standing in. Knowing which single guard to keep is harder than adding all of them.

Note also the wildcard '*' handler threaded through the same map as named events. It costs one extra branch in emit and no extra data structure at all. A larger library would have given wildcards their own registry.