Class: Rydux::Store
- Inherits:
-
Object
- Object
- Rydux::Store
- Defined in:
- lib/rydux/store.rb
Instance Attribute Summary collapse
-
#listeners ⇒ Object
readonly
Returns the value of attribute listeners.
Instance Method Summary collapse
-
#abandon(listener) ⇒ Object
Unsubscribes a listener from the store.
-
#dispatch(*args) ⇒ Object
Dispatches an action to all reducers.
-
#initialize(reducers) ⇒ Store
constructor
A new instance of Store.
-
#state ⇒ Object
Return a clone of the current state so that the user cannot directly modify state, and introduce side effects.
-
#subscribe(caller = nil, &block) ⇒ Object
Allow subscribing either by passing a reference to self or by passing a block which defines the state keys that this listener cares about.
Constructor Details
#initialize(reducers) ⇒ Store
Returns a new instance of Store.
5 6 7 8 |
# File 'lib/rydux/store.rb', line 5 def initialize(reducers) @state, @listeners = {}, [] @reducers = strap_reducers(reducers) end |
Instance Attribute Details
#listeners ⇒ Object (readonly)
Returns the value of attribute listeners.
3 4 5 |
# File 'lib/rydux/store.rb', line 3 def listeners @listeners end |
Instance Method Details
#abandon(listener) ⇒ Object
Unsubscribes a listener from the store
23 24 25 |
# File 'lib/rydux/store.rb', line 23 def abandon(listener) @listeners.delete_if {|l| l[:obj] == listener } end |
#dispatch(*args) ⇒ Object
Dispatches an action to all reducers. Can be called any of the following ways: Takes in an action and an optional callback proc, which will be called after the dispatch is finished. The action can be passed in either as a hash or as two seperate arguments. E.g. dispatch({ type: ‘SOME_ACTION’, payload: { key: ‘value’ } }) is the same as dispatch(‘SOME_ACTION’, { key: ‘value’ }) Here’s an example with a proc: dispatch(‘SOME_ACTION’, { key: ‘value’ }, ->{ puts “The dispatch is done” })
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rydux/store.rb', line 34 def dispatch(*args) if args.first.is_a? Hash _dispatch(args.first, args[1]) else if args[1].is_a? Proc _dispatch({ type: args.first }, args[1]) else _dispatch({ type: args.first, payload: args[1] }, args[2]) end end end |
#state ⇒ Object
Return a clone of the current state so that the user cannot directly modify state, and introduce side effects
48 49 50 |
# File 'lib/rydux/store.rb', line 48 def state State.new(@state) end |
#subscribe(caller = nil, &block) ⇒ Object
Allow subscribing either by passing a reference to self or by passing a block which defines the state keys that this listener cares about
13 14 15 16 17 18 19 20 |
# File 'lib/rydux/store.rb', line 13 def subscribe(caller = nil, &block) if block_given? notify_when = block.call(state) @listeners << { obj: block.binding.receiver, notify_when: notify_when } else @listeners << { obj: caller } end end |