rydux 0.9.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/rydux/store.rb +24 -12
- data/lib/rydux/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7098f20a793a618b8b259cb5db79e3754158e3ae
|
4
|
+
data.tar.gz: 231e6c8aec4ff8096fa44efc8e409c8c5a563bc3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 63a22ba76537430d1a3d14984b0643dd132b3cb9ce08ec7cfd80b55ab74305f75ae6a69d3e99767f4cf11e373cd6304a0e09c07769d2b071ec2418cb79476f0d
|
7
|
+
data.tar.gz: 2683fde74bad9722225b186d87240d151912d515ed1f6e24b09f91ae3d32859b70242c45057549f5f201a72143373d023d31c9c16c89b007a79c2a4fdf4b8cc6
|
data/Gemfile.lock
CHANGED
data/lib/rydux/store.rb
CHANGED
@@ -7,13 +7,21 @@ module Rydux
|
|
7
7
|
@reducers = strap_reducers(reducers)
|
8
8
|
end
|
9
9
|
|
10
|
-
|
11
|
-
|
10
|
+
# Allow subscribing either by passing a reference to self
|
11
|
+
# or by passing a block which defines the state keys that
|
12
|
+
# this listener cares about
|
13
|
+
def subscribe(caller = nil, &block)
|
14
|
+
if block_given?
|
15
|
+
notify_when = block.call(state)
|
16
|
+
@listeners << { obj: block.binding.receiver, notify_when: notify_when }
|
17
|
+
else
|
18
|
+
@listeners << { obj: caller }
|
19
|
+
end
|
12
20
|
end
|
13
21
|
|
14
22
|
# Unsubscribes a listener from the store
|
15
23
|
def abandon(listener)
|
16
|
-
@listeners.
|
24
|
+
@listeners.delete_if {|l| l[:obj] == listener }
|
17
25
|
end
|
18
26
|
|
19
27
|
# Dispatches an action to all reducers. Can be called any of the following ways:
|
@@ -57,21 +65,25 @@ module Rydux
|
|
57
65
|
# Argument 1 should always be the key within state that we're mutating
|
58
66
|
# Argument 2 should be the actual state object
|
59
67
|
def set_state(k, v)
|
60
|
-
@state[k]
|
68
|
+
if @state[k] != v # Only set state if it has actually changed
|
69
|
+
@state[k] = v
|
61
70
|
|
62
|
-
|
63
|
-
|
64
|
-
|
71
|
+
if !self.methods.include? k
|
72
|
+
self.define_singleton_method(k.to_sym) do
|
73
|
+
return State.new(@state[k])
|
74
|
+
end
|
65
75
|
end
|
66
|
-
end
|
67
76
|
|
68
|
-
|
77
|
+
notify_listeners(k)
|
78
|
+
end
|
69
79
|
end
|
70
80
|
|
71
|
-
def notify_listeners
|
81
|
+
def notify_listeners(state_key)
|
72
82
|
@listeners.each do |listener|
|
73
|
-
|
74
|
-
|
83
|
+
# If no notify_when, the user wants ALL state notifications
|
84
|
+
# Otherwise, only send the state notifications they've subscribed to.
|
85
|
+
if (!listener[:notify_when] || listener[:notify_when].include?(state_key)) && listener[:obj].respond_to?(:state_changed)
|
86
|
+
listener[:obj].public_send(:state_changed, state)
|
75
87
|
end
|
76
88
|
end
|
77
89
|
end
|
data/lib/rydux/version.rb
CHANGED