Skip to content

Commit

Permalink
Extract redux-functor
Browse files Browse the repository at this point in the history
  • Loading branch information
flintinatux committed Nov 20, 2017
1 parent 5f5954b commit d151a40
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 99 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.log
.DS_Store
.yarn*
/.nyc_output
/lib
/node_modules
/.nyc_output
5 changes: 3 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
*.log
*.md
.DS_Store
.eslintrc.js
.gitignore
.jshint*
.travis.yml
.yarn*
/.nyc_output
/docs
/node_modules
/src
/test
Expand Down
57 changes: 25 additions & 32 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
| [`action`](#action) | `String -> a -> Action` |
| [`also`](#also) | `Action -> Action -> [Action]` |
| [`error`](#error) | `String -> a -> Action` |
| [`functor`](#functor) | `Store -> Function -> Action -> a` |
| [`handle`](#handle) | `a -> { k: (a, b, Boolean) -> a } -> (a, Action) -> a` |
| [`logError`](#logError) | `(a, b, Boolean) -> a` |
| [`onSuccess`](#onSuccess) | `((a, b) -> a) -> (a, b, Boolean) -> a` |
| [`sideEffect`](#sideEffect) | `(() -> a) -> IO Action` |

### action

`@articulate/ducks/lib/action`

```haskell
action : String -> a -> Action
action :: String -> a -> Action
```

Curried action creator. Accepts a `String` for the `type`, and anything for the `payload`, and then returns an [FSA-compliant](https://github.com/acdlite/flux-standard-action) action with the format `{ type, payload }`.
Expand All @@ -32,13 +33,15 @@ sendEmail({ to: 'example@email.com' }) //=> { type: 'SEND_EMAIL', payload: { to:

### also

`@articulate/ducks/lib/also`

```haskell
also : Action -> Action -> [Action]
also :: Action -> Action -> [Action]
```

Helper to dispatch a follow-up action only after an `Async` action resolves.

Requires the [`functor`](#functor) middleware to be registered, since it combines two actions into a list of actions.
Requires [`redux-functor`](https://github.com/articulate/redux-functor) middleware to be applied, since it combines two actions into an `Array` of actions, and an `Array` is a functor.

```js
const { also, sideEffect } = require('@articulate/ducks')
Expand All @@ -59,8 +62,10 @@ createItem(item).map(also(redirectToItem(item))) //=> Async Error [Action]

### error

`@articulate/ducks/lib/error`

```haskell
error : String -> a -> Action
error :: String -> a -> Action
```

Curried error-action creator. Accepts a `String` for the `type`, and anything for the `payload`, and then returns an [FSA-compliant](https://github.com/acdlite/flux-standard-action) action representing an error with the format `{ type, payload, error: true }`.
Expand All @@ -76,29 +81,12 @@ const sendEmail = error('SEND_EMAIL')
sendEmail(new Error('mailbox full')) //=> { type: 'SEND_EMAIL', payload: Error(...), error: true }
```

### functor

```haskell
functor : Store -> Function -> Action -> a
```

Redux middleware to dispatch actions that are functors. This includes the built-in javascript [`Array`](http://devdocs.io/javascript/global_objects/array) type, along with many other [ADT's](https://github.com/evilsoft/crocks#crocks).

If any action has a property called `map` that is a function, the action is assumed to be a functor.

```js
const { applyMiddleware, combineReducers, createStore } = require('redux')
const { functor } = require('@articulate/ducks')

const reducers = require('../ducks')

const store = createStore(combineReducers(reducers), applyMiddleware(functor))
```

### handle

`@articulate/ducks/lib/handle`

```haskell
handle : a -> { k: (a, b, Boolean) -> a } -> (a, Action) -> a
handle :: a -> { k: (a, b, Boolean) -> a } -> (a, Action) -> a
```

Accepts an initial state and a map of action types to reducers, and returns a reducer than handles multiple action types.
Expand Down Expand Up @@ -131,8 +119,10 @@ state = reducer(state, action('NOT_HANDLED', null)) //=> { counter: 5, color: 'r

### logError

`@articulate/ducks/lib/logError`

```haskell
logError : (a, b, Boolean) -> a
logError :: (a, b, Boolean) -> a
```

A cheater reducer to log error-actions to `console.error` before safely returning the current state untouched. No, this is not a pure function. But it is a very useful one, especially for async actions where the resolved value isn't used, but you still want to know about errors.
Expand All @@ -149,8 +139,10 @@ const reducer = handle({}, {

### onSuccess

`@articulate/ducks/lib/onSuccess`

```haskell
onSuccess : ((a, b) -> a) -> (a, b, Boolean) -> a
onSuccess :: ((a, b) -> a) -> (a, b, Boolean) -> a
```

Wraps a reducer to produce a new reducer. If an error-action is dispatched, the current state is returned untouched. Otherwise, it executes the original reducer.
Expand All @@ -171,18 +163,20 @@ const reducer = handle({}, {

### sideEffect

`@articulate/ducks/lib/sideEffect`

```haskell
sideEffect : (() -> a) -> IO Action
sideEffect :: (() -> a) -> IO Action
```

Safely wraps a side-effect in an [`IO`](https://github.com/evilsoft/crocks#crocks) that returns this [FSA-compliant](https://github.com/acdlite/flux-standard-action) action: `{ type: 'SIDE_EFFECT' }`. Useful for things like redirecting, setting the `document.title`, or logging to the console.

Requires a middleware that can handle [`IO`](https://github.com/evilsoft/crocks#crocks) actions, such as [`redux-io`](https://www.npmjs.com/package/redux-io).

See also [`also`](#also), [`functor`](#functor).
See also [`also`](#also).

```js
const { also, functor, sideEffect } = require('@articulate/ducks')
const { also, sideEffect } = require('@articulate/ducks')
const { applyMiddleware, combineReducers, createStore } = require('redux')
const future = require('redux-future')
const io = require('redux-io')
Expand All @@ -194,8 +188,7 @@ const store = createStore(
combineReducers(reducers),
applyMiddleware(
io('run'),
future,
functor
future
)
)

Expand Down
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
exports.action = require('./lib/action')
exports.also = require('./lib/also')
exports.error = require('./lib/error')
exports.functor = require('./lib/functor')
exports.handle = require('./lib/handle')
exports.logError = require('./lib/logError')
exports.onSuccess = require('./lib/onSuccess')
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
"coverage": "nyc report --reporter=text-lcov | coveralls",
"lint": "eslint src test",
"postversion": "git push --tags origin master",
"prebuild": "yarn clean",
"pretest": "yarn build",
"pretest:coverage": "yarn build",
"preversion": "git checkout master && yarn build",
"prebuild": "yarn run clean",
"pretest": "yarn run build",
"pretest:coverage": "yarn run build",
"preversion": "git checkout master && yarn run build",
"test": "mocha test --reporter=dot",
"test:ci": "yarn lint && yarn test:coverage && yarn coverage",
"test:coverage": "nyc yarn test"
"test:ci": "yarn run lint && yarn run test:coverage && yarn run coverage",
"test:coverage": "nyc yarn run test"
},
"dependencies": {
"crocks": "^0.7.0"
Expand Down
12 changes: 0 additions & 12 deletions src/functor.js

This file was deleted.

45 changes: 0 additions & 45 deletions test/functor.js

This file was deleted.

0 comments on commit d151a40

Please sign in to comment.