Skip to content

Retry with delay and control polling

Compare
Choose a tag to compare
@nikolalsvk nikolalsvk released this 01 Aug 09:07
· 46 commits to master since this release

🎉 New version of render_async is out! In the new version, there are a couple of new features:

  • Retry render_async after some time
  • Control polling by dispatching events
  • Customize content_for name

👾 P.S. There is a render_async Discord server, please join and let's make render_async even better!

♻️ Retry render_async after some time

If you want to retry requests but with some delay in between the calls, you can pass a retry_delay option together with retry_count like so:

<%= render_async users_path,
                 retry_count: 5,
                 retry_delay: 2000 %>

This will make render_async wait for 2 seconds before retrying after each failure. In the end, if the request is still failing after the 5th time, it will dispatch a default error event.

You can read more about this feature in the README here

⏯️ Control polling by dispatching events

You can now control polling with by dispatching these 2 events:

  • 'async-stop' - this will stop polling
  • 'async-start' - this will start polling.

💡 Please note that events need to be dispatched to a render_async container.

An example of how you can do this looks like this:

<%= render_async wave_render_async_path,
                 container_id: 'controllable-interval', # set container_id so we can get it later easily
                 interval: 3000 %>

<button id='stop-polling'>Stop polling</button>
<button id='start-polling'>Start polling</button>

<script>
  var container = document.getElementById('controllable-interval')
  var stopPolling = document.getElementById('stop-polling')
  var startPolling = document.getElementById('start-polling')

  var triggerEventOnContainer = function(eventName) {
    var event = new Event(eventName);

    container.dispatchEvent(event)
  }

  stopPolling.addEventListener('click', function() {
    container.innerHTML = '<p>Polling stopped</p>'
    triggerEventOnContainer('async-stop')
  })
  startPolling.addEventListener('click', function() {
    triggerEventOnContainer('async-start')
  })
</script>

You can read more about it in the Controlled polling section of the README.

Customize content_for name

The content_for name may be customized by passing the content_for_name option to render_async.
This option is especially useful when doing nested async renders to better control the location of the injected JavaScript.

For example:

<%= render_async comment_stats_path, content_for_name: :render_async_comment_stats %>

<%= content_for :render_async_comment_stats %>

This explanation is also available in the README

That's all folks, catch you in the next one!

👾 P.S. There is a render_async Discord server, please join and let's make render_async even better!