Last active
June 10, 2021 18:19
-
-
Save amcgregor/7ce55806409c14ec4c3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Client-side request management utility. | |
# | |
from org.transcrypt.stubs.browser import __pragma__ | |
from illico.common.vendor import promise | |
__pragma__ ('kwargs') # We use **kwargs in this module. | |
__pragma__('skip') # Avoid complaining about undefined global JavaScript symbols during static tests. | |
# TODO: Mock these as appropriate and import the mocks instead. | |
window = 0 | |
__pragma__('noskip') | |
class Progress: | |
def __init__(self, name, value=0, total=0): | |
self._value = 0 | |
self._total = total | |
wrapper = self.wrapper = document.createElement('div') | |
wrapper.id = name + '-wrapper' | |
wrapper.classList.add('progress') | |
bar = self.bar = document.createElement('div') | |
bar.id = name | |
bar.classList.add('progress-bar') | |
wrapper.appendChild(bar) | |
def attach(self, target): | |
target.appendChild(self.wrapper) | |
return self | |
def getValue(self): | |
return self._value | |
def setValue(self, value): | |
self._value = value | |
self._invalidate() | |
value = property(getValue, setValue) | |
def getTotal(self): | |
return self._total | |
def setTotal(self, value): | |
self._total = value | |
self._invalidate() | |
total = property(getTotal, setTotal) | |
def getPercentage(self): | |
if self._total is 0: | |
return 100 | |
return self._value / self._total * 100 | |
percentage = property(getPercentage) | |
def _invalidate(self): | |
self.bar.style.width = str(self.percentage) + '%' | |
# Dynamic Request (XHR) Manager | |
class RequestManager: | |
# This is the primary entry point for XHR request triggering. | |
# It keeps track of the pending requests and indicates progress to the user. | |
# It also handles reporting errors back to the server for capture and logging. | |
def __init__(self): | |
self.hooks = [] | |
self.progress = Progress('rq') | |
def start(self, context=None): | |
window.console.log("RequestManager starting.") | |
if not document.getElementById('rq'): | |
self.progress.attach(document.body) | |
def bindEvents(self, context=None): | |
pass | |
#window.setInterval(self._debug_inc, 1000) | |
#self.progress.total = 12 | |
def _debug_inc(self): | |
if not self.progress.total: | |
return | |
if self.progress.value is self.progress.total: | |
self.progress.value = 0 | |
return | |
self.progress.value += 1 | |
def head(self, url, data=None, headers={}): | |
return self.request('HEAD', url, data, headers) | |
def get(self, url, data=None, headers={}): | |
return self.request('GET', url, data, headers) | |
def post(self, url, data=None, headers={}): | |
return self.request('POST', url, data, headers) | |
def put(self, url, data=None, headers={}): | |
return self.request('PUT', url, data, headers) | |
def patch(self, url, data=None, headers={}): | |
return self.request('PATCH', url, data, headers) | |
def delete(self, url, headers={}): | |
return self.request('DELETE', url, headers=headers) | |
def request(self, method, url, data=None, headers={}): | |
headers = copy(headers) | |
headers['X-Requested-With'] = 'XMLHttpRequest' # Ensure this is always included. | |
request = promise.ajax(method, url, data, headers) | |
self.progress.total += 1 | |
request.then(self._request_done) | |
return request | |
def _request_done(self, error, text, xhr): | |
self.progress.value += 1 | |
window.setTimeout(self._clear_tick, 2500) | |
def _clear_tick(self, e): | |
window.console.debug("Clearing stale load state.") | |
self.progress._value -= 1 # Skip invalidation on this one. | |
self.progress.total -= 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment