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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 227768589d0be878a92145d23af95c075d3cf198
4
- data.tar.gz: 37220686166c5b47e4787751dc712ff19e96eae3
3
+ metadata.gz: ff4a7bee8b3d4dcba4b03571e76464b0ca723726
4
+ data.tar.gz: 30356e6ededd51f480f9b148f271228708339136
5
5
  SHA512:
6
- metadata.gz: 9bbd10687936395ac6da6b8948a7bf7d08fb3651eafc2692a21a708ca3a2a10b33aa7a8bcc12374ce4a05673dbc54f294decfaca9156be423e1670514771e50f
7
- data.tar.gz: ee787426f33df12e0a1f6fc352b8c7323f3f73fab99303c7395b17796449bd27e214e215544bb9e17b4c6d72687fa8db4c2484eb27b3618f996d66a050a1832a
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, last_dispatch_type)
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, last_dispatch_type)
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
@@ -1,7 +1,6 @@
1
1
  module Rydux
2
2
  class Reducer
3
3
  def self.map_state(action, state = {})
4
-
5
4
  end
6
5
  end
7
6
  end
data/lib/rydux/state.rb CHANGED
@@ -3,9 +3,8 @@ require 'ostruct'
3
3
  module Rydux
4
4
  class State < OpenStruct
5
5
  def initialize(state)
6
- state = state.clone
7
6
  super(state)
8
- @structure = state
7
+ @structure = state.clone
9
8
  end
10
9
 
11
10
  def to_s
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(combined_reducers)
6
- @state = {}
7
- @listeners = []
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
- def dispatch(action)
32
- @reducers.each do |k, reducer|
33
- new_state = {}
34
- new_state[k] = reducer.map_state(action, state[k])
35
- set_state(new_state, action[:type])
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 set_state(new_state, last_dispatch = nil)
46
- new_state.each do |k, v|
47
- @state[k] = v
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
- if !self.methods.include? k
50
- self.define_singleton_method(k.to_sym) do
51
- return State.new(state[k])
52
- end
53
- end
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
- notify_listeners(last_dispatch)
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(last_dispatch)
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, last_dispatch)
74
+ listener.public_send(:state_changed, state)
63
75
  end
64
76
  end
65
77
  end
data/lib/rydux/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rydux
2
- VERSION = "0.9.1"
2
+ VERSION = "0.9.2"
3
3
  end
data/lib/rydux.rb CHANGED
@@ -2,7 +2,6 @@ require "rydux/version"
2
2
  require "rydux/store"
3
3
  require "rydux/state"
4
4
  require "rydux/reducer"
5
- require "rydux/rydux"
6
5
 
7
6
  module Rydux
8
7
  end
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.1
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-28 00:00:00.000000000 Z
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
data/lib/rydux/rydux.rb DELETED
@@ -1,9 +0,0 @@
1
- module Rydux
2
- class Rydux
3
-
4
- def self.dispatch(action)
5
-
6
- end
7
-
8
- end
9
- end