|
| 1 | +// aal.js |
1 | 2 | paling = { |
2 | 3 | _host: window.location.origin, |
3 | 4 |
|
@@ -103,63 +104,86 @@ paling = { |
103 | 104 | } |
104 | 105 | }, |
105 | 106 |
|
106 | | - _init: function () { |
107 | | - paling._mock_py_functions(); |
| 107 | + setWebsocketAutoReconnect: function (reconnect = false) { |
| 108 | + paling._websocket_auto_reconnect = reconnect; |
| 109 | + }, |
108 | 110 |
|
109 | | - document.addEventListener("DOMContentLoaded", function (event) { |
110 | | - let page = window.location.pathname.substring(1); |
111 | | - paling._position_window(page); |
| 111 | + setWebsocketAutoReconnectTimeout: function (timeout = 1000) { |
| 112 | + paling._websocket_reconnect_timeout = timeout; |
| 113 | + }, |
112 | 114 |
|
113 | | - let websocket_addr = (paling._host + '/paling').replace('http', 'ws'); |
114 | | - websocket_addr += ('?page=' + page); |
115 | | - paling._websocket = new WebSocket(websocket_addr); |
| 115 | + _websocket_auto_reconnect: false, |
| 116 | + _websocket_reconnect_timeout: 1000, |
116 | 117 |
|
117 | | - paling._websocket.onopen = function () { |
118 | | - for (let i = 0; i < paling._py_functions.length; i++) { |
119 | | - let py_function = paling._py_functions[i]; |
120 | | - paling._import_py_function(py_function); |
121 | | - } |
| 118 | + _init_websocket: function () { |
| 119 | + let page = window.location.pathname.substring(1); |
| 120 | + paling._position_window(page); |
122 | 121 |
|
123 | | - while (paling._mock_queue.length > 0) { |
124 | | - let call = paling._mock_queue.shift(); |
125 | | - paling._websocket.send(paling._toJSON(call)); |
| 122 | + let websocket_addr = (paling._host + '/paling').replace('http', 'ws'); |
| 123 | + websocket_addr += ('?page=' + page); |
| 124 | + paling._websocket = new WebSocket(websocket_addr); |
| 125 | + |
| 126 | + paling._websocket.onopen = function () { |
| 127 | + for (let i = 0; i < paling._py_functions.length; i++) { |
| 128 | + let py_function = paling._py_functions[i]; |
| 129 | + paling._import_py_function(py_function); |
| 130 | + } |
| 131 | + |
| 132 | + while (paling._mock_queue.length > 0) { |
| 133 | + let call = paling._mock_queue.shift(); |
| 134 | + paling._websocket.send(paling._toJSON(call)); |
| 135 | + } |
| 136 | + }; |
| 137 | + |
| 138 | + paling._websocket.onmessage = function (e) { |
| 139 | + let message = JSON.parse(e.data); |
| 140 | + if (message.hasOwnProperty('call')) { |
| 141 | + // Python making a function call into us |
| 142 | + if (message.name in paling._exposed_functions) { |
| 143 | + try { |
| 144 | + let return_val = paling._exposed_functions[message.name](...message.args); |
| 145 | + paling._websocket.send(paling._toJSON({ 'return': message.call, 'status': 'ok', 'value': return_val })); |
| 146 | + } catch (err) { |
| 147 | + debugger |
| 148 | + paling._websocket.send(paling._toJSON( |
| 149 | + { |
| 150 | + 'return': message.call, |
| 151 | + 'status': 'error', |
| 152 | + 'error': err.message, |
| 153 | + 'stack': err.stack |
| 154 | + })); |
| 155 | + } |
126 | 156 | } |
127 | | - }; |
128 | | - |
129 | | - paling._websocket.onmessage = function (e) { |
130 | | - let message = JSON.parse(e.data); |
131 | | - if (message.hasOwnProperty('call')) { |
132 | | - // Python making a function call into us |
133 | | - if (message.name in paling._exposed_functions) { |
134 | | - try { |
135 | | - let return_val = paling._exposed_functions[message.name](...message.args); |
136 | | - paling._websocket.send(paling._toJSON({ 'return': message.call, 'status': 'ok', 'value': return_val })); |
137 | | - } catch (err) { |
138 | | - debugger |
139 | | - paling._websocket.send(paling._toJSON( |
140 | | - { |
141 | | - 'return': message.call, |
142 | | - 'status': 'error', |
143 | | - 'error': err.message, |
144 | | - 'stack': err.stack |
145 | | - })); |
146 | | - } |
| 157 | + } else if (message.hasOwnProperty('return')) { |
| 158 | + // Python returning a value to us |
| 159 | + if (message['return'] in paling._call_return_callbacks) { |
| 160 | + if (message['status'] === 'ok') { |
| 161 | + paling._call_return_callbacks[message['return']].resolve(message.value); |
147 | 162 | } |
148 | | - } else if (message.hasOwnProperty('return')) { |
149 | | - // Python returning a value to us |
150 | | - if (message['return'] in paling._call_return_callbacks) { |
151 | | - if (message['status'] === 'ok') { |
152 | | - paling._call_return_callbacks[message['return']].resolve(message.value); |
153 | | - } |
154 | | - else if (message['status'] === 'error' && paling._call_return_callbacks[message['return']].reject) { |
155 | | - paling._call_return_callbacks[message['return']].reject(message['error']); |
156 | | - } |
| 163 | + else if (message['status'] === 'error' && paling._call_return_callbacks[message['return']].reject) { |
| 164 | + paling._call_return_callbacks[message['return']].reject(message['error']); |
157 | 165 | } |
158 | | - } else { |
159 | | - throw 'Invalid message ' + message; |
160 | 166 | } |
| 167 | + } else { |
| 168 | + throw 'Invalid message ' + message; |
| 169 | + } |
| 170 | + |
| 171 | + }; |
| 172 | + |
| 173 | + paling._websocket.onclose = function (e) { |
| 174 | + if (paling._websocket_auto_reconnect) { |
| 175 | + setTimeout(function () { |
| 176 | + paling._init_websocket(); |
| 177 | + }, paling._websocket_reconnect_timeout); |
| 178 | + } |
| 179 | + }; |
| 180 | + }, |
161 | 181 |
|
162 | | - }; |
| 182 | + _init: function () { |
| 183 | + paling._mock_py_functions(); |
| 184 | + |
| 185 | + document.addEventListener("DOMContentLoaded", function (event) { |
| 186 | + paling._init_websocket(); |
163 | 187 | }); |
164 | 188 | } |
165 | 189 | }; |
|
0 commit comments