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-zen

v0.2.0

Published

A collection of simple hooks, components and functions for React

Downloads

9

Readme

react-zen

A collection of simple utilities for React

yarn add react-zen

What?

Currently, react-zen contains just two utilities:

  • createMirror(fetcher)
  • useSnapshot(handle)

Together, these two functions let you easily consume asynchronous data in your React components. For example, here's how you'd load and display data from a REST API:

import { Suspense } from 'react'
import { createMirror, useSnapshot } from 'react-zen'

// A mirror automatically fetches data as it is required, and purges it
// once it is no longer in use.
const api = createMirror(async url => {
  let response = await fetch(BaseURL+url)
  return response.json()
})

function Screen() {
  // useSnapshot returns your data, loading status, etc.
  let { data } = useSnapshot(api.key('/todos/1'))
  return <div><input checked={data.completed} /> {data.title}</div>
}

function App() {
  return (
    // Make sure to wrap your any component that use `useSnapshot()`
    // with a <Suspense> tag.
    <Suspense fallback={<div>Loading</div>}>
      <Screen />
    </Suspense>
  )
}

Of course, you'll also want to be able to refresh and update your data. Mirrors and snapshots both have a suite of methods to make this easy. You can see how this works at the live example:

See a full featured example at CodeSandbox

API

useSnapshot()

Returns a snapshot of the data specified by the given handle.

export function useSnapshot(
  handle: MirrorHandle,
): {
  data: Data
  key: Key

  /**
   * Set to true after `invalidate` has been called, and stays true until a
   * more recent version of the document is received.
   */
  invalidated: boolean

  /**
   * Indicates that a fetch is currently taking place.
   */
  pending: boolean

  /**
   * Starts as false, and becomes true once `data` is set for the first time.
   */
  primed: boolean

  /**
   * Marks this key's currently stored snapshot as invalid.
   *
   * If there's an active subscription, a new version of the data will be
   * fetched.
   */
  invalidate(): void

  /**
   * Stores the given data. If there is no subscription for it, then the data
   * will be immediately scheduled for purge.
   *
   * In the case a function is given, if this key has a non-empty snapshot,
   * then the updater callback will be called and the returned value will be
   * set as the current data. If the key's snapshot is empty or is not yet
   * primed, then an error will be thrown.
   *
   * This will not change the `pending` status of your data.
   */
  update(dataOrUpdater: Data | MirrorUpdaterCallback): void
}

createMirror()

Create a mirror of the data in some asynchronous source, where data is automatically fetched as required, and purged when no longer needed.

createMirror(fetch: (
  snapshot: Snapshot,
  context: Context
) => Promise<Data>)

Mirror

The object returned by createMirror().

interface Mirror {
  /**
   * Return a handle for the specified key, from which you can get
   * and subscribe to its value.
   */
  key(key: string): MirrorKeyHandle

  /**
   * Return a handle for the specified keys, from which you can get
   * and subscribe to all their values at once.
   */
  key(keys: string[]): MirrorKeyListHandle

  /**
   * Return a list of the keys currently stored in the mirror for the given
   * deps array.
   */
  knownKeys(): Key[]
}

MirrorHandle

As returned by mirror.key(key) and mirror.keys(keys)

interface MirrorHandle {
  key: Key

  /**
   * Returns a promise to a mirrored snapshot for this key.
   */
  get(): Promise<MirrorPrimedSnapshot<Data>>

  /**
   * Returns the latest snapshot for the given data.
   */
  getLatest(): MirrorSnapshot<Data>

  /**
   * Subscribe to updates to snapshots for the given key. Note that this will
   * not immediately emit a snapshot unless subscribing triggers a fetch, and
   * adds/updates a snapshot in the process.
   */
  subscribe(
    callback: MirrorSubscribeCallback<Data, Key>,
  ): MirrorUnsubscribeFunction

  /**
   * Marks this key's currently stored snapshot as invalid.
   *
   * If there's an active subscription, a new version of the data will be
   * fetched.
   */
  invalidate(): void

  /**
   * Stores the given data. If there is no subscription for it, then the data
   * will be immediately scheduled for purge.
   *
   * In the case a function is given, if this key has a non-empty snapshot,
   * then the updater callback will be called and the returned value will be
   * set as the current data. If the key's snapshot is empty or is not yet
   * primed, then an error will be thrown.
   *
   * This will not change the `pending` status of your data.
   */
  update(dataOrUpdater: Data | MirrorUpdaterCallback): void
}

Contributing / Plans

A number of undocumented placeholder functions currently exist, which throw an exception when called. PRs implementing theses would be very welcome. Functions include:

  • mirror.keys(), which should allow you to get/subscribe to a list of keys at once.
  • mirror.hydrateFromState(), which should allow serialized data to be passed from the server to the client.
  • mirror.purge(), which should allow all data within a mirror to be immediately purged.
  • handle.predictUpdate(), which should allow for optimistic updates to be recorded against a key.

A number of other features have already been implemented, but need documentation and testing. These include namespaces and extractState(), both of which would be useful for server side rendering.

Once mirror.keys() has been implemented, it should be possible to create a CollectionMirror, which allows you to subscribe to queries/collections as well as individual records.

On other feature that would go a long way to improving real world use would configurable strategies for fetching invalidated/initial data -- instead of just giving up after the first fetch, as currently happens. Alongside this, it may make sense to have the ability to command a fetch, as opposed to just marking data as invalidated and allowing fetch to happen automatically.

License

react-zen is MIT licensed.