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 🙏

© 2024 – Pkg Stats / Ryan Hefner

xizhi-front-sse

v1.0.8

Published

悉之的react前端sse组件, 基于微软的fetch-event-source进一步封装, 将dom和sse事件的交互封装为组件, 减少直接的dom操作和事件的处理, 简化sse组件的使用

Downloads

14

Readme

xizhi-front-sse

悉之的react前端sse组件, 基于微软的fetch-event-source进一步封装, 将dom和sse事件的交互封装为组件, 减少直接的dom操作和事件的处理, 简化sse组件的使用

本项目中功能代码为apps/Stream下的内容, 其他内容主要为其提供简单模板, 方便调试

如果需要在项目中使用该组件, 请在项目中安装 xizhi-front-sse

pnpm add xizhi-front-sse

使用方法

以apps/template为例, 这是一个最简单的react项目

引入xizhi-front-sse中的FetchStream组件, 并绑定ref, 待会我们会调用

  const ref = useRef<FetchComponentRef>(null)

   <FetchStream
    ref={ref}
    CustomStreamItem={CustomStreamItem}>
  </FetchStream>

CustomStreamItem是流的每一项, 让我们可以自定义显示的内容

  const CustomStreamItem = (props) => {
    const { event } = props
    return <span>{event?.data?.content}</span>
  }
 

dom的配置就完成了, 接下来要做的就是调用ref.current.start()方法, 传入url, 以及配置参数和回调, 这个和fetch的参数是几乎一样的, 详情可以看fetch的文档, 稍有改动的是onmessage第二个值会返回当前所有eventList

    const url = 'http://localhost:3000'+'/stream'
    const defaultHeaders = {
      Authorization: 'token xxx',
    }
    ref.current.start(url, {
      headers: {
        ...defaultHeaders,
        // 传入希望携带的header
      },
      params: {
        // 传入希望携带的参数
      },
      onmessage(event, eventList) {
        // 需要的操作
      },
    })

为了简化操作, 可以通过ref.current.abort()来终止请求, ref.current.reset()重置请求到的内容, 但signal目前会被覆盖, 暂时不支持传入自定义的signal

示例

安装项目后启动dev服务即可, 可以查看template项目的效果

pnpm dev

另外apps/Server可以修改sse的返回内容

展望

希望大家能提出更好的建议, 帮助完善这个项目, 以方便于其他开发者, respect!~