Coder Social home page Coder Social logo

elm-time-machine's Introduction

Time Machine for Elm

It's like a Delorean for your state!

What is it?

elm-time-machine is a library for the Elm Programming Language that tracks the history of your application's state, and contains a pointer letting you, in essence, travel backward or forward to some point in that state's timeline. It was written for the undo/redo functionality for Flittal, a keyboard-driven flow charting tool, but should be general enough for anyone to use.

Usage

Install with:

elm-package install joefiorini/elm-time-machine

Add a property to your state to hold the history:

type alias AppState =
  { contacts  : List Contact
  , history : TimeMachine.History (List Contact)
  }

Initialize it in your default state:

defaultState =
  { contacts = []
  , history = TimeMachine.initialize []
  }

And then record entries whenever your state changes:

step update state =
  case update of
    AddContact contact ->
      let contacts' = contact :: state.contacts
      in
        { state | contacts <- contacts'
                , history <- TimeMachine.record contacts'
        }

To undo call travelBackward:

step update state =
  case update of
    Undo ->
      let history' = TimeMachine.travelBackward state.history
      in
        case history'.current of
          Just contacts' ->
            { state | contacts <- contacts'
                    , history <- history'
            }
          Nothing -> state

To redo call travelForward:

step update state =
  case update of
    Undo ->
      let history' = TimeMachine.travelForward state.history
      in
        case history'.current of
          Just contacts' ->
            { state | contacts <- contacts'
                    , history <- history'
            }
          Nothing -> state

Both of these functions will take you one step in either direction. Rather than returning the item, they return a new history record with the item in current. In the event that you travel all the way back to the beginning of time, you will get Nothing.

For more, see the Elm package API docs.

Contributing

Write a test, run it, repeat. Run tests with:

cd test
make test

To automatically run tests when files change, first install daemontools and entr. On a Mac:

brew install daemontools
brew install entr

Neither of these tools will impact your development environment until you run them.

To start watching run:

cd test
supervise .

Feel free to open an issue or pull request if anything doesn't make sense.

elm-time-machine's People

Contributors

joefiorini avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

thsoft

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.