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

use-channel

v1.2.1

Published

Take over state transmission in React.

Downloads

97

Readme

use-channel

Easy two-way state and no complexity growth in cross levels. Zero-Dependency & Intuitive API & Fully Typed.

GitHub Actions Workflow Status codecov NPM Version

为了简化数据逆向传递及数据跨级穿透传递,建立了通道事件模型。该模型内,不显式关心数据传递的方向,而是以数据源和数据受体为核心建立数据流。

  • 正向事件,即由事件源组件传递到事件接受组件,以on开头,例如onChangeSearch
  • 逆向事件,即由事件接受组件传递到事件源组件,以$on开头,例如$onChangeSearch
  • 为了简化,你可以借助帮助类型TwoWayChannel进行定义。
  • 提供useChannelState, useChannelRef状态钩子,避免凌乱的事件调用。
  • 提供valtio等状态库进行状态同步方法(欢迎提交其他状态库PR)。

In order to simplify the reverse transmission of data and the transmission of data across layers, a channel event model is established. In this model, the direction of data transmission is not explicitly concerned, but the data flow is established with the data source and data receptor as the core.

  • Forward events, that is, events transmitted from the event source component to the event receiving component, start with on, such as onChangeSearch.
  • Reverse events, that is, events transmitted from the event receiving component to the event source component, start with $on, such as $onChangeSearch.
  • For simplicity, you can define with the help of the help type TwoWayChannel.
  • Provide hooks such as useChannelState to avoid messy event calls.
  • Provide utilities to synchronize with state management such as valtio(PRs for others are welcome).

Usage

See more in examples' demo OR clone the repo and pnpm install && pnpm run dev

// for type checks / intellisense
type CounterChannel = TwoWayChannel<{
  onChangeCount: (value: number) => void
  onChangeValue: (value: number) => void
  onChangeValueProp: (value: number) => void
  // + $onChangeCount: (value: number) => void
  // + $onChangeValue: (value: number) => void
}, 'onChangeCount' | 'onChangeValue'>

export function Starter() {
  // create channel (ref-optimized)
  const channel = useChannel<CounterChannel>('Starter') // name is unused yet.

  const { value, setValue } = useChannelSourceStateSync(channel, 'value', 0)
  // actually: channel.listen('$onChangeValue') & channel.send('onChangeValue')

  const { valueProp, setValueProp } = useChannelSourceState(channel, 'valueProp', 0)
  // actually: channel.send('onChangeValueProp')

  const { count, setCount } = useChannelExternalStateSync(channel, 'count', 0)
  // actually: channel.listen('onChangeCount') & channel.send('$onChangeCount')

  const { count: privateCount, setCount: setCountPrivately } = useChannelExternalState(channel, 'count')
  // actually: channel.listen('onChangeCount')

  return (
    <>
      ...
      <Counter channel={channel} /> // Pass channel, for be connected
    </>
  )
}

export function Counter({ channel: rcvChannel }: { channel: Channel<CounterChannel> }) {
  // connect external channel, return type is already mixed.
  const channel = useChannel('Counter').connect(rcvChannel)

  const { count, setCount } = useChannelSourceStateSync(channel, 'count')
  // actually: channel.listen('$onChangeCount') & channel.send('onChangeCount')

  const { value, setValue } = useChannelExternalStateSync(channel, 'value')
  // actually: channel.listen('onChangeValue') & channel.send('$onChangeValue')

  const { value: privateValue, setValue: setValuePrivately } = useChannelExternalState(channel, 'value')
  // actually: channel.listen('onChangeValue')

  const { valueProp, setValueProp } = useChannelExternalState(channel, 'valueProp')
  // actually: channel.listen('onChangeValueProp')
}

API

See details in core.ts

  • type TwoWayChannel<T>
type MyChannel = TwoWayChannel<{
 onChangeValue: (value: string) => void
 // + $onChangeValue: (value: string) => void
}>
  • type TwoWayChannel<T, K>
type MyChannel = TwoWayChannel<{
 onChangeValue: (value: string) => void
 onChangeValue2: (value: string) => void
 // + $onChangeValue: (value: string) => void
}, 'onChangeValue'>
  • type PropsWithChannel<T, P>

  • useChannel<MyChannel>(ChannelName?:string): Channel

  • channel.connect(channel:Channel): Channel

Tip: It's uncommon to use event listener/emitter directly, State Hooks/Ref Hooks instead.

  • channel.listen<K extends keyof MyChannel>(eventName:K, (...value: Parameters<MyChannel[K]>) => void)

  • channel.send<K extends keyof MyChannel>(eventName:K, ...value: Parameters<MyChannel[K]>)

State

Note: Only first parameter in listener is used in state.

  • useChannelSourceState(channel:Channel, name:string, initialValue?)

  • useChannelSourceStateSync(channel:Channel, name:string, initialValue?)

  • useChannelExternalState(channel:Channel, name:string, initialValue?)

  • useChannelExternalStateSync(channel:Channel, name:string, initialValue?)

  • type ChannelStateType

  • useChannelState(type:ChannelStateType channel:Channel, name:string, initialValue?), Prefer to use above distinct hooks.

Ref

Note: Only first parameter in listener is used in ref.

  • useChannelRef(type:ChannelStateType channel:Channel, name:string, initialValue?)
// followings are equivalent:
useChannelRef(channel, 'value')
useChannelRef(channel, 'value', { dispatch: false })
useChannelRef(channel, 'value', { dispatch: false, source: false })

See example

valtio

import { useChannelValtio } from 'use-channel/valtio'
// followings are equivalent:
useChannelValtio(channel, store, ['count'])
useChannelValtio(channel, store, [['count', 'count']]) // channel#count -> store.count
useChannelValtio(channel, store, [['count', 'count', { subscribe: true, listen: true }]])

LICENSE

MIT