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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Easy State Sync Across JavaScript Sandboxes (Frames, Workers, Threads, Webviews)

5 点作者 sourishkrout超过 3 年前

1 comment

rektide超过 3 年前
It&#x27;s not oriented specifically at state-sync&#x27;ing, but Comlink[1] comes to mind.<p>&gt; Comlink.wrap(endpoint) and Comlink.expose(value, endpoint?) <i>Comlink’s goal is to make exposed values from one thread available in the other. &#x27;expose&#x27; exposes &#x27;value&#x27; on endpoint, where endpoint is a postMessage-like interface. &#x27;wrap&#x27; wraps the other end of the message channel and returns a proxy. The proxy will have all properties and functions of the exposed value, but access and invocations are inherently asynchronous. This means that a function that returns a number will now return a promise for a number.</i><p>Creating &amp; exposing an EventEmitter in the main thread then proxy&#x27;ing it on each other thread ought create a reasonably similar experience to this Tangle library. The docs themselves discuss some of the extra difficulties if one is trying to proxy DOM Events[2], talking about this as a common usage pattern, but which is complicated by DOM Events not being cloneable.<p>For another example of Comlink in action, there&#x27;s this blog post on using Redux across threads[3].<p>[1] <a href="https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;comlink" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;comlink</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;GoogleChromeLabs&#x2F;comlink#transfer-handlers-and-event-listeners" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;GoogleChromeLabs&#x2F;comlink#transfer-handler...</a><p>[3] <a href="https:&#x2F;&#x2F;dassur.ma&#x2F;things&#x2F;react-redux-comlink&#x2F;" rel="nofollow">https:&#x2F;&#x2F;dassur.ma&#x2F;things&#x2F;react-redux-comlink&#x2F;</a>
评论 #30027481 未加载