comparison libervia/pages/_browser/bridge.py @ 1314:8998f01088ac

browser (bridge): handle case where `callback` or `errback` is None
author Goffi <goffi@goffi.org>
date Sat, 01 Aug 2020 16:47:24 +0200
parents 999dccf0093e
children b28025a7cc28
comparison
equal deleted inserted replaced
1313:12aa95eeb409 1314:8998f01088ac
8 return lambda *args, **kwargs: self.call(attr, *args, **kwargs) 8 return lambda *args, **kwargs: self.call(attr, *args, **kwargs)
9 9
10 def on_load(self, xhr, ev, callback, errback): 10 def on_load(self, xhr, ev, callback, errback):
11 if xhr.status == 200: 11 if xhr.status == 200:
12 ret = javascript.JSON.parse(xhr.response) 12 ret = javascript.JSON.parse(xhr.response)
13 if ret is None: 13 if callback is not None:
14 callback() 14 if ret is None:
15 else: 15 callback()
16 callback(ret) 16 else:
17 callback(ret)
17 elif xhr.status == 502: 18 elif xhr.status == 502:
18 # PROXY_ERROR is used for bridge error 19 # PROXY_ERROR is used for bridge error
19 ret = javascript.JSON.parse(xhr.response) 20 ret = javascript.JSON.parse(xhr.response)
20 errback(ret) 21 if errback is not None:
22 errback(ret)
21 else: 23 else:
22 print(f"bridge called failed: code: {xhr.response}, text: {xhr.statusText}") 24 print(f"bridge called failed: code: {xhr.response}, text: {xhr.statusText}")
23 errback({"fullname": "InternalError", "message": xhr.statusText}) 25 if errback is not None:
26 errback({"fullname": "InternalError", "message": xhr.statusText})
24 27
25 def call(self, method_name, *args, callback, errback, **kwargs): 28 def call(self, method_name, *args, callback, errback, **kwargs):
26 xhr = window.XMLHttpRequest.new() 29 xhr = window.XMLHttpRequest.new()
27 xhr.bind('load', lambda ev: self.on_load(xhr, ev, callback, errback)) 30 xhr.bind('load', lambda ev: self.on_load(xhr, ev, callback, errback))
28 xhr.bind('error', lambda ev: errback( 31 xhr.bind('error', lambda ev: errback(