Module: Redux

Defined in:
lib/redux.rb,
lib/redux/store.rb,
lib/redux/version.rb,
lib/redux/reducers.rb

Defined Under Namespace

Modules: Reducers Classes: Store

Constant Summary collapse

VERSION =
'4.2.0'

Class Method Summary collapse

Class Method Details

.apply_middleware(*middlewares) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/redux.rb', line 16

def apply_middleware(*middlewares)
  if middlewares.size == 1
    `Opal.global.Redux.applyMiddleware.apply(null, middlewares[0])`
  else
    `Opal.global.Redux.applyMiddleware.apply(null, middlewares)`
  end
end

.bind_action_creators(*args) ⇒ Object



24
25
26
27
# File 'lib/redux.rb', line 24

def bind_action_creators(*args)
  dispatch = args.pop
  `Opal.global.Redux.bindActionCreators(args, dispatch)`
end

.combine_reducers(reducers) ⇒ Object



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

def combine_reducers(reducers)
  %x{
    if (typeof reducers.$to_n === "function") {
      reducers = reducers.$to_n();
    }
    return Opal.global.Redux.combineReducers(reducers);
  }
end

.compose(*functions) ⇒ Object



29
30
31
# File 'lib/redux.rb', line 29

def compose(*functions)
  `Opal.global.Redux.compose(functions)`
end

.create_reducer(&block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/redux.rb', line 33

def create_reducer(&block)
  %x{
    return function(previous_state, action) {
      let state = null;
      if (previous_state === null || typeof(previous_state) === 'undefined') { state = nil; }
      else if (typeof(previous_state) === 'object' && !Array.isArray(previous_state)) {
        state = Opal.Hash.$new(previous_state);
      } else { state = previous_state; }
      var new_state = block.$call(state, Opal.Hash.$new(action));
      if (typeof(previous_state) === 'undefined' || previous_state === null) {
        if (typeof(new_state.$to_n) === "function") { return new_state.$to_n(); }
        return new_state;
      }
      if (state === new_state) { return previous_state; }
      if (typeof(new_state.$to_n) === "function") { return new_state.$to_n(); }
      return previous_state;
    };
  }
end

.create_store(reducer, preloaded_state = nil, enhancer = nil) ⇒ Object



3
4
5
# File 'lib/redux.rb', line 3

def create_store(reducer, preloaded_state = nil, enhancer = nil)
  Redux::Store.new(reducer, preloaded_state, enhancer)
end

.delete_state_path(state, *path) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/redux.rb', line 53

def delete_state_path(state, *path)
  size = path.size - 1
  set_state_path(state, *path[0..-2], nil)
  (2...size).each do |i|
    val = get_state_path(state, *path[0..-i])
    break if val.keys.size > 1
    set_state_path(state, *path[0..-i], nil)
  end
end

.fetch_by_path(*path) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/redux.rb', line 63

def fetch_by_path(*path)
  # get active redux component
  %x{
    var active_component = Opal.Preact.active_redux_component();
    var current_state;
    var final_data;
    var path_last = path.length - 1;
    if (path[path_last].constructor === Array) {
      path[path_last] = JSON.stringify(path[path_last]);
    }
    if (active_component) {
      // try to get data from component state or props or store
      current_state = active_component.data_access()
      if (current_state) {
        final_data = path.reduce(function(prev, curr) { return prev && prev[curr]; }, current_state);
        // if final data doesn't exist, 'null' is returned, so nil or false are ok as final_data
        if (final_data !== null && typeof final_data !== "undefined") { return final_data; }
      }
    } else {
      // try to get data from store
      current_state = Opal.Isomorfeus.store.native.getState();
      final_data = path.reduce(function(prev, curr) { return prev && prev[curr]; }, current_state);
      // if final data doesn't exist, 'null' is returned, so nil or false are ok as final_data
      if (final_data !== null && typeof final_data !== "undefined") { return final_data; }
    }
    return null;
  }
end

.get_state_path(state, *path) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/redux.rb', line 92

def get_state_path(state, *path)
  path.inject(state) do |state_el, path_el|
    if state_el.key?(path_el)
      state_el[path_el]
    else
      return nil
    end
  end
end

.set_state_path(state, *path, value) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/redux.rb', line 102

def set_state_path(state, *path, value)
  last_el = path.last
  path.inject(state) do |state_el, path_el|
    if path_el == last_el
      state_el[path_el] = value
      state_el[path_el]
    elsif !state_el.key?(path_el)
      state_el[path_el] = {}
      state_el[path_el]
    else
      state_el[path_el]
    end
  end
  nil
end