from Hacker News

Show HN: JavaScript PubSub in 163 Bytes

by hmmokidk on 3/31/25, 1:37 AM with 36 comments

  • by sltkr on 4/1/25, 10:39 AM

    The API feels wrong. The object that was passed to pub() is the object that should be received by the callback passed to sub().

    The use of EventTarget/CustomEvent is an implementation detail; it should not be part of the API.

    As a result, every callback implementation is larger because it must explicitly unwrap the CustomEvent object.

    Essentially, the author made the library smaller by pushing necessary code to unwrap the CustomEvent object to the callsites. That's the opposite of what good libraries do!

    The mentioned nano-pubsub gets this right, and it even gets the types correct (which the posted code doesn't even try).

  • by zeroq on 4/1/25, 3:42 PM

    In similar spirit, a minimal implemention of KV store, in 22 bytes:

      export default new Map
  • by arnorhs on 4/1/25, 11:53 AM

    I'm not a huge fan of using CustomEvent for this.. esp. in terms of interoperability (which for these <kb challenges probably doesnt matter)

    personally, i'll just roll with something like this which also is typed etc:

        export function createPubSub<T extends readonly any[]>() {
          const l = new Set<(...args: T) => void>()
    
          return {
            pub: (...args: T) => l.forEach((f) => f(...args)),
            sub: (f: (...args: T) => void) => l.add(f) && (() => l.delete(f)),
          }
        }
    
        // usage:
        const greetings = createPubSub<[string]>()
        const unsubscribe = greetings.sub((name) => {
          console.log('hi there', name)
        })
        greetings.pub('Dudeman')
        unsubscribe()
  • by est on 4/1/25, 8:31 AM

  • by test1072 on 4/1/25, 10:26 AM

    Perhaps "eventlistener" word can be extracted, and dynamically called as string to reduce bytes
  • by giancarlostoro on 4/1/25, 1:56 PM

    So why would I use this as opposed to BroadcastChannel?
  • by thewisenerd on 4/1/25, 7:05 PM

    good to know pub-sub shenanigans are ubiquitous lol

    here's my implementation from a while back with `setTimeout` like semantics; used it to avoid prop-drilling in an internal dashboard (sue me)

    https://gist.github.com/thewisenerd/768db2a0046ca716e28ff14b...

  • by nsonha on 4/1/25, 9:12 AM

    is this like left-pad but for EventTarget? If being small is the PRIMARY goal, then we are already able to do it without a wrapper.
  • by pjc50 on 4/1/25, 8:57 AM

    This is local pubsub within an application, right? i.e. corresponding to C#'s 'event' keyword.
  • by h1fra on 4/1/25, 9:16 AM

    sure if you remove the whole native package it's small
  • by lerp-io on 3/31/25, 8:19 AM

    should this copy paste macro even be a package lol
  • by tipiirai on 4/1/25, 2:18 PM

    Thanks! Definitely going to use `new EventTarget()` in Nue. So obvious.

    https://nuejs.org/

  • by blatantly on 4/1/25, 9:49 AM

    23 byte version:

        // Lib code>>
        s={};call=(n)=>{s[n]()}
        // <<
    
        s.hello=()=>console.log('hello');
        call('hello');
        delete s.hello;