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
- #collect_and_defer_dispatch(action) ⇒ 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 98 99 100 |
# File 'lib/redux/store.rb', line 70 def initialize(reducer, preloaded_state = `null`, enhancer = `null`) @deferred_actions = {} @deferred_dispatcher = nil @last_dispatch_time = Time.now %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
102 103 104 105 106 |
# File 'lib/redux/store.rb', line 102 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
108 109 110 111 112 |
# File 'lib/redux/store.rb', line 108 def add_reducers(new_reducers) self.class.reducers.merge!(new_reducers) next_reducer = Redux.combine_reducers(self.class.reducers) replace_reducer(next_reducer) end |
#collect_and_defer_dispatch(action) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/redux/store.rb', line 128 def collect_and_defer_dispatch(action) if !Isomorfeus.on_ssr? type = action.delete(:type) @deferred_actions[type] = [] unless @deferred_actions.key?(type) @deferred_actions[type].push(action) @last_dispatch_time = `Date.now()` # `console.log(#@last_dispatch_time)` deferred_dispatcher(`Date.now()`) unless @deferred_dispatcher else dispatch(action) end nil end |
#dispatch(action) ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/redux/store.rb', line 114 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
124 125 126 |
# File 'lib/redux/store.rb', line 124 def get_state Hash.new(`this.native.getState()`) end |
#replace_reducer(next_reducer) ⇒ Object
142 143 144 |
# File 'lib/redux/store.rb', line 142 def replace_reducer(next_reducer) `this.native.replaceReducer(next_reducer)` end |
#subscribe(&listener) ⇒ Object
returns function needed to unsubscribe the listener
147 148 149 |
# File 'lib/redux/store.rb', line 147 def subscribe(&listener) `this.native.subscribe(function() { return listener.$call(); })` end |