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.1.6'

Class Method Summary collapse

Class Method Details

.apply_middleware(*middlewares) ⇒ Object



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

def self.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



26
27
28
29
# File 'lib/redux.rb', line 26

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

.combine_reducers(reducers) ⇒ Object



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

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

.compose(*functions) ⇒ Object



31
32
33
# File 'lib/redux.rb', line 31

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

.create_reducer(&block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/redux.rb', line 35

def self.create_reducer(&block)
  %x{
    return (function(previous_state, action) {
      if (!previous_state) { previous_state = {}; }
      var previous_state_hash = Opal.Hash.$new(previous_state);
      var new_state_hash = block.$call(previous_state_hash, Opal.Hash.$new(action));
      if (previous_state_hash === new_state_hash) { return previous_state; }
      if (typeof new_state_hash.$class === "function") { return new_state_hash.$to_n(); }
      return previous_state;
    });
  }
end

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



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

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

.delete_state_path(state, *path) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/redux.rb', line 48

def self.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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/redux.rb', line 58

def self.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



87
88
89
90
91
92
93
94
95
# File 'lib/redux.rb', line 87

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

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/redux.rb', line 97

def self.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.has_key?(path_el)
      state_el[path_el] = {}
      state_el[path_el]
    else
      state_el[path_el]
    end
  end
  nil
end