Skip to content

Instantly share code, notes, and snippets.

@strzibny
Last active January 27, 2018 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strzibny/faec1c76c64d47fc2d6136c0045b363f to your computer and use it in GitHub Desktop.
Save strzibny/faec1c76c64d47fc2d6136c0045b363f to your computer and use it in GitHub Desktop.
Basic ReduxStore implemented in Ruby
class ReduxStore
attr_reader :current_state
def initialize(reducer)
@reducer = reducer
@listeners = []
@current_state = nil
dispatch({})
end
def dispatch(action)
@current_state = @reducer.call(@current_state, action)
@listeners.each { |l| l.call }
end
def subscribe(listener)
@listeners.push(listener)
->{ @listeners.delete(listener) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment