Skip to content

Commit 1e26742

Browse files
committed
Address PR comments
1 parent 76c0892 commit 1e26742

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

src/Controls/samples/Controls.Sample/Resources/Raw/HybridSamplePage/scripts/HybridWebView.js

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,49 @@
9191
}
9292
},
9393

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+
};
103127
} 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+
};
107132
}
133+
134+
json = JSON.stringify(json);
135+
136+
window.HybridWebView.__SendMessageInternal('__InvokeJavaScriptFailed', taskId + '|' + json);
108137
},
109138

110139
"__TriggerAsyncCallback": function __TriggerAsyncCallback(taskId, result) {

src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
using System.Collections.Specialized;
3131
using System.Text.Json.Serialization;
3232
using System.Diagnostics.CodeAnalysis;
33-
using System.Security.AccessControl;
3433

3534
namespace Microsoft.Maui.Handlers
3635
{

0 commit comments

Comments
 (0)