You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Related : #156,#150
According to #156, loadurl will cause @js asynchronously return an epmpty dict.
The reason is that,@jsmacro will first lower to a function call js,then js will wrap the jsstring .Since callingloadurl will deactive the page, so this code(from AtomShell/window.jl):
will call dot(...),wrap the code in this.webContents.executeJavaScript(code),then it will be sent to AtomShell/main.js and get evaluted by handle.eval
The code handle.eval is that:
handlers.eval = function(data, c) {
var result = eval(data.code);
if (data.callback) {
result == undefined && (result = null);
result = {
type: 'callback',
data: {
callback: data.callback,
result: result
}
}
c.write(JSON.stringify(result));
}
}
The problem is that,executeJavaScript runs asynchronously and it only returns Promise{Any}(see here for more details), so we need to wait for it to return.
A naive fix will be:
handlers.eval = function(data, c) {
var result = eval(data.code)
if (data.callback) {
if ((result!=undefined) && (typeof(result.then)==="function")){
result.then(function(res){ // we use then to send the results
res == undefined && (res = null);
msg = {
type: 'callback',
data: {
callback: data.callback,
result: res
}
}
c.write(JSON.stringify(msg))
})
}
else{
result == undefined && (result = null);
msg = {
type: 'callback',
data: {
callback: data.callback,
result: result
}
}
c.write(JSON.stringify(msg))
}
}
}
Some explanations : since handle.eval is also used to createwindows,so we need to use test whether result is promise. Also the code here doesn't process errors.
On my computer this code works fine and fix #156.
The text was updated successfully, but these errors were encountered:
The actual change can actually be much simpler. I think the version of Electron we download has native Promises.
// Make this an async functionhandlers.eval=asyncfunction(data,c){// Await the result (await'ing a non-promise doesn't do anything)varresult=awaiteval(data.code);/* ... */}
Related : #156,#150
According to #156,
loadurl
will cause@js
asynchronously return an epmpty dict.The reason is that,
@js
macro will first lower to a function calljs
,thenjs
will wrap thejsstring
.Since callingloadurl
will deactive the page, so this code(from AtomShell/window.jl):will call
dot(...)
,wrap the code inthis.webContents.executeJavaScript(code)
,then it will be sent toAtomShell/main.js
and get evaluted byhandle.eval
The code
handle.eval
is that:The problem is that,
executeJavaScript
runs asynchronously and it only returnsPromise{Any}
(see here for more details), so we need to wait for it to return.A naive fix will be:
Some explanations : since
handle.eval
is also used tocreatewindows
,so we need to use test whether result is promise. Also the code here doesn't process errors.On my computer this code works fine and fix #156.
The text was updated successfully, but these errors were encountered: