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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@tizoc/refractive

v0.0.7

Published

Lenses and tracked selectors enhancer and hooks for Reductive

Readme

Refractive

npm version

Lenses and tracked selectors hooks for reductive.

A PPX rewriter that generates lenses and selectors for record types can be found here.

NOTE

This is proof of concept at the moment, and while it works, the API is not stable.

Preview

module Store = {
  // Optional: with the [@refractive.derive] annotation the `Lenses` and `Selectors`
  // modules will be autogenerated. Not used in this example.
  // [@refractive.derive]
  type state = {counter: int};

  type action =
    | Increment
    | Decrement;

  // Lenses specify how to read and update part
  // of the state in a functional way.
  module Lenses = {
    let counter =
      Refractive.Lens.make(
        ~get=s => s.counter,
        ~set=(newVal, s) => {...s, counter: newVal},
      );
  };

  // Selectors are the combination of a Lense and
  // a path to the value it affects.
  module Selectors = {
    let counter =
      Refractive.Selector.make(~lens=Lenses.counter, ~path=[|"counter"|]);
  };

  // Module for tracked selectors and updates
  // This module's `modify` and `set` functions must be used to update the state.
  include Refractive.TrackedSelector.Make({});

  // Reducer function, updates must be made using the `modify` and `set` functinos
  // from the instantiated tracked selectors module.
  let reducer = (state, action) => {
    Selectors.(
      switch (action) {
      | Increment => modify(counter, n => n + 1, state)
      | Decrement => modify(counter, n => n - 1, state)
      }
    );
  };

  let store =
    Reductive.Store.create(~reducer, ~preloadedState={counter: 0}, ());
};

// This module contains the `useDispatch` and `useSelector` hooks
// and a Provider component that provides the necessary context
// for those hooks.
// Instantiating this module also creates a new subscription
// for the selectors.
module StoreContext = Refractive.Context.Make(Store);

module AppComponent = {
  open StoreContext.Hooks; // useDispatch and useSelector

  [@react.component]
  let make = () => {
    let dispatch = useDispatch();
    let handleIncr = React.useCallback0(_ => dispatch(Store.Increment));
    let handleDecr = React.useCallback0(_ => dispatch(Store.Decrement));
    let value = useSelector(Store.Selectors.counter);

    <StoreContext.Provider>
      <span> {React.string(string_of_int(value))} </span>
      <button onClick=handleIncr> {React.string("+")} </button>
      <button onClick=handleDecr> {React.string("-")} </button>
    </StoreContext.Provider>;
  };
};

ReactDOMRe.renderToElementWithId(<AppComponent />, "app-root");

See examples/ directory for more examples.

The problem

The way Redux and Reductive work, every component that is interested in part of the store's state value subscribes to changes to the store, and receives a notification after an action has been dispatched. But most components care only about a small part of the store state, and most of the time it doesn't change often. This means that a lot of unnecessary notifications are executed. When there aren't too many components subscribed to the store, this is not an issue, but as more components get subscribed to the store, more overhead accumulates.

Refractive's solution

Refractive avoids this problem by subscribing to changes to the stoure through selectors that encode a path to the part of the store's state that the component is interested in. Reducer functions update the store using selectors and the special set and modify functions. Modifications made using these functions are tracked, and only the components that are subscribed to values affected during the reducing function execution are notified of the change.

Examples

To run the example:

npm install
npm run build
npx webpack

and then open index.html of any of the examples under the examples/ directory in your browser.