Giter VIP home page Giter VIP logo

purescript-aspen's Introduction

purescript-aspen

Latest release Build status Greenkeeper badge

Combine Redux action handlers into a type-safe reducer.

Install

bower install purescript-aspen

Quick start

First write some action handlers. An action handler is a function that handles one and only one Redux action. An Action is of type Action (t :: Symbol) p where t is the type of the action and p is the type of the payload. You can write as many of these as you want. In this example I have 3.

addTask :: State -> Action "ADD_TASK" Task -> State
addTask = ...

toggleCompleteness :: State -> Action "TOGGLE_COMPLETENESS" Id -> State
toggleCompleteness = ...

removeTask :: State -> Action "REMOVE_TASK" Id -> State
removeTask = ...

After we have all of the action handlers, we need to combine them into a reducer using createReducer, handle, and >>=>>. This creates a reducer with the type of all of the supported actions in the signature. This provides compile time safety. All unsupported actions types will not change the state.

reducer
  :: forall p t r1 r2
  .  RowCons t (Action t p) r1
      ( "REMOVE_TASK" :: Action "REMOVE_TASK" Id
      , "TOGGLE_COMPLETENESS" :: Action "ADD_TASK" Id
      , "ADD_TASK" :: Action "ADD_TASK" Task
      | r2)
  => IsSymbol t
  => State -> Action t p -> State
reducer = createReducer $
  handle addTask
  >>=>> toggleCompleteness
  >>=>> removeTask

To see a more detailed tutorial checkout the aspen test.

Motivation

Writing reducers in Purescript is annoying, if done naïvely. All the type un-safety of JavaScript can reflect in the type of the reducer and branching on action.type is annoying. However, we are guaranteed to be immutable and have zero side-effects! These are the two reducer "rules" that are not enforced in JavaScript. If we could just get rid of the problems, life would be great. With aspen, you can have a great life.

Aspen allows you to combine action handlers into a single Redux reducer. Each handled action is reflected in the type of the reducer making it easy to see what each reducer handles at-a-glance. Since the Actions are typed, you cannot have the wrong payload. Doing so, would result in a compile time error! Actions unknown to a reducer are passed through and ignored.

This library is like redux-actions, but PureScript-ian. Thank you for the inspiration.

Documentation

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.