Class: Redux::Store

Inherits:
Object
  • Object
show all
Includes:
Native::Wrapper
Defined in:
lib/redux/store.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reducer, preloaded_state = nil, enhancer = nil) ⇒ Store

Returns a new instance of Store.



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
101
102
103
104
# File 'lib/redux/store.rb', line 72

def initialize(reducer, preloaded_state = nil, enhancer = nil)
  @deferred_actions = {}
  @deferred_dispatcher = nil
  @last_dispatch_time = Time.now
  %x{
    var ogre = Opal.global.Redux;
    var compose = (typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) || ogre.compose;
    if (enhancer == nil) { enhancer = null; }
    if (typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION__) {
      var devext_enhance = window.__REDUX_DEVTOOLS_EXTENSION__();
      if (enhancer) { enhancer = compose([enhancer, devext_enhance]); }
      else { enhancer = devext_enhance; }
    }
    if (preloaded_state == nil) {
      preloaded_state = null;
    } else if (typeof preloaded_state.$class === "function" && preloaded_state.$class() == "Hash") {
      if (preloaded_state.$size() == 0) {
        preloaded_state = null;
      } else {
        preloaded_state = preloaded_state.$to_n();
      }
    }
    if (enhancer && preloaded_state) {
      this.native = ogre.legacy_createStore(reducer, preloaded_state, enhancer);
    } else if (preloaded_state) {
      this.native = ogre.legacy_createStore(reducer, preloaded_state);
    } else if (enhancer) {
      this.native = ogre.legacy_createStore(reducer, enhancer);
    } else {
      this.native = ogre.legacy_createStore(reducer);
    }
  }
end

Class Method Details

.add_middleware(middleware) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/redux/store.rb', line 6

def 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



17
18
19
20
21
22
23
24
25
26
# File 'lib/redux/store.rb', line 17

def 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.key?(reducer.keys.first)
    reducers.merge!(reducer)
  end
end

.add_reducers(new_reducers) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/redux/store.rb', line 28

def 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



41
42
43
44
45
46
47
48
49
# File 'lib/redux/store.rb', line 41

def 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

.middlewaresObject



51
52
53
# File 'lib/redux/store.rb', line 51

def middlewares
  @middlewares ||= []
end

.preloaded_stateObject



59
60
61
# File 'lib/redux/store.rb', line 59

def preloaded_state
  @preloaded_state ||= {}
end

.preloaded_state=(ruby_hash) ⇒ Object



63
64
65
# File 'lib/redux/store.rb', line 63

def preloaded_state=(ruby_hash)
  @preloaded_state = ruby_hash
end

.preloaded_state_merge!(ruby_hash) ⇒ Object



55
56
57
# File 'lib/redux/store.rb', line 55

def preloaded_state_merge!(ruby_hash)
  preloaded_state.merge!(ruby_hash)
end

.reducersObject



67
68
69
# File 'lib/redux/store.rb', line 67

def reducers
  @reducers ||= {}
end

Instance Method Details

#add_reducer(reducer) ⇒ Object



106
107
108
109
110
# File 'lib/redux/store.rb', line 106

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



112
113
114
115
116
# File 'lib/redux/store.rb', line 112

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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/redux/store.rb', line 143

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
    @dispatched = true
    dispatch(action)
  end
  nil
end

#dispatch(action) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/redux/store.rb', line 118

def dispatch(action)
  %x{
    if (typeof action.$class === "function" && action.$class() == "Hash") {
      action = action.$to_n();
    }
    this.native.dispatch(action);
  }
end

#get_stateObject



127
128
129
130
131
132
133
134
135
# File 'lib/redux/store.rb', line 127

def get_state
  %x{
    let res = this.native.getState();
    if (typeof(res) === 'object' && !Array.isArray(res) && res !== null) {
      return Opal.Hash.$new(res);
    }
    return res;
  }
end

#recently_dispatched?Boolean

Returns:

  • (Boolean)


137
138
139
140
141
# File 'lib/redux/store.rb', line 137

def recently_dispatched?
  res = @dispatched
  @dispatched = false
  res
end

#replace_reducer(next_reducer) ⇒ Object



158
159
160
# File 'lib/redux/store.rb', line 158

def replace_reducer(next_reducer)
  `this.native.replaceReducer(next_reducer)`
end

#subscribe(&listener) ⇒ Object

returns function needed to unsubscribe the listener



163
164
165
# File 'lib/redux/store.rb', line 163

def subscribe(&listener)
  `this.native.subscribe(function() { return listener.$call(); })`
end