isomorfeus-redux 4.1.15 → 4.1.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/redux/store.rb +199 -192
  3. data/lib/redux/version.rb +3 -3
  4. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb539389237dfbb5af5a4ed408b9471a01af6ec4dc386775ed0908efb58ab5e6
4
- data.tar.gz: 8a37475a2675ac98ad9b3c397b0f25a5ca8746b350be4d037f954e6137cc5200
3
+ metadata.gz: 1a46845466bba750b171d19966366ae639b0af9cd91682b5ae80a2778de5ff88
4
+ data.tar.gz: 41bd56cf5bc3ef23de58c71c05eec403346eb079b4bd8830d40b61811eb1c873
5
5
  SHA512:
6
- metadata.gz: 0d6a212238da834f2e137c1c7f56dfc077d0d0c97ae5471a1fd0159c5eff703a67f3b02a8347bdcb58eac4a4cc741de8f4c8462666b90109b755fbfb7c0f93da
7
- data.tar.gz: 71e666bcfaf227c4848dae451d02dc8e5dfb553625e5fd2caba0215b2c7a69b85b2b01aede2ec2af5ef0b67203329fbb824ca95bde21b2748c75fd127f892a31
6
+ metadata.gz: 2db38277c88d8492654800bf92b8cba1125d6d2e3cd42ac693fe0fa88ff994c6de3b6d1e3e04c2dc3916294d180d358b8ffc88571e373521886434f963445ea6
7
+ data.tar.gz: 28024774eb4559343c6cf2b2fc3545a2815f16445e2a082fb35fbc2837f63bee73f442fbd8e3e0355cfee1108d12c6b706aa03475c86ca150cfde2e4ec3187e2
data/lib/redux/store.rb CHANGED
@@ -1,192 +1,199 @@
1
- module Redux
2
- class Store
3
- include Native::Wrapper
4
-
5
- class << self
6
- def add_middleware(middleware)
7
- if Isomorfeus.store
8
- `console.warning("Adding middleware after Store initialization may have side effects! Saving state and initializing new store with restored state!")`
9
- middlewares << middleware
10
- preloaded_state = Isomorfeus.store.get_state
11
- init!
12
- else
13
- middlewares << middleware
14
- end
15
- end
16
-
17
- def add_reducer(reducer)
18
- if Isomorfeus.store
19
- # if the store has been initalized already, add the reducer to the instance
20
- Isomorfeus.store.add_reducer(reducer)
21
- else
22
- # otherwise just add it to the reducers, so that they will be used when initializing the store
23
- preloaded_state[reducer.keys.first] = {} unless preloaded_state.key?(reducer.keys.first)
24
- reducers.merge!(reducer)
25
- end
26
- end
27
-
28
- def add_reducers(new_reducers)
29
- if Isomorfeus.store
30
- # if the store has been initalized already, add the reducer to the instance
31
- Isomorfeus.store.add_reducers(new_reducers)
32
- else
33
- # otherwise just add it to the reducers, so that they will be used when initializing the store
34
- new_reducers.each do |key, value|
35
- add_reducer(key => value)
36
- end
37
- end
38
- end
39
-
40
- # called from Isomorfeus.init
41
- def init!
42
- next_reducer = Redux.combine_reducers(@reducers)
43
- if middlewares.any?
44
- enhancer = Redux.apply_middleware(middlewares)
45
- Redux::Store.new(next_reducer, preloaded_state, enhancer)
46
- else
47
- Redux::Store.new(next_reducer, preloaded_state)
48
- end
49
- end
50
-
51
- def middlewares
52
- @middlewares ||= []
53
- end
54
-
55
- def preloaded_state_merge!(ruby_hash)
56
- preloaded_state.merge!(ruby_hash)
57
- end
58
-
59
- def preloaded_state
60
- @preloaded_state ||= {}
61
- end
62
-
63
- def preloaded_state=(ruby_hash)
64
- @preloaded_state = ruby_hash
65
- end
66
-
67
- def reducers
68
- @reducers ||= {}
69
- end
70
- end
71
-
72
- def initialize(reducer, preloaded_state = nil, enhancer = nil)
73
- @deferred_actions = {}
74
- @deferred_dispatcher = nil
75
- @last_dispatch_time = Time.now
76
- %x{
77
- var ogre = Opal.global.Redux;
78
- var compose = (typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) || ogre.compose;
79
- if (enhancer == nil) { enhancer = null; }
80
- if (typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION__) {
81
- var devext_enhance = window.__REDUX_DEVTOOLS_EXTENSION__();
82
- if (enhancer) { enhancer = compose([enhancer, devext_enhance]); }
83
- else { enhancer = devext_enhance; }
84
- }
85
- if (preloaded_state == nil) {
86
- preloaded_state = null;
87
- } else if (typeof preloaded_state.$class === "function" && preloaded_state.$class() == "Hash") {
88
- if (preloaded_state.$size() == 0) {
89
- preloaded_state = null;
90
- } else {
91
- preloaded_state = preloaded_state.$to_n();
92
- }
93
- }
94
- if (enhancer && preloaded_state) {
95
- this.native = ogre.createStore(reducer, preloaded_state, enhancer);
96
- } else if (preloaded_state) {
97
- this.native = ogre.createStore(reducer, preloaded_state);
98
- } else if (enhancer) {
99
- this.native = ogre.createStore(reducer, enhancer);
100
- } else {
101
- this.native = ogre.createStore(reducer);
102
- }
103
- }
104
- end
105
-
106
- def add_reducer(reducer)
107
- self.class.reducers.merge!(reducer)
108
- next_reducer = Redux.combine_reducers(self.class.reducers)
109
- replace_reducer(next_reducer)
110
- end
111
-
112
- def add_reducers(new_reducers)
113
- self.class.reducers.merge!(new_reducers)
114
- next_reducer = Redux.combine_reducers(self.class.reducers)
115
- replace_reducer(next_reducer)
116
- end
117
-
118
- def dispatch(action)
119
- %x{
120
- if (typeof action.$class === "function" && action.$class() == "Hash") {
121
- action = action.$to_n();
122
- }
123
- this.native.dispatch(action);
124
- }
125
- end
126
-
127
- def get_state
128
- %x{
129
- let res = this.native.getState();
130
- if (typeof(res) === 'object' && !Array.isArray(res) && res !== null) {
131
- return Opal.Hash.$new(res);
132
- }
133
- return res;
134
- }
135
- end
136
-
137
- def collect_and_defer_dispatch(action)
138
- if !Isomorfeus.on_ssr?
139
- type = action.delete(:type)
140
- @deferred_actions[type] = [] unless @deferred_actions.key?(type)
141
- @deferred_actions[type].push(action)
142
- @last_dispatch_time = `Date.now()`
143
- # `console.log(#@last_dispatch_time)`
144
- deferred_dispatcher(`Date.now()`) unless @deferred_dispatcher
145
- else
146
- dispatch(action)
147
- end
148
- nil
149
- end
150
-
151
- def replace_reducer(next_reducer)
152
- `this.native.replaceReducer(next_reducer)`
153
- end
154
-
155
- # returns function needed to unsubscribe the listener
156
- def subscribe(&listener)
157
- `this.native.subscribe(function() { return listener.$call(); })`
158
- end
159
-
160
- private
161
-
162
- def deferred_dispatcher(first)
163
- @deferred_dispatcher = true
164
- %x{
165
- setTimeout(function() {
166
- if (#{wait_longer?(first)}) { #{deferred_dispatcher(first)} }
167
- else { #{dispatch_deferred_dispatches} }
168
- }, 10)
169
- }
170
- end
171
-
172
- def dispatch_deferred_dispatches
173
- # `console.log(Date.now())`
174
- @deferred_dispatcher = false
175
- actions = @deferred_actions
176
- @deferred_actions = {}
177
- actions.each do |type, data|
178
- dispatch(type: type, collected: data)
179
- end
180
- end
181
-
182
- def wait_longer?(first)
183
- t = `Date.now()`
184
- time_since_first = `t - first`
185
- # `console.log('delta', time_since_first)`
186
- return true if `typeof Opal.Preact !== 'undefined' && typeof Opal.Preact.render_buffer !== 'undefined' && Opal.Preact.render_buffer.length > 0 && time_since_first < 1000`
187
- return false if time_since_first > 100 # ms
188
- return false if (`t - #@last_dispatch_time`) > 9 # ms
189
- return true
190
- end
191
- end
192
- end
1
+ module Redux
2
+ class Store
3
+ include Native::Wrapper
4
+
5
+ class << self
6
+ def add_middleware(middleware)
7
+ if Isomorfeus.store
8
+ `console.warning("Adding middleware after Store initialization may have side effects! Saving state and initializing new store with restored state!")`
9
+ middlewares << middleware
10
+ preloaded_state = Isomorfeus.store.get_state
11
+ init!
12
+ else
13
+ middlewares << middleware
14
+ end
15
+ end
16
+
17
+ def add_reducer(reducer)
18
+ if Isomorfeus.store
19
+ # if the store has been initalized already, add the reducer to the instance
20
+ Isomorfeus.store.add_reducer(reducer)
21
+ else
22
+ # otherwise just add it to the reducers, so that they will be used when initializing the store
23
+ preloaded_state[reducer.keys.first] = {} unless preloaded_state.key?(reducer.keys.first)
24
+ reducers.merge!(reducer)
25
+ end
26
+ end
27
+
28
+ def add_reducers(new_reducers)
29
+ if Isomorfeus.store
30
+ # if the store has been initalized already, add the reducer to the instance
31
+ Isomorfeus.store.add_reducers(new_reducers)
32
+ else
33
+ # otherwise just add it to the reducers, so that they will be used when initializing the store
34
+ new_reducers.each do |key, value|
35
+ add_reducer(key => value)
36
+ end
37
+ end
38
+ end
39
+
40
+ # called from Isomorfeus.init
41
+ def init!
42
+ next_reducer = Redux.combine_reducers(@reducers)
43
+ if middlewares.any?
44
+ enhancer = Redux.apply_middleware(middlewares)
45
+ Redux::Store.new(next_reducer, preloaded_state, enhancer)
46
+ else
47
+ Redux::Store.new(next_reducer, preloaded_state)
48
+ end
49
+ end
50
+
51
+ def middlewares
52
+ @middlewares ||= []
53
+ end
54
+
55
+ def preloaded_state_merge!(ruby_hash)
56
+ preloaded_state.merge!(ruby_hash)
57
+ end
58
+
59
+ def preloaded_state
60
+ @preloaded_state ||= {}
61
+ end
62
+
63
+ def preloaded_state=(ruby_hash)
64
+ @preloaded_state = ruby_hash
65
+ end
66
+
67
+ def reducers
68
+ @reducers ||= {}
69
+ end
70
+ end
71
+
72
+ def initialize(reducer, preloaded_state = nil, enhancer = nil)
73
+ @deferred_actions = {}
74
+ @deferred_dispatcher = nil
75
+ @last_dispatch_time = Time.now
76
+ %x{
77
+ var ogre = Opal.global.Redux;
78
+ var compose = (typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) || ogre.compose;
79
+ if (enhancer == nil) { enhancer = null; }
80
+ if (typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION__) {
81
+ var devext_enhance = window.__REDUX_DEVTOOLS_EXTENSION__();
82
+ if (enhancer) { enhancer = compose([enhancer, devext_enhance]); }
83
+ else { enhancer = devext_enhance; }
84
+ }
85
+ if (preloaded_state == nil) {
86
+ preloaded_state = null;
87
+ } else if (typeof preloaded_state.$class === "function" && preloaded_state.$class() == "Hash") {
88
+ if (preloaded_state.$size() == 0) {
89
+ preloaded_state = null;
90
+ } else {
91
+ preloaded_state = preloaded_state.$to_n();
92
+ }
93
+ }
94
+ if (enhancer && preloaded_state) {
95
+ this.native = ogre.createStore(reducer, preloaded_state, enhancer);
96
+ } else if (preloaded_state) {
97
+ this.native = ogre.createStore(reducer, preloaded_state);
98
+ } else if (enhancer) {
99
+ this.native = ogre.createStore(reducer, enhancer);
100
+ } else {
101
+ this.native = ogre.createStore(reducer);
102
+ }
103
+ }
104
+ end
105
+
106
+ def add_reducer(reducer)
107
+ self.class.reducers.merge!(reducer)
108
+ next_reducer = Redux.combine_reducers(self.class.reducers)
109
+ replace_reducer(next_reducer)
110
+ end
111
+
112
+ def add_reducers(new_reducers)
113
+ self.class.reducers.merge!(new_reducers)
114
+ next_reducer = Redux.combine_reducers(self.class.reducers)
115
+ replace_reducer(next_reducer)
116
+ end
117
+
118
+ def dispatch(action)
119
+ %x{
120
+ if (typeof action.$class === "function" && action.$class() == "Hash") {
121
+ action = action.$to_n();
122
+ }
123
+ this.native.dispatch(action);
124
+ }
125
+ end
126
+
127
+ def get_state
128
+ %x{
129
+ let res = this.native.getState();
130
+ if (typeof(res) === 'object' && !Array.isArray(res) && res !== null) {
131
+ return Opal.Hash.$new(res);
132
+ }
133
+ return res;
134
+ }
135
+ end
136
+
137
+ def recently_dispatched?
138
+ res = @dispatched
139
+ @dispatched = false
140
+ res
141
+ end
142
+
143
+ def collect_and_defer_dispatch(action)
144
+ if !Isomorfeus.on_ssr?
145
+ type = action.delete(:type)
146
+ @deferred_actions[type] = [] unless @deferred_actions.key?(type)
147
+ @deferred_actions[type].push(action)
148
+ @last_dispatch_time = `Date.now()`
149
+ # `console.log(#@last_dispatch_time)`
150
+ deferred_dispatcher(`Date.now()`) unless @deferred_dispatcher
151
+ else
152
+ @dispatched = true
153
+ dispatch(action)
154
+ end
155
+ nil
156
+ end
157
+
158
+ def replace_reducer(next_reducer)
159
+ `this.native.replaceReducer(next_reducer)`
160
+ end
161
+
162
+ # returns function needed to unsubscribe the listener
163
+ def subscribe(&listener)
164
+ `this.native.subscribe(function() { return listener.$call(); })`
165
+ end
166
+
167
+ private
168
+
169
+ def deferred_dispatcher(first)
170
+ @deferred_dispatcher = true
171
+ %x{
172
+ setTimeout(function() {
173
+ if (#{wait_longer?(first)}) { #{deferred_dispatcher(first)} }
174
+ else { #{dispatch_deferred_dispatches} }
175
+ }, 10)
176
+ }
177
+ end
178
+
179
+ def dispatch_deferred_dispatches
180
+ # `console.log(Date.now())`
181
+ @deferred_dispatcher = false
182
+ actions = @deferred_actions
183
+ @deferred_actions = {}
184
+ actions.each do |type, data|
185
+ dispatch(type: type, collected: data)
186
+ end
187
+ end
188
+
189
+ def wait_longer?(first)
190
+ t = `Date.now()`
191
+ time_since_first = `t - first`
192
+ # `console.log('delta', time_since_first)`
193
+ return true if `typeof Opal.Preact !== 'undefined' && typeof Opal.Preact.render_buffer !== 'undefined' && Opal.Preact.render_buffer.length > 0 && time_since_first < 1000`
194
+ return false if time_since_first > 100 # ms
195
+ return false if (`t - #@last_dispatch_time`) > 9 # ms
196
+ return true
197
+ end
198
+ end
199
+ end
data/lib/redux/version.rb CHANGED
@@ -1,3 +1,3 @@
1
- module Redux
2
- VERSION = '4.1.15'
3
- end
1
+ module Redux
2
+ VERSION = '4.1.16'
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: isomorfeus-redux
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.15
4
+ version: 4.1.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Biedermann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-12 00:00:00.000000000 Z
11
+ date: 2022-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.14.9
33
+ version: 0.14.10
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.14.9
40
+ version: 0.14.10
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: isomorfeus-speednode
43
43
  requirement: !ruby/object:Gem::Requirement