TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: JavaScript PubSub in 163 Bytes

104 点作者 hmmokidk大约 1 个月前

13 条评论

sltkr大约 1 个月前
The API feels wrong. The object that was passed to pub() is the object that should be received by the callback passed to sub().<p>The use of EventTarget&#x2F;CustomEvent is an implementation detail; it should not be part of the API.<p>As a result, every callback implementation is larger because it must explicitly unwrap the CustomEvent object.<p>Essentially, the author made the library smaller by pushing necessary code to unwrap the CustomEvent object to the callsites. That&#x27;s the opposite of what good libraries do!<p>The mentioned nano-pubsub gets this right, and it even gets the types correct (which the posted code doesn&#x27;t even try).
评论 #43549483 未加载
评论 #43547717 未加载
评论 #43549971 未加载
zeroq大约 1 个月前
In similar spirit, a minimal implemention of KV store, in 22 bytes:<p><pre><code> export default new Map</code></pre>
arnorhs大约 1 个月前
I&#x27;m not a huge fan of using CustomEvent for this.. esp. in terms of interoperability (which for these &lt;kb challenges probably doesnt matter)<p>personally, i&#x27;ll just roll with something like this which also is typed etc:<p><pre><code> export function createPubSub&lt;T extends readonly any[]&gt;() { const l = new Set&lt;(...args: T) =&gt; void&gt;() return { pub: (...args: T) =&gt; l.forEach((f) =&gt; f(...args)), sub: (f: (...args: T) =&gt; void) =&gt; l.add(f) &amp;&amp; (() =&gt; l.delete(f)), } } &#x2F;&#x2F; usage: const greetings = createPubSub&lt;[string]&gt;() const unsubscribe = greetings.sub((name) =&gt; { console.log(&#x27;hi there&#x27;, name) }) greetings.pub(&#x27;Dudeman&#x27;) unsubscribe()</code></pre>
评论 #43546240 未加载
评论 #43546172 未加载
评论 #43553167 未加载
评论 #43553430 未加载
est大约 1 个月前
TIL CustomEvent<p><a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;CustomEvent&#x2F;CustomEvent" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;CustomEvent...</a>
评论 #43546799 未加载
test1072大约 1 个月前
Perhaps &quot;eventlistener&quot; word can be extracted, and dynamically called as string to reduce bytes
评论 #43556353 未加载
评论 #43553092 未加载
评论 #43547121 未加载
评论 #43548273 未加载
giancarlostoro大约 1 个月前
So why would I use this as opposed to BroadcastChannel?
评论 #43547041 未加载
thewisenerd大约 1 个月前
good to know pub-sub shenanigans are ubiquitous lol<p>here&#x27;s my implementation from a while back with `setTimeout` like semantics; used it to avoid prop-drilling in an internal dashboard (sue me)<p><a href="https:&#x2F;&#x2F;gist.github.com&#x2F;thewisenerd&#x2F;768db2a0046ca716e28ff14be6c28538" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;thewisenerd&#x2F;768db2a0046ca716e28ff14b...</a>
评论 #43554287 未加载
nsonha大约 1 个月前
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.
评论 #43545847 未加载
评论 #43544692 未加载
pjc50大约 1 个月前
This is local pubsub within an application, right? i.e. corresponding to C#&#x27;s &#x27;event&#x27; keyword.
评论 #43545443 未加载
h1fra大约 1 个月前
sure if you remove the whole native package it&#x27;s small
lerp-io大约 1 个月前
should this copy paste macro even be a package lol
评论 #43533216 未加载
评论 #43533105 未加载
tipiirai大约 1 个月前
Thanks! Definitely going to use `new EventTarget()` in Nue. So obvious.<p><a href="https:&#x2F;&#x2F;nuejs.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;nuejs.org&#x2F;</a>
blatantly大约 1 个月前
23 byte version:<p><pre><code> &#x2F;&#x2F; Lib code&gt;&gt; s={};call=(n)=&gt;{s[n]()} &#x2F;&#x2F; &lt;&lt; s.hello=()=&gt;console.log(&#x27;hello&#x27;); call(&#x27;hello&#x27;); delete s.hello;</code></pre>
评论 #43544876 未加载