rydux 0.9.1 → 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +5 -2
- data/lib/rydux/reducer.rb +0 -1
- data/lib/rydux/state.rb +1 -2
- data/lib/rydux/store.rb +43 -31
- data/lib/rydux/version.rb +1 -1
- data/lib/rydux.rb +0 -1
- metadata +2 -3
- data/lib/rydux/rydux.rb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff4a7bee8b3d4dcba4b03571e76464b0ca723726
|
4
|
+
data.tar.gz: 30356e6ededd51f480f9b148f271228708339136
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52d1ddfb5a9860ee19c7b5bc43f8945f039116e516db46547bb755f8c274c26d06ef5c3e9cd7538e042bf92bc0f20d093492da07fe83ce72f0a1cc06de0d92c5
|
7
|
+
data.tar.gz: dc65de528f92a9d1863c0849d15dfe95a46accb3c4eba51b0e98f962549c4de4e7eb8e6a5f0b48c484be871f9392ebe8047f770f0c9809eb9621043bb9327439
|
data/README.md
CHANGED
@@ -65,7 +65,7 @@ Or install it yourself as:
|
|
65
65
|
# Every instance that subscribes to the store will
|
66
66
|
# get this state_changed method called whenever the state
|
67
67
|
# in the store changes. Do whatever you want with your state here.
|
68
|
-
def state_changed(state
|
68
|
+
def state_changed(state)
|
69
69
|
# ...
|
70
70
|
end
|
71
71
|
end
|
@@ -102,7 +102,7 @@ class Friend
|
|
102
102
|
@users_name = Store.state.user.name
|
103
103
|
end
|
104
104
|
|
105
|
-
def state_changed(state
|
105
|
+
def state_changed(state)
|
106
106
|
@users_name = state.user.name
|
107
107
|
end
|
108
108
|
|
@@ -120,6 +120,9 @@ Store.dispatch(type: 'CHANGE_USER_NAME', payload: { name: 'Mike' })
|
|
120
120
|
friend.greet_user #=> Hello, Mike
|
121
121
|
```
|
122
122
|
|
123
|
+
View the [Documentation](https://github.com/alexdovzhanyn/rydux/wiki) for more information
|
124
|
+
on methods and parameters
|
125
|
+
|
123
126
|
## Development
|
124
127
|
|
125
128
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/rydux/reducer.rb
CHANGED
data/lib/rydux/state.rb
CHANGED
data/lib/rydux/store.rb
CHANGED
@@ -2,21 +2,9 @@ module Rydux
|
|
2
2
|
class Store
|
3
3
|
attr_reader :listeners
|
4
4
|
|
5
|
-
def initialize(
|
6
|
-
@state = {}
|
7
|
-
@
|
8
|
-
@reducers = combined_reducers
|
9
|
-
|
10
|
-
@reducers.each do |k, reducer|
|
11
|
-
if !reducer.ancestors.include? ::Rydux::Reducer
|
12
|
-
raise "Store expected a Reducer or array of reducers, but instead got: #{reducers}"
|
13
|
-
end
|
14
|
-
|
15
|
-
new_state = {}
|
16
|
-
new_state[k] = reducer.map_state(type: nil)
|
17
|
-
|
18
|
-
set_state(new_state)
|
19
|
-
end
|
5
|
+
def initialize(reducers)
|
6
|
+
@state, @listeners = {}, []
|
7
|
+
@reducers = strap_reducers(reducers)
|
20
8
|
end
|
21
9
|
|
22
10
|
def subscribe(listener)
|
@@ -28,38 +16,62 @@ module Rydux
|
|
28
16
|
@listeners.delete(listener)
|
29
17
|
end
|
30
18
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
19
|
+
# Dispatches an action to all reducers. Can be called any of the following ways:
|
20
|
+
# Takes in an action and an optional callback proc, which will be called after the
|
21
|
+
# dispatch is finished.
|
22
|
+
# The action can be passed in either as a hash or as two seperate arguments.
|
23
|
+
# E.g. dispatch({ type: 'SOME_ACTION', payload: { key: 'value' } })
|
24
|
+
# is the same as dispatch('SOME_ACTION', { key: 'value' })
|
25
|
+
# Here's an example with a proc: dispatch('SOME_ACTION', { key: 'value' }, ->{ puts "The dispatch is done" })
|
26
|
+
def dispatch(*args)
|
27
|
+
if args.first.is_a? Hash
|
28
|
+
_dispatch(args.first, args[1])
|
29
|
+
else
|
30
|
+
if args[1].is_a? Proc
|
31
|
+
_dispatch({ type: args.first }, args[1])
|
32
|
+
else
|
33
|
+
_dispatch({ type: args.first, payload: args[1] }, args[2])
|
34
|
+
end
|
36
35
|
end
|
37
36
|
end
|
38
37
|
|
38
|
+
# Return a clone of the current state so that the user cannot directly
|
39
|
+
# modify state, and introduce side effects
|
39
40
|
def state
|
40
41
|
State.new(@state)
|
41
42
|
end
|
42
43
|
|
43
44
|
private
|
44
45
|
|
45
|
-
def
|
46
|
-
|
47
|
-
|
46
|
+
def _dispatch(action, callback = ->{})
|
47
|
+
@reducers.each {|k, reducer| set_state *[k, reducer.map_state(action, state[k])] }
|
48
|
+
callback.call if callback
|
49
|
+
end
|
48
50
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
51
|
+
# Initialize state with the key-value pair associated with each reducer
|
52
|
+
def strap_reducers(reducers)
|
53
|
+
reducers.each {|k, reducer| set_state *[k, reducer.map_state(type: nil)]}
|
54
|
+
reducers
|
55
|
+
end
|
54
56
|
|
55
|
-
|
57
|
+
# Argument 1 should always be the key within state that we're mutating
|
58
|
+
# Argument 2 should be the actual state object
|
59
|
+
def set_state(k, v)
|
60
|
+
@state[k] = v
|
61
|
+
|
62
|
+
if !self.methods.include? k
|
63
|
+
self.define_singleton_method(k.to_sym) do
|
64
|
+
return State.new(@state[k])
|
65
|
+
end
|
56
66
|
end
|
67
|
+
|
68
|
+
notify_listeners
|
57
69
|
end
|
58
70
|
|
59
|
-
def notify_listeners
|
71
|
+
def notify_listeners
|
60
72
|
@listeners.each do |listener|
|
61
73
|
if listener.respond_to? :state_changed
|
62
|
-
listener.public_send(:state_changed, state
|
74
|
+
listener.public_send(:state_changed, state)
|
63
75
|
end
|
64
76
|
end
|
65
77
|
end
|
data/lib/rydux/version.rb
CHANGED
data/lib/rydux.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rydux
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Dovzhanyn
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-06-
|
11
|
+
date: 2018-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,7 +86,6 @@ files:
|
|
86
86
|
- bin/setup
|
87
87
|
- lib/rydux.rb
|
88
88
|
- lib/rydux/reducer.rb
|
89
|
-
- lib/rydux/rydux.rb
|
90
89
|
- lib/rydux/state.rb
|
91
90
|
- lib/rydux/store.rb
|
92
91
|
- lib/rydux/version.rb
|