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

preact-store-adapter

v0.4.4

Published

Connect stores and mutable sources to Preact the correct way.

Downloads

36

Readme

preact-store-adapter

Connect stores and mutable sources to Preact the correct way.

NPM JavaScript Style Guide

Install

yarn install preact-store-adapter

Usage

import {
  StoreAdapterRoot,
  createStoreAdapter,
  useStoreAdapter
} from 'preact-store-adapter';

// An example of a non-Preact global state
const store = Redux.createStore((state = 0, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1
    case 'DECREMENT':
      return state - 1
    default:
      return state
  }
});

// Create our store adapter
const storeAdapter = createStoreAdapter({
  // How to read the store's state
  read: () => store.getState(),

  // how to subscribe to the store
  subscribe: (callback) => store.subscribe(callback),
});

// Simple reading
function Count() {
  // Read our state
  const count = useStoreAdapter(storeAdapter);

  return (
    <h1>Count: {count}</h1>
  );
}

// Transformed reading
function getSnapshot(count) {
  return `Count: ${count}`;
}

function TransformedCount() {
  const message = useStoreAdapter(storeAdapter, {
    getSnapshot,
  });

  return <h1>{message}</h1>;
}

// With state memoization
function shouldUpdate(value) {
  return value > 10;
}

function MemoizedCount() {
  // Will only start re-rendering after count > 10
  const count = useStoreAdapter(storeAdapter, {
    shouldUpdate,
  });

  return <h1>Count: {count}</h1>;
}

// Render our app
<StoreAdapterRoot>
  <Count />
  <TransformCount />
  <MemoizedCount />
</StoreAdapterRoot>

Features

State-agnostic Store Adapters

preact-store-adapter to provide a unified way for Preact to interact with different kinds of external states and mutable sources (much like what React's Mutable Source's future goal is.)

createStoreAdapter(options) requires two fields:

  • read(): State: Provides a way for the store adapter to access the current state of the store.

  • subscribe(callback: Function): Function | undefined: Provides a way for the store adapter to subscribe to the store and receive further updates. It accepts a callback value that is subscribed to the store and must return a cleanup callback which is used to unsubscribe from the store.

  • shouldUpdate(prev: State, next: State): boolean: Optional. Tells when the store adapter should update the tracked state. This is useful for emulating immutable states equality (deep comparison). Defaults to Object.is.

  • keepAlive: boolean: Prevents automatic store cleanup. Defaults to false.

createStoreAdapter then returns a store adapter instance which is used in the Preact environment (specifically, useStoreAdapter).

Hook

useStoreAdapter(storeAdapter) is a hook that provides a way for Preact components to consume states and updates from the external sources. useStoreAdapter accepts a store adapter instance as the first parameter and returns the safe state of the external store. It may also optionally accept an options object as the second parameter:

  • getSnapshot(currentState: State): NewState: provides a way to transform the safe state. It receives the safe state and must return a new state derived from the safe state.
  • shouldUpdate(prev: State, next: State): boolean: tells when should the component update.

Root

<StoreAdapterRoot> is the core of preact-store-adapter. It handles the communication between your Preact app and the mutable sources and also keeps track of safe states. The component must always be found in the root of your Preact app. If there are multiple instances, preact-store-adapter attemps to only implement a single instance per Preact tree.

Safe-state tracking

preact-store-adapter internally keeps track of which state provided by the external source is safe for the UI components to use. This is to prevent UI tearing where components receive different values unexpectedly.

UI correctness

preact-store-adapter detects whenever there's a potential tear in the component, allowing for the component to re-render again with the potential safe state.

Dispatch correctness

preact-store-adapter attempts to correct when stores are dispatched. Different timings and schedules of store dispatches may affect how Preact apps work, which in turn can cause unexpected UI-related issues.

Selective state updates

preact-store-adapter provides a way to transform and filter state, which adds a benefit for the UI to save render time specially if the state is considered equal.

Automatic store cleanup

To prevent memory leaks, preact-store-adapter automatically cleans up store instance internally if those stores has no listeners. createStoreAdapter can accept keepAlive: true to prevent automatic cleanup.

Examples

Redux

import { createStore } from 'redux';

// An example of a non-Preact global state
const store = createStore((state = 0, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1;
    case 'DECREMENT':
      return state - 1;
    default:
      return state;
  }
});

// Create our store adapter
const storeAdapter = createStoreAdapter({
  // How to read the store's state
  read: () => store.getState(),

  // how to subscribe to the store
  subscribe: (callback) => store.subscribe(callback),
});

License

MIT © lxsmnsyc