Skip to content

Commit

Permalink
Remove turbolinks from reactor, rename push_state and replace_state t…
Browse files Browse the repository at this point in the history
…o visit and use Turbo if available
  • Loading branch information
edelvalle committed Feb 26, 2021
1 parent 52b5107 commit 99c4d5f
Show file tree
Hide file tree
Showing 10 changed files with 524 additions and 189 deletions.
27 changes: 0 additions & 27 deletions reactor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,2 @@
from django.core.checks import Warning, register


default_app_config = 'reactor.app.Reactor'


@register()
def check_for_turbolinks_middleware(app_configs, **kwargs):
from . import settings
from django.conf import settings as dj_settings

reactor_middleware = 'reactor.middleware.turbolinks_middleware'
problem = (
settings.INCLUDE_TURBOLINKS and
reactor_middleware not in dj_settings.MIDDLEWARE
)
if problem:
return [Warning(
'Turbolinks middleware is missing',
hint=(
f'When you set REACTOR_INCLUDE_TURBOLINKS, during the '
f'redirects the URL of the browser could be that is not up to '
f'date. To avoid that include `{reactor_middleware}` in the '
f'`MIDDLEWARE`.'
),
id='reactor.W001',
)]
else:
return []
14 changes: 3 additions & 11 deletions reactor/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,9 @@ def remove(self, event):
self.receive_leave(event['id'])
self.send_json(dict(event, type='remove'))

def redirect(self, event):
log.debug(f"<<< REDIRECT {event['url']}")
self.send_json(dict(event, type='redirect'))

def push_state(self, event):
log.debug(f"<<< PUSH-STATE {event['url']}")
self.send_json(dict(event, type='push_state'))

def replace_state(self, event):
log.debug(f"<<< REPLACE-STATE {event['url'], event['title']}")
self.send_json(dict(event, type='replace_state'))
def visit(self, event):
log.debug(f"<<< VISIT {event['action']} {event['url']}")
self.send_json(event)

@classmethod
def decode_json(cls, text_data):
Expand Down
17 changes: 4 additions & 13 deletions reactor/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,13 @@ def destroy(self):
self._destroy_sent = True
send_to_channel(self._channel_name, 'remove', id=self.id)

def _send_redirect(self, url, *args, **kwargs):
url = resolve_url(url, *args, **kwargs)
def visit(self, url: str, action: str = 'advance', **kwargs):
url = resolve_url(url, **kwargs)
if self._channel_name:
send_to_channel(self._channel_name, 'push_state', url=url)
self.freeze()
else:
send_to_channel(self._channel_name, 'visit', url=url, action=action)
elif action == 'advance':
self._redirected_to = url

def _send_replace_state(self, url, _title=None, *args, **kwargs):
url = resolve_url(url, *args, **kwargs)
if self._channel_name:
send_to_channel(
self._channel_name, 'replace_state',
title=_title, url=url
)

def _send_parent(self, _name, **kwargs):
if self._parent_id:
self._send(_name, id=self._parent_id, **kwargs)
Expand Down
10 changes: 0 additions & 10 deletions reactor/middleware.py

This file was deleted.

42 changes: 20 additions & 22 deletions reactor/static/reactor/reactor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,16 @@ reactor_channel.on 'close', ->
el.classList.add('reactor-disconnected')


reactor_channel.on 'message', ({type, title, id, html_diff, url, component_types}) ->
reactor_channel.on 'message', ({type, id, html_diff, url, action, component_types}) ->
console.log '<<<', type.toUpperCase(), id or url or component_types
if type is 'components'
declare_components(component_types)
else if type is 'redirect'
window.location.assign url
else if type is 'push_state'
reactor.push_state url
else if type is 'replace_state'
history.replaceState {}, (title or document.title), url
else
el = document.getElementById(id)
if el?
if type is 'render'
el.apply_diff(html_diff)
else if type is 'remove'
window.requestAnimationFrame ->
el.remove()

switch type
when 'components' then declare_components(component_types)
when 'visit' then reactor.visit url, action: action
when 'render'
document.getElementById(id)?.apply_diff?(html_diff)
when 'remove'
window.requestAnimationFrame ->
document.getElementById(id)?.remove()

TRANSPILER_CACHE = {}

Expand Down Expand Up @@ -330,11 +321,18 @@ reactor.debounce = (delay_name, delay) -> (f) -> (...args) ->
clearTimeout _timeouts[delay_name]
_timeouts[delay_name] = setTimeout (=> f(...args)), delay

reactor.push_state = (url) ->
if Turbolinks?
Turbolinks.visit url
else
reactor.visit = (url, options) ->
try
switch options.action
when 'replace' then window.history.replaceState {}, document.title, url
when 'advance'
if Turbo?
Turbo.visit url, options
else
window.history.pushState {}, document.title, url
catch
window.location.assign url


reactor_channel.open()

Loading

0 comments on commit 99c4d5f

Please sign in to comment.