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

@sanalabs/y-redux

v0.1.13

Published

Redux state synced with Yjs

Downloads

5,769

Readme

@sanalabs/y-redux

This package exports two React components:

  • SyncYJson: Two-way synchronization of a deep YMap/YArray and a Redux state.
  • SyncYAwareness: Synchronization of YDoc awareness states (remote and local) and a Redux state.

Table of Contents

SyncYJson

This is a two-way synchronization of a Redux state and a YMap/YArray. When SyncYJson is mounted it keeps the state in sync by:

  1. Listening for changes to the YType (observeDeep) and writing them to the Redux state (dispatching an action).
  2. Listening for changes to the Redux state (useSelector) and writing them to the YType (Yjs mutation operations).

The YType can be a deep structure containing YMaps, YArrays and JSON primitives.

The Yjs mutations are batched into a transaction.

Retaining referential equality whenever possible

Retaining object references for parts of the state that didn't change is important for performance and allows the caching mechanism of Redux selectors to function correctly.

  • Redux to Yjs: SyncYJson uses patchYJson which creates Yjs operations only for the part of the state that changed.
  • Yjs to Redux: This can be done by using deepPatchJson (exported from @sanalabs/json) in the reducer that applies the Redux updates. See example (TODO).

Usage example

export const App = () => {
  const { yMap, yProvider } = useMemo(() => {
    const yProvider = new YjsProvider() // Eg. HocuspocusProvider or WebrtcProvider
    const yMap = yProvider.document.getMap('data')
    return { yMap, yProvider }
  }, [])

  useEffect(
    () => () => {
      yProvider.destroy()
    },
    [yProvider]
  )

  return (
    <SyncYJson
      yMap={yMap} // YMap to be observed for remote changes by yMap.observeDeep
      setData={setData} // Action creator to be called as dispatch(setData(data))
      selectData={selectData} // Selector to be used as useSelector(selectData)
    />

    // Inside other components you can interact with the synced data as with any normal
    // Redux state by using dispatch and useSelector and it will be seamlessly kept in sync.
    <OtherComponent />
  )
}

SyncYAwareness

Very similar to SyncYJson

FAQ

Why is SyncYJson a component and not a hook?

For performance and convenience. It makes no difference to the consumer of this API since SyncYJson doesn't return anything. Think of the component as a provider component.

The performance issue with hooks is that any time an effect within a hook runs, that triggers a re-render of the surrounding component. Since the hooks within SyncYJson may trigger very often due to remote changes we noticed that it was not convenient to have the functionality as a hook.

Why is SyncYJson a React component and not a more generic Redux integration?

Having this logic as a first class citizen in React makes it easy to control when to use SyncYJson and to have multiple instances for different parts of your application.