Skip to content

Latest commit

 

History

History
109 lines (82 loc) · 2.63 KB

README.md

File metadata and controls

109 lines (82 loc) · 2.63 KB

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.