|
91 | 91 | } |
92 | 92 | }, |
93 | 93 |
|
94 | | - "__InvokeJavaScript": function __InvokeJavaScript(taskId, methodName, args) { |
95 | | - if (methodName[Symbol.toStringTag] === 'AsyncFunction') { |
96 | | - // For async methods, we need to call the method and then trigger the callback when it's done |
97 | | - const asyncPromise = methodName(...args); |
98 | | - asyncPromise |
99 | | - .then(asyncResult => { |
100 | | - window.HybridWebView.__TriggerAsyncCallback(taskId, asyncResult); |
101 | | - }) |
102 | | - .catch(error => console.error(error)); |
| 94 | + "__InvokeJavaScript": async function __InvokeJavaScript(taskId, methodName, args) { |
| 95 | + try { |
| 96 | + var result = null; |
| 97 | + if (methodName[Symbol.toStringTag] === 'AsyncFunction') { |
| 98 | + result = await methodName(...args); |
| 99 | + } else { |
| 100 | + result = methodName(...args); |
| 101 | + } |
| 102 | + window.HybridWebView.__TriggerAsyncCallback(taskId, result); |
| 103 | + } catch (ex) { |
| 104 | + console.error(ex); |
| 105 | + window.HybridWebView.__TriggerAsyncFailedCallback(taskId, ex); |
| 106 | + } |
| 107 | + }, |
| 108 | + |
| 109 | + "__TriggerAsyncFailedCallback": function __TriggerAsyncCallback(taskId, error) { |
| 110 | + |
| 111 | + if (!error) { |
| 112 | + json = { |
| 113 | + Message: "Unknown error", |
| 114 | + StackTrace: Error().stack |
| 115 | + }; |
| 116 | + } else if (error instanceof Error) { |
| 117 | + json = { |
| 118 | + Name: error.name, |
| 119 | + Message: error.message, |
| 120 | + StackTrace: error.stack |
| 121 | + }; |
| 122 | + } else if (typeof (error) === 'string') { |
| 123 | + json = { |
| 124 | + Message: error, |
| 125 | + StackTrace: Error().stack |
| 126 | + }; |
103 | 127 | } else { |
104 | | - // For sync methods, we can call the method and trigger the callback immediately |
105 | | - const syncResult = methodName(...args); |
106 | | - window.HybridWebView.__TriggerAsyncCallback(taskId, syncResult); |
| 128 | + json = { |
| 129 | + Message: JSON.stringify(error), |
| 130 | + StackTrace: Error().stack |
| 131 | + }; |
107 | 132 | } |
| 133 | + |
| 134 | + json = JSON.stringify(json); |
| 135 | + |
| 136 | + window.HybridWebView.__SendMessageInternal('__InvokeJavaScriptFailed', taskId + '|' + json); |
108 | 137 | }, |
109 | 138 |
|
110 | 139 | "__TriggerAsyncCallback": function __TriggerAsyncCallback(taskId, result) { |
|
0 commit comments