Satchel is a data store based on the Flux architecture. It is characterized by exposing an observable state that makes view updates painless and efficient.
Перейти к файлу
dependabot[bot] 20b9140739
Bump css-what from 2.1.0 to 2.1.3
Bumps [css-what](https://github.com/fb55/css-what) from 2.1.0 to 2.1.3.
- [Release notes](https://github.com/fb55/css-what/releases)
- [Commits](https://github.com/fb55/css-what/compare/v2.1.0...v2.1.3)

---
updated-dependencies:
- dependency-name: css-what
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-12-21 00:51:49 +00:00
.github/workflows Switch from Travis to GitHub Actions for CI and publishing (#152) 2021-05-26 16:15:41 -07:00
.vscode Build Infra Updates (#62) 2017-07-11 17:27:45 -07:00
src Use catch instead of finally for browser compatibility (#161) 2022-02-28 12:33:00 -08:00
test Use catch instead of finally for browser compatibility (#161) 2022-02-28 12:33:00 -08:00
.gitignore Add non-breaking ESM support (#167) 2022-07-27 13:21:48 -07:00
.npmignore Build Infra Updates (#62) 2017-07-11 17:27:45 -07:00
.prettierrc Update to latest version of prettier (#134) 2019-11-18 10:33:37 -08:00
CONTRIBUTING.md Add a basic CONTRIBUTING.md (#117) 2019-03-22 16:09:48 -07:00
LICENSE Init Commit 2016-08-02 08:39:16 -07:00
README.md Fix minor typo in README.md (#137) 2020-01-06 09:49:26 -08:00
SECURITY.md Microsoft mandatory file (#169) 2022-09-26 14:32:11 -07:00
jest.config.js Update jest in order to address vulnerable dependencies (#158) 2021-10-13 15:56:12 -07:00
package.json v4.3.1 (#174) 2022-12-20 16:50:58 -08:00
tsconfig.json Build Infra Updates (#62) 2017-07-11 17:27:45 -07:00
tsconfig.release-esm.json Add non-breaking ESM support (#167) 2022-07-27 13:21:48 -07:00
tsconfig.release.json Build Infra Updates (#62) 2017-07-11 17:27:45 -07:00
tslint.json Remove deprecated tslint rule 2017-07-12 11:21:57 -07:00
yarn.lock Bump css-what from 2.1.0 to 2.1.3 2022-12-21 00:51:49 +00:00

README.md

Satchel

Satchel is a dataflow framework based on the Flux architecture. It is characterized by exposing an observable state that makes view updates painless and efficient.

npm Build Status License: MIT

Influences

Satchel is an attempt to synthesize the best of several dataflow patterns typically used to drive a React-based UI. In particular:

  • Flux is not a library itself, but is a dataflow pattern conceived for use with React. In Flux, dataflow is unidirectional, and the only way to modify state is by dispatching actions through a central dispatcher.
  • Redux is an implementation of Flux that consolidates stores into a single state tree and attempts to simplify state changes by making all mutations via pure functions called reducers. Ultimately, however, we found reducers and immutable state cumbersome to deal with, particularly in a large, interconnected app.
  • MobX provides a seamless way to make state observable, and allows React to listen to state changes and rerender in a very performant way. Satchel uses MobX under the covers to allow React components to observe the data they depend on.

Advantages

There are a number of advantages to using Satchel to maintain your application state:

  • Satchel enables a very performant UI, only rerendering the minimal amount necessary. MobX makes UI updates very efficient by automatically detecting specifically what components need to rerender for a given state change.
  • Satchel's datastore allows for isomorphic JavaScript by making it feasible to render on the server and then serialize and pass the application state down to the client.
  • Satchel supports middleware that can act on each action that is dispatched. (For example, for tracing or performance instrumentation.)
  • Satchel is type-safe out of the box, without any extra effort on the consumer's part.

Installation

Install via NPM:

npm install satcheljs --save

In order to use Satchel with React, you'll also need MobX and the MobX React bindings:

npm install mobx --save

npm install mobx-react --save

Usage

The following examples assume you're developing in Typescript.

Create a store with some initial state

import { createStore } from 'satcheljs';

let getStore = createStore(
    'todoStore',
    { todos: [] }
);

Create a component that consumes your state

Notice the @observer decorator on the component—this is what tells MobX to rerender the component whenever the data it relies on changes.

import { observer } from 'mobx-react';

@observer
class TodoListComponent extends React.Component<any, any> {
    render() {
        return (
            <div>
                {getStore().todos.map(todo => <div>{todo.text}</div>)}
            </div>
        );
    }
}

Implement an action creator

Note that, as a convenience, Satchel action creators created with the action API both create and dispatch the action. This is typically how you want to use action creators. If you want to create and dispatch the actions separately you can use the actionCreator and dispatch APIs.

import { action } from 'satcheljs';

let addTodo = action(
    'ADD_TODO',
    (text: string) => ({ text: text })
);

// This creates and dispatches an ADD_TODO action
addTodo('Take out trash');

Implement a mutator

You specify what action a mutator subscribes to by providing the corresponding action creator. If you're using TypeScript, the type of actionMessage is automatically inferred.

import { mutator } from 'satcheljs';

mutator(addTodo, (actionMessage) => {
    getStore().todos.push({
        id: Math.random(),
        text: actionMessage.text
    });
};

Orchestrators

Orchestrators are like mutators—they subscribe to actions—but they serve a different purpose. While mutators modify the store, orchestrators are responsible for side effects. Side effects might include making a server call or even dispatching further actions.

The following example shows how an orchestrator can persist a value to a server before updating the store.

import { action, orchestrator } from 'satcheljs';

let requestAddTodo = action(
    'REQUEST_ADD_TODO',
    (text: string) => ({ text: text })
);

orchestrator(requestAddTodo, async (actionMessage) => {
    await addTodoOnServer(actionMessage.text);
    addTodo(actionMessage.text);
});

mutatorAction

In many cases a given action only needs to be handled by one mutator. Satchel provides this utility API which encapsulates action creation, dispatch, and handling in one simple function call.

The addTodo mutator above could be implemented as follows:

let addTodo = mutatorAction(
    'ADD_TODO',
    function addTodo(text: string) {
        getStore().todos.push({
            id: Math.random(),
            text: actionMessage.text
        });
    });

This is a succinct and easy way to write mutators, but it comes with a restriction: the action creator is not exposed, so no other mutators or orchestrators can subscribe to it. If an action needs multiple handlers then it must use the full pattern with action creators and handlers implemented separately.

License - MIT