# dispatchr

> A Flux dispatcher for applications that run on the server and the client.

Latest version **1.3.0** (published 2022-06-10) · 0 weekly downloads

## Install

```sh
npm install dispatchr
pnpm add dispatchr
yarn add dispatchr
bun add dispatchr
```

## Health

**Score 33/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2022-06-10 |
| First published | 2014-05-29 |
| Weekly downloads | 0 |
| TypeScript types | separate (@types/dispatchr) |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 28.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1789 |
| Author | Michael Ridgway |
| Maintainers | vijar, mridgway, lingyan, redonkulus |
| Keywords | yahoo, flux, react, dispatcher |

## Links

- npm: https://www.npmjs.com/package/dispatchr
- Repository: https://github.com/yahoo/fluxible
- Homepage: https://github.com/yahoo/fluxible#readme
- Issues: https://github.com/yahoo/fluxible/issues
- npm.io page: https://npm.io/package/dispatchr

## Dependencies (2)

- [inherits](https://npm.io/package/inherits.md) ^2.0.4
- [eventemitter3](https://npm.io/package/eventemitter3.md) ^4.0.7

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.3.0 (latest) — 2022-06-10
- 1.2.1 — 2021-06-21
- 1.2.0 — 2017-05-10
- 1.1.1 — 2017-02-22
- 1.1.0 — 2016-10-12
- 1.0.3 — 2016-09-19
- 1.0.2 — 2016-09-13
- 1.0.0 — 2016-06-22
- 0.3.3 — 2015-05-18
- 0.3.2 — 2015-04-07
- 0.3.1 — 2015-04-01
- 0.3.0 — 2015-03-30
- 0.2.14 — 2015-03-09
- 0.2.13 — 2015-03-05
- 0.2.12 — 2015-01-12
- … 25 more at https://npm.io/package/dispatchr/versions

## README

# Dispatchr 

[![npm version](https://badge.fury.io/js/dispatchr.svg)](http://badge.fury.io/js/dispatchr)

A [Flux][] dispatcher for applications that run on the server and the client.

## Usage

For a more detailed example, see our [example applications](../../examples).

```js
var ExampleStore = require('./stores/ExampleStore.js');
var dispatcher = require('dispatchr').createDispatcher({
    stores: [ExampleStore]
});

var contextOptions = {};
var dispatcherContext = dispatcher.createContext(contextOptions);

dispatcherContext.dispatch('NAVIGATE', {});
// Action has been handled fully
```

## Differences from [Facebook's Flux Dispatcher](https://github.com/facebook/flux/blob/master/src/Dispatcher.js)

Dispatchr's main goal is to facilitate server-side rendering of Flux 
applications while also working on the client-side to encourage code reuse. In 
order to isolate stores between requests on the server-side, we have opted to 
instantiate the dispatcher and stores classes per request.

In addition, action registration is done by stores as a unit rather than 
individual callbacks. This allows us to lazily instantiate stores as the events 
that they handle are dispatched. Since instantiation of stores is handled by the 
dispatcher, we can keep track of the stores that were used during a request and 
dehydrate their state to the client when the server has completed its execution.

Lastly, we are able to enforce the Flux flow by restricting access to the 
dispatcher from stores. Instead of stores directly requiring a singleton 
dispatcher, we pass a dispatcher interface to the constructor of the stores to 
provide access to only the functions that should be available to it: `waitFor` 
and `getStore`. This prevents the stores from dispatching an entirely new 
action, which should only be done by action creators to enforce the 
unidirectional flow that is Flux.

## Helper Utilities

These utilities make creating stores less verbose and provide some `change` 
related functions that are common amongst all store implementations. These 
store helpers also implement a basic `shouldDehydrate` function that returns 
true if `emitChange` has been called by the store and false otherwise.

### BaseStore

`require('dispatchr/addons/BaseStore')` provides a base store class for 
extending. Provides `getContext`, `emitChange`, `addChangeListener`, and 
`removeChangeListener` functions. Example:

```js
var inherits = require('inherits');
var BaseStore = require('dispatchr/addons/BaseStore');
var MyStore = function (dispatcherInterface) {
    BaseStore.apply(this, arguments);
};
inherits(MyStore, BaseStore);
MyStore.storeName = 'MyStore';
MyStore.handlers = {
    'NAVIGATE': function (payload) { ... this.emitChange() ... }
};
MyStore.prototype.getFoo = function () { var context = this.getContext(), ... }
module.exports = MyStore;
```


### createStore

`require('dispatchr/addons/createStore')` provides a helper function for 
creating stores similar to React's `createClass` function. The created store 
class will extend BaseStore and have the same built-in functions. Example:

```js
var createStore = require('dispatchr/addons/createStore');
var MyStore = createStore({
    initialize: function () {}, // Called immediately after instantiation
    storeName: 'MyStore',
    handlers: {
        'NAVIGATE': function (payload) { ... this.emitChange() ... }
    }
    foo: function () { ... }
});
module.exports = MyStore;
```


## APIs

- [Dispatchr](https://github.com/yahoo/fluxible/blob/master/packages/dispatchr/docs/dispatchr.md)
- [Store](https://github.com/yahoo/fluxible/blob/master/packages/dispatchr/docs/store.md)



## License

This software is free to use under the Yahoo! Inc. BSD license.
See the [LICENSE file][] for license text and copyright information.

[LICENSE file]: https://github.com/yahoo/fluxible/blob/master/LICENSE.md
[Flux]: http://facebook.github.io/flux/docs/overview.html

---
_Source: https://npm.io/package/dispatchr · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
