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

react-external-subject

v0.4.3

Published

Wrap mutable sources into React-safe mutable source

Downloads

26

Readme

react-external-subject

Turn third-party mutable sources into React-safe source

NPM JavaScript Style GuideOpen in CodeSandbox

Install

yarn add react-external-subject

Usage

import {
  ExternalSubjectSynchronizer,
  createExternalSubject,
  useExternalSubject,
  } from 'react-external-subject';

// An example of a mutable source
let source = 0;

// Wrap our mutable source into an external subject
const subject = createExternalSubject({
  read: () => source,
});

// Create a component that reads from our mutable source
function ObserverA() {
  const value = useExternalSubject(subject, false);
  return <h1>Value: {value}</h1>;
}

// Create a component similar to ObserverA but suspends
function ObserverB() {
  const value = useExternalSubject(subject, true);
  return <h1>Value: {value}</h1>;
}

// Create a component with a render-time side-effect
function Effectful() {
  // Update our source
  source = 1337;
  return <h1>Effectful: {source}</h1>;
}

function App() {
  return (
    <ExternalSubjectSynchronizer>
      <ObserverA />
      <Effectful />
      {/*
        By the time ObserverB renders, the source has already 
        updated, creating a "tear".
      */}
      <ObserverB />
    </ExternalSubjectSynchronizer>
  );
}

Features

react-external-subject is a library that wraps mutable sources not usually managed by React into a React-safe external subject.

When a component subscribes to an external subject, the external subject maintains and keeps track of a state that is safe for all components to share and render.

If it detects a tear in between renders, or between render and commit phase, the external subject asynchronously requests an update to the safe state and notifies all components to update their received value.

Components with suspense mode active will suspend until they receive the new safe state.

Memoization

External subjects may receive a memoization option through options.shouldUpdate. This function is used internally to compare the proposed state and the safe state. By default, it uses the Object.is function.

Subscription

External subjects may subscribe to a mutable source through options.subscribe. This function receives a callback that is used to subscribe to the source for receiving updates. options.subscribe may return a cleanup function, potentially for cleaning subscription.

Lazy Subscription

External subjects may lazily subscribe/unsubscribe through options.lazySubscribe. When set to true, subjects only begins subscribing to the mutable source when a listener is attached to them (e.g. through useExternalSubject.). If the subject has zero listeners, the subject automatically unsubscribes. By default, external subjects subscribes immediately on creation and never unsubscribes.

Hook

useExternalSubject(subject, suspense = false) is used to observe external subjects' state. If suspense is set to false, useExternalSubject will return the last safe state if it detects a tearing during render, otherwise, useExternalSubject suspends the component until the new safe state is updated. Regardless, useExternalSubject will always guarantee that the component will receive a state that is safe to render as well as all of the components will receive the same value, preventing the UI from tearing.

Alternatives

  • use-subscription provides a mechanism that allows the component to update immediately when it detects that the value has gone stale between render and commit phase. However, this does not prevent the component from tearing during render phase alone.
  • useMutableSource is an upcoming first-party React hook that wraps mutable sources into React-safe (Concurrent-safe, that is) mutable sources. The hook is also partially the basis of this whole library, the difference is that useMutableSource keeps tracks of the mutable source's version in contrast with useExternalSubject that keeps tracks the state of the mutable source. useMutableSource also works with the internal Fiber architecture to handle tearing.

License

MIT © lxsmnsyc