Elm middleware for Redux
Switch branches/tags
dev greenkeeper-ava-0.16.0 greenkeeper-ava-0.17.0 greenkeeper-ava-0.18.0 greenkeeper-ava-0.18.1 greenkeeper-ava-0.18.2 greenkeeper-ava-0.19.0 greenkeeper-ava-0.19.1 greenkeeper-babel-eslint-7.0.0 greenkeeper-babel-eslint-7.1.0 greenkeeper-babel-eslint-7.1.1 greenkeeper-babel-eslint-7.2.0 greenkeeper-babel-eslint-7.2.1 greenkeeper-babel-eslint-7.2.2 greenkeeper-babel-eslint-7.2.3 greenkeeper-babel-loader-6.2.7 greenkeeper-babel-loader-7.0.0 greenkeeper-babel-preset-es2015-6.16.0 greenkeeper-camelcase-4.0.0 greenkeeper-camelcase-4.1.0 greenkeeper-codecov-2.0.1 greenkeeper-codecov-2.0.2 greenkeeper-codecov-2.1.0 greenkeeper-codecov-2.2.0 greenkeeper-elm-0.17.0 greenkeeper-elm-0.18.0 greenkeeper-mocha-2.5.0 greenkeeper-mocha-3.0.0 greenkeeper-mocha-3.0.1 greenkeeper-mocha-3.0.2 greenkeeper-mocha-3.1.0 greenkeeper-mocha-3.1.1 greenkeeper-mocha-3.1.2 greenkeeper-mocha-3.2.0 greenkeeper-mocha-3.3.0 greenkeeper-mocha-3.4.0 greenkeeper-mocha-3.4.1 greenkeeper-mocha-3.4.2 greenkeeper-npm-run-all-4.0.0 greenkeeper-npm-run-all-4.0.1 greenkeeper-npm-run-all-4.0.2 greenkeeper-nyc-9.0.1 greenkeeper-nyc-10.0.0 greenkeeper-nyc-10.1.0 greenkeeper-nyc-10.1.2 greenkeeper-nyc-10.2.0 greenkeeper-nyc-10.3.0 greenkeeper-nyc-10.3.2 greenkeeper-ramda-0.22.0 greenkeeper-ramda-0.22.1 greenkeeper-ramda-0.23.0 greenkeeper-ramda-0.24.0 greenkeeper-react-redux-5.0.0 greenkeeper-react-redux-5.0.2 greenkeeper-react-redux-5.0.3 greenkeeper-react-redux-5.0.4 greenkeeper-react-redux-5.0.5 greenkeeper-react-router-3.0.0 greenkeeper-react-router-3.0.1 greenkeeper-react-router-3.0.2 greenkeeper-react-router-4.0.0 greenkeeper-react-router-4.1.0 greenkeeper-react-router-4.1.1 greenkeeper-redux-actions-0.12.0 greenkeeper-redux-actions-0.13.0 greenkeeper-redux-actions-1.0.0 greenkeeper-redux-actions-1.0.1 greenkeeper-redux-actions-1.1.0 greenkeeper-redux-actions-1.2.0 greenkeeper-redux-actions-1.2.1 greenkeeper-redux-actions-1.2.2 greenkeeper-redux-actions-2.0.2 greenkeeper-redux-actions-2.0.3 greenkeeper-sinon-2.0.0 greenkeeper-sinon-2.1.0 greenkeeper-sinon-2.2.0 greenkeeper-sinon-2.3.0 greenkeeper-sinon-2.3.1 greenkeeper-sinon-2.3.2 greenkeeper-standard-9.0.0 greenkeeper-standard-9.0.2 greenkeeper-standard-10.0.0 greenkeeper-standard-10.0.1 greenkeeper-standard-10.0.2 greenkeeper-webpack-2.2.1 greenkeeper-webpack-2.3.0 greenkeeper-webpack-2.3.1 greenkeeper-webpack-2.3.2 greenkeeper-webpack-2.3.3 greenkeeper-webpack-2.4.0 greenkeeper-webpack-2.4.1 greenkeeper-webpack-2.5.0 greenkeeper-webpack-2.5.1 greenkeeper-webpack-2.6.0 greenkeeper-webpack-2.6.1 greenkeeper-webpack-dev-server-2.2.1 greenkeeper-webpack-dev-server-2.3.0 greenkeeper-webpack-dev-server-2.4.0 greenkeeper-webpack-dev-server-2.4.1 greenkeeper-webpack-dev-server-2.4.2 greenkeeper-webpack-dev-server-2.4.3 greenkeeper-webpack-dev-server-2.4.4 greenkeeper-webpack-dev-server-2.4.5 greenkeeper-xyz-1.1.0 greenkeeper-xyz-2.0.0 greenkeeper-xyz-2.0.1 greenkeeper-xyz-2.1.0 master v017
Nothing to show
Clone or download
Pull request Compare This branch is 20 commits ahead of stoeffel:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
logo
src
test
.editorconfig
.gitignore
.travis.yml
LICENSE
README.md
elm-package.json
package-lock.json
package.json

README.md

redux-elm-middleware

Elm middleware for redux

NOTE: This is a forked version of the original package. The reason behind forking is to provide better and faster support for this package, including bug fixes and improvements.

Installation

You need to install redux-elm-middleware for JS and Elm.

$ npm i -S @cureous/redux-elm-middleware

redux-elm-middleware is currently only published to npm. You will need to add the following to your elm-package.json:

  "source-directories": [
    ".",
    "node_modules/@cureous/redux-elm-middleware/src",
    ...
  ]

Usage

Setup Redux Middleware

import createElmMiddleware, { reducer as elmReducer } from '@cureous/redux-elm-middleware';

// Import your Elm Reducer
import Elm from './Reducer.elm';

const reducer = combineReducers({
  elm: elmReducer
  // ...reducers
});

// create a worker of your Elm reducer
const elmStore = Elm.Reducer.worker();

// create the middleware
const { run, elmMiddleware } = createElmMiddleware(elmStore);

// create the Redux store and pass the elmMiddleware
const store = createStore(reducer, {}, compose(
  applyMiddleware(elmMiddleware),
  window.devToolsExtension ? window.devToolsExtension() : f => f
));

// you need to run the elm middleware and pass the Redux store
run(store)

Since v6.0.0, redux-elm-middleware will dispatch actions that correspond to external ports.

Example:

Using such a port in Reducer:

port externalCmd : String -> Cmd msg

Will result in dispatching the following action:

{
    "type": "@@elm.out/EXTERNAL_CMD",
    "payload": ""
}

Elm root reducer

The root reducer from redux-elm-middleware simply takes all actions from your elm reducers and returns the payload as the next state.

The new model returned in your elm reducers update function is dispatched as a new action to the Redux store.

E.g.

{
  type: '@@elm.in/Increment',
  payload: {
    counter: 3
  }
}

Creating a Reducer in Elm

A reducer in Elm looks like a normal TEA module without the view.

port module Reducer exposing (Model, Msg, init, update, subscriptions) -- Name of the module must match the worker


import Redux

-- define ports for all actions which should be handled by the elm reducer
port increment : ({} -> msg) -> Sub msg

-- define all subscriptions of your reducer
subscriptions : Model -> Sub Msg
subscriptions _ =
    Sub.batch
        [ increment <| always Increment
        -- ...
        ]

-- In order for the Elm model to cross the border safely it must be encoded as a JSON value.
encode : Model -> Json.Encode.Value
encode model =
    ...

-- MODEL
-- UPDATE

-- START THE REDUCER
main =
    Redux.program
        { init = init
        , update = update
        , encode = encode
        , subscriptions = subscriptions
        }

Flags support

In 5.0.0 programWithFlags was added and now you can pass down needed flags from JavaScript to your Elm reducer. programWithFlags works the same way as Elm works with flags.

Updated version from the example above will be the following:

// create a worker of your elm reducer
// and pass flags to it
const elmStore = Elm.Reducer.worker({
    bar: 'baz'
});
type alias Flags =
    { bar : String }

type alias Model =
    { foo: String }

init : Flags -> ( Model, Cmd Msg )
init { bar } =
    ({ foo = bar }, Cmd.none)

main =
    Redux.programWithFlags
        { init = init
        , update = update
        , encode = encode
        , subscriptions = subscriptions
        }

Running the Example

NOTE: Example is not fully updated to show all features of the forked version.

  • npm install
  • npm run example
  • open 127.0.0.1:8080

Feedback and contributons welcome!