npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

subscribe-proxy

v0.1.2

Published

Client-side latest-value cache and I/O occupancy for push/pull topic subscriptions.

Readme

subscribe-proxy

客户端最新值缓存 + I/O 占用,不是 broker。与 React 无关。

内部是两套互不干扰的引擎:PushEngine 与 PullEngine。每套按 topic 缓存最新一份 SPPayload,占用归零即丢掉。同一 topic 可以有多份 subscription 占用;subscription 对象就是占用身份。

Push 的 put 通知每一位占用者。Pull 里 pullIntervalMs > 0 的占用每次 put 都通知;省略或 ≤0 只收第一帧(缓存回放或之后第一次成功的 put),后续 put 不再推给这份占用。

安装

npm install subscribe-proxy

本地联调可用 file: 协议指向本仓库。

用法

import { createSubscribeProxy } from 'subscribe-proxy'

type PowerSample = { kw: number }

const proxy = createSubscribeProxy({ dataSource })

const subscription = proxy.subscribe<PowerSample>({
  topic: 'chart.power',
  method: 'pull',
  pullIntervalMs: 1000,
  onMessage: (payload) => {
    console.log(payload.data, payload.time)
  },
})

subscription.unsubscribe()
proxy.dispose()

一次 subscribe 只订一个 topic。method 决定进哪套引擎。subscribe<TData> 声明这次占用的 payload.data 类型;省略则为 unknown。成功且该 topic 仍有占用(因而仍有缓存)时,立刻 onMessage 一次当前值。之后每次 put 再调一次。没有读口:数据只从 onMessage 来。method 不是 push / pull 会抛错。空 topic 返回空订阅,不占位、不开 I/O。

DataSource

调用方提供同一个 dataSource,两套引擎各取所需:

type SPDataSource = {
  subscribe: (topic: string, onMessage: (payload: SPPayload) => void) => () => void
  fetch: (topic: string) => Promise<SPPayload>
}

type SPPayload = {
  data: unknown
  time: number
}

Push 只用 subscribe(一次一个 topic),Pull 只用 fetch。某 topic 占用从 0→1 才开 I/O,最后一份退订才停 I/O 并丢掉缓存;第二份占用不重绑远端。put 写入缓存时不做相等跳过、不做浅拷贝;写入后把 payload 当不可变。无人占用时迟到的 put 直接丢弃。

Pull 间隔

pullIntervalMs 只属于 pull 请求。省略或 ≤0 只拉一次、不轮询,且 onMessage 只收第一帧。同一 topic 多份 pull 占用时,按大于 0 的最小间隔拉。

结构

Client  -- proxy.subscribe({ method, topic }) -->  SubscribeProxy
                                                    PushEngine  -- dataSource.subscribe(topic)
                                                    PullEngine  -- dataSource.fetch(topic)

同一 topic 可以同时存在于两套引擎,互不覆盖。

观察面

业务页不要碰。调试可先取内部引擎:

const { pushEngine, pullEngine } = proxy.getInternal()

const stop = pushEngine.watch((event) => {
  // put | subscribe | unsubscribe | remote-start | remote-stop | pull-plan
})

const state = pullEngine.getDebugState()
// 直接返回内部表,不复制:payloads / occupancy / applied / epochs / waitingTopics

watch 返回取消函数。getDebugState() 返回的 Map / Set 是引擎内部引用。

构建

npm install
npm run build

示例

demo/ 是独立 Vite 应用,不随包发布。改库可直接热更新:

npm run demo