rydux 0.9.2 → 0.9.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ff4a7bee8b3d4dcba4b03571e76464b0ca723726
4
- data.tar.gz: 30356e6ededd51f480f9b148f271228708339136
3
+ metadata.gz: 7098f20a793a618b8b259cb5db79e3754158e3ae
4
+ data.tar.gz: 231e6c8aec4ff8096fa44efc8e409c8c5a563bc3
5
5
  SHA512:
6
- metadata.gz: 52d1ddfb5a9860ee19c7b5bc43f8945f039116e516db46547bb755f8c274c26d06ef5c3e9cd7538e042bf92bc0f20d093492da07fe83ce72f0a1cc06de0d92c5
7
- data.tar.gz: dc65de528f92a9d1863c0849d15dfe95a46accb3c4eba51b0e98f962549c4de4e7eb8e6a5f0b48c484be871f9392ebe8047f770f0c9809eb9621043bb9327439
6
+ metadata.gz: 63a22ba76537430d1a3d14984b0643dd132b3cb9ce08ec7cfd80b55ab74305f75ae6a69d3e99767f4cf11e373cd6304a0e09c07769d2b071ec2418cb79476f0d
7
+ data.tar.gz: 2683fde74bad9722225b186d87240d151912d515ed1f6e24b09f91ae3d32859b70242c45057549f5f201a72143373d023d31c9c16c89b007a79c2a4fdf4b8cc6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rydux (0.9.1)
4
+ rydux (0.9.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/lib/rydux/store.rb CHANGED
@@ -7,13 +7,21 @@ module Rydux
7
7
  @reducers = strap_reducers(reducers)
8
8
  end
9
9
 
10
- def subscribe(listener)
11
- @listeners << listener
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.delete(listener)
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] = v
68
+ if @state[k] != v # Only set state if it has actually changed
69
+ @state[k] = v
61
70
 
62
- if !self.methods.include? k
63
- self.define_singleton_method(k.to_sym) do
64
- return State.new(@state[k])
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
- notify_listeners
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
- if listener.respond_to? :state_changed
74
- listener.public_send(:state_changed, state)
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
@@ -1,3 +1,3 @@
1
1
  module Rydux
2
- VERSION = "0.9.2"
2
+ VERSION = "0.9.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rydux
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Dovzhanyn