Class: Redux::Store
- Inherits:
-
Object
- Object
- Redux::Store
- Includes:
- Native::Wrapper
- Defined in:
- lib/redux/store.rb
Class Method Summary collapse
- .add_middleware(middleware) ⇒ Object
- .add_reducer(reducer) ⇒ Object
- .add_reducers(new_reducers) ⇒ Object
-
.init! ⇒ Object
called from Isomorfeus.init.
- .middlewares ⇒ Object
- .preloaded_state ⇒ Object
- .preloaded_state=(ruby_hash) ⇒ Object
- .preloaded_state_merge!(ruby_hash) ⇒ Object
- .reducers ⇒ Object
Instance Method Summary collapse
- #add_reducer(reducer) ⇒ Object
- #add_reducers(new_reducers) ⇒ Object
- #dispatch(action) ⇒ Object
- #get_state ⇒ Object
-
#initialize(reducer, preloaded_state = `null`, enhancer = `null`) ⇒ Store
constructor
A new instance of Store.
- #replace_reducer(next_reducer) ⇒ Object
-
#subscribe(&listener) ⇒ Object
returns function needed to unsubscribe the listener.
Constructor Details
#initialize(reducer, preloaded_state = `null`, enhancer = `null`) ⇒ Store
Returns a new instance of Store.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/redux/store.rb', line 70 def initialize(reducer, preloaded_state = `null`, enhancer = `null`) %x{ var compose = (typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) || Opal.global.Redux.compose; var devext_enhance; if (typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION__) { devext_enhance = window.__REDUX_DEVTOOLS_EXTENSION__(); } var real_preloaded_state; if (typeof preloaded_state.$class === "function" && preloaded_state.$class() == "Hash") { if (preloaded_state.$size() == 0) { real_preloaded_state = null; } else { real_preloaded_state = preloaded_state.$to_n(); } } else if (preloaded_state == nil) { real_preloaded_state = null; } else { real_preloaded_state = preloaded_state; } if (enhancer && real_preloaded_state) { this.native = Opal.global.Redux.createStore(reducer, real_preloaded_state, compose(enhancer)); } else if (real_preloaded_state) { this.native = Opal.global.Redux.createStore(reducer, real_preloaded_state, devext_enhance); } else if (enhancer) { this.native = Opal.global.Redux.createStore(reducer, compose(enhancer)); } else { this.native = Opal.global.Redux.createStore(reducer, devext_enhance); } } end |
Class Method Details
.add_middleware(middleware) ⇒ Object
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/redux/store.rb', line 5 def self.add_middleware(middleware) if Isomorfeus.store `console.warning("Adding middleware after Store initialization may have side effects! Saving state and initializing new store with restored state!")` middlewares << middleware preloaded_state = Isomorfeus.store.get_state init! else middlewares << middleware end end |
.add_reducer(reducer) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/redux/store.rb', line 16 def self.add_reducer(reducer) if Isomorfeus.store # if the store has been initalized already, add the reducer to the instance Isomorfeus.store.add_reducer(reducer) else # otherwise just add it to the reducers, so that they will be used when initializing the store preloaded_state[reducer.keys.first] = {} unless preloaded_state.has_key?(reducer.keys.first) reducers.merge!(reducer) end end |
.add_reducers(new_reducers) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/redux/store.rb', line 27 def self.add_reducers(new_reducers) if Isomorfeus.store # if the store has been initalized already, add the reducer to the instance Isomorfeus.store.add_reducers(new_reducers) else # otherwise just add it to the reducers, so that they will be used when initializing the store new_reducers.each do |key, value| add_reducer(key => value) end end end |
.init! ⇒ Object
called from Isomorfeus.init
40 41 42 43 44 45 46 47 48 |
# File 'lib/redux/store.rb', line 40 def self.init! next_reducer = Redux.combine_reducers(@reducers) if middlewares.any? enhancer = Redux.apply_middleware(middlewares) Redux::Store.new(next_reducer, preloaded_state, enhancer) else Redux::Store.new(next_reducer, preloaded_state) end end |
.middlewares ⇒ Object
50 51 52 |
# File 'lib/redux/store.rb', line 50 def self.middlewares @middlewares ||= [] end |
.preloaded_state ⇒ Object
58 59 60 |
# File 'lib/redux/store.rb', line 58 def self.preloaded_state @preloaded_state ||= {} end |
.preloaded_state=(ruby_hash) ⇒ Object
62 63 64 |
# File 'lib/redux/store.rb', line 62 def self.preloaded_state=(ruby_hash) @preloaded_state = ruby_hash end |
.preloaded_state_merge!(ruby_hash) ⇒ Object
54 55 56 |
# File 'lib/redux/store.rb', line 54 def self.preloaded_state_merge!(ruby_hash) preloaded_state.merge!(ruby_hash) end |
.reducers ⇒ Object
66 67 68 |
# File 'lib/redux/store.rb', line 66 def self.reducers @reducers ||= {} end |
Instance Method Details
#add_reducer(reducer) ⇒ Object
99 100 101 102 103 |
# File 'lib/redux/store.rb', line 99 def add_reducer(reducer) self.class.reducers.merge!(reducer) next_reducer = Redux.combine_reducers(self.class.reducers) replace_reducer(next_reducer) end |
#add_reducers(new_reducers) ⇒ Object
105 106 107 108 109 |
# File 'lib/redux/store.rb', line 105 def add_reducers(new_reducers) self.class.reducers.merge!(new_reducers) next_reducer = Redux.combine_reducers(self.class.reducers) replace_reducer(next_reducer) end |
#dispatch(action) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/redux/store.rb', line 111 def dispatch(action) %x{ if (typeof action.$class === "function" && action.$class() == "Hash") { this.native.dispatch(action.$to_n()); } else { this.native.dispatch(action); } } end |
#get_state ⇒ Object
121 122 123 |
# File 'lib/redux/store.rb', line 121 def get_state Hash.new(`this.native.getState()`) end |
#replace_reducer(next_reducer) ⇒ Object
125 126 127 |
# File 'lib/redux/store.rb', line 125 def replace_reducer(next_reducer) `this.native.replaceReducer(next_reducer)` end |
#subscribe(&listener) ⇒ Object
returns function needed to unsubscribe the listener
130 131 132 |
# File 'lib/redux/store.rb', line 130 def subscribe(&listener) `this.native.subscribe(function() { return listener$.call(); })` end |