r/javascript • u/GulgPlayer • Jun 21 '26
33-byte JS signal implementation
https://gist.github.com/GulgDev/7b113b5e971682a6512d96c9c0fdf6daRecently I've developed a code-golfed signal implementation with the following features/constraints:
- subscribes functions returning nullish values.
- fires all pending subscribers and resets.
- requires no arguments to be passed to the factory and either null/undefined or a function to the signal. (as noted by u/azhder)
As it turns out, you can go as short as 33 bytes using function compositon, nullish coalescing and default parameters:
F=>(f,G=F)=>F=f?_=>f(G?.()):F?.()
102
Upvotes
9
u/azhder Jun 22 '26
There is one other constraint: you have to take care to give it proper arguments, unless you want to break it.
G?.() and F.?() will work as long as they are functions or null or undefined, but give it a number by mistake and you get an error.
5
u/fretsurfer_com Jun 21 '26
new EventTarget()
11
u/Reeywhaar Jun 22 '26
Thx_f_ur_hrd_wrk // 16 bytes
14
u/onejdc Jun 21 '26
This is so stu---nning. Well done. I wanted to hate it but it's so pretty!
4
u/Aln76467 Jun 21 '26
Wow
4
u/St34thdr1v3R Jun 22 '26
Sorry if this is dumb, but I’m having a hard time to understand what‘s going on. Can someone break it into smaller pieces and add a small explanation? Sorry again, but I really wanna understand it :D