• Stars
    star
    149
  • Rank 248,619 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Elm middleware for redux

redux-elm-middleware

Elm middleware for redux ✨

logo

Build Status codecov Dependency Status npm version

Installation

You need to install redux-elm-middleware for js and elm.

$ npm i redux-elm-middleware -S

Redux-elm-middleware is currently only published to npm. You will need to add the following to you elm-package.json

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

Usage

Setup Redux Middleware

import createElmMiddleware from 'redux-elm-middleware'
import { reducer as elmReducer } from 'redux-elm-middleware'

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

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


// 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)

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.

f.e.

{
  type: '@@elm/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
        }

Motivation

  • write bulletproof businesslogic
  • handle state and effects
    • pure
    • in one place
    • with a safetynet
  • still have the rich react/redux ecosystem at your paws
    • components
    • middlewares
      • routing
      • persistent state (localstorage)
      • offline support
      • ui state ( redux-ui )
  • sneak a nice functional language into your projects
  • don't have to commit 100% to it
  • slowly convert a redux/react app into elm

Running the Example

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

Feedback and contributons welcome!

More Repositories

1

awesome-fp-js

😎 A curated list of awesome functional programming stuff in js
5,975
star
2

awesome-frp-js

A curated list of awesome functional reactive programming stuff in js
286
star
3

material-iterm

πŸ’» Light & dark material theme for iterm2
238
star
4

react-motion-drawer

Drawer built with react-motion
JavaScript
221
star
5

react-cond

Lisp-Style conditional rendering in react.
JavaScript
178
star
6

elm-verify-examples

Elm
167
star
7

awesome-ama-answers

A curated list of awesome AMA answers
127
star
8

redux-io

FSA-compliant io monad middleware for redux
JavaScript
66
star
9

compose-function

Function composition
JavaScript
57
star
10

redux-future

FSA-compliant future monad middleware for redux
JavaScript
53
star
11

.dotfiles

🏑
Vim Script
50
star
12

babel-plugin-array-includes

Replaces `arr.includes(val)' with `arr.indexOf(val) >= 0`.
JavaScript
44
star
13

react-mini

Create minimalistic react components
JavaScript
37
star
14

elm-verify

Elm
35
star
15

redux-saga-test

Helper for testing redux-saga
JavaScript
27
star
16

gulp-param

Add params to your tasks
JavaScript
26
star
17

elmi-to-json

Haskell
25
star
18

redux-either

FSA-compliant either monad middleware for redux
JavaScript
23
star
19

react-form-hoc

Higher order component for react forms
JavaScript
22
star
20

gh-contributors-table

πŸ™‡ Say "arigatō" to your contributors
JavaScript
21
star
21

recompose-examples

WIP Examples with recompose
JavaScript
20
star
22

mergesort

O(n log n)
JavaScript
20
star
23

underscore.string.fp

This is a wrapper for underscore.string to use it as a FP-library or with Ramda/lodash-fp
JavaScript
20
star
24

react-material-card

A material design card for react
JavaScript
18
star
25

elm-online

Subscribe to online/offline events πŸ“Ά
Elm
12
star
26

haskell-simple-dsl-examples

Simple examples on how to implement a DSL in Haskell.
Haskell
11
star
27

tasty-test-reporter

An ingredient for tasty that prints a summary and outputs junit xml that works with jenkins.
Haskell
11
star
28

elm-interface-to-json

Haskell
9
star
29

inquander

Inquirer for commander. If no arguments and options are passed to your commander app, it runs inquirer.
JavaScript
8
star
30

pm2-interactive-ui

πŸš₯ interactive cli for pm2
JavaScript
7
star
31

partition-all

Clojure's partition-all in js
JavaScript
7
star
32

extjs5-the-missing-components

This package adds missing components to extjs5.
JavaScript
7
star
33

react-mini-this

React components as pure functions with function bind syntax
JavaScript
6
star
34

debug-view

Elm
6
star
35

arityN

Wraps a function with a function of a sertain arity.
JavaScript
5
star
36

nix-test-runner

Test runner for nix expressions
Rust
5
star
37

grulp

:neckbeard: Never have to worry again if the project is using grunt or gulp. Just use grulp
JavaScript
5
star
38

editable

Elm
5
star
39

elm-reflection

Haskell
5
star
40

radioactive-react-example

Simple example with react and radioactive
JavaScript
5
star
41

create-constants

Create constants
JavaScript
5
star
42

bind-it

DEPRECATED: use stoeffel/bind-first and stoeffel/bind-last
JavaScript
4
star
43

ext.promise

promise for extjs
JavaScript
4
star
44

valid-sift

Check if it's a valid sift filter
JavaScript
3
star
45

compare.js

Comparator functions for you favorite sort-algo
JavaScript
3
star
46

reflux-hoc

Higher-order Component for reflux
JavaScript
3
star
47

if-change-then-notify

Notify me if a certain file changes
JavaScript
3
star
48

set-extra

Elm
3
star
49

Tetris

A lill tetris developed using raphaeljs
JavaScript
3
star
50

pretty-diff

Pretty printing a diff of two values
Haskell
3
star
51

elm-game-of-life

Game of Life in Elm
Elm
3
star
52

expand-object-keys

Expand dotty object keys to objects
JavaScript
3
star
53

underscore.string.cli

String manipulations on the commandline
JavaScript
2
star
54

.dots

🏑
Shell
2
star
55

css-longhand-cli

CLI for css-longhand
JavaScript
2
star
56

react-compose

Compose react components
2
star
57

reassemble-string

🏭 Reassemble your strings
JavaScript
2
star
58

burnhub

Burndown chart for github issues
JavaScript
2
star
59

guard-function

πŸ’‚β€β™‚οΈ You shall not pass!
JavaScript
2
star
60

retrieve-arguments

Retrieves the argumentnames of a function
JavaScript
2
star
61

gulp-macros

sweetjs macros for gulp
JavaScript
2
star
62

abclou

PureScript
2
star
63

tmux-tui

TUI to manage tmux sessions 🚧
Haskell
2
star
64

SEPS-GRP1

JavaScript
2
star
65

advent-of-code-2016

my solutions http://adventofcode.com/
Haskell
2
star
66

elm-check-updates

JavaScript
1
star
67

nur-packages

Nix
1
star
68

format-string

Format a string, using values from an object.
JavaScript
1
star
69

haskell-simple-quasi-quoter

code for small demo on how to use QuasiQuoter
Haskell
1
star
70

Aoc2021

Haskell
1
star
71

use-module

angularjs-style DI for nodemodules
JavaScript
1
star
72

resetable

Elm
1
star
73

reverse-arguments

Reverse the arguments passed to a function.
JavaScript
1
star
74

elm-playground

Elm
1
star
75

bn.js.logo

1
star
76

hosts_alias

my first ruby gem
Ruby
1
star
77

i18next-playground

JavaScript
1
star
78

stoeffel.github.io

🏑
CSS
1
star
79

sencha-leaflet

Leaflet component for sencha touch 2
JavaScript
1
star
80

lan-camp

CSS
1
star
81

rest-param

πŸ’¬ Returns a function with an appended rest param
JavaScript
1
star
82

array-comprehensions

Functional array comprehension
JavaScript
1
star
83

fparser-cli

a cli for bylexus's fparser
JavaScript
1
star
84

underscore.hasInside

Recursivly searching for a property by name
JavaScript
1
star