render_async 2.0.2 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c7bdbe323ba586e914af5f41c95a452317c5018b
4
- data.tar.gz: ebc1f2cdfe99db8e749249ec3f1b89bd52aa3bcb
3
+ metadata.gz: bc0f4801636b4b88873e40623ea250d27f35ee11
4
+ data.tar.gz: e4407a2cd9bfd7cf409dfe0d6846446bb6370db7
5
5
  SHA512:
6
- metadata.gz: 1d1c360782f7ea323773761617141c62a85f42c54e692e9ca9a0243ad03eb0e083073bb3d848511a0501001b52a47d30763edf9ba16afaccaeb9e7d53e872d06
7
- data.tar.gz: ea5c1b9f074e5d55276a26f694883daeb42dd93ce9ecc13b2eb4e271cc6212e30c94f0dc22501324fe4294ff6ac094ebca0cb8cf471fe30f87c4279aa92c1db1
6
+ metadata.gz: 5ea2036461e908f64a6d3ee180a28663052f31393f1a7c600cc8a3ec7319b2bfbd4a6c7e5534175f933accc944c154d3b2e6629e69f0feaf71b46f7b02f2b343
7
+ data.tar.gz: d4df153331955b0bef1111e5a9095f8ab01568bd69bbfe1bca014a5423e7d71f11859658ec27d2a356b565ce25346c8331b30ad57fb7d7b60f16ba18941e1738
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![Test Coverage](https://codeclimate.com/github/renderedtext/render_async/badges/coverage.svg)](https://codeclimate.com/github/renderedtext/render_async/coverage)
6
6
  [![Help Contribute to Open Source](https://www.codetriage.com/renderedtext/render_async/badges/users.svg)](https://www.codetriage.com/renderedtext/render_async)
7
7
 
8
- ![render_async](https://semaphoreci.com/blog/assets/images/2017-06-08/speed-up-rendering-rails-pages-with-render-async-6c40eb39.png)
8
+ ![render_async](http://s2blog.wpengine.com/wp-content/uploads/assets/images/2017-06-08/speed-up-rendering-rails-pages-with-render-async.png)
9
9
 
10
10
  # render_async
11
11
 
@@ -79,6 +79,8 @@ Advanced usage includes information on different options, such as:
79
79
  - [Passing in an HTML element name](#passing-in-an-html-element-name)
80
80
  - [Passing in a placeholder](#passing-in-a-placeholder)
81
81
  - [Passing in an event name](#passing-in-an-event-name)
82
+ - [Retry on failure](#retry-on-failure)
83
+ - [Polling](#polling)
82
84
  - [Handling errors](#handling-errors)
83
85
  - [Caching](#caching)
84
86
  - [Doing non-GET requests](#doing-non-get-requests)
@@ -233,6 +235,42 @@ document.addEventListener("users-loaded", function() {
233
235
  NOTE: Dispatching events is also supported for older browsers that don't
234
236
  support Event constructor.
235
237
 
238
+ ### Retry on failure
239
+
240
+ `render_async` can retry your requests if they fail for some reason.
241
+
242
+ If you want `render_async` to retry a request for number of times, you can do
243
+ this:
244
+ ```erb
245
+ <%= render_async users_path, retry_count: 5, error_message: "Couldn't fetch it" %>
246
+ ```
247
+
248
+ Now render_async will retry `users_path` for 5 times. If it succedes in
249
+ between, it will stop with dispatching requests. If it fails after 5 times,
250
+ it will show an [error message](#handling-errors) which you need to specify.
251
+
252
+ This can show useful when you know your requests often fail, and you don't want
253
+ to refresh the whole page just to retry them.
254
+
255
+ ### Polling
256
+
257
+ You can call `render_async` with interval argument. This will make render_async
258
+ call specified path at specified interval.
259
+
260
+ By doing this:
261
+ ```erb
262
+ <%= render_async comments_path, interval: 5000 %>
263
+ ```
264
+ You are telling `render_async` to fetch comments_path every 5 seconds.
265
+
266
+ This can be handy if you want to enable polling for a specific URL.
267
+
268
+ NOTE: By passing interval to `render_async`, initial container element
269
+ will remain in HTML tree, it will not be replaced with request response.
270
+ You can handle how that container element is rendered and its style by
271
+ [passing in an HTML element name](#passing-in-an-html-element-name) and
272
+ [HTML element class](#passing-in-a-container-class-name).
273
+
236
274
  ### Handling errors
237
275
 
238
276
  `render_async` let's you handle errors by allowing you to pass in `error_message`
@@ -18,6 +18,8 @@
18
18
  headers: headers,
19
19
  error_message: error_message,
20
20
  error_event_name: error_event_name,
21
+ retry_count: retry_count,
22
+ interval: interval,
21
23
  turbolinks: RenderAsync.configuration.turbolinks } %>
22
24
  <% else %>
23
25
  <%= render partial: 'render_async/request_vanilla',
@@ -30,6 +32,8 @@
30
32
  headers: headers,
31
33
  error_message: error_message,
32
34
  error_event_name: error_event_name,
35
+ retry_count: retry_count,
36
+ interval: interval,
33
37
  turbolinks: RenderAsync.configuration.turbolinks } %>
34
38
  <% end %>
35
39
  <% end %>
@@ -6,7 +6,7 @@ if (window.jQuery) {
6
6
  }
7
7
  <% end %>
8
8
 
9
- var _listener = function() {
9
+ var _listener = function(currentRetryCount) {
10
10
  var headers = <%= headers.to_json.html_safe %>;
11
11
  var csrfTokenElement = document.querySelector('meta[name="csrf-token"]')
12
12
  if (csrfTokenElement)
@@ -18,7 +18,12 @@ if (window.jQuery) {
18
18
  data: "<%= escape_javascript(data.to_s.html_safe) %>",
19
19
  headers: headers
20
20
  }).done(function(response) {
21
- $("#<%= container_id %>").replaceWith(response);
21
+ <% if interval %>
22
+ $("#<%= container_id %>").empty();
23
+ $("#<%= container_id %>").append(response);
24
+ <% else %>
25
+ $("#<%= container_id %>").replaceWith(response);
26
+ <% end %>
22
27
 
23
28
  <% if event_name.present? %>
24
29
  var event = undefined;
@@ -31,6 +36,13 @@ if (window.jQuery) {
31
36
  document.dispatchEvent(event);
32
37
  <% end %>
33
38
  }).fail(function(response) {
39
+ var skipErrorMessage = false;
40
+ <% if retry_count > 0 %>
41
+ skipErrorMessage = retry(currentRetryCount)
42
+ <% end %>
43
+
44
+ if (skipErrorMessage) return;
45
+
34
46
  $("#<%= container_id %>").replaceWith('<%= error_message.try(:html_safe) %>');
35
47
 
36
48
  <% if error_event_name.present? %>
@@ -46,8 +58,29 @@ if (window.jQuery) {
46
58
  });
47
59
  };
48
60
 
61
+ <% if retry_count > 0 %>
62
+ var retry = function(currentRetryCount) {
63
+ if (typeof(currentRetryCount) === 'number') {
64
+ if (currentRetryCount >= <%= retry_count %>)
65
+ return false;
66
+
67
+ _listener(currentRetryCount + 1);
68
+ return true;
69
+ }
70
+
71
+ _listener(1);
72
+ return true;
73
+ }
74
+ <% end %>
75
+
49
76
  <% if turbolinks %>
50
77
  $(document).one('turbolinks:load', _listener);
78
+ <% elsif interval %>
79
+ var _intervalFunction = function() {
80
+ _listener();
81
+ setInterval(_listener, <%= interval %>);
82
+ }
83
+ $(document).ready(_intervalFunction);
51
84
  <% else %>
52
85
  $(document).ready(_listener);
53
86
  <% end %>
@@ -5,7 +5,7 @@
5
5
  }
6
6
  <% end %>
7
7
 
8
- var _listener = function() {
8
+ var _listener = function(currentRetryCount) {
9
9
  var request = new XMLHttpRequest();
10
10
  var asyncRequest = true;
11
11
  var SUCCESS = 200;
@@ -26,7 +26,11 @@
26
26
  if (request.readyState === 4) {
27
27
  if (request.status >= SUCCESS && request.status < ERROR) {
28
28
  var container = document.getElementById('<%= container_id %>');
29
+ <% if interval %>
30
+ container.innerHTML = request.response;
31
+ <% else %>
29
32
  container.outerHTML = request.response;
33
+ <% end %>
30
34
 
31
35
  <% if event_name.present? %>
32
36
  var event = undefined;
@@ -39,6 +43,13 @@
39
43
  document.dispatchEvent(event);
40
44
  <% end %>
41
45
  } else {
46
+ var skipErrorMessage = false;
47
+ <% if retry_count > 0 %>
48
+ skipErrorMessage = retry(currentRetryCount)
49
+ <% end %>
50
+
51
+ if (skipErrorMessage) return;
52
+
42
53
  var container = document.getElementById('<%= container_id %>');
43
54
  container.outerHTML = '<%= error_message.try(:html_safe) %>';
44
55
 
@@ -60,11 +71,32 @@
60
71
  request.send(body);
61
72
  };
62
73
 
74
+ <% if retry_count > 0 %>
75
+ var retry = function(currentRetryCount) {
76
+ if (typeof(currentRetryCount) === 'number') {
77
+ if (currentRetryCount >= <%= retry_count %>)
78
+ return false;
79
+
80
+ _listener(currentRetryCount + 1);
81
+ return true;
82
+ }
83
+
84
+ _listener(1);
85
+ return true;
86
+ }
87
+ <% end %>
88
+
63
89
  <% if turbolinks %>
64
90
  document.addEventListener("turbolinks:load", function (e) {
65
91
  e.target.removeEventListener(e.type, arguments.callee);
66
92
  _listener.call(this);
67
93
  });
94
+ <% elsif interval %>
95
+ var _intervalFunction = function() {
96
+ _listener();
97
+ setInterval(_listener, <%= interval %>);
98
+ }
99
+ document.addEventListener("DOMContentLoaded", _intervalFunction);
68
100
  <% else %>
69
101
  document.addEventListener("DOMContentLoaded", _listener);
70
102
  <% end %>
@@ -1,3 +1,3 @@
1
1
  module RenderAsync
2
- VERSION = "2.0.2".freeze
2
+ VERSION = "2.1.0".freeze
3
3
  end
@@ -28,6 +28,8 @@ module RenderAsync
28
28
  headers = options.delete(:headers) || {}
29
29
  error_message = options.delete(:error_message)
30
30
  error_event_name = options.delete(:error_event_name)
31
+ retry_count = options.delete(:retry_count) || 0
32
+ interval = options.delete(:interval)
31
33
 
32
34
  render 'render_async/render_async', html_element_name: html_element_name,
33
35
  container_id: container_id,
@@ -40,7 +42,9 @@ module RenderAsync
40
42
  data: data,
41
43
  headers: headers,
42
44
  error_message: error_message,
43
- error_event_name: error_event_name
45
+ error_event_name: error_event_name,
46
+ retry_count: retry_count,
47
+ interval: interval
44
48
  end
45
49
 
46
50
  private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: render_async
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Grubbe
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2019-01-04 00:00:00.000000000 Z
12
+ date: 2019-05-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake