1.25.8 • Published 7 months ago

taverne v1.25.8

Weekly downloads
834
License
MIT
Repository
github
Last release
7 months ago

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!
1.25.8

7 months ago

1.25.7

7 months ago

1.25.6

3 years ago

1.25.0

3 years ago

1.25.1

3 years ago

1.25.4

3 years ago

1.25.5

3 years ago

1.25.2

3 years ago

1.25.3

3 years ago

1.24.14

3 years ago

1.24.15

3 years ago

1.24.13

3 years ago

1.24.12

3 years ago

1.24.11

3 years ago

1.23.6

3 years ago

1.23.7

3 years ago

1.23.8

3 years ago

1.24.10

3 years ago

1.24.1

3 years ago

1.24.2

3 years ago

1.24.0

3 years ago

1.24.5

3 years ago

1.24.6

3 years ago

1.24.3

3 years ago

1.24.4

3 years ago

1.24.9

3 years ago

1.24.7

3 years ago

1.24.8

3 years ago

1.21.0

3 years ago

1.23.2

3 years ago

1.23.3

3 years ago

1.23.0

3 years ago

1.23.1

3 years ago

1.23.4

3 years ago

1.23.5

3 years ago

1.22.0

3 years ago

1.19.1

3 years ago

1.20.0

3 years ago

1.19.0

3 years ago

1.18.1

3 years ago

1.17.0

3 years ago

1.16.14

3 years ago

1.16.13

3 years ago

1.16.12

3 years ago

1.16.11

3 years ago

1.16.10

3 years ago

1.16.9

3 years ago

1.16.8

3 years ago

1.16.7

3 years ago

1.16.3

3 years ago

1.16.2

3 years ago

1.16.1

3 years ago

1.16.0

3 years ago

1.15.8

3 years ago

1.16.6

3 years ago

1.16.5

3 years ago

1.16.4

3 years ago

1.15.7

3 years ago

1.15.6

3 years ago

1.15.5

3 years ago

1.15.4

3 years ago

1.15.3

3 years ago

1.15.2

3 years ago

1.15.1

3 years ago

1.15.0

3 years ago

1.14.1

3 years ago

1.14.0

3 years ago

1.13.2

3 years ago

1.13.1

3 years ago

1.13.0

3 years ago

1.12.1

3 years ago

1.12.0

3 years ago

1.13.6

3 years ago

1.13.5

3 years ago

1.13.4

3 years ago

1.13.3

3 years ago

1.13.7

3 years ago

1.11.0

3 years ago