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

taverne

v1.25.8

Published

Elementary Flux implementation for your React state management

Downloads

41

Readme

La Taverne

La Taverne is an elementary Flux implementation to manage a global app state.

It provides an optional, yet easy integration with React using custom hooks.

action->dispatcher->store->view

🕵️ Demo

📦 installation

> npm i --save taverne

🐿️ Instanciate your taverne with your barrels

Once your barrels are ready, you can instanciate your taverne and dispatch:

import createLaTaverne from 'taverne';
import books from './barrels/books';
import potions from './barrels/potions';
import handcrafts from './barrels/handcrafts';

const {dispatch, taverne} = createLaTaverne({
  books,
  potions,
  handcrafts
});

🧬 Create a barrel

A "Barrel" is an initialState and a list of reactions.

const ADD_BOOK = 'ADD_BOOK';

const addBook = {
  on: ADD_BOOK,
  reduce: (state, payload) => {
    const {book} = payload;
    state.entities.push(book);
  }
};

export default {
  initialState: {entities: []},
  reactions: [addBook]
};

export {ADD_BOOK};

🧚 Reactions

  • A reaction will be triggered when an action is dispatched with action.type === on.
const doSomethingWithThisBarrel = {
  on: 'ACTION_TYPE',
  reduce: (state, payload) => {
    /*
      Just update the state with your payload.
      Here, `state` is the draftState used by `Immer.produce`
      You taverne will then record your next immutable state.
    */
    state.foo = 'bar';
  },
  perform: (parameters, dispatch, getState) => {
    /*
      Optional sync or async function.
      It will be called before `reduce`

      When it is done, reduce will receive the result in
      the `payload` parameter.

      You can `dispatch` next steps from here as well
    */
  }
};
  • reduce is called using Immer, so mutate the state exactly as you would with the draftState parameter in produce.

  • If you have some business to do before reducing, for example calling an API, use the perform function, either sync or async.

    Then reduce will be called with the result once it's done.

🎨 React integration

La Taverne has a context Provider <Taverne> which provides 2 utilities:

  • the pour hook to access your global state anywhere
  • the dispatch function
/* src/app.js */
import React from 'react';
import {render} from 'react-dom';
import {Taverne} from 'taverne/hooks';

render(
  <Taverne dispatch={dispatch} taverne={taverne}>
    <App id={id} />
  </Taverne>,
  container
);
/* src/feature/books/container.js */
import {useTaverne} from 'taverne/hooks';

const BooksContainer = props => {
  const {dispatch, pour} = useTaverne();
  const books = pour('books');

  return <BooksComponent books={books} />;
};

See the complete React integration steps here.

You can "pour" specific parts of the "taverne", to allow accurate local rendering from your global app state.

🔆 Middlewares

You can create more generic middlewares to operate any actions.

Your middlewares must implement onDispatch: (action, dispatch, getState) => {}

const customMiddleware = taverne => {
  const instance = {
    onDispatch: (action, dispatch, getState) => {}
  };

  return instance;
};

Then instanciate La Taverne with your list of middlewares as 2nd parameter:

const {dispatch, taverne} = createLaTaverne(barrels, [customMiddleware]);

example: plugging the redux devtools extension with this middleware

🐛 Redux devtools

Using devtools with La Taverne provides debugging without losing performance:

  • gathered debounced actions
  • nested actions
  • optional state filtering to improve performance
import createLaTaverne from 'taverne';
import {createDevtools} from 'taverne/middlewares';
import books from './barrels/books';

const devtools = createDevtools();
const {dispatch, taverne} = createLaTaverne({books}, [devtools]);

When your app state is too big, you'll hit performance issues with Redux dev tools.

In this case you may need to skip part of state from tracking;

const devtools = createDevtools({
  applyStateFiltering? : state => ({
    ...state,
    hugeObject: '<skipped>'
  })
});

🏗️ development

  • 📓 Few local dev notes for the curious ones.
  • ✅ Issues and PR Welcomed!