Last active
January 27, 2018 18:07
Basic ReduxStore implemented in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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