Skip to content

Instantly share code, notes, and snippets.

@hatashiro
Last active May 1, 2017 05:06
Show Gist options
  • Select an option

  • Save hatashiro/4d3299ca395eb99972aaaf143e8d6977 to your computer and use it in GitHub Desktop.

Select an option

Save hatashiro/4d3299ca395eb99972aaaf143e8d6977 to your computer and use it in GitHub Desktop.
Pure functional state management for Vuex
  • Make state immutable (in PS everything is immutable so 0 overhead for this)
  • No getters, no mutations: Lens, or something similar, may work here
    • get :: forall state a. (state -> a) -> ActionT state () a
    • commit :: forall state. (state -> state) -> ActionT state () Unit
  • actions :: forall state eff. ActionT state (Aff eff) Unit?
data AppState = AppState { counter :: Int }

type Action eff = ActionT AppState (Aff eff) Unit

-- guess we already have lenses for AppState

reqIncreaseCounter :: Action (http :: HTTP)
reqIncreaseCounter = do
  c <- get (_AppState..counter)
  increased <- someRequest c
  increaseCounter increased

increaseCounter :: Int -> Action ()
increaseCounter increased = do
  commit (_AppState..counter +~ increased)
@hatashiro
Copy link
Copy Markdown
Author

Thinking about how to express computed properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment