Created
September 22, 2021 22:00
-
-
Save zHaytam/39b7378caf0d4e67983f38b32e53db54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cachedFunctions = {} | |
function batchJsInterop(calls) { | |
return calls.map(call => { | |
try { | |
// Find function if needed | |
if (!(call.identifier in cachedFunctions)) { | |
var result = window; | |
var lastSegment = null; | |
call.identifier.split('.').forEach(seg => { | |
lastSegment = result; | |
result = result[seg]; | |
}); | |
result.bind(lastSegment); | |
cachedFunctions[call.identifier] = result; | |
} | |
// Invoke | |
let args = call.args ? call.args : []; | |
return { | |
success: true, | |
returnValue: cachedFunctions[call.identifier].apply(null, args) | |
}; | |
} catch (e) { | |
return { | |
success: false, | |
error: e.message | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment