Skip to content Skip to footer
Show HN: JavaScript PubSub in 163 Bytes by hmmokidk

Show HN: JavaScript PubSub in 163 Bytes by hmmokidk

14 Comments

  • Post Author
    lerp-io
    Posted March 31, 2025 at 8:19 am

    should this copy paste macro even be a package lol

  • Post Author
    est
    Posted April 1, 2025 at 8:31 am
  • Post Author
    RazorDev
    Posted April 1, 2025 at 8:53 am

    [flagged]

  • Post Author
    pjc50
    Posted April 1, 2025 at 8:57 am

    This is local pubsub within an application, right? i.e. corresponding to C#'s 'event' keyword.

  • Post Author
    nsonha
    Posted April 1, 2025 at 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.

  • Post Author
    h1fra
    Posted April 1, 2025 at 9:16 am

    sure if you remove the whole native package it's small

  • Post Author
    blatantly
    Posted April 1, 2025 at 9:49 am

    23 byte version:

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

  • Post Author
    test1072
    Posted April 1, 2025 at 10:26 am

    Perhaps "eventlistener" word can be extracted, and dynamically called as string to reduce bytes

  • Post Author
    sltkr
    Posted April 1, 2025 at 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).

  • Post Author
    arnorhs
    Posted April 1, 2025 at 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()

  • Post Author
    giancarlostoro
    Posted April 1, 2025 at 1:56 pm

    So why would I use this as opposed to BroadcastChannel?

  • Post Author
    tipiirai
    Posted April 1, 2025 at 2:18 pm

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

    https://nuejs.org/

  • Post Author
    zeroq
    Posted April 1, 2025 at 3:42 pm

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

      export default new Map

  • Post Author
    curtisszmania
    Posted April 1, 2025 at 5:42 pm

    [dead]

Leave a comment

In the Shadows of Innovation”

© 2025 HackTech.info. All Rights Reserved.

Sign Up to Our Newsletter

Be the first to know the latest updates

Whoops, you're not connected to Mailchimp. You need to enter a valid Mailchimp API key.